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 neededimport {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.
constvertexClient=awaitgetVertexClient();// If you have access to `signer`, you can call `getAddress()` directly instead of reaching into `vertexClient.context`constaddress=await (vertexClient.context.signerOrProvider asWallet).getAddress();constsubaccountName='default';// 1 USDC (6 decimals)constdepositAmount=10**6;constmintTx=awaitvertexClient.spot._mintMockERC20({ amount: depositAmount, productId:0,});constmintTxReceipt=awaitmintTx.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.
Now we can deposit the tokens. This transaction is on-chain.
constdepositTx=awaitvertexClient.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.
Now, call the getSubaccountEngineSummary function to retrieve an overview of your subaccount, including balances.
constsubaccountData=awaitvertexClient.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';asyncfunctionmain() {constvertexClient=awaitgetVertexClient();// If you have access to `signer`, you can call `getAddress()` directly instead of reaching into `vertexClient.context`constaddress=await (vertexClient.context.signerOrProvider asWallet ).getAddress();constsubaccountName='default';// 1 USDC (6 decimals)constdepositAmount=10**6;// TESTNET ONLY - Mint yourself some tokensconstmintTx=awaitvertexClient.spot._mintMockERC20({ amount: depositAmount, productId:0, });// Mint goes on-chain, so wait for confirmationconstmintTxReceipt=awaitmintTx.wait();prettyPrintJson('Mint Tx Hash',mintTxReceipt.transactionHash);// Deposits move ERC20, so require approval, this is on-chain as wellconstapproveTx=awaitvertexClient.spot.approveAllowance({ amount: depositAmount, productId:0, });constapproveTxReceipt=awaitapproveTx.wait();prettyPrintJson('Approve Tx Hash',approveTxReceipt.transactionHash);// Now execute the deposit, which goes on-chainconstdepositTx=awaitvertexClient.spot.deposit({// Your choice of name for the subaccount, this subaccount will be credited subaccountName, amount: depositAmount, productId:0, });constdepositTxReceipt=awaitdepositTx.wait();prettyPrintJson('Deposit Tx Hash',depositTxReceipt.transactionHash);// Inject a delay for our offchain engine to pick up the transaction and credit your accountawaitnewPromise((resolve) =>setTimeout(resolve,10000));// For on-chain state, you can use `getSubaccountSummary` - these should match upconstsubaccountData=awaitvertexClient.subaccount.getEngineSubaccountSummary({ subaccountOwner: address, subaccountName, });prettyPrintJson('Subaccount Data After Deposit', subaccountData);}main();