Deposit Funds

Import the functions

We'll use a few of the common functions, assuming that they are in a common.ts file. The withdraw step requires a nonce as the transaction is executed against the off-chain engine.

// Change the import source as needed
import {getVertexClient, prettyPrintJson} from './common';

Mint a mock ERC20 token for testing

Grab a client object and mint mock tokens for the relevant product. This is only available on Testnet for obvious reasons.

Minting is on-chain, so we wait for the transaction confirmation for chain state to propagate.

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;

const mintTx = await vertexClient.spot._mintMockERC20({
  amount: depositAmount,
  productId: 0,
});

const mintTxReceipt = await mintTx.wait();
prettyPrintJson('Mint Tx Hash', mintTxReceipt.transactionHash);

Make a deposit

First, call approveAllowance to approve the deposit amount.

This is also an on-chain transaction with a confirmation hash.

const approveTx = await vertexClient.spot.approveAllowance({
  amount: depositAmount,
  productId: 0,
});

const approveTxReceipt = await approveTx.wait();
prettyPrintJson('Approve Tx Hash', approveTxReceipt.transactionHash);

Now we can deposit the tokens. This transaction is on-chain.

const depositTx = await vertexClient.spot.deposit({
  // Your choice of name for the subaccount, this subaccount will be credited
  subaccountName: 'default',
  amount: depositAmount,
  productId: 0,
});

Subaccounts

  • A subaccount is an independent trading account within Vertex, allowing traders to manage risk across independent subaccounts

  • Subaccounts are associated by a string name (max 12 char.) and the owner wallet address

After this, we inject a short delay while the offchain sequencer picks up the transaction and credits the account.

await new Promise((resolve) => setTimeout(resolve, 10000));

Query Subaccount balance

Now, call the getSubaccountEngineSummary function to retrieve an overview of your subaccount, including balances.

const subaccountData =
  await vertexClient.subaccount.getEngineSubaccountSummary({
    subaccountOwner: address,
    subaccountName,
  });
prettyPrintJson('Subaccount Data After Deposit', subaccountData);

Engine vs. Contract Queries

  • You may notice that there is a getSubaccountEngineSummary and getSubaccountSummary

  • The engine variants retrieve the state from the off-chain engine, while the contract variants retrieve the state from the on-chain contracts.

  • These should ultimately match up, given that our off-chain engine is fully synced and operational.

You should see that your balance associated with productId of 0 now reflects your deposit amount.

Full example

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);
}

main();

Last updated