Add RpcClient::new_with_commitment()
This commit is contained in:
parent
3827739708
commit
c5299b60ed
@ -48,6 +48,7 @@ use std::{
|
|||||||
|
|
||||||
pub struct RpcClient {
|
pub struct RpcClient {
|
||||||
sender: Box<dyn RpcSender + Send + Sync + 'static>,
|
sender: Box<dyn RpcSender + Send + Sync + 'static>,
|
||||||
|
commitment_config: CommitmentConfig,
|
||||||
default_cluster_transaction_encoding: RwLock<Option<UiTransactionEncoding>>,
|
default_cluster_transaction_encoding: RwLock<Option<UiTransactionEncoding>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,27 +73,41 @@ fn serialize_encode_transaction(
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl RpcClient {
|
impl RpcClient {
|
||||||
pub fn new_sender<T: RpcSender + Send + Sync + 'static>(sender: T) -> Self {
|
fn new_sender<T: RpcSender + Send + Sync + 'static>(
|
||||||
|
sender: T,
|
||||||
|
commitment_config: CommitmentConfig,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
sender: Box::new(sender),
|
sender: Box::new(sender),
|
||||||
default_cluster_transaction_encoding: RwLock::new(None),
|
default_cluster_transaction_encoding: RwLock::new(None),
|
||||||
|
commitment_config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(url: String) -> Self {
|
pub fn new(url: String) -> Self {
|
||||||
Self::new_sender(HttpSender::new(url))
|
Self::new_with_commitment(url, CommitmentConfig::default())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_with_commitment(url: String, commitment_config: CommitmentConfig) -> Self {
|
||||||
|
Self::new_sender(HttpSender::new(url), commitment_config)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_with_timeout(url: String, timeout: Duration) -> Self {
|
pub fn new_with_timeout(url: String, timeout: Duration) -> Self {
|
||||||
Self::new_sender(HttpSender::new_with_timeout(url, timeout))
|
Self::new_sender(
|
||||||
|
HttpSender::new_with_timeout(url, timeout),
|
||||||
|
CommitmentConfig::default(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_mock(url: String) -> Self {
|
pub fn new_mock(url: String) -> Self {
|
||||||
Self::new_sender(MockSender::new(url))
|
Self::new_sender(MockSender::new(url), CommitmentConfig::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_mock_with_mocks(url: String, mocks: Mocks) -> Self {
|
pub fn new_mock_with_mocks(url: String, mocks: Mocks) -> Self {
|
||||||
Self::new_sender(MockSender::new_with_mocks(url, mocks))
|
Self::new_sender(
|
||||||
|
MockSender::new_with_mocks(url, mocks),
|
||||||
|
CommitmentConfig::default(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_socket(addr: SocketAddr) -> Self {
|
pub fn new_socket(addr: SocketAddr) -> Self {
|
||||||
@ -106,10 +121,14 @@ impl RpcClient {
|
|||||||
|
|
||||||
pub fn confirm_transaction(&self, signature: &Signature) -> ClientResult<bool> {
|
pub fn confirm_transaction(&self, signature: &Signature) -> ClientResult<bool> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.confirm_transaction_with_commitment(signature, CommitmentConfig::default())?
|
.confirm_transaction_with_commitment(signature, self.commitment_config)?
|
||||||
.value)
|
.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn commitment(&self) -> CommitmentConfig {
|
||||||
|
self.commitment_config
|
||||||
|
}
|
||||||
|
|
||||||
pub fn confirm_transaction_with_commitment(
|
pub fn confirm_transaction_with_commitment(
|
||||||
&self,
|
&self,
|
||||||
signature: &Signature,
|
signature: &Signature,
|
||||||
@ -128,7 +147,13 @@ impl RpcClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_transaction(&self, transaction: &Transaction) -> ClientResult<Signature> {
|
pub fn send_transaction(&self, transaction: &Transaction) -> ClientResult<Signature> {
|
||||||
self.send_transaction_with_config(transaction, RpcSendTransactionConfig::default())
|
self.send_transaction_with_config(
|
||||||
|
transaction,
|
||||||
|
RpcSendTransactionConfig {
|
||||||
|
preflight_commitment: Some(self.commitment_config.commitment),
|
||||||
|
..RpcSendTransactionConfig::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_cluster_transaction_encoding(&self) -> Result<UiTransactionEncoding, RpcError> {
|
fn default_cluster_transaction_encoding(&self) -> Result<UiTransactionEncoding, RpcError> {
|
||||||
@ -221,7 +246,13 @@ impl RpcClient {
|
|||||||
&self,
|
&self,
|
||||||
transaction: &Transaction,
|
transaction: &Transaction,
|
||||||
) -> RpcResult<RpcSimulateTransactionResult> {
|
) -> RpcResult<RpcSimulateTransactionResult> {
|
||||||
self.simulate_transaction_with_config(transaction, RpcSimulateTransactionConfig::default())
|
self.simulate_transaction_with_config(
|
||||||
|
transaction,
|
||||||
|
RpcSimulateTransactionConfig {
|
||||||
|
commitment: Some(self.commitment_config),
|
||||||
|
..RpcSimulateTransactionConfig::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn simulate_transaction_with_config(
|
pub fn simulate_transaction_with_config(
|
||||||
@ -249,7 +280,7 @@ impl RpcClient {
|
|||||||
&self,
|
&self,
|
||||||
signature: &Signature,
|
signature: &Signature,
|
||||||
) -> ClientResult<Option<transaction::Result<()>>> {
|
) -> ClientResult<Option<transaction::Result<()>>> {
|
||||||
self.get_signature_status_with_commitment(signature, CommitmentConfig::default())
|
self.get_signature_status_with_commitment(signature, self.commitment_config)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_signature_statuses(
|
pub fn get_signature_statuses(
|
||||||
@ -307,7 +338,7 @@ impl RpcClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_slot(&self) -> ClientResult<Slot> {
|
pub fn get_slot(&self) -> ClientResult<Slot> {
|
||||||
self.get_slot_with_commitment(CommitmentConfig::default())
|
self.get_slot_with_commitment(self.commitment_config)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_slot_with_commitment(
|
pub fn get_slot_with_commitment(
|
||||||
@ -325,7 +356,7 @@ impl RpcClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn total_supply(&self) -> ClientResult<u64> {
|
pub fn total_supply(&self) -> ClientResult<u64> {
|
||||||
self.total_supply_with_commitment(CommitmentConfig::default())
|
self.total_supply_with_commitment(self.commitment_config)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn total_supply_with_commitment(
|
pub fn total_supply_with_commitment(
|
||||||
@ -343,7 +374,7 @@ impl RpcClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_vote_accounts(&self) -> ClientResult<RpcVoteAccountStatus> {
|
pub fn get_vote_accounts(&self) -> ClientResult<RpcVoteAccountStatus> {
|
||||||
self.get_vote_accounts_with_commitment(CommitmentConfig::default())
|
self.get_vote_accounts_with_commitment(self.commitment_config)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_vote_accounts_with_commitment(
|
pub fn get_vote_accounts_with_commitment(
|
||||||
@ -503,7 +534,7 @@ impl RpcClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_epoch_info(&self) -> ClientResult<EpochInfo> {
|
pub fn get_epoch_info(&self) -> ClientResult<EpochInfo> {
|
||||||
self.get_epoch_info_with_commitment(CommitmentConfig::default())
|
self.get_epoch_info_with_commitment(self.commitment_config)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_epoch_info_with_commitment(
|
pub fn get_epoch_info_with_commitment(
|
||||||
@ -517,7 +548,7 @@ impl RpcClient {
|
|||||||
&self,
|
&self,
|
||||||
slot: Option<Slot>,
|
slot: Option<Slot>,
|
||||||
) -> ClientResult<Option<RpcLeaderSchedule>> {
|
) -> ClientResult<Option<RpcLeaderSchedule>> {
|
||||||
self.get_leader_schedule_with_commitment(slot, CommitmentConfig::default())
|
self.get_leader_schedule_with_commitment(slot, self.commitment_config)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_leader_schedule_with_commitment(
|
pub fn get_leader_schedule_with_commitment(
|
||||||
@ -612,7 +643,7 @@ impl RpcClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_account(&self, pubkey: &Pubkey) -> ClientResult<Account> {
|
pub fn get_account(&self, pubkey: &Pubkey) -> ClientResult<Account> {
|
||||||
self.get_account_with_commitment(pubkey, CommitmentConfig::default())?
|
self.get_account_with_commitment(pubkey, self.commitment_config)?
|
||||||
.value
|
.value
|
||||||
.ok_or_else(|| RpcError::ForUser(format!("AccountNotFound: pubkey={}", pubkey)).into())
|
.ok_or_else(|| RpcError::ForUser(format!("AccountNotFound: pubkey={}", pubkey)).into())
|
||||||
}
|
}
|
||||||
@ -660,7 +691,7 @@ impl RpcClient {
|
|||||||
|
|
||||||
pub fn get_multiple_accounts(&self, pubkeys: &[Pubkey]) -> ClientResult<Vec<Option<Account>>> {
|
pub fn get_multiple_accounts(&self, pubkeys: &[Pubkey]) -> ClientResult<Vec<Option<Account>>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.get_multiple_accounts_with_commitment(pubkeys, CommitmentConfig::default())?
|
.get_multiple_accounts_with_commitment(pubkeys, self.commitment_config)?
|
||||||
.value)
|
.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -714,7 +745,7 @@ impl RpcClient {
|
|||||||
/// Request the balance of the account `pubkey`.
|
/// Request the balance of the account `pubkey`.
|
||||||
pub fn get_balance(&self, pubkey: &Pubkey) -> ClientResult<u64> {
|
pub fn get_balance(&self, pubkey: &Pubkey) -> ClientResult<u64> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.get_balance_with_commitment(pubkey, CommitmentConfig::default())?
|
.get_balance_with_commitment(pubkey, self.commitment_config)?
|
||||||
.value)
|
.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -756,7 +787,7 @@ impl RpcClient {
|
|||||||
|
|
||||||
/// Request the transaction count.
|
/// Request the transaction count.
|
||||||
pub fn get_transaction_count(&self) -> ClientResult<u64> {
|
pub fn get_transaction_count(&self) -> ClientResult<u64> {
|
||||||
self.get_transaction_count_with_commitment(CommitmentConfig::default())
|
self.get_transaction_count_with_commitment(self.commitment_config)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_transaction_count_with_commitment(
|
pub fn get_transaction_count_with_commitment(
|
||||||
@ -768,7 +799,7 @@ impl RpcClient {
|
|||||||
|
|
||||||
pub fn get_recent_blockhash(&self) -> ClientResult<(Hash, FeeCalculator)> {
|
pub fn get_recent_blockhash(&self) -> ClientResult<(Hash, FeeCalculator)> {
|
||||||
let (blockhash, fee_calculator, _last_valid_slot) = self
|
let (blockhash, fee_calculator, _last_valid_slot) = self
|
||||||
.get_recent_blockhash_with_commitment(CommitmentConfig::default())?
|
.get_recent_blockhash_with_commitment(self.commitment_config)?
|
||||||
.value;
|
.value;
|
||||||
Ok((blockhash, fee_calculator))
|
Ok((blockhash, fee_calculator))
|
||||||
}
|
}
|
||||||
@ -825,10 +856,7 @@ impl RpcClient {
|
|||||||
blockhash: &Hash,
|
blockhash: &Hash,
|
||||||
) -> ClientResult<Option<FeeCalculator>> {
|
) -> ClientResult<Option<FeeCalculator>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.get_fee_calculator_for_blockhash_with_commitment(
|
.get_fee_calculator_for_blockhash_with_commitment(blockhash, self.commitment_config)?
|
||||||
blockhash,
|
|
||||||
CommitmentConfig::default(),
|
|
||||||
)?
|
|
||||||
.value)
|
.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -904,7 +932,7 @@ impl RpcClient {
|
|||||||
|
|
||||||
pub fn get_token_account(&self, pubkey: &Pubkey) -> ClientResult<Option<UiTokenAccount>> {
|
pub fn get_token_account(&self, pubkey: &Pubkey) -> ClientResult<Option<UiTokenAccount>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.get_token_account_with_commitment(pubkey, CommitmentConfig::default())?
|
.get_token_account_with_commitment(pubkey, self.commitment_config)?
|
||||||
.value)
|
.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -965,7 +993,7 @@ impl RpcClient {
|
|||||||
|
|
||||||
pub fn get_token_account_balance(&self, pubkey: &Pubkey) -> ClientResult<UiTokenAmount> {
|
pub fn get_token_account_balance(&self, pubkey: &Pubkey) -> ClientResult<UiTokenAmount> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.get_token_account_balance_with_commitment(pubkey, CommitmentConfig::default())?
|
.get_token_account_balance_with_commitment(pubkey, self.commitment_config)?
|
||||||
.value)
|
.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -989,7 +1017,7 @@ impl RpcClient {
|
|||||||
.get_token_accounts_by_delegate_with_commitment(
|
.get_token_accounts_by_delegate_with_commitment(
|
||||||
delegate,
|
delegate,
|
||||||
token_account_filter,
|
token_account_filter,
|
||||||
CommitmentConfig::default(),
|
self.commitment_config,
|
||||||
)?
|
)?
|
||||||
.value)
|
.value)
|
||||||
}
|
}
|
||||||
@ -1028,7 +1056,7 @@ impl RpcClient {
|
|||||||
.get_token_accounts_by_owner_with_commitment(
|
.get_token_accounts_by_owner_with_commitment(
|
||||||
owner,
|
owner,
|
||||||
token_account_filter,
|
token_account_filter,
|
||||||
CommitmentConfig::default(),
|
self.commitment_config,
|
||||||
)?
|
)?
|
||||||
.value)
|
.value)
|
||||||
}
|
}
|
||||||
@ -1060,7 +1088,7 @@ impl RpcClient {
|
|||||||
|
|
||||||
pub fn get_token_supply(&self, mint: &Pubkey) -> ClientResult<UiTokenAmount> {
|
pub fn get_token_supply(&self, mint: &Pubkey) -> ClientResult<UiTokenAmount> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.get_token_supply_with_commitment(mint, CommitmentConfig::default())?
|
.get_token_supply_with_commitment(mint, self.commitment_config)?
|
||||||
.value)
|
.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1141,7 +1169,7 @@ impl RpcClient {
|
|||||||
|
|
||||||
/// Poll the server to confirm a transaction.
|
/// Poll the server to confirm a transaction.
|
||||||
pub fn poll_for_signature(&self, signature: &Signature) -> ClientResult<()> {
|
pub fn poll_for_signature(&self, signature: &Signature) -> ClientResult<()> {
|
||||||
self.poll_for_signature_with_commitment(signature, CommitmentConfig::default())
|
self.poll_for_signature_with_commitment(signature, self.commitment_config)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Poll the server to confirm a transaction.
|
/// Poll the server to confirm a transaction.
|
||||||
@ -1249,10 +1277,9 @@ impl RpcClient {
|
|||||||
&self,
|
&self,
|
||||||
transaction: &Transaction,
|
transaction: &Transaction,
|
||||||
) -> ClientResult<Signature> {
|
) -> ClientResult<Signature> {
|
||||||
self.send_and_confirm_transaction_with_spinner_and_config(
|
self.send_and_confirm_transaction_with_spinner_and_commitment(
|
||||||
transaction,
|
transaction,
|
||||||
CommitmentConfig::default(),
|
self.commitment_config,
|
||||||
RpcSendTransactionConfig::default(),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user