solana-pipkit 2.2.0

Pragmatic Rust utilities for Solana development - safety, speed, analytics, and DeFi
Documentation
//! Liquidity pool interactions for Raydium, Orca, and other DEXes.

use solana_client::rpc_client::RpcClient;
use solana_sdk::pubkey::Pubkey;
use crate::{Result, ToolkitError};
use super::Dex;

#[derive(Debug, Clone)]
pub struct PoolInfo {
    pub address: Pubkey,
    pub dex: Dex,
    pub token_a: TokenInfo,
    pub token_b: TokenInfo,
    pub reserve_a: u64,
    pub reserve_b: u64,
    pub lp_mint: Pubkey,
    pub lp_supply: u64,
    pub fee_rate_bps: u16,
    pub tvl_usd: Option<f64>,
    pub volume_24h_usd: Option<f64>,
    pub apy: Option<f64>,
}

#[derive(Debug, Clone)]
pub struct TokenInfo {
    pub mint: Pubkey,
    pub symbol: Option<String>,
    pub decimals: u8,
    pub vault: Pubkey,
}

#[derive(Debug, Clone)]
pub struct SwapQuote {
    pub input_mint: Pubkey,
    pub output_mint: Pubkey,
    pub input_amount: u64,
    pub output_amount: u64,
    pub min_output_amount: u64,
    pub price_impact_pct: f64,
    pub fee_amount: u64,
    pub route: Vec<Pubkey>,
}

#[derive(Debug, Clone)]
pub struct PoolReserves {
    pub token_a: u64,
    pub token_b: u64,
    pub last_update_slot: u64,
}

pub struct PoolManager<'a> {
    client: &'a RpcClient,
}

impl<'a> PoolManager<'a> {
    pub fn new(client: &'a RpcClient) -> Self {
        Self { client }
    }

    pub async fn get_info(&self, pool: &Pubkey) -> Result<PoolInfo> {
        let account = self.client
            .get_account(pool)
            .map_err(|e| ToolkitError::RpcError(e.to_string()))?;

        let dex = super::identify_dex(&account.owner).unwrap_or(Dex::Unknown);

        match dex {
            Dex::RaydiumAmm => self.parse_raydium_amm_pool(pool, &account.data).await,
            Dex::RaydiumClmm => self.parse_raydium_clmm_pool(pool, &account.data).await,
            Dex::OrcaWhirlpool => self.parse_orca_whirlpool(pool, &account.data).await,
            _ => Err(ToolkitError::UnsupportedOperation("Unknown pool type".to_string())),
        }
    }

    pub async fn find_pools(&self, _token_a: &Pubkey, _token_b: &Pubkey) -> Result<Vec<PoolInfo>> {
        // In production, this would query an indexer or iterate known pool registries
        // For now, return empty and let caller use Jupiter for routing
        Ok(Vec::new())
    }

    pub async fn get_reserves(&self, pool: &Pubkey) -> Result<PoolReserves> {
        let info = self.get_info(pool).await?;
        
        Ok(PoolReserves {
            token_a: info.reserve_a,
            token_b: info.reserve_b,
            last_update_slot: 0, // Would need slot from account info
        })
    }

    async fn parse_raydium_amm_pool(&self, pool: &Pubkey, data: &[u8]) -> Result<PoolInfo> {
        // Raydium AMM V4 pool layout (simplified)
        // Full layout is ~752 bytes
        if data.len() < 200 {
            return Err(ToolkitError::ParseError("Invalid Raydium pool data".to_string()));
        }

        // Extract key fields from the pool account
        // Note: Actual offsets depend on the exact Raydium version
        let token_a_mint = Pubkey::try_from(&data[72..104])
            .map_err(|_| ToolkitError::ParseError("Invalid token A mint".to_string()))?;
        let token_b_mint = Pubkey::try_from(&data[104..136])
            .map_err(|_| ToolkitError::ParseError("Invalid token B mint".to_string()))?;
        let token_a_vault = Pubkey::try_from(&data[136..168])
            .map_err(|_| ToolkitError::ParseError("Invalid token A vault".to_string()))?;
        let token_b_vault = Pubkey::try_from(&data[168..200])
            .map_err(|_| ToolkitError::ParseError("Invalid token B vault".to_string()))?;

        // Get vault balances for reserves
        let reserve_a = self.get_vault_balance(&token_a_vault).await?;
        let reserve_b = self.get_vault_balance(&token_b_vault).await?;

        Ok(PoolInfo {
            address: *pool,
            dex: Dex::RaydiumAmm,
            token_a: TokenInfo {
                mint: token_a_mint,
                symbol: None,
                decimals: 9, // Would need to fetch from mint
                vault: token_a_vault,
            },
            token_b: TokenInfo {
                mint: token_b_mint,
                symbol: None,
                decimals: 9,
                vault: token_b_vault,
            },
            reserve_a,
            reserve_b,
            lp_mint: Pubkey::default(), // Would extract from pool data
            lp_supply: 0,
            fee_rate_bps: 25, // Raydium default is 0.25%
            tvl_usd: None,
            volume_24h_usd: None,
            apy: None,
        })
    }

