waterpump-solana-core 0.1.0

Core Solana utilities: RPC context, transaction builder, retry helpers
Documentation
//! RPC context for Solana client operations
//!
//! Provides a wrapper around `RpcClient` with a configurable commitment level,
//! enabling ergonomic commitment control across all trait methods that need
//! RPC access.

use std::{fmt, sync::Arc};

use solana_client::nonblocking::rpc_client::RpcClient;
use solana_commitment_config::CommitmentConfig;

/// Context wrapping an RPC client with a commitment configuration.
///
/// `RpcContext` replaces bare `&RpcClient` parameters in trait methods,
/// giving callers a single place to specify commitment level while still
/// providing transparent access to the underlying client.
#[derive(Clone)]
pub struct RpcContext {
    /// The underlying RPC client.
    pub client: Arc<RpcClient>,
    /// The commitment level to use for queries.
    pub commitment: CommitmentConfig,
}

impl RpcContext {
    /// Create a new `RpcContext` with the default commitment level
    /// (`confirmed`).
    pub fn new(client: Arc<RpcClient>) -> Self {
        Self { client, commitment: CommitmentConfig::confirmed() }
    }

    /// Create a new `RpcContext` with a specific commitment level.
    pub fn with_commitment(client: Arc<RpcClient>, commitment: CommitmentConfig) -> Self {
        Self { client, commitment }
    }

    /// Create a new `RpcContext` with `finalized` commitment.
    pub fn finalized(client: Arc<RpcClient>) -> Self {
        Self { client, commitment: CommitmentConfig::finalized() }
    }

    /// Create a new `RpcContext` with `confirmed` commitment.
    pub fn confirmed(client: Arc<RpcClient>) -> Self {
        Self { client, commitment: CommitmentConfig::confirmed() }
    }
}

impl fmt::Debug for RpcContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RpcContext").field("commitment", &self.commitment).finish_non_exhaustive()
    }
}

impl From<Arc<RpcClient>> for RpcContext {
    fn from(client: Arc<RpcClient>) -> Self { Self::new(client) }
}