();
reserve_infos.push(ReserveInfo::from_account_data(reserve, &acc.data).unwrap());
}
let vault = VaultInfo::from_account_data(vault_pubkey, &vault_data.data, &reserve_infos).unwrap();
// Derive the user's token + shares ATAs
let user_token_ata = get_associated_token_address(&owner, &vault.token_mint);
let (shares_mint, _) = pda::shares_mint(&KVAULT_PROGRAM_ID, &vault_pubkey);
let user_shares_ata = get_associated_token_address(&owner, &shares_mint);
// Build a deposit instruction for 1 USDC — PDAs + remaining accounts are derived for you
let deposit_ix =
helpers::deposit::deposit(&vault, owner, user_token_ata, user_shares_ata, 1_000_000);
```
</Tab>
</Tabs>
<Note>
For the full deposit example, including transaction sending and confirmation, check out [Deposit Operations](/docs/build/developers/earn/operations/deposit).
</Note>
### Withdraw
<Tabs>
<Tab title="TypeScript">
```typescript theme={null}
import { createSolanaRpc, address } from '@solana/kit';
import { KaminoVault } from '@kamino-finance/klend-sdk';
import { Decimal } from 'decimal.js';
const vault = new KaminoVault(
createSolanaRpc('https://api.mainnet-beta.solana.com'),
address('HDsayqAsDWy3QvANGqh2yNraqcD8Fnjgh73Mhb3WRS5E')
);
// Withdraw 1.5 shares
const withdrawIxs = await vault.withdrawIxs(
user, // TransactionSigner (multisig cannot sign directly)
new Decimal(1.5) // shares to withdraw
);
console.log('Withdraw Instructions:', withdrawIxs);
```
<a href="https://github.com/Kamino-Finance/klend-sdk/blob/master/examples/kvault-tutorial/earn-vaults-05-user-withdraw-example/index.ts">
<Icon icon="github" />
<span>View Code</span>
</a>
</Tab>
<Tab title="Rust">
```rust expandable theme={null}
use kvault_interface::{
from_account_data, helpers, pda, state::VaultState, ReserveInfo, VaultInfo, KVAULT_PROGRAM_ID,
};
use solana_client::rpc_client::RpcClient;
use solana_pubkey::Pubkey;
use spl_associated_token_account::get_associated_token_address;
let rpc = RpcClient::new("https://api.mainnet-beta.solana.com".to_string());
let vault_pubkey: Pubkey = "HDsayqAsDWy3QvANGqh2yNraqcD8Fnjgh73Mhb3WRS5E".parse().unwrap();
let reserve_pubkey: Pubkey = "D6q6wuQSrifJKZYpR1M8R4YawnLDtDsMmWM1NbBmgJ59".parse().unwrap();
// Load the vault, the disinvest-target reserve, and every active reserve
let vault_data = rpc.get_account(&vault_pubkey).unwrap();
let reserve_data = rpc.get_account(&reserve_pubkey).unwrap();
let reserve = ReserveInfo::from_account_data(reserve_pubkey, &reserve_data.data).unwrap();
let vault_state = from_account_data::<VaultState>(&vault_data.data).unwrap();
let mut reserve_infos = Vec::new();
for r in VaultInfo::active_reserve_addresses(vault_state) {
let acc = rpc.get_account(&r).unwrap();
reserve_infos.push(ReserveInfo::from_account_data(r, &acc.data).unwrap());
}
let vault = VaultInfo::from_account_data(vault_pubkey, &vault_data.data, &reserve_infos).unwrap();
let user_token_ata = get_associated_token_address(&owner, &vault.token_mint);
let (shares_mint, _) = pda::shares_mint(&KVAULT_PROGRAM_ID, &vault_pubkey);
let user_shares_ata = get_associated_token_address(&owner, &shares_mint);
// Burn 1.5 shares (raw units) — disinvests from the reserve if available liquidity is low
let withdraw_ix =
helpers::withdraw::withdraw(&vault, owner, user_token_ata, user_shares_ata, &reserve, 1_500_000);
```
</Tab>
</Tabs>
<Note>
For the full withdraw example, including transaction sending and confirmation, check out [Withdraw Operations](/docs/build/developers/earn/operations/withdraw).
</Note>
## Key Data Structures
<div>
<Tabs>
<Tab title="TypeScript">
##### VaultState
```typescript theme={null}
class VaultState {
tokenMint: Address
tokenVault: Address
sharesMint: Address
tokenAvailable: BN
sharesIssued: BN
performanceFeeBps: BN
managementFeeBps: BN
vaultAllocationStrategy: Array<VaultAllocation>
unallocatedTokensCap: BN
unallocatedWeight: BN
minDepositAmount: BN
withdrawalPenaltyLamports: BN
withdrawalPenaltyBps: BN
cumulativeEarnedInterestSf: BN
cumulativeMgmtFeesSf: BN
cumulativePerfFeesSf: BN
name: Array<number>
vaultFarm: Address
creationTimestamp: BN
allowAllocationsInWhitelistedReservesOnly: Boolean
allowInvestInWhitelistedReservesOnly: Boolean
}
```
<a href="https://github.com/Kamino-Finance/klend-sdk/blob/636ccc98fd3aacfb49ed5d9d5f74ba527a9c75f9/src/%40codegen/kvault/accounts/VaultState.ts#L96">
<Icon icon="github" />
<span>View Full TypeScript VaultState</span>
</a>
<Expandable title="Fields">
| Field | Type | Description |
| - | - | - |
| `tokenMint` | `Address` | SPL token mint address that this vault accepts |
| `tokenVault` | `Address` | Vault's token account holding deposited assets |
| `sharesMint` | `Address` | Mint address for vault share tokens |
| `tokenAvailable` | `BN` | Amount of tokens available in the vault (lamports) |
| `sharesIssued` | `BN` | Total number of vault shares issued to depositors (lamports) |
| `performanceFeeBps` | `BN` | Performance fee in basis points (100 = 1%) |
| `managementFeeBps` | `BN` | Management fee in basis points |
| `vaultAllocationStrategy` | `Array<VaultAllocation>` | Array of allocation strategies across reserves |
| `unallocatedTokensCap` | `BN` | Maximum unallocated token amount (lamports) |
| `unallocatedWeight` | `BN` | Relative weight for unallocated funds |
| `minDepositAmount` | `BN` | Minimum deposit amount per transaction (lamports) |
| `withdrawalPenaltyLamports` | `BN` | Absolute withdrawal penalty |
| `withdrawalPenaltyBps` | `BN` | Percentage withdrawal penalty |
| `cumulativeEarnedInterestSf` | `BN` | Cumulative earned interest (scaled fraction) |
| `cumulativeMgmtFeesSf` | `BN` | Cumulative management fees (scaled fraction) |
| `cumulativePerfFeesSf` | `BN` | Cumulative performance fees (scaled fraction) |
| `name` | `Array<number>` | Vault name as byte array |
| `vaultFarm` | `Address` | Associated farm address for rewards |
| `creationTimestamp` | `BN` | Unix timestamp of vault creation |
| `allowAllocationsInWhitelistedReservesOnly` | `Boolean` | Restrict allocations to verified reserves |
| `allowInvestInWhitelistedReservesOnly` | `Boolean` | Restrict investments to verified reserves |
</Expandable>
<Expandable title="VaultAllocation Structure">
<div>
<br /> VaultAllocation
```typescript theme={null}
class VaultAllocation {
reserve: Address
ctokenVault: Address
targetAllocationWeight: BN
tokenAllocationCap: BN
ctokenAllocation: BN
reserveType: number
priority: number
lastInvestSlot: BN
tokenTargetAllocationSf: BN
}
```
<a href="https://github.com/Kamino-Finance/klend-sdk/blob/636ccc98fd3aacfb49ed5d9d5f74ba527a9c75f9/src/%40codegen/kvault/types/VaultAllocation.ts#L35">
<Icon icon="github" />
<span>View Full TypeScript VaultAllocation</span>
</a>
| Field | Type | Description |
| - | - | - |
| `reserve` | `Address` | Lending reserve address where funds are allocated |
| `ctokenVault` | `Address` | Vault's cToken account for this reserve |
| `targetAllocationWeight` | `BN` | Relative integer weight for this allocation |
| `tokenAllocationCap` | `BN` | Maximum tokens that can be allocated to this reserve |
| `ctokenAllocation` | `BN` | Current amount of cTokens held in this allocation |
| `reserveType` | `number` | `standard` or `conditional` (as number); Allocation behavior (see Reserve Types) |
| `priority` | `number` | Priority ordering for conditional investments (see Reserve Types) |
| `lastInvestSlot` | `BN` | Last slot when funds were invested in this reserve |
| `tokenTargetAllocationSf` | `BN` | Target allocation amount (scaled fraction) |
</div>
</Expandable>
</Tab>
<Tab title="JSON">
##### VaultState
```json theme={null}
{
"tokenMint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
"tokenVault": "...",
"sharesMint": "...",
"tokenAvailable": "1000000000000",
"sharesIssued": "950000000000",
"performanceFeeBps": "1500",
"managementFeeBps": "200",
"vaultAllocationStrategy": [],
"unallocatedTokensCap": "0",
"unallocatedWeight": "0",
"minDepositAmount": "0",
"withdrawalPenaltyLamports": "0",
"withdrawalPenaltyBps": "0",
"cumulativeEarnedInterestSf": "0",
"cumulativeMgmtFeesSf": "0",
"cumulativePerfFeesSf": "0",
"name": [99, 108, 105, 102, 102, 111, 114, 100],
"vaultFarm": "...",
"creationTimestamp": "1234567890",
"allowAllocationsInWhitelistedReservesOnly": false,
"allowInvestInWhitelistedReservesOnly": false
}
```
<a href="https://github.com/Kamino-Finance/klend-sdk/blob/636ccc98fd3aacfb49ed5d9d5f74ba527a9c75f9/src/%40codegen/kvault/accounts/VaultState.ts#L57C1-L94C2">
<Icon icon="github" />
<span>View Full VaultStateJSON</span>
</a>
<Expandable title="Fields">
| Field | Type | Description |
| - | - | - |
| `tokenMint` | `string` | SPL token mint address that this vault accepts |
| `tokenVault` | `string` | Vault's token account holding deposited assets |
| `sharesMint` | `string` | Mint address for vault share tokens |
| `tokenAvailable` | `string` | Amount of tokens available in the vault |
| `sharesIssued` | `string` | Total number of vault shares issued to depositors |
| `performanceFeeBps` | `string` | Performance fee in basis points (100 = 1%) |
| `managementFeeBps` | `string` | Management fee in basis points |
| `vaultAllocationStrategy` | `array` | Array of allocation strategies across reserves |
| `unallocatedTokensCap` | `string` | Maximum unallocated token amount (lamports) |
| `unallocatedWeight` | `string` | Relative weight for unallocated funds |
| `minDepositAmount` | `string` | Minimum deposit amount per transaction (lamports) |
| `withdrawalPenaltyLamports` | `string` | Absolute withdrawal penalty |
| `withdrawalPenaltyBps` | `string` | Percentage withdrawal penalty |
| `cumulativeEarnedInterestSf` | `string` | Cumulative earned interest (scaled fraction) |
| `cumulativeMgmtFeesSf` | `string` | Cumulative management fees (scaled fraction) |
| `cumulativePerfFeesSf` | `string` | Cumulative performance fees (scaled fraction) |
| `name` | `array` | Vault name as byte array |
| `vaultFarm` | `string` | Associated farm address for rewards |
| `creationTimestamp` | `string` | Unix timestamp of vault creation |
| `allowAllocationsInWhitelistedReservesOnly` | `boolean` | Restrict allocations to verified reserves |
| `allowInvestInWhitelistedReservesOnly` | `boolean` | Restrict investments to verified reserves |
</Expandable>
<Expandable title="VaultAllocation Structure">
<div>
<br /> VaultAllocationJSON
```json theme={null}
{
"reserve": "...",
"ctokenVault": "...",
"targetAllocationWeight": "100",
"tokenAllocationCap": "1000000000",
"ctokenAllocation": "500000000",
"reserveType": 0,
"priority": 0,
"lastInvestSlot": "123456789",
"tokenTargetAllocationSf": "0"
}
```
<a href="https://github.com/Kamino-Finance/klend-sdk/blob/636ccc98fd3aacfb49ed5d9d5f74ba527a9c75f9/src/%40codegen/kvault/types/VaultAllocation.ts#L21C1-L33C2">
<Icon icon="github" />
<span>View Full VaultAllocationJSON</span>
</a>
| Field | Type | Description |
| - | - | - |
| `reserve` | `string` | Lending reserve address where funds are allocated |
| `ctokenVault` | `string` | Vault's cToken account for this reserve |
| `targetAllocationWeight` | `string` | Relative integer weight for this allocation |
| `tokenAllocationCap` | `string` | Maximum tokens that can be allocated to this reserve |
| `ctokenAllocation` | `string` | Current amount of cTokens held in this allocation |
| `reserveType` | `number` | `0` = standard, `1` = conditional; allocation behavior (see Reserve Types) |
| `priority` | `number` | Priority ordering for conditional investments (see Reserve Types) |
| `lastInvestSlot` | `string` | Last slot when funds were invested in this reserve |
| `tokenTargetAllocationSf` | `string` | Target allocation amount (scaled fraction) |
</div>
</Expandable>
</Tab>
<Tab title="Rust">
##### VaultState
```rust theme={null}
pub struct VaultState {
pub token_mint: Pubkey
pub token_vault: Pubkey
pub shares_mint: Pubkey
pub token_available: u64
pub shares_issued: u64
pub performance_fee_bps: u64
pub management_fee_bps: u64
pub vault_allocation_strategy: [VaultAllocation; MAX_RESERVES]
pub unallocated_tokens_cap: u64
pub unallocated_weight: u64
pub min_deposit_amount: u64
pub withdrawal_penalty_lamports: u64
pub withdrawal_penalty_bps: u64
pub cumulative_earned_interest_sf: u128
pub cumulative_mgmt_fees_sf: u128
pub cumulative_perf_fees_sf: u128
pub name: [u8; 40]
pub vault_farm: Pubkey
pub creation_timestamp: u64
pub allow_allocations_in_whitelisted_reserves_only: bool
pub allow_invest_in_whitelisted_reserves_only: bool
}
```
<a href="https://github.com/Kamino-Finance/kvault/blob/0321f6287d236b8286495ff7b7a410fcb93f0599/programs/kvault/src/state.rs#L17">
<Icon icon="github" />
<span>View Full Rust VaultState</span>
</a>
<Expandable title="Fields">
| Field | Type | Description |
| - | - | - |
| `token_mint` | `Pubkey` | SPL token mint address that this vault accepts |
| `token_vault` | `Pubkey` | Vault's token account holding deposited assets |
| `shares_mint` | `Pubkey` | Mint address for vault share tokens |
| `token_available` | `u64` | Amount of tokens available in the vault |
| `shares_issued` | `u64` | Total number of vault shares issued to depositors |
| `performance_fee_bps` | `u64` | Performance fee in basis points (100 = 1%) |
| `management_fee_bps` | `u64` | Management fee in basis points |
| `vault_allocation_strategy` | `[VaultAllocation; MAX_RESERVES]` | Fixed array of allocation strategies across reserves (MAX\_RESERVES = 25) |
| `unallocated_tokens_cap` | `u64` | Maximum unallocated token amount (lamports) |
| `unallocated_weight` | `u64` | Relative weight for unallocated funds |
| `min_deposit_amount` | `u64` | Minimum deposit amount per transaction (lamports) |
| `withdrawal_penalty_lamports` | `u64` | Absolute withdrawal penalty |
| `withdrawal_penalty_bps` | `u64` | Percentage withdrawal penalty |
| `cumulative_earned_interest_sf` | `u128` | Cumulative earned interest (scaled fraction) |
| `cumulative_mgmt_fees_sf` | `u128` | Cumulative management fees (scaled fraction) |
| `cumulative_perf_fees_sf` | `u128` | Cumulative performance fees (scaled fraction) |
| `name` | `[u8; 40]` | Vault name as byte array |
| `vault_farm` | `Pubkey` | Associated farm address for rewards |
| `creation_timestamp` | `u64` | Unix timestamp of vault creation |
| `allow_allocations_in_whitelisted_reserves_only` | `bool` | Restrict allocations to verified reserves |
| `allow_invest_in_whitelisted_reserves_only` | `bool` | Restrict investments to verified reserves |
</Expandable>
<Expandable title="VaultAllocation Structure">
<div>
<br /> VaultAllocation
```rust theme={null}
pub struct VaultAllocation {
pub reserve: Pubkey
pub ctoken_vault: Pubkey
pub target_allocation_weight: u64
pub token_allocation_cap: u64
pub ctoken_vault_bump: u64
pub reserve_type: u8
pub allocation_priority: u8
pub ctoken_allocation: u64
pub last_invest_slot: u64
pub token_target_allocation_sf: u128
}
```
<a href="https://github.com/Kamino-Finance/kvault/blob/0321f6287d236b8286495ff7b7a410fcb93f0599/programs/kvault/src/state.rs#L425">
<Icon icon="github" />
<span>View Full Rust VaultAllocation</span>
</a>
| Field | Type | Description |
| - | - | - |
| `reserve` | `Pubkey` | Lending reserve address where funds are allocated |
| `ctoken_vault` | `Pubkey` | Vault's cToken account for this reserve |
| `target_allocation_weight` | `u64` | Relative integer weight for this allocation |
| `token_allocation_cap` | `u64` | Maximum tokens that can be allocated to this reserve |
| `ctoken_vault_bump` | `u64` | PDA bump seed for the cToken vault |
| `reserve_type` | `u8` | `standard` or `conditional` (as number); Allocation behavior (see Reserve Types) |
| `allocation_priority` | `u8` | Priority ordering for conditional investments (see Reserve Types) |
| `ctoken_allocation` | `u64` | Current amount of cTokens held in this allocation |
| `last_invest_slot` | `u64` | Last slot when funds were invested in this reserve |
| `token_target_allocation_sf` | `u128` | Target allocation amount (scaled fraction) |
</div>
</Expandable>
</Tab>
</Tabs>
</div>
## Relevant Links
<CardGroup>
<Card title="Fetch Vaults Data via the API" icon="globe" href="https://api-docs.kamino.com/">
Access vault metrics and performance data through REST endpoints.
</Card>
<Card title="Smart contract repository" icon="github" href="https://github.com/Kamino-Finance/klend">
Explore the on-chain program source code and documentation.
</Card>
<Card title="NPM SDK package" icon="npm" href="https://www.npmjs.com/package/@kamino-finance/klend-sdk">
Install the TypeScript SDK to integrate vault operations.
</Card>
<Card title="Rust Interface Crate" icon="rust" href="https://crates.io/crates/kvault-interface">
Lightweight instruction builder for Rust bots and on-chain CPI.
</Card>
<Card title="More Earn tutorials" icon="book" href="/docs/build/tutorials/earn/index">
Step-by-step guides for building with Kamino Earn vaults.
</Card>
</CardGroup>
# Deposit
Source: https://kamino.com/docs/build/developers/earn/operations/deposit
Deposit assets into Kamino Earn vaults
Deposit assets into Kamino Earn vaults to start earning yield. The SDK handles transaction building, instruction creation, and vault interaction.
## Complete Flow
Implement the full deposit flow either in an off-chain TypeScript client or via on-chain Rust CPI within your Anchor program.
<Tabs>
<Tab title="TypeScript">
<Steps>
<Step>
### Import Dependencies
Import the required packages for Solana RPC communication, Kamino SDK operations, and Kit transaction building.
```typescript theme={null}
import {
createSolanaRpc,
createSolanaRpcSubscriptions,
address,
pipe,
createTransactionMessage,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
appendTransactionMessageInstructions,
signTransactionMessageWithSigners,
sendAndConfirmTransactionFactory,
getSignatureFromTransaction,
} from '@solana/kit';
import { KaminoVault } from '@kamino-finance/klend-sdk';
import { parseKeypairFile } from '@kamino-finance/klend-sdk/dist/utils/signer.js';
import { Decimal } from 'decimal.js';
```
<Note>
`@solana/kit` provides modern utilities for RPC, transaction building, and signing. `@kamino-finance/klend-sdk` contains vault operation methods.
</Note>
</Step>
<Step>
### Load Keypair and Initialize Vault
Load the keypair from file, initialize RPC connections, and create the vault instance.
```typescript theme={null}
const KEYPAIR_FILE = '/path/to/your/keypair.json';
const signer = await parseKeypairFile(KEYPAIR_FILE);
const rpc = createSolanaRpc('https://api.mainnet-beta.solana.com');
const rpcSubscriptions = createSolanaRpcSubscriptions('wss://api.mainnet-beta.solana.com');
const vault = new KaminoVault(
rpc,
address('HDsayqAsDWy3QvANGqh2yNraqcD8Fnjgh73Mhb3WRS5E') // USDC vault
);
```
<Note>
`parseKeypairFile` loads an existing keypair from a JSON file.
</Note>
</Step>
<Step>
### Build Deposit Instructions
Generate deposit instructions for the specified amount.
```typescript theme={null}
const depositAmount = new Decimal(1.0);
const bundle = await vault.depositIxs(signer, depositAmount);
const instructions = [...(bundle.depositIxs || [])];
if (!instructions.length) {
throw new Error('No instructions returned by Kamino SDK');
}
```
<Info>
The `depositIxs` method returns the necessary instructions to deposit assets into the vault.
</Info>
</Step>
<Step>
### Build and Send Transaction
Fetch the latest blockhash and construct the transaction message.
```typescript theme={null}
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
const tr프로젝트로 돌아가기