Integrate the Castle ecosystem of products into your app to supercharge your UX
Client-Side Integration
Client-side integrations are powered by our SDK, which lives alongside the program code in the Vault repository. The primary actions a user takes are deposit and withdraw.
The SDK exposes deposit() and withdraw(), both abstract away intermediate instructions like creating ATAs, (un)wrapping sol, refreshing or reconciling the vault.
If you run into transaction size limits (for example: sending transactions as part of governance proposals that increase the bytes to over 1232), then you can split up certain instructions into their own transactions. Each section will provide code for both single SDK call and multi-SDK call cases.
Here's the full list of instructions the vault contract expects:
refresh: (permissionless) Refreshes the vaults accounts
reconcile: (permissionless) Withdraws from the underlying lending markets to allow a user to withdraw
deposit: Sends reserve token (e.g. USDC) assets into the vault in exchange for LP tokens
withdraw: Sends LP tokens into the vault in exchange for reserve token assets
The VaultClient contains static and member functions for loading information about a specific vault, along with all the necessary instructions for depositing and withdrawing.
import { VaultClient, VaultConfig } from'@castlefinance/vault-sdk'// Pull down the appropriate vault from the API.constconfigResponse=awaitfetch('https://api.castle.finance/configs')constvaults= (awaitresponse.json()) asVaultConfig[]constvault=vaults.find( (v) =>v.deploymentEnv =='mainnet'&&v.token_label =='USDC')// Create the vault clientconstvaultClient=awaitVaultClient.load(newanchor.Provider(...),vault.vault_id,vault.deploymentEnv)
Here are the following instructions needed to make a deposit:
Create the user's LP token account if it does not exist
Refresh the Vault
Deposit into the Vault
Note: The refresh and deposit instructions need to be sent in the same transaction.
Deposit (single SDK call)
// Get the user's reserve token ATAconstuserReserveTokenAccount=awaitsplToken.Token.getAssociatedTokenAddress(ASSOCIATED_TOKEN_PROGRAM_ID,TOKEN_PROGRAM_ID,vaultClient.getReserveTokenMint(), reserveTokenAccountOwner,// e.g. wallet.pubkey or DAO's accounttrue);// Deposit into the vaultconstsig=awaitvaultClient.deposit(wallet, amount, userReserveTokenAccount)
Deposit (multi-SDK call)
Create LP Token Account Transaction:
let createLpAcctIx:TransactionInstruction|undefined=undefined// Owner of the SPL Token account, e.g. the user or DAO. // In this example we assume a wallet logged-in user.constreserveTokenAccountOwner=wallet.publicKeyconstuserLpTokenAccount=awaitToken.getAssociatedTokenAddress(ASSOCIATED_TOKEN_PROGRAM_ID,TOKEN_PROGRAM_ID,vaultClient.getLpTokenMint(), reserveTokenAccountOwner,true)constuserLpTokenAccountInfo=awaitvaultClient.program.provider.connection.getAccountInfo( userLpTokenAccount )if (userLpTokenAccountInfo ==null) { createLpAcctIx =Token.createAssociatedTokenAccountInstruction(ASSOCIATED_TOKEN_PROGRAM_ID,TOKEN_PROGRAM_ID,vaultClient.getLpTokenMint(), userLpTokenAccount, reserveTokenAccountOwner,wallet.publicKey // payer )}// Construct, sign, and sendconsttx=newTransaction.add(createLpAcctIx)constsignedTx=awaitwallet.signTransaction(tx)constsig=connection.send(signedTx)
Refresh and Deposit into the Vault:
// Get the refresh instructionconstrefreshIx=vaultClient.getRefreshIx()// Get the user reserve ATAconstuserReserveTokenAccount=awaitToken.getAssociatedTokenAddress(ASSOCIATED_TOKEN_PROGRAM_ID,TOKEN_PROGRAM_ID,vaultClient.getReserveTokenMint(), reserveTokenAccountOwner,true,)// Get the deposit instructionconstdepositIx=vaultClient.getDepositIx(newanchor.BN(amount), reserveTokenOwner, userLpTokenAccount, userReserveTokenAccount,)// Construct, sign, and sendlet tx =newTransaction();tx.instructions = [refreshIx, depositIx];constsignedTx=awaitwallet.signTransaction(tx)constsig=connection.send(signedTx)
Withdraw
Withdrawing requires the following transactions to be sent:
// Get reconcile transactions (note: each tx has a prepended refresh ix)consttxs=awaitvaultClient.getReconcileTxs(amount)// Send all of themtxs.forEach(tx => {...})
Note: There may be up to 3 different reconcile transactions. Simply send off all of them.
Create the ATAs as needed and withdraw from the vault:
// Create the user reserve ATA if it does not exist alreadylet createReserveAcctIx:TransactionInstruction|undefined=undefinedconstuserReserveTokenAccount=awaitToken.getAssociatedTokenAddress(ASSOCIATED_TOKEN_PROGRAM_ID,TOKEN_PROGRAM_ID,vaultClient.getReserveTokenMint(), lpTokenAccountOwner,true)constuserReserveTokenAccountInfo=awaitvaultClient.program.provider.connection.getAccountInfo( userReserveTokenAccount )if (userReserveTokenAccountInfo ==null) { createReserveAcctIx =Token.createAssociatedTokenAccountInstruction(ASSOCIATED_TOKEN_PROGRAM_ID,TOKEN_PROGRAM_ID,vaultClient.getReserveTokenMint(), userReserveTokenAccount, lpTokenAccountOwner,wallet.publicKey )}// Get the refresh instructionconstrefreshIx=vaultClient.getRefreshIx()// Get the LP ATAconstuserLpTokenAccount=awaitToken.getAssociatedTokenAddress(ASSOCIATED_TOKEN_PROGRAM_ID,TOKEN_PROGRAM_ID,vaultClient.getLpTokenMint(), lpTokenAccountOwner,true)// Get withdraw instruction. User selects the LP token to deposit back// into the vault in exchange for the reserve tokenconstwithdrawIx=vaultClient.program.instruction.withdraw(newanchor.BN(amount), lpTokenAccountOwner, userLpTokenAccount, userReserveTokenAccount,)// Construct, sign, and sendlet tx =newTransaction();tx.instructions = [refreshIx, withdraw];constsignedTx=awaitwallet.signTransaction(tx)constsig=connection.send(signedTx)
Final Notes
These docs are still a work-in-progress. Please reach out to our Twitter or Discord if you have any questions or something is not working.