#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use std::time::{SystemTime, UNIX_EPOCH};
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
use web_time::{SystemTime, UNIX_EPOCH};
mod execution;
use std::fmt;
use crate::{
config::HttpConfig,
domain::{
Source, Timestamp,
solana::{
AccountLookup, Context, Hash, NativeBalance, Network, Observation, Operation, Pubkey,
ReadOptions, TokenBalance,
},
},
error::{Error, ProviderError},
transport::{HttpClient, OperationBudget},
};
use super::wire::{
BinaryAccount, ParsedAccount, RequestOptions, SlotValue, error_policy, invalid_response,
};
#[derive(Clone, Debug)]
pub struct SolanaHttpConfig {
network: Network,
http: HttpConfig,
}
impl SolanaHttpConfig {
#[must_use]
pub const fn new(network: Network, http: HttpConfig) -> Self {
Self { network, http }
}
#[must_use]
pub const fn network(&self) -> &Network {
&self.network
}
#[must_use]
pub const fn http_config(&self) -> &HttpConfig {
&self.http
}
}
pub struct SolanaClient {
config: SolanaHttpConfig,
http: HttpClient,
}
impl SolanaClient {
pub async fn connect(config: SolanaHttpConfig) -> Result<Self, Error> {
let budget = OperationBudget::new(config.http_config().limits())?;
let http = HttpClient::new(config.http_config())?;
let client = Self { config, http };
budget.run(client.verify_genesis(&budget)).await?;
Ok(client)
}
#[must_use]
pub const fn config(&self) -> &SolanaHttpConfig {
&self.config
}
#[must_use]
pub const fn genesis_hash(&self) -> Hash {
self.config.network().genesis_hash()
}
pub async fn get_native_balance(
&self,
address: Pubkey,
options: ReadOptions,
) -> Result<Observation<NativeBalance>, Error> {
let budget = OperationBudget::new(self.config.http_config().limits())?;
budget
.run(async {
self.verify_genesis(&budget).await?;
let result: SlotValue<u64> = self
.http
.read_rpc(
&budget,
"getBalance",
&(address, RequestOptions::new(options, None)),
error_policy,
)
.await?
.ok_or_else(invalid_response)?;
let context = self.context(
Operation::NativeBalance,
options,
result.context.slot,
"getBalance",
)?;
Observation::native_balance(
NativeBalance::new(self.config.network().clone(), address, result.value),
context,
)
.map_err(|_| invalid_response())
})
.await
}
pub async fn get_account(
&self,
address: Pubkey,
options: ReadOptions,
) -> Result<Observation<AccountLookup>, Error> {
let budget = OperationBudget::new(self.config.http_config().limits())?;
budget
.run(async {
self.verify_genesis(&budget).await?;
let result: SlotValue<Option<BinaryAccount>> = self
.http
.read_rpc(
&budget,
"getAccountInfo",
&(address, RequestOptions::new(options, Some("base64"))),
error_policy,
)
.await?
.ok_or_else(invalid_response)?;
let context = self.context(
Operation::Account,
options,
result.context.slot,
"getAccountInfo",
)?;
let account = result
.value
.map(|value| value.into_account(self.config.network().clone(), address))
.transpose()?;
let lookup = AccountLookup::new(self.config.network().clone(), address, account)
.map_err(|_| invalid_response())?;
Observation::account(lookup, context).map_err(|_| invalid_response())
})
.await
}
pub async fn get_token_balance(
&self,
token_account: Pubkey,
options: ReadOptions,
) -> Result<Observation<TokenBalance>, Error> {
let budget = OperationBudget::new(self.config.http_config().limits())?;
budget
.run(async {
self.verify_genesis(&budget).await?;
let result: SlotValue<Option<ParsedAccount>> = self
.http
.read_rpc(
&budget,
"getAccountInfo",
&(
token_account,
RequestOptions::new(options, Some("jsonParsed")),
),
error_policy,
)
.await?
.ok_or_else(invalid_response)?;
let context = self.context(
Operation::TokenBalance,
options,
result.context.slot,
"getAccountInfo",
)?;
let balance = result
.value
.ok_or(Error::UnavailableData)?
.into_balance(self.config.network().clone(), token_account)?;
Observation::token_balance(balance, context).map_err(|_| invalid_response())
})
.await
}
async fn verify_genesis(&self, budget: &OperationBudget) -> Result<(), Error> {
let value: Hash = self
.http
.read_rpc(budget, "getGenesisHash", &[] as &[(); 0], error_policy)
.await?
.ok_or_else(invalid_response)?;
if value != self.config.network().genesis_hash() {
return Err(Error::Provider(ProviderError::ChainMismatch));
}
Ok(())
}
fn context(
&self,
operation: Operation,
options: ReadOptions,
slot: u64,
method: &'static str,
) -> Result<Context, Error> {
let retrieved_at = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|duration| Timestamp::from_unix_seconds(duration.as_secs()))
.map_err(|_| Error::Configuration)?;
let source = Source::new(
self.config.http_config().provider_id(),
method,
env!("CARGO_PKG_VERSION"),
)?;
Context::new(
operation,
self.config.network().clone(),
options,
slot,
source,
retrieved_at,
)
.map_err(|_| invalid_response())
}
}
impl fmt::Debug for SolanaClient {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("SolanaClient")
.field("config", &self.config)
.finish_non_exhaustive()
}
}
impl super::NativeBalanceReader for SolanaClient {
fn network(&self) -> &Network {
self.config.network()
}
async fn get_native_balance(
&self,
address: Pubkey,
options: ReadOptions,
) -> Result<Observation<NativeBalance>, Error> {
Self::get_native_balance(self, address, options).await
}
}
impl super::AccountReader for SolanaClient {
fn network(&self) -> &Network {
self.config.network()
}
async fn get_account(
&self,
address: Pubkey,
options: ReadOptions,
) -> Result<Observation<AccountLookup>, Error> {
Self::get_account(self, address, options).await
}
}
impl super::TokenBalanceReader for SolanaClient {
fn network(&self) -> &Network {
self.config.network()
}
async fn get_token_balance(
&self,
address: Pubkey,
options: ReadOptions,
) -> Result<Observation<TokenBalance>, Error> {
Self::get_token_balance(self, address, options).await
}
}