Meteora
  • Meteora: The most dynamic and sustainable liquidity layer on Solana
  • PRODUCT OVERVIEW
    • Meteora Liquidity Pools
      • DLMM Overview
        • What is DLMM?
        • DLMM Program
        • Dynamic Fees
        • Strategies & Use Cases
        • DLMM Farming Rewards
      • DLMM Launch Pool Overview
      • Dynamic AMM Overview
        • What is a Dynamic AMM Pool?
        • Dynamic AMM LP Fee and APY Calculation
        • Creating a Dynamic AMM Pool via the UI
        • Claiming Fees from Permanently Locked Liquidity
        • Dynamic AMM Stable Pools
        • Dynamic LST Pools
        • Additional yield from Dynamic Vaults
        • Dynamic AMM Farm Overview
      • DAMM v2 Overview
      • Memecoin Pool Overview
        • Memecoin Pool v2
          • What is Memecoin Pool v2?
        • Memecoin Pool v1
          • What is Memecoin Pool v1?
          • Permanently Locking Liquidity
      • Stake2Earn Pool Overview
        • What is a Stake2Earn Pool?
        • Stake2Earn for Launchpads
      • Multi-Token Stable Pool Overview
    • Alpha Vault Overview
    • Dynamic Vault Overview
      • What is a Dynamic Vault?
      • Dynamic Vault Program
      • Hermes - Meteora's Keeper
        • Algorithm to find optimal yield allocations
        • Rebalance crank
        • Operation fee calculation
      • Design Goals
      • Security
      • Dynamic Vaults Whitepaper
      • Dynamic Vaults Community Explainers
      • Affiliate Program for Dynamic Vault
        • Become an Affiliate Partner (Dynamic Vaults)
    • Dynamic Bonding Curve (DBC) Overview
      • What is the Dynamic Bonding Curve?
      • Customizable Pool Configuration
      • Bonding Curve Formula
      • DBC Migrator Keeper
  • Meteora's Anti-Sniper Suite (A.S.S.)
    • Meteora’s Anti-Sniper Suite
      • Dynamic Fees
      • Fee Scheduler
      • Alpha Vault
  • INTEGRATION
    • DLMM Integration
      • DLMM SDK
        • DLMM TypeScript SDK
        • CPI Examples
      • DLMM API
      • Fetching information on locked liquidity in a DLMM
    • Dynamic AMM Pool Integration
      • Dynamic AMM SDK
        • Dynamic AMM TypeScript SDK
        • CPI Examples
      • Dynamic AMM API
        • Pool Info
        • Pool State
      • Setting Pool and Fee Config for Dynamic AMM Pools
      • Create Dynamic Pool with Timestamp/Slot Activation
      • Dynamic AMM - Farm Integration
    • DAMM v2 Integration
      • DAMM v2 SDK
        • DAMM v2 TypeScript SDK
        • DAMM v2 Rust SDK
      • Setting Pool and Fee Config for DAMM v2
      • Technical FAQ
    • Memecoin Pool Integration
      • Memecoin Pool v2 Integration
        • Setting Pool and Fee Config for Memecoin Pool v2
      • Memecoin Pool v1 Integration
        • TypeScript Code Examples
        • CPI Examples
        • Setting Pool and Fee Config for Memecoin Pool v1
        • Track permanently-locked liquidity in Memecoin Pool v1
        • Track Protocol Fee from swaps in Memecoin Pool v1
    • Stake2Earn Pool Integration
    • Dynamic Vault Integration
      • Using TypeScript-Client
      • Using Rust-Client
      • Using CPI
      • Vault API
        • Vault Info
        • Vault State
      • Vault Developer Resources
    • Alpha Vault Integration
      • Alpha Vault TypeScript SDK
      • Alpha Vault without Whitelist Setup
      • Alpha Vault with Whitelist Setup
    • Dynamic Bonding Curve (DBC) Integration
      • DBC SDK
        • DBC TypeScript SDK
        • DBC Rust SDK
      • DBC Fee Scheduler Formula
      • DBC Scripts
      • Program Repo
      • Launchpad Template
      • Technical FAQ
  • TOKEN LAUNCH POOLS
    • Steps to Create a Pool for a Token Launch
      • Create: DLMM Launch Pool
      • Create: Dynamic AMM Pool
      • Create: Memecoin Pool v1
      • Create: DAMM v2 Pool
      • Create: Stake2Earn Pool
      • Create: Pools with Alpha Vault
        • Create: DLMM Launch Pool with Alpha Vault
        • Create: Dynamic AMM Pool with Alpha Vault
        • Create: Memecoin Pool with Alpha Vault
        • Create: Stake2Earn Pool with Alpha Vault
    • Anti-Sniper Fee Suite for a Token Launch
  • Resources
    • Audits
    • Meteora Program IDs
    • Meteora APIs
    • Devnet Testing
    • Community Data Dashboards & Tools
    • Meteora Brand Assets
    • THE MASSIVE METEORA STIMULUS PACKAGE
      • Overview
      • 1. Dynamic Liquidity Market Maker (DLMM)
      • 2. Formation Of An LP Army DAO
      • 3. The 10% Stimulus Proposal
  • USER FAQ
    • Getting Started LPing
      • Supported Wallets
      • Prepare SOL
      • SOL required for Rent
      • What is Wrapped SOL?
      • What is an AMM?
      • What does it mean to provide liquidity?
      • How to swap to the tokens required for adding liquidity to a pool
      • How to quickly check if a token has any risks
      • Viewing your transaction history
      • My wallet has been compromised. What should I do?
    • Differences between DLMM and Dynamic Pools
    • DLMM FAQ
    • Dynamic AMM FAQ
      • How is the pool price of the token calculated in a Dynamic AMM?
      • What is a Meteora LP token?
      • How do I see fees earned on a Dynamic AMM Pool?
      • How to track your earnings for a Dynamic Pool?
      • What is Virtual Price in a Dynamic Pool?
      • How do LP tokens, fees, and virtual price work for Dynamic Pools?
      • Why must I add liquidity in non-stable Dynamic Pools using a 50:50 value ratio?
      • What is AMP in a Dynamic Pool with stable coins?
      • Why is the USDT-USDC pool not 1:1 in ratio of assets?
      • Can I create an LST, FX, or Multi-token pool using the Dynamic Pool creation tool?
    • Alpha Vault FAQ
    • Why is the token sometimes not picked up and tradable on Jupiter?
    • How do I create a new farm?
    • Video Tutorials to Get Started
      • LP Army Boot Camp
      • DLMM Strategy Sessions / Jam Sessions
  • Security and Risks
    • Risk of Impermanent Loss (IL)
    • Risk of depositing into an imbalanced pool / pool with price out of sync
    • Smart contract risk
    • Risk of a stablecoin depeg
    • Operational risk for dynamic vaults and pools
    • Lending risk for dynamic vaults and pools
  • legal
    • Terms of Service
    • Stake2Earn Terms of Service
