solana_sdk/
client.rs

1//! Defines traits for blocking (synchronous) and non-blocking (asynchronous)
2//! communication with a Solana server as well a a trait that encompasses both.
3//!
4//! //! Synchronous implementations are expected to create transactions, sign them, and send
5//! them with multiple retries, updating blockhashes and resigning as-needed.
6//!
7//! Asynchronous implementations are expected to create transactions, sign them, and send
8//! them but without waiting to see if the server accepted it.
9
10#![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    /// Create a transaction from the given message, and send it to the
37    /// server, retrying as-needed.
38    fn send_and_confirm_message<T: Signers + ?Sized>(
39        &self,
40        keypairs: &T,
41        message: Message,
42    ) -> Result<Signature>;
43
44    /// Create a transaction from a single instruction that only requires
45    /// a single signer. Then send it to the server, retrying as-needed.
46    fn send_and_confirm_instruction(
47        &self,
48        keypair: &Keypair,
49        instruction: Instruction,
50    ) -> Result<Signature>;
51
52    /// Transfer lamports from `keypair` to `pubkey`, retrying until the
53    /// transfer completes or produces and error.
54    fn transfer_and_confirm(
55        &self,
56        lamports: u64,
57        keypair: &Keypair,
58        pubkey: &Pubkey,
59    ) -> Result<Signature>;
60
61    /// Get an account or None if not found.
62    fn get_account_data(&self, pubkey: &Pubkey) -> Result<Option<Vec<u8>>>;
63
64    /// Get an account or None if not found.
65    fn get_account(&self, pubkey: &Pubkey) -> Result<Option<Account>>;
66
67    /// Get an account or None if not found. Uses explicit commitment configuration.
68    fn get_account_with_commitment(
69        &self,
70        pubkey: &Pubkey,
71        commitment_config: CommitmentConfig,
72    ) -> Result<Option<Account>>;
73
74    /// Get account balance or 0 if not found.
75    fn get_balance(&self, pubkey: &Pubkey) -> Result<u64>;
76
77    /// Get account balance or 0 if not found. Uses explicit commitment configuration.
78    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    /// Get signature status.
87    fn get_signature_status(
88        &self,
89        signature: &Signature,
90    ) -> Result<Option<transaction::Result<()>>>;
91
92    /// Get signature status. Uses explicit commitment configuration.
93    fn get_signature_status_with_commitment(
94        &self,
95        signature: &Signature,
96        commitment_config: CommitmentConfig,
97    ) -> Result<Option<transaction::Result<()>>>;
98
99    /// Get last known slot
100    fn get_slot(&self) -> Result<Slot>;
101
102    /// Get last known slot. Uses explicit commitment configuration.
103    fn get_slot_with_commitment(&self, commitment_config: CommitmentConfig) -> Result<u64>;
104
105    /// Get transaction count
106    fn get_transaction_count(&self) -> Result<u64>;
107
108    /// Get transaction count. Uses explicit commitment configuration.
109    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    /// Poll until the signature has been confirmed by at least `min_confirmed_blocks`
117    fn poll_for_signature_confirmation(
118        &self,
119        signature: &Signature,
120        min_confirmed_blocks: usize,
121    ) -> Result<usize>;
122
123    /// Poll to confirm a transaction.
124    fn poll_for_signature(&self, signature: &Signature) -> Result<()>;
125
126    /// Get last known blockhash
127    fn get_latest_blockhash(&self) -> Result<Hash>;
128
129    /// Get latest blockhash with last valid block height. Uses explicit commitment configuration.
130    fn get_latest_blockhash_with_commitment(
131        &self,
132        commitment_config: CommitmentConfig,
133    ) -> Result<(Hash, u64)>;
134
135    /// Check if the blockhash is valid
136    fn is_blockhash_valid(&self, blockhash: &Hash, commitment: CommitmentConfig) -> Result<bool>;
137
138    /// Calculate the fee for a `Message`
139    fn get_fee_for_message(&self, message: &Message) -> Result<u64>;
140}
141
142pub trait AsyncClient {
143    /// Send a signed transaction, but don't wait to see if the server accepted it.
144    fn async_send_transaction(&self, transaction: Transaction) -> Result<Signature> {
145        self.async_send_versioned_transaction(transaction.into())
146    }
147
148    /// Send a batch of signed transactions without confirmation.
149    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    /// Send a signed versioned transaction, but don't wait to see if the server accepted it.
155    fn async_send_versioned_transaction(
156        &self,
157        transaction: VersionedTransaction,
158    ) -> Result<Signature>;
159
160    /// Send a batch of signed versioned transactions without confirmation.
161    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    /// Create a transaction from the given message, and send it to the
172    /// server, but don't wait for to see if the server accepted it.
173    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    /// Create a transaction from a single instruction that only requires
184    /// a single signer. Then send it to the server, but don't wait for a reply.
185    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    /// Attempt to transfer lamports from `keypair` to `pubkey`, but don't wait to confirm.
196    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}