use crate::client::LightconeClient;
use crate::error::SdkError;
use solana_pubkey::Pubkey;
#[cfg(feature = "solana-rpc")]
use solana_client::nonblocking::rpc_client::RpcClient as SolanaRpcClient;
#[cfg(feature = "solana-rpc")]
pub(crate) fn require_solana_rpc(client: &LightconeClient) -> Result<&SolanaRpcClient, SdkError> {
client.solana_rpc_client.as_ref().ok_or_else(|| {
SdkError::Other("RPC client not configured — use .rpc_url() on the builder".to_string())
})
}
pub struct Rpc<'a> {
pub(crate) client: &'a LightconeClient,
}
impl<'a> Rpc<'a> {
#[cfg(feature = "solana-rpc")]
pub fn inner(&self) -> Result<&SolanaRpcClient, SdkError> {
require_solana_rpc(self.client)
}
pub fn get_exchange_pda(&self) -> Pubkey {
crate::program::pda::get_exchange_pda(&self.client.program_id).0
}
pub fn get_global_deposit_token_pda(&self, mint: &Pubkey) -> Pubkey {
crate::program::pda::get_global_deposit_token_pda(mint, &self.client.program_id).0
}
pub fn get_user_global_deposit_pda(&self, user: &Pubkey, mint: &Pubkey) -> Pubkey {
crate::program::pda::get_user_global_deposit_pda(user, mint, &self.client.program_id).0
}
}
#[cfg(feature = "solana-rpc")]
impl<'a> Rpc<'a> {
pub async fn get_latest_blockhash(&self) -> Result<solana_hash::Hash, SdkError> {
let rpc = require_solana_rpc(self.client)?;
rpc.get_latest_blockhash()
.await
.map_err(|e| SdkError::Program(crate::program::error::SdkError::Rpc(e)))
}
pub async fn get_exchange(&self) -> Result<crate::program::accounts::Exchange, SdkError> {
let rpc = require_solana_rpc(self.client)?;
let (pda, _) = crate::program::pda::get_exchange_pda(&self.client.program_id);
let account = rpc.get_account(&pda).await.map_err(|e| {
SdkError::Program(crate::program::error::SdkError::AccountNotFound(format!(
"Exchange: {}",
e
)))
})?;
Ok(crate::program::accounts::Exchange::deserialize(
&account.data,
)?)
}
pub async fn get_global_deposit_token(
&self,
mint: &Pubkey,
) -> Result<crate::program::accounts::GlobalDepositToken, SdkError> {
let rpc = require_solana_rpc(self.client)?;
let (pda, _) =
crate::program::pda::get_global_deposit_token_pda(mint, &self.client.program_id);
let account = rpc.get_account(&pda).await.map_err(|e| {
SdkError::Program(crate::program::error::SdkError::AccountNotFound(format!(
"GlobalDepositToken: {}",
e
)))
})?;
Ok(crate::program::accounts::GlobalDepositToken::deserialize(
&account.data,
)?)
}
}