Withdrawals
Convert encrypted Tongo balance back to ERC20 tokens.
Basic Withdrawal
const withdrawOp = await account.withdraw({
to: starknetAddress, // Destination address
amount: 500n // Amount to withdraw
});
const tx = await signer.execute([withdrawOp.toCalldata()]);
await provider.waitForTransaction(tx.transaction_hash);
What Happens
- Encrypted balance is decreased
- ERC20 tokens are transferred to destination address
- Amount is converted using contract rate
- Nonce is incremented
Complete Example
// Check balance before withdrawal
const stateBefore = await account.state();
console.log("Balance:", stateBefore.balance); // 1000n
// Withdraw 500 tokens to my wallet
const withdrawOp = await account.withdraw({
to: signer.address,
amount: 500n
});
const tx = await signer.execute([withdrawOp.toCalldata()]);
await provider.waitForTransaction(tx.transaction_hash);
// Check balance after withdrawal
const stateAfter = await account.state();
console.log("Balance:", stateAfter.balance); // 500n
console.log("Nonce:", stateAfter.nonce); // Incremented
Error Handling
try {
await account.withdraw({ to: address, amount: 999999n });
} catch (error) {
console.error(error.message); // "You dont have enought balance"
}