1#![cfg(feature = "full")]
11
12use {
13 crate::{
14 clock::Slot,
15 commitment_config::CommitmentConfig,
16 epoch_info::EpochInfo,
17 hash::Hash,
18 instruction::Instruction,
19 message::Message,
20 pubkey::Pubkey,
21 signature::{Keypair, Signature},
22 signer::Signer,
23 signers::Signers,
24 system_instruction,
25 transaction::{self, Transaction, VersionedTransaction},
26 transport::Result,
27 },
28 solana_account::Account,
29};
30
31pub trait Client: SyncClient + AsyncClient {
32 fn tpu_addr(&self) -> String;
33}
34
35pub trait SyncClient {
36 fn send_and_confirm_message<T: Signers + ?Sized>(
39 &self,
40 keypairs: &T,
41 message: Message,
42 ) -> Result<Signature>;
43
44 fn send_and_confirm_instruction(
47 &self,
48 keypair: &Keypair,
49 instruction: Instruction,
50 ) -> Result<Signature>;
51
52 fn transfer_and_confirm(
55 &self,
56 lamports: u64,
57 keypair: &Keypair,
58 pubkey: &Pubkey,
59 ) -> Result<Signature>;
60
61 fn get_account_data(&self, pubkey: &Pubkey) -> Result<Option<Vec<u8>>>;
63
64 fn get_account(&self, pubkey: &Pubkey) -> Result<Option<Account>>;
66
67 fn get_account_with_commitment(
69 &self,
70 pubkey: &Pubkey,
71 commitment_config: CommitmentConfig,
72 ) -> Result<Option<Account>>;
73
74 fn get_balance(&self, pubkey: &Pubkey) -> Result<u64>;
76
77 fn get_balance_with_commitment(
79 &self,
80 pubkey: &Pubkey,
81 commitment_config: CommitmentConfig,
82 ) -> Result<u64>;
83
84 fn get_minimum_balance_for_rent_exemption(&self, data_len: usize) -> Result<u64>;
85
86 fn get_signature_status(
88 &self,
89 signature: &Signature,
90 ) -> Result<Option<transaction::Result<()>>>;
91
92 fn get_signature_status_with_commitment(
94 &self,
95 signature: &Signature,
96 commitment_config: CommitmentConfig,
97 ) -> Result<Option<transaction::Result<()>>>;
98
99 fn get_slot(&self) -> Result<Slot>;
101
102 fn get_slot_with_commitment(&self, commitment_config: CommitmentConfig) -> Result<u64>;
104
105 fn get_transaction_count(&self) -> Result<u64>;
107
108 fn get_transaction_count_with_commitment(
110 &self,
111 commitment_config: CommitmentConfig,
112 ) -> Result<u64>;
113
114 fn get_epoch_info(&self) -> Result<EpochInfo>;
115
116 fn poll_for_signature_confirmation(
118 &self,
119 signature: &Signature,
120 min_confirmed_blocks: usize,
121 ) -> Result<usize>;
122
123 fn poll_for_signature(&self, signature: &Signature) -> Result<()>;
125
126 fn get_latest_blockhash(&self) -> Result<Hash>;
128
129 fn get_latest_blockhash_with_commitment(
131 &self,
132 commitment_config: CommitmentConfig,
133 ) -> Result<(Hash, u64)>;
134
135 fn is_blockhash_valid(&self, blockhash: &Hash, commitment: CommitmentConfig) -> Result<bool>;
137
138 fn get_fee_for_message(&self, message: &Message) -> Result<u64>;
140}
141
142pub trait AsyncClient {
143 fn async_send_transaction(&self, transaction: Transaction) -> Result<Signature> {
145 self.async_send_versioned_transaction(transaction.into())
146 }
147
148 fn async_send_batch(&self, transactions: Vec<Transaction>) -> Result<()> {
150 let transactions = transactions.into_iter().map(Into::into).collect();
151 self.async_send_versioned_transaction_batch(transactions)
152 }
153
154 fn async_send_versioned_transaction(
156 &self,
157 transaction: VersionedTransaction,
158 ) -> Result<Signature>;
159
160 fn async_send_versioned_transaction_batch(
162 &self,
163 transactions: Vec<VersionedTransaction>,
164 ) -> Result<()> {
165 for t in transactions {
166 self.async_send_versioned_transaction(t)?;
167 }
168 Ok(())
169 }
170
171 fn async_send_message<T: Signers + ?Sized>(
174 &self,
175 keypairs: &T,
176 message: Message,
177 recent_blockhash: Hash,
178 ) -> Result<Signature> {
179 let transaction = Transaction::new(keypairs, message, recent_blockhash);
180 self.async_send_transaction(transaction)
181 }
182
183 fn async_send_instruction(
186 &self,
187 keypair: &Keypair,
188 instruction: Instruction,
189 recent_blockhash: Hash,
190 ) -> Result<Signature> {
191 let message = Message::new(&[instruction], Some(&keypair.pubkey()));
192 self.async_send_message(&[keypair], message, recent_blockhash)
193 }
194
195 fn async_transfer(
197 &self,
198 lamports: u64,
199 keypair: &Keypair,
200 pubkey: &Pubkey,
201 recent_blockhash: Hash,
202 ) -> Result<Signature> {
203 let transfer_instruction =
204 system_instruction::transfer(&keypair.pubkey(), pubkey, lamports);
205 self.async_send_instruction(keypair, transfer_instruction, recent_blockhash)
206 }
207}