wp-solana-pool-traits 0.1.1

Traits and utilities for Solana liquidity pool operations: PoolViewer, PoolInfuser, PositionViewer
Documentation
use anyhow::Result;
use async_trait::async_trait;
use wp_solana_rpc::RpcContext;

use crate::types::Price;

/// Slot0 information structure containing current pool state
///
/// Similar to Uniswap V3's slot0, this contains the current price and tick
/// information for the pool. This is the most frequently accessed pool state
/// data.
#[derive(Debug, Clone)]
pub struct Slot0 {
    /// Current sqrt price (Q64.64 format)
    pub sqrt_price: u128,
    /// Current tick index
    pub tick_current_index: i32,
    /// Total liquidity in the pool
    pub liquidity: u128,
}

/// Trait for viewing pool state and prices
///
/// This trait provides methods to query pool information without modifying
/// state. It's similar to evm-pool-lib's PoolViewer but adapted for Solana's
/// RPC client.
#[async_trait]
pub trait PoolViewer: Send + Sync {
    /// Get the current price of token A in terms of token B
    ///
    /// # Arguments
    /// * `ctx` - The RPC context to use for queries
    ///
    /// # Returns
    /// A Price representing token A per token B
    async fn currency_a_price(&self, ctx: &RpcContext) -> Result<Price>;

    /// Get the current price of token B in terms of token A
    ///
    /// # Arguments
    /// * `ctx` - The RPC context to use for queries
    ///
    /// # Returns
    /// A Price representing token B per token A
    async fn currency_b_price(&self, ctx: &RpcContext) -> Result<Price>;

    /// Get the current liquidity in the pool
    ///
    /// # Arguments
    /// * `ctx` - The RPC context to use for queries
    ///
    /// # Returns
    /// The total liquidity in the pool
    async fn liquidity(&self, ctx: &RpcContext) -> Result<u128>;

    /// Get the current tick index
    ///
    /// # Arguments
    /// * `ctx` - The RPC context to use for queries
    ///
    /// # Returns
    /// The current tick index
    async fn tick_current_index(&self, ctx: &RpcContext) -> Result<i32>;

    /// Get the current sqrt price
    ///
    /// # Arguments
    /// * `ctx` - The RPC context to use for queries
    ///
    /// # Returns
    /// The current sqrt price (Q64.64 format)
    async fn sqrt_price(&self, ctx: &RpcContext) -> Result<u128>;

    /// Get slot0 information (current pool state)
    ///
    /// This method fetches and returns the current pool state (slot0),
    /// including current price, tick, and liquidity information. This is
    /// the most frequently accessed pool data.
    ///
    /// # Arguments
    /// * `ctx` - The RPC context to use for queries
    ///
    /// # Returns
    /// A `Slot0` struct containing current pool state information
    async fn get_slot0(&self, ctx: &RpcContext) -> Result<Slot0>;
}