Withdraw from Earn Vault
Withdraw your vault shares to receive the underlying tokens plus accrued interest.1
Import Dependencies
Import the required packages for Solana RPC communication, Kamino SDK operations, and Kit transaction building.2
Load Signer and Initialize RPC
Load your keypair from a file and initialize RPC connections.Replace
/path/to/your/keypair.json with the actual path to your keypair file. parseKeypairFile loads an existing keypair from a JSON file. The rpcSubscriptions enables real-time transaction confirmation.3
Load the Vault
Initialize the vault and load its state, reserves, and farm state once. The withdraw instructions need all three.4
Convert Tokens to Shares
Convert the token amount you want back into vault shares, capped at the shares you hold.withdrawIxs takes an amount of vault shares, not tokens. Shares grow in value as interest accrues, so one share is worth more than one token. To withdraw everything, pass userShares.totalShares.5
Build Withdraw Instructions
Generate the unstake and withdraw instructions.Deposits stake shares in the vault’s farm, so
unstakeFromFarmIfNeededIxs must come before withdrawIxs. Without it, withdrawing staked shares fails with CannotWithdrawZeroShares.6
Load the Vault’s Lookup Table
Fetch the vault’s address lookup table to keep the transaction small.Some vaults need the lookup table for a withdraw to fit in one transaction. Without it, a withdraw from one vault we tested was 1,274 bytes, over Solana’s 1,232-byte limit.
7
Build and Sign Transaction
Use Kit’s functional pipe pattern to build, compress, and sign the transaction with a fresh blockhash.8
Send and Confirm Transaction
Send the withdraw transaction and wait for confirmation.Once the withdraw transaction is confirmed, your vault shares are burned and you receive the underlying tokens plus any accrued interest.