    async fn parse_raydium_clmm_pool(&self, _pool: &Pubkey, data: &[u8]) -> Result<PoolInfo> {
        // CLMM pools have different structure with concentrated liquidity
        if data.len() < 100 {
            return Err(ToolkitError::ParseError("Invalid CLMM pool data".to_string()));
        }

        // Placeholder - would need actual CLMM deserialization
        Err(ToolkitError::UnsupportedOperation("CLMM parsing not yet implemented".to_string()))
    }

    async fn parse_orca_whirlpool(&self, _pool: &Pubkey, data: &[u8]) -> Result<PoolInfo> {
        // Orca Whirlpool layout
        if data.len() < 100 {
            return Err(ToolkitError::ParseError("Invalid Whirlpool data".to_string()));
        }

        // Placeholder - would need actual Whirlpool deserialization
        Err(ToolkitError::UnsupportedOperation("Whirlpool parsing not yet implemented".to_string()))
    }

    async fn get_vault_balance(&self, vault: &Pubkey) -> Result<u64> {
        let account = self.client
            .get_account(vault)
            .map_err(|e| ToolkitError::RpcError(e.to_string()))?;

        if account.data.len() < 72 {
            return Err(ToolkitError::ParseError("Invalid token account".to_string()));
        }

        let amount = u64::from_le_bytes(
            account.data[64..72].try_into()
                .map_err(|_| ToolkitError::ParseError("Invalid amount".to_string()))?
        );

        Ok(amount)
    }
}

/// Calculate constant product swap output (x * y = k).
pub fn calculate_constant_product_swap(
    reserve_in: u64,
    reserve_out: u64,
    amount_in: u64,
    fee_bps: u16,
) -> SwapResult {
    let amount_in_with_fee = amount_in as u128 * (10000 - fee_bps as u128) / 10000;
    let numerator = amount_in_with_fee * reserve_out as u128;
    let denominator = reserve_in as u128 + amount_in_with_fee;
    let amount_out = (numerator / denominator) as u64;

    let fee_amount = amount_in.saturating_sub(amount_in_with_fee as u64);

    // Price impact calculation
    let price_before = reserve_out as f64 / reserve_in as f64;
    let new_reserve_in = reserve_in + amount_in;
    let new_reserve_out = reserve_out - amount_out;
    let price_after = new_reserve_out as f64 / new_reserve_in as f64;
    let price_impact = ((price_before - price_after) / price_before).abs() * 100.0;

    SwapResult {
        amount_out,
        fee_amount,
        price_impact_pct: price_impact,
    }
}

#[derive(Debug, Clone)]
pub struct SwapResult {
    pub amount_out: u64,
    pub fee_amount: u64,
    pub price_impact_pct: f64,
}

/// Calculate the price of token A in terms of token B.
pub fn calculate_pool_price(reserve_a: u64, reserve_b: u64, decimals_a: u8, decimals_b: u8) -> f64 {
    let adjusted_a = reserve_a as f64 / 10_f64.powi(decimals_a as i32);
    let adjusted_b = reserve_b as f64 / 10_f64.powi(decimals_b as i32);
    adjusted_b / adjusted_a
}

/// Calculate TVL from reserves and prices.
pub fn calculate_tvl(reserve_a: u64, reserve_b: u64, price_a_usd: f64, price_b_usd: f64, decimals_a: u8, decimals_b: u8) -> f64 {
    let value_a = (reserve_a as f64 / 10_f64.powi(decimals_a as i32)) * price_a_usd;
    let value_b = (reserve_b as f64 / 10_f64.powi(decimals_b as i32)) * price_b_usd;
    value_a + value_b
}

/// Check if pool has sufficient liquidity for a swap.
pub fn has_sufficient_liquidity(reserve: u64, amount: u64, min_ratio: f64) -> bool {
    amount as f64 / reserve as f64 <= min_ratio
}