SDK

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:

  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

Vault Client

Installation

SDK NPM: https://www.npmjs.com/package/@castlefinance/vault-sdk

yarn add @castlefinance/vault-sdk

Usage

The VaultClient contains static and member functions for loading information about a specific vault, along with all the necessary instructions for depositing and withdrawing.

Here is an example deployed vault along with the Anchor annotations: https://explorer.solana.com/address/9n6ekjHHgkPB9fVuWHzH6iNuxBxN22hEBryZXYFg6cNk/anchor-account?cluster=devnet

Deposit

Here are the following instructions needed to make a deposit:

  1. Create the user's LP token account if it does not exist

  2. Refresh the Vault

  3. Deposit into the Vault

Note: The refresh and deposit instructions need to be sent in the same transaction.

Deposit (single SDK call)

Deposit (multi-SDK call)

Create LP Token Account Transaction:

Refresh and Deposit into the Vault:

Withdraw

Withdrawing requires the following transactions to be sent:

  1. Transaction #1

    1. Refresh the vault

    2. Reconcile the markets

  2. Transaction #2

    1. Refresh the vault

    2. Withdraw from the vault

Withdraw (single SDK call)

Withdraw (multi-SDK call)

Create the reconcile transactions:

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:

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.

Last updated