1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
use solana_client::{rpc_client::RpcClient, rpc_config::RpcSendTransactionConfig}; use solana_runtime::bank_client::BankClient; use solana_sdk::{ account::Account, client::{AsyncClient, SyncClient}, clock::Slot, commitment_config::CommitmentConfig, fee_calculator::FeeCalculator, hash::Hash, message::Message, pubkey::Pubkey, signature::{Signature, Signer}, signers::Signers, system_instruction, transaction::Transaction, transport::{Result, TransportError}, }; use solana_transaction_status::TransactionStatus; pub trait Client { fn send_transaction1(&self, transaction: Transaction) -> Result<Signature>; fn get_signature_statuses1( &self, signatures: &[Signature], ) -> Result<Vec<Option<TransactionStatus>>>; fn get_balance1(&self, pubkey: &Pubkey) -> Result<u64>; fn get_fees1(&self) -> Result<(Hash, FeeCalculator, Slot)>; fn get_slot1(&self) -> Result<Slot>; fn get_account1(&self, pubkey: &Pubkey) -> Result<Option<Account>>; } impl Client for RpcClient { fn send_transaction1(&self, transaction: Transaction) -> Result<Signature> { self.send_transaction_with_config( &transaction, RpcSendTransactionConfig { skip_preflight: true, ..RpcSendTransactionConfig::default() }, ) .map_err(|e| TransportError::Custom(e.to_string())) } fn get_signature_statuses1( &self, signatures: &[Signature], ) -> Result<Vec<Option<TransactionStatus>>> { self.get_signature_statuses(signatures) .map(|response| response.value) .map_err(|e| TransportError::Custom(e.to_string())) } fn get_balance1(&self, pubkey: &Pubkey) -> Result<u64> { self.get_balance(pubkey) .map_err(|e| TransportError::Custom(e.to_string())) } fn get_fees1(&self) -> Result<(Hash, FeeCalculator, Slot)> { let result = self .get_recent_blockhash_with_commitment(CommitmentConfig::default()) .map_err(|e| TransportError::Custom(e.to_string()))?; Ok(result.value) } fn get_slot1(&self) -> Result<Slot> { self.get_slot() .map_err(|e| TransportError::Custom(e.to_string())) } fn get_account1(&self, pubkey: &Pubkey) -> Result<Option<Account>> { self.get_account(pubkey) .map(Some) .map_err(|e| TransportError::Custom(e.to_string())) } } impl Client for BankClient { fn send_transaction1(&self, transaction: Transaction) -> Result<Signature> { self.async_send_transaction(transaction) } fn get_signature_statuses1( &self, signatures: &[Signature], ) -> Result<Vec<Option<TransactionStatus>>> { signatures .iter() .map(|signature| { self.get_signature_status(signature).map(|opt| { opt.map(|status| TransactionStatus { slot: 0, confirmations: None, status, err: None, }) }) }) .collect() } fn get_balance1(&self, pubkey: &Pubkey) -> Result<u64> { self.get_balance(pubkey) } fn get_fees1(&self) -> Result<(Hash, FeeCalculator, Slot)> { self.get_recent_blockhash_with_commitment(CommitmentConfig::default()) } fn get_slot1(&self) -> Result<Slot> { self.get_slot() } fn get_account1(&self, pubkey: &Pubkey) -> Result<Option<Account>> { self.get_account(pubkey) } } pub struct ThinClient<'a> { client: Box<dyn Client + 'a>, dry_run: bool, } impl<'a> ThinClient<'a> { pub fn new<C: Client + 'a>(client: C, dry_run: bool) -> Self { Self { client: Box::new(client), dry_run, } } pub fn send_transaction(&self, transaction: Transaction) -> Result<Signature> { if self.dry_run { return Ok(Signature::default()); } self.client.send_transaction1(transaction) } pub fn poll_for_confirmation(&self, signature: &Signature) -> Result<()> { while self.get_signature_statuses(&[*signature])?[0].is_none() { std::thread::sleep(std::time::Duration::from_millis(500)); } Ok(()) } pub fn get_signature_statuses( &self, signatures: &[Signature], ) -> Result<Vec<Option<TransactionStatus>>> { self.client.get_signature_statuses1(signatures) } pub fn send_and_confirm_message<S: Signers>( &self, message: Message, signers: &S, ) -> Result<(Transaction, Slot)> { if self.dry_run { return Ok((Transaction::new_unsigned(message), std::u64::MAX)); } let (blockhash, _fee_caluclator, last_valid_slot) = self.get_fees()?; let transaction = Transaction::new(signers, message, blockhash); self.send_transaction(transaction.clone())?; Ok((transaction, last_valid_slot)) } pub fn transfer<S: Signer>( &self, lamports: u64, sender_keypair: &S, to_pubkey: &Pubkey, ) -> Result<(Transaction, u64)> { let create_instruction = system_instruction::transfer(&sender_keypair.pubkey(), &to_pubkey, lamports); let message = Message::new(&[create_instruction], Some(&sender_keypair.pubkey())); self.send_and_confirm_message(message, &[sender_keypair]) } pub fn get_fees(&self) -> Result<(Hash, FeeCalculator, Slot)> { self.client.get_fees1() } pub fn get_slot(&self) -> Result<Slot> { self.client.get_slot1() } pub fn get_balance(&self, pubkey: &Pubkey) -> Result<u64> { self.client.get_balance1(pubkey) } pub fn get_account(&self, pubkey: &Pubkey) -> Result<Option<Account>> { self.client.get_account1(pubkey) } }