Skip to main content
Withdraw assets from Kamino Earn vaults to redeem your shares for underlying tokens. The SDK handles transaction building and vault interaction.

Complete Flow

Implement the full withdrawal flow either in an off-chain TypeScript client or via on-chain Rust CPI within your Anchor program.
1

Import Dependencies

Import the required packages for Solana RPC communication, Kamino SDK operations, and Kit transaction building.
@solana/kit provides modern utilities for RPC, transaction building, and signing. @kamino-finance/klend-sdk contains vault operation methods.
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.

Full Code Example