pub mod global;
pub mod types;
pub mod wallet;
use solana_client::{
nonblocking::rpc_client::RpcClient, rpc_client::GetConfirmedSignaturesForAddress2Config,
};
use solana_sdk::epoch_info::EpochInfo;
use std::sync::Arc;
use crate::{
global::{SOLANA_DEV_NET_URL, SOLANA_MAIN_NET_URL, SOLANA_TEST_NET_URL},
types::Mode,
wallet::Wallet,
};
pub struct Solana {
mode: Mode,
pub client: Option<Arc<RpcClient>>,
}
impl Solana {
pub fn new(mode: Mode) -> Result<Solana, String> {
let mut url = String::new();
match mode {
Mode::MAIN => {
url = SOLANA_MAIN_NET_URL.to_string();
}
Mode::TEST => {
url = SOLANA_TEST_NET_URL.to_string();
}
Mode::DEV => {
url = SOLANA_DEV_NET_URL.to_string();
}
_ => {
return Err("create solana client mode does not meet requirements".to_string());
}
}
let client = RpcClient::new(url.clone());
Ok(Self {
mode,
client: Some(Arc::new(client)),
})
}
pub fn client_arc(&self) -> Arc<RpcClient> {
Arc::clone(&self.client.as_ref().unwrap())
}
pub async fn core_version(&self) -> Result<String, String> {
match self.client_arc().get_version().await {
Ok(version) => {
return Ok(version.solana_core);
}
Err(e) => {
return Err(format!("get core version error: {:?}", e));
}
}
}
pub async fn feature_set(&self) -> Result<String, String> {
match self.client_arc().get_version().await {
Ok(version) => {
return Ok(version.feature_set.unwrap().to_string());
}
Err(e) => {
return Err(format!("get core version error: {:?}", e));
}
}
}
pub async fn block_height(&self) -> Result<u64, String> {
match self.client_arc().get_block_height().await {
Ok(h) => {
return Ok(h);
}
Err(e) => {
return Err(format!("get core version error: {:?}", e));
}
}
}
pub async fn last_block_hash(&self) -> Result<String, String> {
match self.client_arc().get_latest_blockhash().await {
Ok(h) => {
return Ok(h.to_string());
}
Err(e) => {
return Err(format!("get core version error: {:?}", e));
}
}
}
pub async fn slot(&self) -> Result<u64, String> {
match self.client_arc().get_slot().await {
Ok(slot) => {
return Ok(slot);
}
Err(e) => {
return Err(format!("get core version error: {:?}", e));
}
}
}
pub async fn epoch(&self) -> Result<EpochInfo, String> {
match self.client_arc().get_epoch_info().await {
Ok(epoch) => {
return Ok(epoch);
}
Err(e) => {
return Err(format!("get core version error: {:?}", e));
}
}
}
}