1#![cfg(feature = "full")]
11
12use crate::{
13 account::Account,
14 clock::Slot,
15 commitment_config::CommitmentConfig,
16 epoch_info::EpochInfo,
17 fee_calculator::{FeeCalculator, FeeRateGovernor},
18 hash::Hash,
19 instruction::Instruction,
20 message::Message,
21 pubkey::Pubkey,
22 signature::{Keypair, Signature},
23 signer::Signer,
24 signers::Signers,
25 system_instruction,
26 transaction::{self, Transaction, VersionedTransaction},
27 transport::Result,
28};
29
30pub trait Client: SyncClient + AsyncClient {
31 fn tpu_addr(&self) -> String;
32}
33
34pub trait SyncClient {
35 fn send_and_confirm_message<T: Signers>(
38 &self,
39 keypairs: &T,
40 message: Message,
41 ) -> Result<Signature>;
42
43 fn send_and_confirm_instruction(
46 &self,
47 keypair: &Keypair,
48 instruction: Instruction,
49 ) -> Result<Signature>;
50
51 fn transfer_and_confirm(
54 &self,
55 lamports: u64,
56 keypair: &Keypair,
57 pubkey: &Pubkey,
58 ) -> Result<Signature>;
59
60 fn get_account_data(&self, pubkey: &Pubkey) -> Result<Option<Vec<u8>>>;
62
63 fn get_account(&self, pubkey: &Pubkey) -> Result<Option<Account>>;
65
66 fn get_account_with_commitment(
68 &self,
69 pubkey: &Pubkey,
70 commitment_config: CommitmentConfig,
71 ) -> Result<Option<Account>>;
72
73 fn get_balance(&self, pubkey: &Pubkey) -> Result<u64>;
75
76 fn get_balance_with_commitment(
78 &self,
79 pubkey: &Pubkey,
80 commitment_config: CommitmentConfig,
81 ) -> Result<u64>;
82
83 fn get_minimum_balance_for_rent_exemption(&self, data_len: usize) -> Result<u64>;
84
85 #[deprecated(since = "1.9.0", note = "Please use `get_latest_blockhash` instead")]
87 fn get_recent_blockhash(&self) -> Result<(Hash, FeeCalculator)>;
88
89 #[deprecated(
91 since = "1.9.0",
92 note = "Please use `get_latest_blockhash_with_commitment` and `get_latest_blockhash_with_commitment` instead"
93 )]
94 fn get_recent_blockhash_with_commitment(
95 &self,
96 commitment_config: CommitmentConfig,
97 ) -> Result<(Hash, FeeCalculator, Slot)>;
98
99 #[deprecated(
102 since = "1.9.0",
103 note = "Please use `get_fee_for_message` or `is_blockhash_valid` instead"
104 )]
105 fn get_fee_calculator_for_blockhash(&self, blockhash: &Hash) -> Result<Option<FeeCalculator>>;
106
107 #[deprecated(
109 since = "1.9.0",
110 note = "Please do not use, will no longer be available in the future"
111 )]
112 fn get_fee_rate_governor(&self) -> Result<FeeRateGovernor>;
113
114 fn get_signature_status(
116 &self,
117 signature: &Signature,
118 ) -> Result<Option<transaction::Result<()>>>;
119
120 fn get_signature_status_with_commitment(
122 &self,
123 signature: &Signature,
124 commitment_config: CommitmentConfig,
125 ) -> Result<Option<transaction::Result<()>>>;
126
127 fn get_slot(&self) -> Result<Slot>;
129
130 fn get_slot_with_commitment(&self, commitment_config: CommitmentConfig) -> Result<u64>;
132
133 fn get_transaction_count(&self) -> Result<u64>;
135
136 fn get_transaction_count_with_commitment(
138 &self,
139 commitment_config: CommitmentConfig,
140 ) -> Result<u64>;
141
142 fn get_epoch_info(&self) -> Result<EpochInfo>;
143
144 fn poll_for_signature_confirmation(
146 &self,
147 signature: &Signature,
148 min_confirmed_blocks: usize,
149 ) -> Result<usize>;
150
151 fn poll_for_signature(&self, signature: &Signature) -> Result<()>;
153
154 #[deprecated(
155 since = "1.9.0",
156 note = "Please do not use, will no longer be available in the future"
157 )]
158 fn get_new_blockhash(&self, blockhash: &Hash) -> Result<(Hash, FeeCalculator)>;
159
160 fn get_latest_blockhash(&self) -> Result<Hash>;
162
163 fn get_latest_blockhash_with_commitment(
165 &self,
166 commitment_config: CommitmentConfig,
167 ) -> Result<(Hash, u64)>;
168
169 fn is_blockhash_valid(&self, blockhash: &Hash, commitment: CommitmentConfig) -> Result<bool>;
171
172 fn get_fee_for_message(&self, message: &Message) -> Result<u64>;
174}
175
176pub trait AsyncClient {
177 fn async_send_transaction(&self, transaction: Transaction) -> Result<Signature> {
179 self.async_send_versioned_transaction(transaction.into())
180 }
181
182 fn async_send_batch(&self, transactions: Vec<Transaction>) -> Result<()> {
184 let transactions = transactions.into_iter().map(Into::into).collect();
185 self.async_send_versioned_transaction_batch(transactions)
186 }
187
188 fn async_send_versioned_transaction(
190 &self,
191 transaction: VersionedTransaction,
192 ) -> Result<Signature>;
193
194 fn async_send_versioned_transaction_batch(
196 &self,
197 transactions: Vec<VersionedTransaction>,
198 ) -> Result<()> {
199 for t in transactions {
200 self.async_send_versioned_transaction(t)?;
201 }
202 Ok(())
203 }
204
205 fn async_send_message<T: Signers>(
208 &self,
209 keypairs: &T,
210 message: Message,
211 recent_blockhash: Hash,
212 ) -> Result<Signature> {
213 let transaction = Transaction::new(keypairs, message, recent_blockhash);
214 self.async_send_transaction(transaction)
215 }
216
217 fn async_send_instruction(
220 &self,
221 keypair: &Keypair,
222 instruction: Instruction,
223 recent_blockhash: Hash,
224 ) -> Result<Signature> {
225 let message = Message::new(&[instruction], Some(&keypair.pubkey()));
226 self.async_send_message(&[keypair], message, recent_blockhash)
227 }
228
229 fn async_transfer(
231 &self,
232 lamports: u64,
233 keypair: &Keypair,
234 pubkey: &Pubkey,
235 recent_blockhash: Hash,
236 ) -> Result<Signature> {
237 let transfer_instruction =
238 system_instruction::transfer(&keypair.pubkey(), pubkey, lamports);
239 self.async_send_instruction(keypair, transfer_instruction, recent_blockhash)
240 }
241}