110 lines
4.3 KiB
Plaintext
110 lines
4.3 KiB
Plaintext
---
|
|
title: Deposit
|
|
description: CPI instruction to deposit assets into a Ranger Earn vault
|
|
---
|
|
|
|
The `deposit_vault` instruction deposits assets into a Ranger Earn vault and mints LP tokens to the depositor.
|
|
|
|
## Discriminator
|
|
|
|
```rust
|
|
/// sha256("global:deposit_vault")[0..8]
|
|
fn get_deposit_vault_discriminator() -> [u8; 8] {
|
|
[126, 224, 21, 255, 228, 53, 117, 33]
|
|
}
|
|
```
|
|
|
|
## Parameters
|
|
|
|
| Parameter | Type | Description |
|
|
|---|---|---|
|
|
| `amount` | `u64` | The amount of asset tokens to deposit |
|
|
|
|
## Accounts
|
|
|
|
| Account | Mutability | Signer | Description |
|
|
|---|---|---|---|
|
|
| `user_transfer_authority` | Immutable | Yes | The user depositing assets |
|
|
| `protocol` | Immutable | No | Global Ranger Earn protocol state account |
|
|
| `vault` | Mutable | No | The target vault state account |
|
|
| `vault_asset_mint` | Immutable | No | The mint of the asset being deposited |
|
|
| `vault_lp_mint` | Mutable | No | The vault's LP mint |
|
|
| `user_asset_ata` | Mutable | No | The user's asset token account (source) |
|
|
| `vault_asset_idle_ata` | Mutable | No | The vault's idle asset token account (destination) |
|
|
| `vault_asset_idle_auth` | Immutable | No | PDA authority over `vault_asset_idle_ata` |
|
|
| `user_lp_ata` | Mutable | No | The user's LP token account (destination) |
|
|
| `vault_lp_mint_auth` | Immutable | No | PDA authority for minting LP tokens |
|
|
| `asset_token_program` | Immutable | No | Token Program or Token-2022 for assets |
|
|
| `lp_token_program` | Immutable | No | Token Program for LP tokens |
|
|
| `system_program` | Immutable | No | Solana System Program |
|
|
|
|
## CPI Struct
|
|
|
|
```rust
|
|
pub struct DepositVaultParams<'info> {
|
|
pub user_transfer_authority: AccountInfo<'info>,
|
|
pub protocol: AccountInfo<'info>,
|
|
pub vault: AccountInfo<'info>,
|
|
pub vault_asset_mint: AccountInfo<'info>,
|
|
pub vault_lp_mint: AccountInfo<'info>,
|
|
pub user_asset_ata: AccountInfo<'info>,
|
|
pub vault_asset_idle_ata: AccountInfo<'info>,
|
|
pub vault_asset_idle_auth: AccountInfo<'info>,
|
|
pub user_lp_ata: AccountInfo<'info>,
|
|
pub vault_lp_mint_auth: AccountInfo<'info>,
|
|
pub asset_token_program: AccountInfo<'info>,
|
|
pub lp_token_program: AccountInfo<'info>,
|
|
pub system_program: AccountInfo<'info>,
|
|
pub voltr_vault_program: AccountInfo<'info>,
|
|
}
|
|
```
|
|
|
|
## Implementation
|
|
|
|
```rust
|
|
use anchor_lang::prelude::*;
|
|
use anchor_lang::solana_program::{
|
|
account_info::AccountInfo,
|
|
instruction::{ AccountMeta, Instruction },
|
|
program::invoke,
|
|
};
|
|
|
|
impl<'info> DepositVaultParams<'info> {
|
|
pub fn deposit_vault(&self, amount: u64) -> Result<()> {
|
|
let mut instruction_data = get_deposit_vault_discriminator().to_vec();
|
|
instruction_data.extend_from_slice(&amount.to_le_bytes());
|
|
|
|
let account_metas = vec![
|
|
AccountMeta::new_readonly(*self.user_transfer_authority.key, true),
|
|
AccountMeta::new_readonly(*self.protocol.key, false),
|
|
AccountMeta::new(*self.vault.key, false),
|
|
AccountMeta::new_readonly(*self.vault_asset_mint.key, false),
|
|
AccountMeta::new(*self.vault_lp_mint.key, false),
|
|
AccountMeta::new(*self.user_asset_ata.key, false),
|
|
AccountMeta::new(*self.vault_asset_idle_ata.key, false),
|
|
AccountMeta::new_readonly(*self.vault_asset_idle_auth.key, false),
|
|
AccountMeta::new(*self.user_lp_ata.key, false),
|
|
AccountMeta::new_readonly(*self.vault_lp_mint_auth.key, false),
|
|
AccountMeta::new_readonly(*self.asset_token_program.key, false),
|
|
AccountMeta::new_readonly(*self.lp_token_program.key, false),
|
|
AccountMeta::new_readonly(*self.system_program.key, false),
|
|
];
|
|
|
|
let instruction = Instruction {
|
|
program_id: *self.voltr_vault_program.key,
|
|
accounts: account_metas,
|
|
data: instruction_data,
|
|
};
|
|
|
|
invoke(&instruction, &self.to_account_infos())
|
|
.map_err(|_| ErrorCodes::CpiToVoltrVaultFailed.into())
|
|
}
|
|
}
|
|
```
|
|
|
|
<Info>
|
|
The `user_transfer_authority` must sign the transaction. The vault's internal PDAs (`vault_asset_idle_auth`, `vault_lp_mint_auth`) are signed by the Ranger Earn Vault program during the CPI — your program does not need to provide their seeds.
|
|
</Info>
|
|
|
|
Full reference implementation: [deposit_vault.rs](https://github.com/voltrxyz/vault-cpi/blob/main/references/deposit_vault.rs)
|