Actions
withdraw
Actions
withdraw
Withdraw tokens from a staking contract.
useWithdraw()
Withdraws tokens from a staking contract after the lockup period. Requires prior staking.
Parameters
- encryptKey (string): User’s decryption PIN
- wallet (WalletData): Wallet credentials
- contractAddress (string): Staking contract address
- amount (string | number): Tokens to withdraw
- recipient (string): Address to receive withdrawn tokens
- decimals (number): Token decimals (default: 18)
Example Implementation
'use client'
import { useWithdraw } from "@chipi-pay/chipi-sdk";
import { useState } from "react";
const STAKING_CONTRACT = "0xYourStakingContractAddress";
// Sample wallet data - replace with your wallet in production
const sampleWallet = {
publicKey: "0x123...yourPublicKeyHere",
privateKey: "encrypted:key:data" // This would be encrypted data in production
};
export function WithdrawForm() {
const { withdrawAsync, withdrawData, isLoading, error } = useWithdraw();
const [form, setForm] = useState({
pin: '',
amount: '',
recipient: ''
});
const handleWithdraw = async (e: React.FormEvent) => {
e.preventDefault();
try {
await withdrawAsync({
encryptKey: form.pin,
wallet: {
publicKey: sampleWallet.publicKey,
encryptedPrivateKey: sampleWallet.privateKey
},
contractAddress: STAKING_CONTRACT,
amount: form.amount,
recipient: form.recipient || sampleWallet.publicKey,
decimals: 18
});
} catch (err) {
console.error('Withdrawal failed:', err);
}
};
return (
<div className="bg-white rounded-xl shadow-lg p-6">
<h2 className="text-2xl font-bold mb-4">Withdraw Tokens</h2>
<form onSubmit={handleWithdraw} className="space-y-4">
<div>
<label className="block text-sm font-medium mb-1">Security PIN</label>
<input
type="password"
value={form.pin}
onChange={(e) => setForm({...form, pin: e.target.value})}
className="w-full p-2 border rounded-md"
required
/>
</div>
<div>
<label className="block text-sm font-medium mb-1">Amount</label>
<input
type="number"
value={form.amount}
onChange={(e) => setForm({...form, amount: e.target.value})}
className="w-full p-2 border rounded-md"
required
/>
</div>
<div>
<label className="block text-sm font-medium mb-1">Recipient (optional)</label>
<input
type="text"
value={form.recipient}
onChange={(e) => setForm({...form, recipient: e.target.value})}
className="w-full p-2 border rounded-md"
placeholder={sampleWallet.publicKey || 'Your wallet address'}
/>
</div>
<button
type="submit"
disabled={isLoading}
className="w-full bg-orange-600 text-white py-2 px-4 rounded-md hover:bg-orange-700 disabled:bg-gray-400"
>
{isLoading ? 'Processing...' : 'Withdraw'}
</button>
</form>
{withdrawData && (
<div className="mt-4 p-3 bg-gray-50 rounded-md">
<p className="text-sm font-mono break-all">
Withdrawal TX: {withdrawData}
</p>
</div>
)}
{error && (
<div className="mt-4 p-3 bg-red-50 text-red-700 rounded-md">
Error: {error.message}
</div>
)}
</div>
);
}
## Related Actions
- **[Stake](stake.mdx)** - Deposit tokens into staking contracts
- **[Transfer](/actions/transfer.mdx)** - Move withdrawn funds
:::warning
Withdrawals may have a 7-day cooldown period depending on the protocol.
:::
On this page