Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

  1. Encrypted balance is decreased
  2. ERC20 tokens are transferred to destination address
  3. Amount is converted using contract rate
  4. 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"
}

Next Steps