Powered by GitBook
On this page
  • Add mercurial-vault to dependencies
  • Access Vault information through Vault State
  • To deposit liquidity into the vault
  • Withdraw liquidity from pool
  • Withdraw direct from Lending Platforms
  1. INTEGRATION
  2. Dynamic Vault Integration

Using Rust-Client

Add mercurial-vault to dependencies

If you are using rust, you can include the mercurial-vault link in your Cargo.toml file under "dependencies".

mercurial-vault = { version="0.4.2", features= ["cpi", "mainnet"] }

You can access all the main functions after you've added the mercurial-vault dependency to your code.

Access Vault information through Vault State

Find the vault pubkey based on the token_mint (USDC, USDT, or SOL)

let (vault, _vault_bump) = Pubkey::find_program_address(
    &[b"vault".as_ref(), token_mint.as_ref(), mercurial_vault::get_base_key().as_ref()],
    &mercurial_vault::id(),

Get the vault_state using the vault pubkey

let vault_state: mercurial_vault::state::Vault = program_client.account(vault)?;

Once you have the vault state, you can retrieve key vault information:

Total unlocked amount in vault

Get the total amount of tokens that are available for withdrawal. The vault will lock the yield claimed initially and release them over a pre-defined period of time.

// Total unlocked amount in the vault available for withdrawal
// current_time is current Solana node timestamp
total_unlocked_amount = vault_data.get_unlocked_amount(current_time)

Total reserve in the vault

We currently keep 10% of the total deposits in the vault to support swaps and immediate withdrawals.

// Get the vault reserve value

// Get the token vault data
let token_data: anchor_spl::token::TokenAccount =
    program_client.account(vault_data.token_vault)?;
    
vault_reserves = token_data.amount //vault reserve value

Total LP supply for the vault

Retrieve the total LP token amount in the vault.

// Get total total LP supply
let token_mint: anchor_spl::token::Mint = program_client.account(vault_data.lp_mint)?;
total_lp_supply = token_mint.supply

Vault virtual price calculation

Get the current virtual price for the vault.

// Calculate vault virtual price using unlocked amount and lp supply
virtual_price = total_unlocked_amount / total_lp_supply

To deposit liquidity into the vault

Retrieve the vault, token_vault, lp_mint, user_token and user_lp values

// Retrieve the value for vault
// You can use the one of our supported token_mints (you can find the token_mint values in the "Constants" section
let (vault, _vault_bump) = Pubkey::find_program_address(
    &[b"vault".as_ref(), token_mint.as_ref(), mercurial_vault::get_base_key().as_ref()],
    &mercurial_vault::id(),
);

// Retrieve the value of token_vault
let (token_vault, _token_vault_bump) = Pubkey::find_program_address(
    &[b"token_vault".as_ref(), vault.as_ref()],
    &program_client.id(),
);

//Retrieve the value of vault_state to get the lp_mint value
let vault_state: mercurial_vault::state::Vault = program_client.account(vault)?;
let lp_mint = vault_state.lp_mint;

// Retrieve user_token and user_lp values using the get_or_create_ata fn
let user_token = get_or_create_ata(program_client, token_mint, program_client.payer())?;
let user_lp = get_or_create_ata(program_client, lp_mint, program_client.payer())?;

Execute the instruction to deposit liquidity into the pool

let builder = program_client
    .request()
    .accounts(mercurial_vault::accounts::DepositWithdrawLiquidity {
        vault: vault,
        token_vault: token_vault,
        lp_mint: lp_mint,
        user_token,
        user_lp,
        user: program_client.payer(),
        token_program: spl_token::id(),
    })
    .args(mercurial_vault::instruction::Deposit {
        token_amount, // total amount of tokens to be deposited
        minimum_lp_token_amount: 0,
});

let signature = builder.send()?;

Withdraw liquidity from pool

Retrieve the vault, token_vault, lp_mint, user_token and user_lp values

// Retrieve the vault
let (vault, _vault_bump) = Pubkey::find_program_address(
    &[b"vault".as_ref(), token_mint.as_ref(), mercurial_vault::get_base_key().as_ref()],
    &mercurial_vault::id(),
);

// Retrieve the vault of token_vault
let (token_vault, _token_vault_bump) = Pubkey::find_program_address(
    &[b"token_vault".as_ref(), vault.as_ref()],
    &program_client.id(),
);

//Retrieve the value of vault_state to get the lp_mint value
let vault_state: mercurial_vault::state::Vault = program_client.account(vault)?;
let lp_mint = vault_state.lp_mint;

// Retrieve user_token and user_lp values using the get_or_create_ata fn
let user_token = get_or_create_ata(program_client, token_mint, program_client.payer())?;
let user_lp = get_or_create_ata(program_client, lp_mint, program_client.payer())?;

Execute the instruction to withdraw liquidity from pool.

let builder = program_client
    .request()
    .accounts(mercurial_vault::accounts::DepositWithdrawLiquidity {
        vault: vault,
        token_vault: token_vault,
        lp_mint: lp_mint,
        user_token,
        user_lp,
        user: program_client.payer(),
        token_program: spl_token::id(),
    })
    .args(mercurial_vault::instruction::Withdraw {
        unmint_amount, //total amount of LP tokens to withdraw
        min_out_amount: 0,
    });

let signature = builder.send()?;

Withdraw direct from Lending Platforms

If you are integrating the vaults directly onto your platform, you can use this function to withdraw funds directly from a selected lending platform.

You can retrieve the list of pubkeys of the lending platform pools integrated with the vault

// Get the vault pubkey using the token_mint of the vault
let (vault, _) = Pubkey::find_program_address(
    &[b"vault".as_ref(), token_mint.as_ref(), mercurial_vault::get_base_key().as_ref()],
    &mercurial_vault::id(),
);

// Retrieve the vault state
let vault_data: mercurial_vault::state::Vault = program_client.account(vault)?;

// Iterate through all the lending platform pools integrated with the vault
for (i, &strategy_pubkey) in vault_data.strategies.iter().enumerate(){
    // get strategy pubkey
    println!("{}", strategy_pubkey);
}

Call the function to withdraw funds from the selected strategy pool.

// Get the strategy_state using the strategy_pubkey
let strategy_state: mercurial_vault::state::Strategy =
    program_client.account(strategy_pubkey)?;

// Call the fn to withdraw funds directly from the strategy
let strategy_handler = get_strategy_handler(strategy_state.strategy_type);
strategy_handler.withdraw_directly_from_strategy(
    &program_client,
    strategy,
    token_mint,
    base,
    unmint_amount,
)?

You can find the example implementation for withdrawing from a Mango via the link below:

PreviousUsing TypeScript-ClientNextUsing CPI

Last updated 6 months ago

https://github.com/mercurial-finance/vault-sdk/blob/main/rust-client/src/strategy_handler/mango.rs