SDK
Integrate the Castle ecosystem of products into your app to supercharge your UX
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:
- 1.
refresh
: (permissionless) Refreshes the vaults accounts - 2.
reconcile
: (permissionless) Withdraws from the underlying lending markets to allow a user to withdraw - 3.
deposit
: Sends reserve token (e.g. USDC) assets into the vault in exchange for LP tokens - 4.
withdraw
: Sends LP tokens into the vault in exchange for reserve token assets
yarn add @castlefinance/vault-sdk
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.
const configResponse = await fetch('https://api.castle.finance/configs')
const vaults = (await response.json()) as VaultConfig[]
const vault = vaults.find(
(v) => v.deploymentEnv == 'mainnet' && v.token_label == 'USDC'
)
// Create the vault client
const vaultClient = await VaultClient.load(
new anchor.Provider(...),
vault.vault_id,
vault.deploymentEnv
)
Here is an example deployed vault along with the Anchor annotations: https://explorer.solana.com/address/9n6ekjHHgkPB9fVuWHzH6iNuxBxN22hEBryZXYFg6cNk/anchor-account?cluster=devnet