solana_client_traits/
lib.rs1use {
11 solana_account::Account,
12 solana_commitment_config::CommitmentConfig,
13 solana_epoch_info::EpochInfo,
14 solana_hash::Hash,
15 solana_instruction::Instruction,
16 solana_keypair::Keypair,
17 solana_message::Message,
18 solana_pubkey::Pubkey,
19 solana_signature::Signature,
20 solana_signer::{signers::Signers, Signer},
21 solana_system_interface::instruction::transfer,
22 solana_transaction::{versioned::VersionedTransaction, Transaction},
23 solana_transaction_error::{TransactionResult, TransportResult as Result},
24};
25
26pub trait Client: SyncClient + AsyncClient {
27 fn tpu_addr(&self) -> String;
28}
29
30pub trait SyncClient {
31 fn send_and_confirm_message<T: Signers + ?Sized>(
34 &self,
35 keypairs: &T,
36 message: Message,
37 ) -> Result<Signature>;
38
39 fn send_and_confirm_instruction(
42 &self,
43 keypair: &Keypair,
44 instruction: Instruction,
45 ) -> Result<Signature>;
46
47 fn transfer_and_confirm(
50 &self,
51 lamports: u64,
52 keypair: &Keypair,
53 pubkey: &Pubkey,
54 ) -> Result<Signature>;
55
56 fn get_account_data(&self, pubkey: &Pubkey) -> Result<Option<Vec<u8>>>;
58
59 fn get_account(&self, pubkey: &Pubkey) -> Result<Option<Account>>;
61
62 fn get_account_with_commitment(
64 &self,
65 pubkey: &Pubkey,
66 commitment_config: CommitmentConfig,
67 ) -> Result<Option<Account>>;
68
69 fn get_balance(&self, pubkey: &Pubkey) -> Result<u64>;
71
72 fn get_balance_with_commitment(
74 &self,
75 pubkey: &Pubkey,
76 commitment_config: CommitmentConfig,
77 ) -> Result<u64>;
78
79 fn get_minimum_balance_for_rent_exemption(&self, data_len: usize) -> Result<u64>;
80
81 fn get_signature_status(&self, signature: &Signature) -> Result<Option<TransactionResult<()>>>;
83
84 fn get_signature_status_with_commitment(
86 &self,
87 signature: &Signature,
88 commitment_config: CommitmentConfig,
89 ) -> Result<Option<TransactionResult<()>>>;
90
91 fn get_slot(&self) -> Result<u64>;
93
94 fn get_slot_with_commitment(&self, commitment_config: CommitmentConfig) -> Result<u64>;
96
97 fn get_transaction_count(&self) -> Result<u64>;
99
100 fn get_transaction_count_with_commitment(
102 &self,
103 commitment_config: CommitmentConfig,
104 ) -> Result<u64>;
105
106 fn get_epoch_info(&self) -> Result<EpochInfo>;
107
108 fn poll_for_signature_confirmation(
110 &self,
111 signature: &Signature,
112 min_confirmed_blocks: usize,
113 ) -> Result<usize>;
114
115 fn poll_for_signature(&self, signature: &Signature) -> Result<()>;
117
118 fn get_latest_blockhash(&self) -> Result<Hash>;
120
121 fn get_latest_blockhash_with_commitment(
123 &self,
124 commitment_config: CommitmentConfig,
125 ) -> Result<(Hash, u64)>;
126
127 fn is_blockhash_valid(&self, blockhash: &Hash, commitment: CommitmentConfig) -> Result<bool>;
129
130 fn get_fee_for_message(&self, message: &Message) -> Result<u64>;
132}
133
134pub trait AsyncClient {
135 fn async_send_transaction(&self, transaction: Transaction) -> Result<Signature> {
137 self.async_send_versioned_transaction(transaction.into())
138 }
139
140 fn async_send_batch(&self, transactions: Vec<Transaction>) -> Result<()> {
142 let transactions = transactions.into_iter().map(Into::into).collect();
143 self.async_send_versioned_transaction_batch(transactions)
144 }
145
146 fn async_send_versioned_transaction(
148 &self,
149 transaction: VersionedTransaction,
150 ) -> Result<Signature>;
151
152 fn async_send_versioned_transaction_batch(
154 &self,
155 transactions: Vec<VersionedTransaction>,
156 ) -> Result<()> {
157 for t in transactions {
158 self.async_send_versioned_transaction(t)?;
159 }
160 Ok(())
161 }
162
163 fn async_send_message<T: Signers + ?Sized>(
166 &self,
167 keypairs: &T,
168 message: Message,
169 recent_blockhash: Hash,
170 ) -> Result<Signature> {
171 let transaction = Transaction::new(keypairs, message, recent_blockhash);
172 self.async_send_transaction(transaction)
173 }
174
175 fn async_send_instruction(
178 &self,
179 keypair: &Keypair,
180 instruction: Instruction,
181 recent_blockhash: Hash,
182 ) -> Result<Signature> {
183 let message = Message::new(&[instruction], Some(&keypair.pubkey()));
184 self.async_send_message(&[keypair], message, recent_blockhash)
185 }
186
187 fn async_transfer(
189 &self,
190 lamports: u64,
191 keypair: &Keypair,
192 pubkey: &Pubkey,
193 recent_blockhash: Hash,
194 ) -> Result<Signature> {
195 let transfer_instruction = transfer(&keypair.pubkey(), pubkey, lamports);
196 self.async_send_instruction(keypair, transfer_instruction, recent_blockhash)
197 }
198}