Withdraw Funds

This section assumes you have followed the instructions and run the code in the deposit funds example.

Call the withdraw function

Note that there is a fee for withdrawals, in our case, it should be 0.1 USDC, which is 10**5 including decimals.

const withdrawTx = await vertexClient.spot.withdraw({
  amount: depositAmount - 10 ** 5,
  productId: 0,
  subaccountName: 'default',
});

prettyPrintJson('Withdraw Tx', withdrawTx);

Check balance

Get and print the summary data of the subaccount using getSubaccountSummary. You should see that your balance is now 0.

Full example - deposit and withdraw

import {getVertexClient, prettyPrintJson} from './common';

async function main() {
  const vertexClient = await getVertexClient();

  // If you have access to `signer`, you can call `getAddress()` directly instead of reaching into `vertexClient.context`
  const address = await (
    vertexClient.context.signerOrProvider as Wallet
  ).getAddress();
  const subaccountName = 'default';
  // 1 USDC (6 decimals)
  const depositAmount = 10 ** 6;

  // TESTNET ONLY - Mint yourself some tokens
  const mintTx = await vertexClient.spot._mintMockERC20({
    amount: depositAmount,
    productId: 0,
  });
  // Mint goes on-chain, so wait for confirmation
  const mintTxReceipt = await mintTx.wait();
  prettyPrintJson('Mint Tx Hash', mintTxReceipt.transactionHash);

  // Deposits move ERC20, so require approval, this is on-chain as well
  const approveTx = await vertexClient.spot.approveAllowance({
    amount: depositAmount,
    productId: 0,
  });
  const approveTxReceipt = await approveTx.wait();
  prettyPrintJson('Approve Tx Hash', approveTxReceipt.transactionHash);

  // Now execute the deposit, which goes on-chain
  const depositTx = await vertexClient.spot.deposit({
    // Your choice of name for the subaccount, this subaccount will be credited
    subaccountName,
    amount: depositAmount,
    productId: 0,
  });
  const depositTxReceipt = await depositTx.wait();
  prettyPrintJson('Deposit Tx Hash', depositTxReceipt.transactionHash);

  // Inject a delay for our offchain engine to pick up the transaction and credit your account
  await new Promise((resolve) => setTimeout(resolve, 10000));

  // For on-chain state, you can use `getSubaccountSummary` - these should match up
  const subaccountData =
    await vertexClient.subaccount.getEngineSubaccountSummary({
      subaccountOwner: address,
      subaccountName,
    });
  prettyPrintJson('Subaccount Data After Deposit', subaccountData);

  // Now withdraw your funds, this goes to the off-chain engine
  // We're withdrawing less than 10 as there are withdrawal fees of 0.1 USDC
  const withdrawTx = await vertexClient.spot.withdraw({
    amount: depositAmount - 10 ** 5,
    productId: 0,
    subaccountName,
  });
  prettyPrintJson('Withdraw Tx', withdrawTx);

  // Your new subaccount summary should have zero balances
  const subaccountDataAfterWithdraw =
    await vertexClient.subaccount.getEngineSubaccountSummary({
      subaccountOwner: address,
      subaccountName,
    });
  prettyPrintJson(
    'Subaccount Data After Withdrawal',
    subaccountDataAfterWithdraw,
  );
}

main();

Last updated