|
| 1 | +use anchor_lang::{prelude::Clock, AccountDeserialize}; |
| 2 | +use clockwork_utils::ProgramLogsDeserializable; |
| 3 | +use solana_client::{ |
| 4 | + client_error, rpc_client::RpcClient, |
| 5 | + rpc_response::RpcSimulateTransactionResult, |
| 6 | +}; |
| 7 | +use solana_sdk::{ |
| 8 | + commitment_config::CommitmentConfig, |
| 9 | + hash::Hash, |
| 10 | + instruction::Instruction, |
| 11 | + program_error::ProgramError, |
| 12 | + pubkey::Pubkey, |
| 13 | + signature::{Keypair, Signature, Signer}, |
| 14 | + signers::Signers, |
| 15 | + transaction::Transaction, |
| 16 | +}; |
| 17 | +use std::{ |
| 18 | + fmt::Debug, |
| 19 | + ops::{Deref, DerefMut}, |
| 20 | + str::FromStr, |
| 21 | +}; |
| 22 | +use thiserror::Error; |
| 23 | + |
| 24 | + |
| 25 | +#[derive(Debug, Error)] |
| 26 | +pub enum ClientError { |
| 27 | + #[error(transparent)] |
| 28 | + Client(#[from] client_error::ClientError), |
| 29 | + |
| 30 | + #[error(transparent)] |
| 31 | + Program(#[from] ProgramError), |
| 32 | + |
| 33 | + #[error("Failed to deserialize account data")] |
| 34 | + DeserializationError, |
| 35 | +} |
| 36 | + |
| 37 | +pub type ClientResult<T> = Result<T, ClientError>; |
| 38 | + |
| 39 | +pub struct Client { |
| 40 | + pub client: RpcClient, |
| 41 | + pub payer: Keypair, |
| 42 | +} |
| 43 | + |
| 44 | +impl Client { |
| 45 | + pub fn new(payer: Keypair, url: String) -> Self { |
| 46 | + let client = RpcClient::new_with_commitment::<String>(url, CommitmentConfig::processed()); |
| 47 | + Self { client, payer } |
| 48 | + } |
| 49 | + |
| 50 | + pub fn get<T: AccountDeserialize>(&self, pubkey: &Pubkey) -> ClientResult<T> { |
| 51 | + let data = self.client.get_account_data(pubkey)?; |
| 52 | + T::try_deserialize(&mut data.as_slice()).map_err(|_| ClientError::DeserializationError) |
| 53 | + } |
| 54 | + |
| 55 | + pub fn get_clock(&self) -> ClientResult<Clock> { |
| 56 | + let clock_pubkey = Pubkey::from_str("SysvarC1ock11111111111111111111111111111111").unwrap(); |
| 57 | + let clock_data = self.client.get_account_data(&clock_pubkey)?; |
| 58 | + bincode::deserialize::<Clock>(&clock_data).map_err(|_| ClientError::DeserializationError) |
| 59 | + } |
| 60 | + |
| 61 | + pub fn get_return_data<T: ProgramLogsDeserializable>( |
| 62 | + &self, |
| 63 | + ix: Instruction, |
| 64 | + ) -> ClientResult<T> { |
| 65 | + // let result = self.simulate_transaction(&[ix.clone()], &[self.payer()])?; |
| 66 | + |
| 67 | + // After we can upgrade our Solana SDK version to 1.14.0 we can just use the below code: |
| 68 | + // let data = result.logs; |
| 69 | + // Ok(T::try_from_slice(logs, &data) |
| 70 | + // .map_err(|_| ClientError::DeserializationError)?) |
| 71 | + // |
| 72 | + // But for the time being since RpcSimulateTransactionResult.data does not exist yet, |
| 73 | + // We can only parse the logs ourselves to find the return_data |
| 74 | + let logs = self.get_instruction_logs(ix.clone())?; |
| 75 | + T::try_from_program_logs(logs, &ix.program_id) |
| 76 | + .map_err(|_| ClientError::DeserializationError) |
| 77 | + } |
| 78 | + |
| 79 | + pub fn get_instruction_logs(&self, ix: Instruction) -> ClientResult<Vec<String>> { |
| 80 | + let result = self.simulate_transaction(&[ix], &[self.payer()])?; |
| 81 | + let logs = result.logs.ok_or(ClientError::DeserializationError)?; |
| 82 | + Ok(logs) |
| 83 | + } |
| 84 | + |
| 85 | + pub fn payer(&self) -> &Keypair { |
| 86 | + &self.payer |
| 87 | + } |
| 88 | + |
| 89 | + pub fn payer_pubkey(&self) -> Pubkey { |
| 90 | + self.payer.pubkey() |
| 91 | + } |
| 92 | + |
| 93 | + pub fn latest_blockhash(&self) -> ClientResult<Hash> { |
| 94 | + Ok(self.client.get_latest_blockhash()?) |
| 95 | + } |
| 96 | + |
| 97 | + pub fn airdrop(&self, to_pubkey: &Pubkey, lamports: u64) -> ClientResult<Signature> { |
| 98 | + let blockhash = self.client.get_latest_blockhash()?; |
| 99 | + let signature = self.request_airdrop_with_blockhash(to_pubkey, lamports, &blockhash)?; |
| 100 | + self.confirm_transaction_with_spinner(&signature, &blockhash, self.commitment())?; |
| 101 | + Ok(signature) |
| 102 | + } |
| 103 | + |
| 104 | + pub fn send_and_confirm<T: Signers>( |
| 105 | + &self, |
| 106 | + ixs: &[Instruction], |
| 107 | + signers: &T, |
| 108 | + ) -> ClientResult<Signature> { |
| 109 | + let tx = self.transaction(ixs, signers)?; |
| 110 | + Ok(self.send_and_confirm_transaction(&tx)?) |
| 111 | + } |
| 112 | + |
| 113 | + pub fn simulate_transaction<T: Signers>( |
| 114 | + &self, |
| 115 | + ixs: &[Instruction], |
| 116 | + signers: &T, |
| 117 | + ) -> ClientResult<RpcSimulateTransactionResult> { |
| 118 | + let tx = self.transaction(ixs, signers)?; |
| 119 | + let result = self.client.simulate_transaction(&tx)?; |
| 120 | + if result.value.err.is_some() { |
| 121 | + Err(ClientError::DeserializationError) |
| 122 | + } else { |
| 123 | + Ok(result.value) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + fn transaction<T: Signers>( |
| 128 | + &self, |
| 129 | + ixs: &[Instruction], |
| 130 | + signers: &T, |
| 131 | + ) -> ClientResult<Transaction> { |
| 132 | + let mut tx = Transaction::new_with_payer(ixs, Some(&self.payer_pubkey())); |
| 133 | + tx.sign(signers, self.latest_blockhash()?); |
| 134 | + Ok(tx) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +impl Debug for Client { |
| 140 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 141 | + write!(f, "RPC client payer {}", self.payer_pubkey()) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +impl Deref for Client { |
| 146 | + type Target = RpcClient; |
| 147 | + |
| 148 | + fn deref(&self) -> &Self::Target { |
| 149 | + &self.client |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +impl DerefMut for Client { |
| 154 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 155 | + &mut self.client |
| 156 | + } |
| 157 | +} |
| 158 | + |
0 commit comments