2019-11-06 13:15:00 -08:00
|
|
|
use crate::{
|
2020-03-17 16:58:02 -07:00
|
|
|
client_error::{ClientError, ClientErrorKind, Result as ClientResult},
|
2020-05-20 18:40:45 -07:00
|
|
|
http_sender::HttpSender,
|
|
|
|
mock_sender::{MockSender, Mocks},
|
2020-08-08 22:40:13 -07:00
|
|
|
rpc_config::RpcAccountInfoConfig,
|
2020-07-27 11:42:49 -07:00
|
|
|
rpc_config::{
|
|
|
|
RpcGetConfirmedSignaturesForAddress2Config, RpcLargestAccountsConfig,
|
2020-10-05 21:47:47 -07:00
|
|
|
RpcProgramAccountsConfig, RpcSendTransactionConfig, RpcSimulateTransactionConfig,
|
|
|
|
RpcTokenAccountsFilter,
|
2020-07-27 11:42:49 -07:00
|
|
|
},
|
2020-11-03 14:37:41 -08:00
|
|
|
rpc_request::{RpcError, RpcRequest, RpcResponseErrorData, TokenAccountsFilter},
|
2020-03-24 05:05:38 -07:00
|
|
|
rpc_response::*,
|
2020-05-20 18:40:45 -07:00
|
|
|
rpc_sender::RpcSender,
|
2019-11-06 13:15:00 -08:00
|
|
|
};
|
2019-03-16 17:17:44 -07:00
|
|
|
use bincode::serialize;
|
2020-03-17 16:58:02 -07:00
|
|
|
use indicatif::{ProgressBar, ProgressStyle};
|
2019-03-16 22:37:20 -07:00
|
|
|
use log::*;
|
|
|
|
use serde_json::{json, Value};
|
2020-09-18 19:45:13 -07:00
|
|
|
use solana_account_decoder::{
|
|
|
|
parse_token::{TokenAccountType, UiTokenAccount, UiTokenAmount},
|
|
|
|
UiAccount, UiAccountData, UiAccountEncoding,
|
|
|
|
};
|
2019-09-27 22:00:30 -07:00
|
|
|
use solana_sdk::{
|
|
|
|
account::Account,
|
2020-04-29 13:12:38 -07:00
|
|
|
clock::{
|
|
|
|
Slot, UnixTimestamp, DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT,
|
|
|
|
MAX_HASH_AGE_IN_SECONDS,
|
|
|
|
},
|
2020-06-17 11:18:48 -07:00
|
|
|
commitment_config::{CommitmentConfig, CommitmentLevel},
|
2020-05-20 16:42:46 -07:00
|
|
|
epoch_info::EpochInfo,
|
2019-10-22 13:41:18 -07:00
|
|
|
epoch_schedule::EpochSchedule,
|
2020-02-28 12:27:01 -08:00
|
|
|
fee_calculator::{FeeCalculator, FeeRateGovernor},
|
2019-09-27 22:00:30 -07:00
|
|
|
hash::Hash,
|
|
|
|
pubkey::Pubkey,
|
2020-02-20 12:13:23 -08:00
|
|
|
signature::Signature,
|
2020-09-30 17:44:17 -07:00
|
|
|
transaction::{self, uses_durable_nonce, Transaction},
|
2019-09-27 22:00:30 -07:00
|
|
|
};
|
2020-04-15 20:51:05 -07:00
|
|
|
use solana_transaction_status::{
|
2020-09-23 22:10:29 -07:00
|
|
|
EncodedConfirmedBlock, EncodedConfirmedTransaction, TransactionStatus, UiTransactionEncoding,
|
2020-04-15 20:51:05 -07:00
|
|
|
};
|
2020-03-17 16:58:02 -07:00
|
|
|
use solana_vote_program::vote_state::MAX_LOCKOUT_HISTORY;
|
2019-09-27 22:00:30 -07:00
|
|
|
use std::{
|
|
|
|
net::SocketAddr,
|
2020-10-20 19:50:24 -07:00
|
|
|
sync::RwLock,
|
2019-09-27 22:00:30 -07:00
|
|
|
thread::sleep,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
2019-03-16 22:37:20 -07:00
|
|
|
|
|
|
|
pub struct RpcClient {
|
2020-05-20 18:40:45 -07:00
|
|
|
sender: Box<dyn RpcSender + Send + Sync + 'static>,
|
2020-11-16 23:07:10 -08:00
|
|
|
commitment_config: CommitmentConfig,
|
2020-10-20 19:50:24 -07:00
|
|
|
default_cluster_transaction_encoding: RwLock<Option<UiTransactionEncoding>>,
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
|
|
|
|
2020-10-05 21:47:47 -07:00
|
|
|
fn serialize_encode_transaction(
|
|
|
|
transaction: &Transaction,
|
|
|
|
encoding: UiTransactionEncoding,
|
|
|
|
) -> ClientResult<String> {
|
|
|
|
let serialized = serialize(transaction)
|
|
|
|
.map_err(|e| ClientErrorKind::Custom(format!("transaction serialization failed: {}", e)))?;
|
|
|
|
let encoded = match encoding {
|
|
|
|
UiTransactionEncoding::Base58 => bs58::encode(serialized).into_string(),
|
|
|
|
UiTransactionEncoding::Base64 => base64::encode(serialized),
|
|
|
|
_ => {
|
|
|
|
return Err(ClientErrorKind::Custom(format!(
|
|
|
|
"unsupported transaction encoding: {}. Supported encodings: base58, base64",
|
|
|
|
encoding
|
|
|
|
))
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(encoded)
|
|
|
|
}
|
|
|
|
|
2019-03-16 22:37:20 -07:00
|
|
|
impl RpcClient {
|
2020-11-16 23:07:10 -08:00
|
|
|
fn new_sender<T: RpcSender + Send + Sync + 'static>(
|
|
|
|
sender: T,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> Self {
|
2019-03-16 22:37:20 -07:00
|
|
|
Self {
|
2020-05-20 18:40:45 -07:00
|
|
|
sender: Box::new(sender),
|
2020-10-20 19:50:24 -07:00
|
|
|
default_cluster_transaction_encoding: RwLock::new(None),
|
2020-11-16 23:07:10 -08:00
|
|
|
commitment_config,
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 18:40:45 -07:00
|
|
|
pub fn new(url: String) -> Self {
|
2020-11-16 23:07:10 -08:00
|
|
|
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)
|
2020-05-20 18:40:45 -07:00
|
|
|
}
|
|
|
|
|
2020-07-23 08:18:46 -07:00
|
|
|
pub fn new_with_timeout(url: String, timeout: Duration) -> Self {
|
2020-11-16 23:07:10 -08:00
|
|
|
Self::new_sender(
|
|
|
|
HttpSender::new_with_timeout(url, timeout),
|
|
|
|
CommitmentConfig::default(),
|
|
|
|
)
|
2020-07-23 08:18:46 -07:00
|
|
|
}
|
|
|
|
|
2019-03-16 22:37:20 -07:00
|
|
|
pub fn new_mock(url: String) -> Self {
|
2020-11-16 23:07:10 -08:00
|
|
|
Self::new_sender(MockSender::new(url), CommitmentConfig::default())
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
|
|
|
|
2019-12-27 12:35:49 -08:00
|
|
|
pub fn new_mock_with_mocks(url: String, mocks: Mocks) -> Self {
|
2020-11-16 23:07:10 -08:00
|
|
|
Self::new_sender(
|
|
|
|
MockSender::new_with_mocks(url, mocks),
|
|
|
|
CommitmentConfig::default(),
|
|
|
|
)
|
2019-12-27 12:35:49 -08:00
|
|
|
}
|
|
|
|
|
2019-03-16 22:37:20 -07:00
|
|
|
pub fn new_socket(addr: SocketAddr) -> Self {
|
|
|
|
Self::new(get_rpc_request_str(addr, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_socket_with_timeout(addr: SocketAddr, timeout: Duration) -> Self {
|
|
|
|
let url = get_rpc_request_str(addr, false);
|
2020-07-23 08:18:46 -07:00
|
|
|
Self::new_with_timeout(url, timeout)
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
|
|
|
|
2020-03-27 15:46:00 -07:00
|
|
|
pub fn confirm_transaction(&self, signature: &Signature) -> ClientResult<bool> {
|
2019-11-12 11:49:41 -08:00
|
|
|
Ok(self
|
2020-11-16 23:07:10 -08:00
|
|
|
.confirm_transaction_with_commitment(signature, self.commitment_config)?
|
2019-11-12 11:49:41 -08:00
|
|
|
.value)
|
2019-11-06 19:08:03 -08:00
|
|
|
}
|
|
|
|
|
2020-11-16 23:07:10 -08:00
|
|
|
pub fn commitment(&self) -> CommitmentConfig {
|
|
|
|
self.commitment_config
|
|
|
|
}
|
|
|
|
|
2019-11-06 19:08:03 -08:00
|
|
|
pub fn confirm_transaction_with_commitment(
|
|
|
|
&self,
|
2020-03-27 15:46:00 -07:00
|
|
|
signature: &Signature,
|
2019-11-06 19:08:03 -08:00
|
|
|
commitment_config: CommitmentConfig,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> RpcResult<bool> {
|
2020-04-06 03:04:54 -07:00
|
|
|
let Response { context, value } = self.get_signature_statuses(&[*signature])?;
|
2019-11-12 11:49:41 -08:00
|
|
|
|
2020-04-04 23:31:24 -07:00
|
|
|
Ok(Response {
|
|
|
|
context,
|
|
|
|
value: value[0]
|
|
|
|
.as_ref()
|
2020-04-06 03:04:54 -07:00
|
|
|
.filter(|result| result.satisfies_commitment(commitment_config))
|
2020-04-04 23:31:24 -07:00
|
|
|
.map(|result| result.status.is_ok())
|
|
|
|
.unwrap_or_default(),
|
|
|
|
})
|
2019-11-06 19:08:03 -08:00
|
|
|
}
|
|
|
|
|
2020-03-27 15:46:00 -07:00
|
|
|
pub fn send_transaction(&self, transaction: &Transaction) -> ClientResult<Signature> {
|
2020-11-16 23:07:10 -08:00
|
|
|
self.send_transaction_with_config(
|
|
|
|
transaction,
|
|
|
|
RpcSendTransactionConfig {
|
|
|
|
preflight_commitment: Some(self.commitment_config.commitment),
|
|
|
|
..RpcSendTransactionConfig::default()
|
|
|
|
},
|
|
|
|
)
|
2020-05-31 21:54:12 -07:00
|
|
|
}
|
|
|
|
|
2020-10-20 19:50:24 -07:00
|
|
|
fn default_cluster_transaction_encoding(&self) -> Result<UiTransactionEncoding, RpcError> {
|
|
|
|
let default_cluster_transaction_encoding =
|
|
|
|
self.default_cluster_transaction_encoding.read().unwrap();
|
|
|
|
if let Some(encoding) = *default_cluster_transaction_encoding {
|
|
|
|
Ok(encoding)
|
|
|
|
} else {
|
|
|
|
drop(default_cluster_transaction_encoding);
|
|
|
|
let cluster_version = self.get_version().map_err(|e| {
|
|
|
|
RpcError::RpcRequestError(format!("cluster version query failed: {}", e))
|
|
|
|
})?;
|
|
|
|
let cluster_version =
|
|
|
|
semver::Version::parse(&cluster_version.solana_core).map_err(|e| {
|
|
|
|
RpcError::RpcRequestError(format!("failed to parse cluster version: {}", e))
|
|
|
|
})?;
|
|
|
|
// Prefer base64 since 1.3.16
|
|
|
|
let encoding = if cluster_version < semver::Version::new(1, 3, 16) {
|
|
|
|
UiTransactionEncoding::Base58
|
|
|
|
} else {
|
|
|
|
UiTransactionEncoding::Base64
|
|
|
|
};
|
|
|
|
*self.default_cluster_transaction_encoding.write().unwrap() = Some(encoding);
|
|
|
|
Ok(encoding)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 21:54:12 -07:00
|
|
|
pub fn send_transaction_with_config(
|
|
|
|
&self,
|
|
|
|
transaction: &Transaction,
|
|
|
|
config: RpcSendTransactionConfig,
|
|
|
|
) -> ClientResult<Signature> {
|
2020-10-20 19:50:24 -07:00
|
|
|
let encoding = if let Some(encoding) = config.encoding {
|
|
|
|
encoding
|
|
|
|
} else {
|
|
|
|
self.default_cluster_transaction_encoding()?
|
|
|
|
};
|
2020-10-12 09:14:12 -07:00
|
|
|
let config = RpcSendTransactionConfig {
|
|
|
|
encoding: Some(encoding),
|
|
|
|
..config
|
|
|
|
};
|
2020-10-05 21:47:47 -07:00
|
|
|
let serialized_encoded = serialize_encode_transaction(transaction, encoding)?;
|
2020-11-03 14:37:41 -08:00
|
|
|
let signature_base58_str: String = match self.send(
|
2020-05-31 21:54:12 -07:00
|
|
|
RpcRequest::SendTransaction,
|
|
|
|
json!([serialized_encoded, config]),
|
2020-11-03 14:37:41 -08:00
|
|
|
) {
|
|
|
|
Ok(signature_base58_str) => signature_base58_str,
|
|
|
|
Err(err) => {
|
|
|
|
if let ClientErrorKind::RpcError(RpcError::RpcResponseError {
|
|
|
|
code,
|
|
|
|
message,
|
|
|
|
data,
|
|
|
|
}) = &err.kind
|
|
|
|
{
|
|
|
|
debug!("{} {}", code, message);
|
|
|
|
if let RpcResponseErrorData::SendTransactionPreflightFailure(
|
|
|
|
RpcSimulateTransactionResult {
|
|
|
|
logs: Some(logs), ..
|
|
|
|
},
|
|
|
|
) = data
|
|
|
|
{
|
|
|
|
for (i, log) in logs.iter().enumerate() {
|
|
|
|
debug!("{:>3}: {}", i + 1, log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
};
|
2020-05-10 08:51:53 -07:00
|
|
|
|
|
|
|
let signature = signature_base58_str
|
|
|
|
.parse::<Signature>()
|
|
|
|
.map_err(|err| Into::<ClientError>::into(RpcError::ParseError(err.to_string())))?;
|
|
|
|
// A mismatching RPC response signature indicates an issue with the RPC node, and
|
|
|
|
// should not be passed along to confirmation methods. The transaction may or may
|
|
|
|
// not have been submitted to the cluster, so callers should verify the success of
|
|
|
|
// the correct transaction signature independently.
|
|
|
|
if signature != transaction.signatures[0] {
|
|
|
|
Err(RpcError::RpcRequestError(format!(
|
|
|
|
"RPC node returned mismatched signature {:?}, expected {:?}",
|
|
|
|
signature, transaction.signatures[0]
|
|
|
|
))
|
|
|
|
.into())
|
|
|
|
} else {
|
|
|
|
Ok(transaction.signatures[0])
|
2019-03-16 17:17:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:08:19 -07:00
|
|
|
pub fn simulate_transaction(
|
|
|
|
&self,
|
|
|
|
transaction: &Transaction,
|
2020-06-06 10:18:28 -07:00
|
|
|
) -> RpcResult<RpcSimulateTransactionResult> {
|
2020-11-16 23:07:10 -08:00
|
|
|
self.simulate_transaction_with_config(
|
|
|
|
transaction,
|
|
|
|
RpcSimulateTransactionConfig {
|
|
|
|
commitment: Some(self.commitment_config),
|
|
|
|
..RpcSimulateTransactionConfig::default()
|
|
|
|
},
|
|
|
|
)
|
2020-10-05 21:47:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn simulate_transaction_with_config(
|
|
|
|
&self,
|
|
|
|
transaction: &Transaction,
|
|
|
|
config: RpcSimulateTransactionConfig,
|
|
|
|
) -> RpcResult<RpcSimulateTransactionResult> {
|
2020-10-20 19:50:24 -07:00
|
|
|
let encoding = if let Some(encoding) = config.encoding {
|
|
|
|
encoding
|
|
|
|
} else {
|
|
|
|
self.default_cluster_transaction_encoding()?
|
|
|
|
};
|
2020-10-12 09:14:12 -07:00
|
|
|
let config = RpcSimulateTransactionConfig {
|
|
|
|
encoding: Some(encoding),
|
|
|
|
..config
|
|
|
|
};
|
2020-10-05 21:47:47 -07:00
|
|
|
let serialized_encoded = serialize_encode_transaction(transaction, encoding)?;
|
2020-05-19 12:08:19 -07:00
|
|
|
self.send(
|
|
|
|
RpcRequest::SimulateTransaction,
|
2020-10-11 08:37:45 -07:00
|
|
|
json!([serialized_encoded, config]),
|
2020-05-19 12:08:19 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-16 17:17:44 -07:00
|
|
|
pub fn get_signature_status(
|
|
|
|
&self,
|
2020-03-27 15:46:00 -07:00
|
|
|
signature: &Signature,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> ClientResult<Option<transaction::Result<()>>> {
|
2020-11-16 23:07:10 -08:00
|
|
|
self.get_signature_status_with_commitment(signature, self.commitment_config)
|
2019-11-06 13:15:00 -08:00
|
|
|
}
|
|
|
|
|
2020-04-06 03:04:54 -07:00
|
|
|
pub fn get_signature_statuses(
|
2020-04-04 23:31:24 -07:00
|
|
|
&self,
|
|
|
|
signatures: &[Signature],
|
|
|
|
) -> RpcResult<Vec<Option<TransactionStatus>>> {
|
|
|
|
let signatures: Vec<_> = signatures.iter().map(|s| s.to_string()).collect();
|
2020-05-22 08:53:53 -07:00
|
|
|
self.send(RpcRequest::GetSignatureStatuses, json!([signatures]))
|
2020-04-04 23:31:24 -07:00
|
|
|
}
|
|
|
|
|
2020-10-02 12:35:39 -07:00
|
|
|
pub fn get_signature_statuses_with_history(
|
|
|
|
&self,
|
|
|
|
signatures: &[Signature],
|
|
|
|
) -> RpcResult<Vec<Option<TransactionStatus>>> {
|
|
|
|
let signatures: Vec<_> = signatures.iter().map(|s| s.to_string()).collect();
|
|
|
|
self.send(
|
|
|
|
RpcRequest::GetSignatureStatuses,
|
|
|
|
json!([signatures, {
|
|
|
|
"searchTransactionHistory": true
|
|
|
|
}]),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-11-06 13:15:00 -08:00
|
|
|
pub fn get_signature_status_with_commitment(
|
|
|
|
&self,
|
2020-03-27 15:46:00 -07:00
|
|
|
signature: &Signature,
|
2019-11-06 13:15:00 -08:00
|
|
|
commitment_config: CommitmentConfig,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> ClientResult<Option<transaction::Result<()>>> {
|
2020-05-10 08:51:53 -07:00
|
|
|
let result: Response<Vec<Option<TransactionStatus>>> = self.send(
|
|
|
|
RpcRequest::GetSignatureStatuses,
|
2020-04-06 03:04:54 -07:00
|
|
|
json!([[signature.to_string()]]),
|
2019-11-06 13:15:00 -08:00
|
|
|
)?;
|
2020-03-26 18:21:01 -07:00
|
|
|
Ok(result.value[0]
|
|
|
|
.clone()
|
2020-04-06 03:04:54 -07:00
|
|
|
.filter(|result| result.satisfies_commitment(commitment_config))
|
2020-03-26 18:21:01 -07:00
|
|
|
.map(|status_meta| status_meta.status))
|
2019-03-16 17:17:44 -07:00
|
|
|
}
|
|
|
|
|
2020-04-15 20:51:05 -07:00
|
|
|
pub fn get_signature_status_with_commitment_and_history(
|
|
|
|
&self,
|
|
|
|
signature: &Signature,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
search_transaction_history: bool,
|
|
|
|
) -> ClientResult<Option<transaction::Result<()>>> {
|
2020-05-10 08:51:53 -07:00
|
|
|
let result: Response<Vec<Option<TransactionStatus>>> = self.send(
|
|
|
|
RpcRequest::GetSignatureStatuses,
|
2020-04-15 20:51:05 -07:00
|
|
|
json!([[signature.to_string()], {
|
|
|
|
"searchTransactionHistory": search_transaction_history
|
|
|
|
}]),
|
|
|
|
)?;
|
|
|
|
Ok(result.value[0]
|
|
|
|
.clone()
|
|
|
|
.filter(|result| result.satisfies_commitment(commitment_config))
|
|
|
|
.map(|status_meta| status_meta.status))
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_slot(&self) -> ClientResult<Slot> {
|
2020-11-16 23:07:10 -08:00
|
|
|
self.get_slot_with_commitment(self.commitment_config)
|
2019-11-06 13:15:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_slot_with_commitment(
|
|
|
|
&self,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> ClientResult<Slot> {
|
2020-05-22 08:53:53 -07:00
|
|
|
self.send(RpcRequest::GetSlot, json!([commitment_config]))
|
2019-08-27 15:17:03 -07:00
|
|
|
}
|
|
|
|
|
2020-05-10 11:05:14 -07:00
|
|
|
pub fn supply_with_commitment(
|
|
|
|
&self,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> RpcResult<RpcSupply> {
|
2020-05-22 08:53:53 -07:00
|
|
|
self.send(RpcRequest::GetSupply, json!([commitment_config]))
|
2020-05-10 11:05:14 -07:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn total_supply(&self) -> ClientResult<u64> {
|
2020-11-16 23:07:10 -08:00
|
|
|
self.total_supply_with_commitment(self.commitment_config)
|
2020-03-09 01:28:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn total_supply_with_commitment(
|
|
|
|
&self,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> ClientResult<u64> {
|
2020-05-22 08:53:53 -07:00
|
|
|
self.send(RpcRequest::GetTotalSupply, json!([commitment_config]))
|
2020-03-09 01:28:44 -07:00
|
|
|
}
|
|
|
|
|
2020-05-12 20:05:05 -07:00
|
|
|
pub fn get_largest_accounts_with_config(
|
|
|
|
&self,
|
|
|
|
config: RpcLargestAccountsConfig,
|
|
|
|
) -> RpcResult<Vec<RpcAccountBalance>> {
|
2020-05-22 08:53:53 -07:00
|
|
|
self.send(RpcRequest::GetLargestAccounts, json!([config]))
|
2020-05-12 20:05:05 -07:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_vote_accounts(&self) -> ClientResult<RpcVoteAccountStatus> {
|
2020-11-16 23:07:10 -08:00
|
|
|
self.get_vote_accounts_with_commitment(self.commitment_config)
|
2020-03-03 16:53:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_vote_accounts_with_commitment(
|
|
|
|
&self,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> ClientResult<RpcVoteAccountStatus> {
|
2020-05-22 08:53:53 -07:00
|
|
|
self.send(RpcRequest::GetVoteAccounts, json!([commitment_config]))
|
2019-10-06 20:30:22 -07:00
|
|
|
}
|
|
|
|
|
2020-11-12 13:48:34 -08:00
|
|
|
pub fn wait_for_max_stake(
|
|
|
|
&self,
|
|
|
|
commitment: CommitmentConfig,
|
|
|
|
max_stake_percent: f32,
|
|
|
|
) -> ClientResult<()> {
|
|
|
|
let mut current_percent;
|
|
|
|
loop {
|
|
|
|
let vote_accounts = self.get_vote_accounts_with_commitment(commitment)?;
|
|
|
|
|
|
|
|
let mut max = 0;
|
|
|
|
let total_active_stake = vote_accounts
|
|
|
|
.current
|
|
|
|
.iter()
|
|
|
|
.chain(vote_accounts.delinquent.iter())
|
|
|
|
.map(|vote_account| {
|
|
|
|
max = std::cmp::max(max, vote_account.activated_stake);
|
|
|
|
vote_account.activated_stake
|
|
|
|
})
|
|
|
|
.sum::<u64>();
|
|
|
|
current_percent = 100f32 * max as f32 / total_active_stake as f32;
|
|
|
|
if current_percent < max_stake_percent {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
info!(
|
|
|
|
"Waiting for stake to drop below {} current: {:.1}",
|
|
|
|
max_stake_percent, current_percent
|
|
|
|
);
|
|
|
|
sleep(Duration::from_secs(10));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_cluster_nodes(&self) -> ClientResult<Vec<RpcContactInfo>> {
|
2020-05-22 08:53:53 -07:00
|
|
|
self.send(RpcRequest::GetClusterNodes, Value::Null)
|
2019-11-13 14:58:14 -08:00
|
|
|
}
|
|
|
|
|
2020-09-23 22:10:29 -07:00
|
|
|
pub fn get_confirmed_block(&self, slot: Slot) -> ClientResult<EncodedConfirmedBlock> {
|
2020-07-01 13:06:40 -07:00
|
|
|
self.get_confirmed_block_with_encoding(slot, UiTransactionEncoding::Json)
|
2020-03-24 05:05:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_confirmed_block_with_encoding(
|
|
|
|
&self,
|
|
|
|
slot: Slot,
|
2020-07-01 13:06:40 -07:00
|
|
|
encoding: UiTransactionEncoding,
|
2020-09-23 22:10:29 -07:00
|
|
|
) -> ClientResult<EncodedConfirmedBlock> {
|
2020-05-22 08:53:53 -07:00
|
|
|
self.send(RpcRequest::GetConfirmedBlock, json!([slot, encoding]))
|
2019-12-18 21:31:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_confirmed_blocks(
|
|
|
|
&self,
|
|
|
|
start_slot: Slot,
|
|
|
|
end_slot: Option<Slot>,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> ClientResult<Vec<Slot>> {
|
2020-05-10 08:51:53 -07:00
|
|
|
self.send(
|
|
|
|
RpcRequest::GetConfirmedBlocks,
|
|
|
|
json!([start_slot, end_slot]),
|
|
|
|
)
|
2019-12-18 21:31:38 -08:00
|
|
|
}
|
|
|
|
|
2020-09-30 13:47:12 -07:00
|
|
|
pub fn get_confirmed_blocks_with_limit(
|
|
|
|
&self,
|
|
|
|
start_slot: Slot,
|
|
|
|
limit: usize,
|
|
|
|
) -> ClientResult<Vec<Slot>> {
|
|
|
|
self.send(
|
|
|
|
RpcRequest::GetConfirmedBlocksWithLimit,
|
|
|
|
json!([start_slot, limit]),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-15 20:51:05 -07:00
|
|
|
pub fn get_confirmed_signatures_for_address(
|
|
|
|
&self,
|
|
|
|
address: &Pubkey,
|
|
|
|
start_slot: Slot,
|
|
|
|
end_slot: Slot,
|
|
|
|
) -> ClientResult<Vec<Signature>> {
|
2020-05-10 08:51:53 -07:00
|
|
|
let signatures_base58_str: Vec<String> = self.send(
|
|
|
|
RpcRequest::GetConfirmedSignaturesForAddress,
|
|
|
|
json!([address.to_string(), start_slot, end_slot]),
|
|
|
|
)?;
|
2020-04-20 22:01:09 -07:00
|
|
|
|
|
|
|
let mut signatures = vec![];
|
|
|
|
for signature_base58_str in signatures_base58_str {
|
|
|
|
signatures.push(
|
|
|
|
signature_base58_str.parse::<Signature>().map_err(|err| {
|
|
|
|
Into::<ClientError>::into(RpcError::ParseError(err.to_string()))
|
|
|
|
})?,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(signatures)
|
2020-04-15 20:51:05 -07:00
|
|
|
}
|
|
|
|
|
2020-07-27 11:42:49 -07:00
|
|
|
pub fn get_confirmed_signatures_for_address2(
|
|
|
|
&self,
|
|
|
|
address: &Pubkey,
|
|
|
|
) -> ClientResult<Vec<RpcConfirmedTransactionStatusWithSignature>> {
|
2020-08-15 09:42:17 -07:00
|
|
|
self.get_confirmed_signatures_for_address2_with_config(
|
|
|
|
address,
|
|
|
|
GetConfirmedSignaturesForAddress2Config::default(),
|
|
|
|
)
|
2020-07-27 11:42:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_confirmed_signatures_for_address2_with_config(
|
|
|
|
&self,
|
|
|
|
address: &Pubkey,
|
2020-08-15 09:42:17 -07:00
|
|
|
config: GetConfirmedSignaturesForAddress2Config,
|
2020-07-27 11:42:49 -07:00
|
|
|
) -> ClientResult<Vec<RpcConfirmedTransactionStatusWithSignature>> {
|
|
|
|
let config = RpcGetConfirmedSignaturesForAddress2Config {
|
2020-08-15 09:42:17 -07:00
|
|
|
before: config.before.map(|signature| signature.to_string()),
|
|
|
|
until: config.until.map(|signature| signature.to_string()),
|
|
|
|
limit: config.limit,
|
2020-07-27 11:42:49 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
let result: Vec<RpcConfirmedTransactionStatusWithSignature> = self.send(
|
|
|
|
RpcRequest::GetConfirmedSignaturesForAddress2,
|
|
|
|
json!([address.to_string(), config]),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2020-04-15 20:51:05 -07:00
|
|
|
pub fn get_confirmed_transaction(
|
|
|
|
&self,
|
|
|
|
signature: &Signature,
|
2020-07-01 13:06:40 -07:00
|
|
|
encoding: UiTransactionEncoding,
|
2020-09-23 22:10:29 -07:00
|
|
|
) -> ClientResult<EncodedConfirmedTransaction> {
|
2020-05-10 08:51:53 -07:00
|
|
|
self.send(
|
|
|
|
RpcRequest::GetConfirmedTransaction,
|
|
|
|
json!([signature.to_string(), encoding]),
|
|
|
|
)
|
2020-04-15 20:51:05 -07:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_block_time(&self, slot: Slot) -> ClientResult<UnixTimestamp> {
|
2020-05-10 08:51:53 -07:00
|
|
|
let request = RpcRequest::GetBlockTime;
|
2020-05-22 08:53:53 -07:00
|
|
|
let response = self.sender.send(request, json!([slot]));
|
2019-11-25 23:40:36 -08:00
|
|
|
|
|
|
|
response
|
|
|
|
.map(|result_json| {
|
|
|
|
if result_json.is_null() {
|
2020-03-12 23:20:49 -07:00
|
|
|
return Err(RpcError::ForUser(format!("Block Not Found: slot={}", slot)).into());
|
2019-11-25 23:40:36 -08:00
|
|
|
}
|
2020-03-12 23:20:49 -07:00
|
|
|
let result = serde_json::from_value(result_json)
|
2020-05-10 08:51:53 -07:00
|
|
|
.map_err(|err| ClientError::new_with_request(err.into(), request))?;
|
2019-11-25 23:40:36 -08:00
|
|
|
trace!("Response block timestamp {:?} {:?}", slot, result);
|
|
|
|
Ok(result)
|
|
|
|
})
|
2020-05-10 08:51:53 -07:00
|
|
|
.map_err(|err| err.into_with_request(request))?
|
2019-11-25 23:40:36 -08:00
|
|
|
}
|
|
|
|
|
2020-05-20 16:42:46 -07:00
|
|
|
pub fn get_epoch_info(&self) -> ClientResult<EpochInfo> {
|
2020-11-16 23:07:10 -08:00
|
|
|
self.get_epoch_info_with_commitment(self.commitment_config)
|
2019-11-24 16:34:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_epoch_info_with_commitment(
|
|
|
|
&self,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-05-20 16:42:46 -07:00
|
|
|
) -> ClientResult<EpochInfo> {
|
2020-05-22 08:53:53 -07:00
|
|
|
self.send(RpcRequest::GetEpochInfo, json!([commitment_config]))
|
2019-12-17 15:26:31 -08:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_leader_schedule(
|
|
|
|
&self,
|
|
|
|
slot: Option<Slot>,
|
|
|
|
) -> ClientResult<Option<RpcLeaderSchedule>> {
|
2020-11-16 23:07:10 -08:00
|
|
|
self.get_leader_schedule_with_commitment(slot, self.commitment_config)
|
2019-12-17 15:26:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_leader_schedule_with_commitment(
|
|
|
|
&self,
|
|
|
|
slot: Option<Slot>,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> ClientResult<Option<RpcLeaderSchedule>> {
|
2020-05-10 08:51:53 -07:00
|
|
|
self.send(
|
|
|
|
RpcRequest::GetLeaderSchedule,
|
|
|
|
json!([slot, commitment_config]),
|
|
|
|
)
|
2019-09-27 22:00:30 -07:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_epoch_schedule(&self) -> ClientResult<EpochSchedule> {
|
2020-05-22 08:53:53 -07:00
|
|
|
self.send(RpcRequest::GetEpochSchedule, Value::Null)
|
2019-10-22 13:41:18 -07:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_identity(&self) -> ClientResult<Pubkey> {
|
2020-05-22 08:53:53 -07:00
|
|
|
let rpc_identity: RpcIdentity = self.send(RpcRequest::GetIdentity, Value::Null)?;
|
2020-03-04 14:44:21 -08:00
|
|
|
|
2020-05-10 08:51:53 -07:00
|
|
|
rpc_identity.identity.parse::<Pubkey>().map_err(|_| {
|
|
|
|
ClientError::new_with_request(
|
|
|
|
RpcError::ParseError("Pubkey".to_string()).into(),
|
|
|
|
RpcRequest::GetIdentity,
|
|
|
|
)
|
|
|
|
})
|
2020-03-04 14:44:21 -08:00
|
|
|
}
|
|
|
|
|
2020-05-29 11:50:25 -07:00
|
|
|
pub fn get_inflation_governor(&self) -> ClientResult<RpcInflationGovernor> {
|
|
|
|
self.send(RpcRequest::GetInflationGovernor, Value::Null)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_inflation_rate(&self) -> ClientResult<RpcInflationRate> {
|
|
|
|
self.send(RpcRequest::GetInflationRate, Value::Null)
|
2019-06-12 16:43:05 -07:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_version(&self) -> ClientResult<RpcVersionInfo> {
|
2020-05-22 08:53:53 -07:00
|
|
|
self.send(RpcRequest::GetVersion, Value::Null)
|
2019-08-08 10:13:06 -07:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn minimum_ledger_slot(&self) -> ClientResult<Slot> {
|
2020-05-22 08:53:53 -07:00
|
|
|
self.send(RpcRequest::MinimumLedgerSlot, Value::Null)
|
2020-01-21 12:05:04 -08:00
|
|
|
}
|
|
|
|
|
2020-05-09 09:06:32 -07:00
|
|
|
pub fn send_and_confirm_transaction(
|
2019-03-16 17:17:44 -07:00
|
|
|
&self,
|
2020-05-09 09:06:32 -07:00
|
|
|
transaction: &Transaction,
|
2020-03-27 15:46:00 -07:00
|
|
|
) -> ClientResult<Signature> {
|
2020-09-16 16:47:55 -07:00
|
|
|
let signature = self.send_transaction(transaction)?;
|
2020-09-30 17:44:17 -07:00
|
|
|
let recent_blockhash = if uses_durable_nonce(transaction).is_some() {
|
|
|
|
self.get_recent_blockhash_with_commitment(CommitmentConfig::recent())?
|
|
|
|
.value
|
|
|
|
.0
|
|
|
|
} else {
|
|
|
|
transaction.message.recent_blockhash
|
|
|
|
};
|
2020-09-16 16:47:55 -07:00
|
|
|
let status = loop {
|
|
|
|
let status = self.get_signature_status(&signature)?;
|
|
|
|
if status.is_none() {
|
|
|
|
if self
|
|
|
|
.get_fee_calculator_for_blockhash_with_commitment(
|
|
|
|
&recent_blockhash,
|
|
|
|
CommitmentConfig::recent(),
|
|
|
|
)?
|
|
|
|
.value
|
|
|
|
.is_none()
|
|
|
|
{
|
2019-03-16 17:17:44 -07:00
|
|
|
break status;
|
|
|
|
}
|
2019-04-05 19:56:17 -07:00
|
|
|
} else {
|
2020-09-16 16:47:55 -07:00
|
|
|
break status;
|
|
|
|
}
|
|
|
|
if cfg!(not(test)) {
|
|
|
|
// Retry twice a second
|
|
|
|
sleep(Duration::from_millis(500));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Some(result) = status {
|
|
|
|
match result {
|
|
|
|
Ok(_) => Ok(signature),
|
|
|
|
Err(err) => Err(err.into()),
|
2019-03-16 17:17:44 -07:00
|
|
|
}
|
2020-09-16 16:47:55 -07:00
|
|
|
} else {
|
|
|
|
Err(RpcError::ForUser(
|
|
|
|
"unable to confirm transaction. \
|
|
|
|
This can happen in situations such as transaction expiration \
|
|
|
|
and insufficient fee-payer funds"
|
|
|
|
.to_string(),
|
|
|
|
)
|
|
|
|
.into())
|
2019-03-16 17:17:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 09:12:03 -08:00
|
|
|
/// Note that `get_account` returns `Err(..)` if the account does not exist whereas
|
|
|
|
/// `get_account_with_commitment` returns `Ok(None)` if the account does not exist.
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_account(&self, pubkey: &Pubkey) -> ClientResult<Account> {
|
2020-11-16 23:07:10 -08:00
|
|
|
self.get_account_with_commitment(pubkey, self.commitment_config)?
|
2019-11-13 13:41:54 -08:00
|
|
|
.value
|
2020-03-12 23:20:49 -07:00
|
|
|
.ok_or_else(|| RpcError::ForUser(format!("AccountNotFound: pubkey={}", pubkey)).into())
|
2019-11-06 13:15:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_account_with_commitment(
|
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> RpcResult<Option<Account>> {
|
2020-08-08 22:40:13 -07:00
|
|
|
let config = RpcAccountInfoConfig {
|
2020-08-15 22:06:39 -07:00
|
|
|
encoding: Some(UiAccountEncoding::Base64),
|
2020-08-08 22:40:13 -07:00
|
|
|
commitment: Some(commitment_config),
|
2020-08-10 15:35:29 -07:00
|
|
|
data_slice: None,
|
2020-08-08 22:40:13 -07:00
|
|
|
};
|
2020-05-20 18:40:45 -07:00
|
|
|
let response = self.sender.send(
|
2020-05-10 08:51:53 -07:00
|
|
|
RpcRequest::GetAccountInfo,
|
2020-08-08 22:40:13 -07:00
|
|
|
json!([pubkey.to_string(), config]),
|
2019-11-06 13:15:00 -08:00
|
|
|
);
|
2019-03-16 22:37:20 -07:00
|
|
|
|
|
|
|
response
|
2019-11-12 11:49:41 -08:00
|
|
|
.map(|result_json| {
|
|
|
|
if result_json.is_null() {
|
2020-03-12 23:20:49 -07:00
|
|
|
return Err(
|
|
|
|
RpcError::ForUser(format!("AccountNotFound: pubkey={}", pubkey)).into(),
|
|
|
|
);
|
2019-11-12 11:49:41 -08:00
|
|
|
}
|
2020-01-15 14:33:53 -08:00
|
|
|
let Response {
|
|
|
|
context,
|
2020-08-15 17:56:09 -07:00
|
|
|
value: rpc_account,
|
2020-06-30 21:55:11 -07:00
|
|
|
} = serde_json::from_value::<Response<Option<UiAccount>>>(result_json)?;
|
2020-01-15 14:33:53 -08:00
|
|
|
trace!("Response account {:?} {:?}", pubkey, rpc_account);
|
2020-06-30 21:55:11 -07:00
|
|
|
let account = rpc_account.and_then(|rpc_account| rpc_account.decode());
|
2020-01-15 14:33:53 -08:00
|
|
|
Ok(Response {
|
|
|
|
context,
|
|
|
|
value: account,
|
|
|
|
})
|
2019-03-16 22:37:20 -07:00
|
|
|
})
|
2019-05-13 12:49:37 -07:00
|
|
|
.map_err(|err| {
|
2020-03-12 23:20:49 -07:00
|
|
|
Into::<ClientError>::into(RpcError::ForUser(format!(
|
|
|
|
"AccountNotFound: pubkey={}: {}",
|
|
|
|
pubkey, err
|
|
|
|
)))
|
2019-11-12 11:49:41 -08:00
|
|
|
})?
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
|
|
|
|
2020-09-03 10:35:06 -07:00
|
|
|
pub fn get_multiple_accounts(&self, pubkeys: &[Pubkey]) -> ClientResult<Vec<Option<Account>>> {
|
|
|
|
Ok(self
|
2020-11-16 23:07:10 -08:00
|
|
|
.get_multiple_accounts_with_commitment(pubkeys, self.commitment_config)?
|
2020-09-03 10:35:06 -07:00
|
|
|
.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_multiple_accounts_with_commitment(
|
|
|
|
&self,
|
|
|
|
pubkeys: &[Pubkey],
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> RpcResult<Vec<Option<Account>>> {
|
|
|
|
let config = RpcAccountInfoConfig {
|
|
|
|
encoding: Some(UiAccountEncoding::Base64),
|
|
|
|
commitment: Some(commitment_config),
|
|
|
|
data_slice: None,
|
|
|
|
};
|
|
|
|
let pubkeys: Vec<_> = pubkeys.iter().map(|pubkey| pubkey.to_string()).collect();
|
2020-09-23 11:11:56 -07:00
|
|
|
let response = self.send(RpcRequest::GetMultipleAccounts, json!([pubkeys, config]))?;
|
2020-09-03 10:35:06 -07:00
|
|
|
let Response {
|
|
|
|
context,
|
|
|
|
value: accounts,
|
2020-09-23 11:11:56 -07:00
|
|
|
} = serde_json::from_value::<Response<Vec<Option<UiAccount>>>>(response)?;
|
2020-09-03 10:35:06 -07:00
|
|
|
let accounts: Vec<Option<Account>> = accounts
|
2020-09-23 11:11:56 -07:00
|
|
|
.into_iter()
|
|
|
|
.map(|rpc_account| rpc_account.map(|a| a.decode()).flatten())
|
2020-09-03 10:35:06 -07:00
|
|
|
.collect();
|
|
|
|
Ok(Response {
|
|
|
|
context,
|
|
|
|
value: accounts,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_account_data(&self, pubkey: &Pubkey) -> ClientResult<Vec<u8>> {
|
2019-11-13 13:41:54 -08:00
|
|
|
Ok(self.get_account(pubkey)?.data)
|
2019-05-08 15:50:23 -07:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_minimum_balance_for_rent_exemption(&self, data_len: usize) -> ClientResult<u64> {
|
2020-05-10 08:51:53 -07:00
|
|
|
let request = RpcRequest::GetMinimumBalanceForRentExemption;
|
2019-09-26 10:57:13 -07:00
|
|
|
let minimum_balance_json = self
|
2020-05-20 18:40:45 -07:00
|
|
|
.sender
|
2020-05-22 08:53:53 -07:00
|
|
|
.send(request, json!([data_len]))
|
2020-05-10 08:51:53 -07:00
|
|
|
.map_err(|err| err.into_with_request(request))?;
|
2019-09-26 10:57:13 -07:00
|
|
|
|
2020-05-10 08:51:53 -07:00
|
|
|
let minimum_balance: u64 = serde_json::from_value(minimum_balance_json)
|
|
|
|
.map_err(|err| ClientError::new_with_request(err.into(), request))?;
|
2019-09-26 10:57:13 -07:00
|
|
|
trace!(
|
|
|
|
"Response minimum balance {:?} {:?}",
|
|
|
|
data_len,
|
|
|
|
minimum_balance
|
|
|
|
);
|
|
|
|
Ok(minimum_balance)
|
|
|
|
}
|
|
|
|
|
2019-11-06 13:15:00 -08:00
|
|
|
/// Request the balance of the account `pubkey`.
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_balance(&self, pubkey: &Pubkey) -> ClientResult<u64> {
|
2019-11-12 11:49:41 -08:00
|
|
|
Ok(self
|
2020-11-16 23:07:10 -08:00
|
|
|
.get_balance_with_commitment(pubkey, self.commitment_config)?
|
2019-11-12 11:49:41 -08:00
|
|
|
.value)
|
2019-11-06 13:15:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_balance_with_commitment(
|
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> RpcResult<u64> {
|
2020-05-10 08:51:53 -07:00
|
|
|
self.send(
|
|
|
|
RpcRequest::GetBalance,
|
|
|
|
json!([pubkey.to_string(), commitment_config]),
|
|
|
|
)
|
2019-05-08 15:50:23 -07:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_program_accounts(&self, pubkey: &Pubkey) -> ClientResult<Vec<(Pubkey, Account)>> {
|
2020-10-02 13:58:50 -07:00
|
|
|
self.get_program_accounts_with_config(
|
|
|
|
pubkey,
|
|
|
|
RpcProgramAccountsConfig {
|
|
|
|
filters: None,
|
|
|
|
account_config: RpcAccountInfoConfig {
|
|
|
|
encoding: Some(UiAccountEncoding::Base64),
|
|
|
|
..RpcAccountInfoConfig::default()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2020-09-29 09:53:00 -07:00
|
|
|
|
2020-10-02 13:58:50 -07:00
|
|
|
pub fn get_program_accounts_with_config(
|
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
config: RpcProgramAccountsConfig,
|
|
|
|
) -> ClientResult<Vec<(Pubkey, Account)>> {
|
2020-09-29 09:53:00 -07:00
|
|
|
let accounts: Vec<RpcKeyedAccount> = self.send(
|
|
|
|
RpcRequest::GetProgramAccounts,
|
|
|
|
json!([pubkey.to_string(), config]),
|
|
|
|
)?;
|
2020-07-31 13:53:49 -07:00
|
|
|
parse_keyed_accounts(accounts, RpcRequest::GetProgramAccounts)
|
2019-07-11 11:38:52 -07:00
|
|
|
}
|
|
|
|
|
2019-11-06 13:15:00 -08:00
|
|
|
/// Request the transaction count.
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_transaction_count(&self) -> ClientResult<u64> {
|
2020-11-16 23:07:10 -08:00
|
|
|
self.get_transaction_count_with_commitment(self.commitment_config)
|
2019-11-06 13:15:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_transaction_count_with_commitment(
|
|
|
|
&self,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> ClientResult<u64> {
|
2020-05-22 08:53:53 -07:00
|
|
|
self.send(RpcRequest::GetTransactionCount, json!([commitment_config]))
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_recent_blockhash(&self) -> ClientResult<(Hash, FeeCalculator)> {
|
2020-05-26 12:06:21 -07:00
|
|
|
let (blockhash, fee_calculator, _last_valid_slot) = self
|
2020-11-16 23:07:10 -08:00
|
|
|
.get_recent_blockhash_with_commitment(self.commitment_config)?
|
2020-05-26 12:06:21 -07:00
|
|
|
.value;
|
|
|
|
Ok((blockhash, fee_calculator))
|
2019-11-06 13:15:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_recent_blockhash_with_commitment(
|
|
|
|
&self,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-05-26 12:06:21 -07:00
|
|
|
) -> RpcResult<(Hash, FeeCalculator, Slot)> {
|
|
|
|
let (context, blockhash, fee_calculator, last_valid_slot) = if let Ok(Response {
|
|
|
|
context,
|
|
|
|
value:
|
|
|
|
RpcFees {
|
|
|
|
blockhash,
|
|
|
|
fee_calculator,
|
|
|
|
last_valid_slot,
|
|
|
|
},
|
|
|
|
}) =
|
|
|
|
self.send::<Response<RpcFees>>(RpcRequest::GetFees, json!([commitment_config]))
|
|
|
|
{
|
|
|
|
(context, blockhash, fee_calculator, last_valid_slot)
|
|
|
|
} else if let Ok(Response {
|
2019-11-12 11:49:41 -08:00
|
|
|
context,
|
2020-01-14 23:25:45 -08:00
|
|
|
value:
|
|
|
|
RpcBlockhashFeeCalculator {
|
|
|
|
blockhash,
|
|
|
|
fee_calculator,
|
|
|
|
},
|
2020-05-26 12:06:21 -07:00
|
|
|
}) = self.send::<Response<RpcBlockhashFeeCalculator>>(
|
2020-05-10 08:51:53 -07:00
|
|
|
RpcRequest::GetRecentBlockhash,
|
|
|
|
json!([commitment_config]),
|
2020-05-26 12:06:21 -07:00
|
|
|
) {
|
|
|
|
(context, blockhash, fee_calculator, 0)
|
|
|
|
} else {
|
|
|
|
return Err(ClientError::new_with_request(
|
|
|
|
RpcError::ParseError("RpcBlockhashFeeCalculator or RpcFees".to_string()).into(),
|
|
|
|
RpcRequest::GetRecentBlockhash,
|
|
|
|
));
|
|
|
|
};
|
2020-05-10 08:51:53 -07:00
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
let blockhash = blockhash.parse().map_err(|_| {
|
2020-05-10 08:51:53 -07:00
|
|
|
ClientError::new_with_request(
|
2020-03-12 23:20:49 -07:00
|
|
|
RpcError::ParseError("Hash".to_string()).into(),
|
2020-05-10 08:51:53 -07:00
|
|
|
RpcRequest::GetRecentBlockhash,
|
2019-05-13 12:49:37 -07:00
|
|
|
)
|
|
|
|
})?;
|
2019-11-12 11:49:41 -08:00
|
|
|
Ok(Response {
|
|
|
|
context,
|
2020-05-26 12:06:21 -07:00
|
|
|
value: (blockhash, fee_calculator, last_valid_slot),
|
2019-11-12 11:49:41 -08:00
|
|
|
})
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:01:31 -08:00
|
|
|
pub fn get_fee_calculator_for_blockhash(
|
|
|
|
&self,
|
|
|
|
blockhash: &Hash,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> ClientResult<Option<FeeCalculator>> {
|
2020-05-26 16:23:58 -07:00
|
|
|
Ok(self
|
2020-11-16 23:07:10 -08:00
|
|
|
.get_fee_calculator_for_blockhash_with_commitment(blockhash, self.commitment_config)?
|
2020-05-26 16:23:58 -07:00
|
|
|
.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_fee_calculator_for_blockhash_with_commitment(
|
|
|
|
&self,
|
|
|
|
blockhash: &Hash,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> RpcResult<Option<FeeCalculator>> {
|
|
|
|
let Response { context, value } = self.send::<Response<Option<RpcFeeCalculator>>>(
|
2020-05-10 08:51:53 -07:00
|
|
|
RpcRequest::GetFeeCalculatorForBlockhash,
|
2020-05-26 16:23:58 -07:00
|
|
|
json!([blockhash.to_string(), commitment_config]),
|
2020-05-10 08:51:53 -07:00
|
|
|
)?;
|
|
|
|
|
2020-05-26 16:23:58 -07:00
|
|
|
Ok(Response {
|
|
|
|
context,
|
|
|
|
value: value.map(|rf| rf.fee_calculator),
|
|
|
|
})
|
2020-03-06 16:01:31 -08:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_fee_rate_governor(&self) -> RpcResult<FeeRateGovernor> {
|
2020-02-28 12:27:01 -08:00
|
|
|
let Response {
|
|
|
|
context,
|
|
|
|
value: RpcFeeRateGovernor { fee_rate_governor },
|
2020-05-22 08:53:53 -07:00
|
|
|
} =
|
|
|
|
self.send::<Response<RpcFeeRateGovernor>>(RpcRequest::GetFeeRateGovernor, Value::Null)?;
|
2020-05-10 08:51:53 -07:00
|
|
|
|
2020-02-28 12:27:01 -08:00
|
|
|
Ok(Response {
|
|
|
|
context,
|
|
|
|
value: fee_rate_governor,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_new_blockhash(&self, blockhash: &Hash) -> ClientResult<(Hash, FeeCalculator)> {
|
2019-09-27 10:36:38 -07:00
|
|
|
let mut num_retries = 0;
|
2019-09-20 15:50:43 -07:00
|
|
|
let start = Instant::now();
|
2019-09-27 10:36:38 -07:00
|
|
|
while start.elapsed().as_secs() < 5 {
|
2019-05-13 12:49:37 -07:00
|
|
|
if let Ok((new_blockhash, fee_calculator)) = self.get_recent_blockhash() {
|
2019-03-16 17:17:44 -07:00
|
|
|
if new_blockhash != *blockhash {
|
2019-05-13 12:49:37 -07:00
|
|
|
return Ok((new_blockhash, fee_calculator));
|
2019-03-16 17:17:44 -07:00
|
|
|
}
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
|
|
|
debug!("Got same blockhash ({:?}), will retry...", blockhash);
|
2019-03-16 17:17:44 -07:00
|
|
|
|
|
|
|
// Retry ~twice during a slot
|
|
|
|
sleep(Duration::from_millis(
|
2019-08-19 23:22:56 -07:00
|
|
|
500 * DEFAULT_TICKS_PER_SLOT / DEFAULT_TICKS_PER_SECOND,
|
2019-03-16 17:17:44 -07:00
|
|
|
));
|
2019-09-27 10:36:38 -07:00
|
|
|
num_retries += 1;
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
2020-03-12 23:20:49 -07:00
|
|
|
Err(RpcError::ForUser(format!(
|
|
|
|
"Unable to get new blockhash after {}ms (retried {} times), stuck at {}",
|
|
|
|
start.elapsed().as_millis(),
|
|
|
|
num_retries,
|
|
|
|
blockhash
|
2019-03-16 17:17:44 -07:00
|
|
|
))
|
2020-03-12 23:20:49 -07:00
|
|
|
.into())
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
|
|
|
|
2020-09-06 10:54:01 -07:00
|
|
|
pub fn get_first_available_block(&self) -> ClientResult<Slot> {
|
|
|
|
self.send(RpcRequest::GetFirstAvailableBlock, Value::Null)
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn get_genesis_hash(&self) -> ClientResult<Hash> {
|
2020-05-22 08:53:53 -07:00
|
|
|
let hash_str: String = self.send(RpcRequest::GetGenesisHash, Value::Null)?;
|
2020-05-10 08:51:53 -07:00
|
|
|
let hash = hash_str.parse().map_err(|_| {
|
|
|
|
ClientError::new_with_request(
|
2020-03-12 23:20:49 -07:00
|
|
|
RpcError::ParseError("Hash".to_string()).into(),
|
2020-05-10 08:51:53 -07:00
|
|
|
RpcRequest::GetGenesisHash,
|
2019-08-21 18:16:40 -07:00
|
|
|
)
|
|
|
|
})?;
|
2019-11-08 20:56:57 -08:00
|
|
|
Ok(hash)
|
2019-08-21 18:16:40 -07:00
|
|
|
}
|
|
|
|
|
2020-09-18 19:45:13 -07:00
|
|
|
pub fn get_token_account(&self, pubkey: &Pubkey) -> ClientResult<Option<UiTokenAccount>> {
|
|
|
|
Ok(self
|
2020-11-16 23:07:10 -08:00
|
|
|
.get_token_account_with_commitment(pubkey, self.commitment_config)?
|
2020-09-18 19:45:13 -07:00
|
|
|
.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_token_account_with_commitment(
|
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> RpcResult<Option<UiTokenAccount>> {
|
|
|
|
let config = RpcAccountInfoConfig {
|
|
|
|
encoding: Some(UiAccountEncoding::JsonParsed),
|
|
|
|
commitment: Some(commitment_config),
|
|
|
|
data_slice: None,
|
|
|
|
};
|
|
|
|
let response = self.sender.send(
|
|
|
|
RpcRequest::GetAccountInfo,
|
|
|
|
json!([pubkey.to_string(), config]),
|
|
|
|
);
|
|
|
|
|
|
|
|
response
|
|
|
|
.map(|result_json| {
|
|
|
|
if result_json.is_null() {
|
|
|
|
return Err(
|
|
|
|
RpcError::ForUser(format!("AccountNotFound: pubkey={}", pubkey)).into(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
let Response {
|
|
|
|
context,
|
|
|
|
value: rpc_account,
|
|
|
|
} = serde_json::from_value::<Response<Option<UiAccount>>>(result_json)?;
|
|
|
|
trace!("Response account {:?} {:?}", pubkey, rpc_account);
|
|
|
|
let response = {
|
|
|
|
if let Some(rpc_account) = rpc_account {
|
|
|
|
if let UiAccountData::Json(account_data) = rpc_account.data {
|
|
|
|
let token_account_type: TokenAccountType =
|
|
|
|
serde_json::from_value(account_data.parsed)?;
|
|
|
|
if let TokenAccountType::Account(token_account) = token_account_type {
|
|
|
|
return Ok(Response {
|
|
|
|
context,
|
|
|
|
value: Some(token_account),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(Into::<ClientError>::into(RpcError::ForUser(format!(
|
|
|
|
"Account could not be parsed as token account: pubkey={}",
|
|
|
|
pubkey
|
|
|
|
))))
|
|
|
|
};
|
|
|
|
response?
|
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
Into::<ClientError>::into(RpcError::ForUser(format!(
|
|
|
|
"AccountNotFound: pubkey={}: {}",
|
|
|
|
pubkey, err
|
|
|
|
)))
|
|
|
|
})?
|
|
|
|
}
|
|
|
|
|
2020-08-07 10:37:39 -07:00
|
|
|
pub fn get_token_account_balance(&self, pubkey: &Pubkey) -> ClientResult<UiTokenAmount> {
|
2020-07-31 13:53:49 -07:00
|
|
|
Ok(self
|
2020-11-16 23:07:10 -08:00
|
|
|
.get_token_account_balance_with_commitment(pubkey, self.commitment_config)?
|
2020-07-31 13:53:49 -07:00
|
|
|
.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_token_account_balance_with_commitment(
|
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-08-07 10:37:39 -07:00
|
|
|
) -> RpcResult<UiTokenAmount> {
|
2020-07-31 13:53:49 -07:00
|
|
|
self.send(
|
|
|
|
RpcRequest::GetTokenAccountBalance,
|
|
|
|
json!([pubkey.to_string(), commitment_config]),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_token_accounts_by_delegate(
|
|
|
|
&self,
|
|
|
|
delegate: &Pubkey,
|
|
|
|
token_account_filter: TokenAccountsFilter,
|
2020-08-11 09:14:17 -07:00
|
|
|
) -> ClientResult<Vec<RpcKeyedAccount>> {
|
2020-07-31 13:53:49 -07:00
|
|
|
Ok(self
|
|
|
|
.get_token_accounts_by_delegate_with_commitment(
|
|
|
|
delegate,
|
|
|
|
token_account_filter,
|
2020-11-16 23:07:10 -08:00
|
|
|
self.commitment_config,
|
2020-07-31 13:53:49 -07:00
|
|
|
)?
|
|
|
|
.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_token_accounts_by_delegate_with_commitment(
|
|
|
|
&self,
|
|
|
|
delegate: &Pubkey,
|
|
|
|
token_account_filter: TokenAccountsFilter,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-08-11 09:14:17 -07:00
|
|
|
) -> RpcResult<Vec<RpcKeyedAccount>> {
|
2020-07-31 13:53:49 -07:00
|
|
|
let token_account_filter = match token_account_filter {
|
|
|
|
TokenAccountsFilter::Mint(mint) => RpcTokenAccountsFilter::Mint(mint.to_string()),
|
|
|
|
TokenAccountsFilter::ProgramId(program_id) => {
|
|
|
|
RpcTokenAccountsFilter::ProgramId(program_id.to_string())
|
|
|
|
}
|
|
|
|
};
|
2020-08-11 09:14:17 -07:00
|
|
|
|
|
|
|
let config = RpcAccountInfoConfig {
|
|
|
|
encoding: Some(UiAccountEncoding::JsonParsed),
|
|
|
|
commitment: Some(commitment_config),
|
|
|
|
data_slice: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.send(
|
|
|
|
RpcRequest::GetTokenAccountsByOwner,
|
|
|
|
json!([delegate.to_string(), token_account_filter, config]),
|
|
|
|
)
|
2020-07-31 13:53:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_token_accounts_by_owner(
|
|
|
|
&self,
|
|
|
|
owner: &Pubkey,
|
|
|
|
token_account_filter: TokenAccountsFilter,
|
2020-08-11 09:14:17 -07:00
|
|
|
) -> ClientResult<Vec<RpcKeyedAccount>> {
|
2020-07-31 13:53:49 -07:00
|
|
|
Ok(self
|
|
|
|
.get_token_accounts_by_owner_with_commitment(
|
|
|
|
owner,
|
|
|
|
token_account_filter,
|
2020-11-16 23:07:10 -08:00
|
|
|
self.commitment_config,
|
2020-07-31 13:53:49 -07:00
|
|
|
)?
|
|
|
|
.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_token_accounts_by_owner_with_commitment(
|
|
|
|
&self,
|
|
|
|
owner: &Pubkey,
|
|
|
|
token_account_filter: TokenAccountsFilter,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-08-11 09:14:17 -07:00
|
|
|
) -> RpcResult<Vec<RpcKeyedAccount>> {
|
2020-07-31 13:53:49 -07:00
|
|
|
let token_account_filter = match token_account_filter {
|
|
|
|
TokenAccountsFilter::Mint(mint) => RpcTokenAccountsFilter::Mint(mint.to_string()),
|
|
|
|
TokenAccountsFilter::ProgramId(program_id) => {
|
|
|
|
RpcTokenAccountsFilter::ProgramId(program_id.to_string())
|
|
|
|
}
|
|
|
|
};
|
2020-08-11 09:14:17 -07:00
|
|
|
|
|
|
|
let config = RpcAccountInfoConfig {
|
|
|
|
encoding: Some(UiAccountEncoding::JsonParsed),
|
|
|
|
commitment: Some(commitment_config),
|
|
|
|
data_slice: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.send(
|
2020-07-31 13:53:49 -07:00
|
|
|
RpcRequest::GetTokenAccountsByOwner,
|
2020-08-11 09:14:17 -07:00
|
|
|
json!([owner.to_string(), token_account_filter, config]),
|
|
|
|
)
|
2020-07-31 13:53:49 -07:00
|
|
|
}
|
|
|
|
|
2020-08-07 10:37:39 -07:00
|
|
|
pub fn get_token_supply(&self, mint: &Pubkey) -> ClientResult<UiTokenAmount> {
|
2020-07-31 13:53:49 -07:00
|
|
|
Ok(self
|
2020-11-16 23:07:10 -08:00
|
|
|
.get_token_supply_with_commitment(mint, self.commitment_config)?
|
2020-07-31 13:53:49 -07:00
|
|
|
.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_token_supply_with_commitment(
|
|
|
|
&self,
|
|
|
|
mint: &Pubkey,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-08-07 10:37:39 -07:00
|
|
|
) -> RpcResult<UiTokenAmount> {
|
2020-07-31 13:53:49 -07:00
|
|
|
self.send(
|
|
|
|
RpcRequest::GetTokenSupply,
|
|
|
|
json!([mint.to_string(), commitment_config]),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-01 21:44:07 -07:00
|
|
|
fn poll_balance_with_timeout_and_commitment(
|
2019-03-16 22:37:20 -07:00
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
polling_frequency: &Duration,
|
|
|
|
timeout: &Duration,
|
2019-11-06 13:15:00 -08:00
|
|
|
commitment_config: CommitmentConfig,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> ClientResult<u64> {
|
2019-03-16 22:37:20 -07:00
|
|
|
let now = Instant::now();
|
|
|
|
loop {
|
2020-06-08 17:38:14 -07:00
|
|
|
match self.get_balance_with_commitment(&pubkey, commitment_config) {
|
2019-03-16 22:37:20 -07:00
|
|
|
Ok(bal) => {
|
2019-11-12 11:49:41 -08:00
|
|
|
return Ok(bal.value);
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
sleep(*polling_frequency);
|
|
|
|
if now.elapsed() > *timeout {
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 13:15:00 -08:00
|
|
|
pub fn poll_get_balance_with_commitment(
|
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> ClientResult<u64> {
|
2019-11-06 13:15:00 -08:00
|
|
|
self.poll_balance_with_timeout_and_commitment(
|
|
|
|
pubkey,
|
|
|
|
&Duration::from_millis(100),
|
|
|
|
&Duration::from_secs(1),
|
|
|
|
commitment_config,
|
|
|
|
)
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
|
|
|
|
2019-11-06 13:15:00 -08:00
|
|
|
pub fn wait_for_balance_with_commitment(
|
|
|
|
&self,
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
expected_balance: Option<u64>,
|
|
|
|
commitment_config: CommitmentConfig,
|
|
|
|
) -> Option<u64> {
|
2019-03-16 23:29:11 -07:00
|
|
|
const LAST: usize = 30;
|
|
|
|
for run in 0..LAST {
|
2020-06-08 17:38:14 -07:00
|
|
|
let balance_result = self.poll_get_balance_with_commitment(pubkey, commitment_config);
|
2019-03-16 23:29:11 -07:00
|
|
|
if expected_balance.is_none() {
|
|
|
|
return balance_result.ok();
|
|
|
|
}
|
|
|
|
trace!(
|
2020-05-10 08:51:53 -07:00
|
|
|
"wait_for_balance_with_commitment [{}] {:?} {:?}",
|
2019-03-16 23:29:11 -07:00
|
|
|
run,
|
|
|
|
balance_result,
|
|
|
|
expected_balance
|
|
|
|
);
|
|
|
|
if let (Some(expected_balance), Ok(balance_result)) = (expected_balance, balance_result)
|
|
|
|
{
|
|
|
|
if expected_balance == balance_result {
|
|
|
|
return Some(balance_result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-03-16 22:37:20 -07:00
|
|
|
/// Poll the server to confirm a transaction.
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn poll_for_signature(&self, signature: &Signature) -> ClientResult<()> {
|
2020-11-16 23:07:10 -08:00
|
|
|
self.poll_for_signature_with_commitment(signature, self.commitment_config)
|
2019-11-06 13:15:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Poll the server to confirm a transaction.
|
|
|
|
pub fn poll_for_signature_with_commitment(
|
|
|
|
&self,
|
|
|
|
signature: &Signature,
|
|
|
|
commitment_config: CommitmentConfig,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> ClientResult<()> {
|
2019-03-16 22:37:20 -07:00
|
|
|
let now = Instant::now();
|
2019-11-06 13:15:00 -08:00
|
|
|
loop {
|
2020-03-27 15:46:00 -07:00
|
|
|
if let Ok(Some(_)) =
|
2020-06-08 17:38:14 -07:00
|
|
|
self.get_signature_status_with_commitment(&signature, commitment_config)
|
2020-03-27 15:46:00 -07:00
|
|
|
{
|
2019-11-06 13:15:00 -08:00
|
|
|
break;
|
|
|
|
}
|
2019-03-16 22:37:20 -07:00
|
|
|
if now.elapsed().as_secs() > 15 {
|
2020-03-12 23:20:49 -07:00
|
|
|
return Err(RpcError::ForUser(format!(
|
|
|
|
"signature not found after {} seconds",
|
|
|
|
now.elapsed().as_secs()
|
|
|
|
))
|
|
|
|
.into());
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
|
|
|
sleep(Duration::from_millis(250));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:43:21 -07:00
|
|
|
/// Poll the server to confirm a transaction.
|
|
|
|
pub fn poll_for_signature_confirmation(
|
|
|
|
&self,
|
|
|
|
signature: &Signature,
|
|
|
|
min_confirmed_blocks: usize,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> ClientResult<usize> {
|
2019-03-21 07:43:21 -07:00
|
|
|
let mut now = Instant::now();
|
|
|
|
let mut confirmed_blocks = 0;
|
|
|
|
loop {
|
|
|
|
let response = self.get_num_blocks_since_signature_confirmation(signature);
|
|
|
|
match response {
|
|
|
|
Ok(count) => {
|
|
|
|
if confirmed_blocks != count {
|
|
|
|
info!(
|
2019-07-01 13:21:00 -07:00
|
|
|
"signature {} confirmed {} out of {} after {} ms",
|
|
|
|
signature,
|
|
|
|
count,
|
|
|
|
min_confirmed_blocks,
|
|
|
|
now.elapsed().as_millis()
|
2019-03-21 07:43:21 -07:00
|
|
|
);
|
|
|
|
now = Instant::now();
|
|
|
|
confirmed_blocks = count;
|
|
|
|
}
|
|
|
|
if count >= min_confirmed_blocks {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
debug!("check_confirmations request failed: {:?}", err);
|
|
|
|
}
|
|
|
|
};
|
2019-11-06 13:15:00 -08:00
|
|
|
if now.elapsed().as_secs() > 20 {
|
2019-07-01 13:21:00 -07:00
|
|
|
info!(
|
|
|
|
"signature {} confirmed {} out of {} failed after {} ms",
|
|
|
|
signature,
|
|
|
|
confirmed_blocks,
|
|
|
|
min_confirmed_blocks,
|
|
|
|
now.elapsed().as_millis()
|
|
|
|
);
|
2019-07-02 20:56:10 -07:00
|
|
|
if confirmed_blocks > 0 {
|
|
|
|
return Ok(confirmed_blocks);
|
|
|
|
} else {
|
2020-03-12 23:20:49 -07:00
|
|
|
return Err(RpcError::ForUser(format!(
|
|
|
|
"signature not found after {} seconds",
|
|
|
|
now.elapsed().as_secs()
|
|
|
|
))
|
|
|
|
.into());
|
2019-07-02 20:56:10 -07:00
|
|
|
}
|
2019-03-21 07:43:21 -07:00
|
|
|
}
|
2019-09-24 16:01:18 -07:00
|
|
|
sleep(Duration::from_millis(250));
|
2019-03-21 07:43:21 -07:00
|
|
|
}
|
2019-07-02 20:56:10 -07:00
|
|
|
Ok(confirmed_blocks)
|
2019-03-21 07:43:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_num_blocks_since_signature_confirmation(
|
|
|
|
&self,
|
2019-12-18 21:26:11 -08:00
|
|
|
signature: &Signature,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> ClientResult<usize> {
|
2020-05-10 08:51:53 -07:00
|
|
|
let result: Response<Vec<Option<TransactionStatus>>> = self.send(
|
|
|
|
RpcRequest::GetSignatureStatuses,
|
|
|
|
json!([[signature.to_string()]]),
|
|
|
|
)?;
|
2020-03-26 18:21:01 -07:00
|
|
|
|
|
|
|
let confirmations = result.value[0]
|
|
|
|
.clone()
|
|
|
|
.ok_or_else(|| {
|
2020-05-10 08:51:53 -07:00
|
|
|
ClientError::new_with_request(
|
2020-03-26 18:21:01 -07:00
|
|
|
ClientErrorKind::Custom("signature not found".to_string()),
|
2020-05-10 08:51:53 -07:00
|
|
|
RpcRequest::GetSignatureStatuses,
|
2020-03-26 18:21:01 -07:00
|
|
|
)
|
|
|
|
})?
|
|
|
|
.confirmations
|
|
|
|
.unwrap_or(MAX_LOCKOUT_HISTORY + 1);
|
|
|
|
Ok(confirmations)
|
2019-03-21 07:43:21 -07:00
|
|
|
}
|
|
|
|
|
2020-05-09 09:06:32 -07:00
|
|
|
pub fn send_and_confirm_transaction_with_spinner(
|
2020-03-17 16:58:02 -07:00
|
|
|
&self,
|
2020-05-09 09:06:32 -07:00
|
|
|
transaction: &Transaction,
|
2020-05-31 23:00:51 -07:00
|
|
|
) -> ClientResult<Signature> {
|
2020-11-16 23:07:10 -08:00
|
|
|
self.send_and_confirm_transaction_with_spinner_and_commitment(
|
2020-05-31 23:00:51 -07:00
|
|
|
transaction,
|
2020-11-16 23:07:10 -08:00
|
|
|
self.commitment_config,
|
2020-06-17 11:18:48 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send_and_confirm_transaction_with_spinner_and_commitment(
|
|
|
|
&self,
|
|
|
|
transaction: &Transaction,
|
|
|
|
commitment: CommitmentConfig,
|
|
|
|
) -> ClientResult<Signature> {
|
|
|
|
self.send_and_confirm_transaction_with_spinner_and_config(
|
|
|
|
transaction,
|
|
|
|
commitment,
|
2020-11-16 09:14:15 -08:00
|
|
|
RpcSendTransactionConfig {
|
|
|
|
preflight_commitment: Some(commitment.commitment),
|
|
|
|
..RpcSendTransactionConfig::default()
|
|
|
|
},
|
2020-05-31 23:00:51 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send_and_confirm_transaction_with_spinner_and_config(
|
|
|
|
&self,
|
|
|
|
transaction: &Transaction,
|
2020-06-17 11:18:48 -07:00
|
|
|
commitment: CommitmentConfig,
|
2020-05-31 23:00:51 -07:00
|
|
|
config: RpcSendTransactionConfig,
|
2020-03-27 15:46:00 -07:00
|
|
|
) -> ClientResult<Signature> {
|
2020-06-17 11:18:48 -07:00
|
|
|
let desired_confirmations = match commitment.commitment {
|
|
|
|
CommitmentLevel::Max | CommitmentLevel::Root => MAX_LOCKOUT_HISTORY + 1,
|
|
|
|
_ => 1,
|
|
|
|
};
|
2020-03-19 11:10:35 -07:00
|
|
|
let mut confirmations = 0;
|
|
|
|
|
|
|
|
let progress_bar = new_spinner_progress_bar();
|
|
|
|
|
2020-09-16 16:47:55 -07:00
|
|
|
progress_bar.set_message(&format!(
|
|
|
|
"[{}/{}] Finalizing transaction {}",
|
|
|
|
confirmations, desired_confirmations, transaction.signatures[0],
|
|
|
|
));
|
2020-09-30 17:44:17 -07:00
|
|
|
let recent_blockhash = if uses_durable_nonce(transaction).is_some() {
|
|
|
|
self.get_recent_blockhash_with_commitment(CommitmentConfig::recent())?
|
|
|
|
.value
|
|
|
|
.0
|
|
|
|
} else {
|
|
|
|
transaction.message.recent_blockhash
|
|
|
|
};
|
2020-09-16 16:47:55 -07:00
|
|
|
let signature = self.send_transaction_with_config(transaction, config)?;
|
|
|
|
let (signature, status) = loop {
|
|
|
|
// Get recent commitment in order to count confirmations for successful transactions
|
|
|
|
let status =
|
|
|
|
self.get_signature_status_with_commitment(&signature, CommitmentConfig::recent())?;
|
|
|
|
if status.is_none() {
|
|
|
|
if self
|
|
|
|
.get_fee_calculator_for_blockhash_with_commitment(
|
|
|
|
&recent_blockhash,
|
|
|
|
CommitmentConfig::recent(),
|
|
|
|
)?
|
|
|
|
.value
|
|
|
|
.is_none()
|
|
|
|
{
|
2020-03-27 15:46:00 -07:00
|
|
|
break (signature, status);
|
2020-03-17 16:58:02 -07:00
|
|
|
}
|
|
|
|
} else {
|
2020-09-16 16:47:55 -07:00
|
|
|
break (signature, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg!(not(test)) {
|
|
|
|
sleep(Duration::from_millis(500));
|
2020-03-17 16:58:02 -07:00
|
|
|
}
|
|
|
|
};
|
2020-09-16 16:47:55 -07:00
|
|
|
if let Some(result) = status {
|
|
|
|
if let Err(err) = result {
|
|
|
|
return Err(err.into());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(RpcError::ForUser(
|
|
|
|
"unable to confirm transaction. \
|
|
|
|
This can happen in situations such as transaction expiration \
|
|
|
|
and insufficient fee-payer funds"
|
|
|
|
.to_string(),
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
2020-04-29 13:12:38 -07:00
|
|
|
let now = Instant::now();
|
2020-03-17 16:58:02 -07:00
|
|
|
loop {
|
2020-06-17 11:18:48 -07:00
|
|
|
match commitment.commitment {
|
|
|
|
CommitmentLevel::Max | CommitmentLevel::Root =>
|
|
|
|
// Return when default (max) commitment is reached
|
|
|
|
// Failed transactions have already been eliminated, `is_some` check is sufficient
|
|
|
|
{
|
|
|
|
if self.get_signature_status(&signature)?.is_some() {
|
|
|
|
progress_bar.set_message("Transaction confirmed");
|
|
|
|
progress_bar.finish_and_clear();
|
|
|
|
return Ok(signature);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Return when one confirmation has been reached
|
|
|
|
if confirmations >= desired_confirmations {
|
|
|
|
progress_bar.set_message("Transaction reached commitment");
|
|
|
|
progress_bar.finish_and_clear();
|
|
|
|
return Ok(signature);
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 16:58:02 -07:00
|
|
|
}
|
|
|
|
progress_bar.set_message(&format!(
|
2020-04-13 22:04:20 -07:00
|
|
|
"[{}/{}] Finalizing transaction {}",
|
2020-03-19 11:10:35 -07:00
|
|
|
confirmations + 1,
|
2020-06-17 11:18:48 -07:00
|
|
|
desired_confirmations,
|
2020-04-13 22:04:20 -07:00
|
|
|
signature,
|
2020-03-17 16:58:02 -07:00
|
|
|
));
|
|
|
|
sleep(Duration::from_millis(500));
|
2020-04-29 13:12:38 -07:00
|
|
|
confirmations = self
|
|
|
|
.get_num_blocks_since_signature_confirmation(&signature)
|
|
|
|
.unwrap_or(confirmations);
|
|
|
|
if now.elapsed().as_secs() >= MAX_HASH_AGE_IN_SECONDS as u64 {
|
|
|
|
return Err(
|
2020-05-09 09:06:32 -07:00
|
|
|
RpcError::ForUser("transaction not finalized. \
|
|
|
|
This can happen when a transaction lands in an abandoned fork. \
|
|
|
|
Please retry.".to_string()).into(),
|
2020-04-29 13:12:38 -07:00
|
|
|
);
|
|
|
|
}
|
2020-03-17 16:58:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:20:49 -07:00
|
|
|
pub fn validator_exit(&self) -> ClientResult<bool> {
|
2020-05-22 08:53:53 -07:00
|
|
|
self.send(RpcRequest::ValidatorExit, Value::Null)
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
|
|
|
|
2020-05-22 08:53:53 -07:00
|
|
|
pub fn send<T>(&self, request: RpcRequest, params: Value) -> ClientResult<T>
|
2020-05-10 08:51:53 -07:00
|
|
|
where
|
|
|
|
T: serde::de::DeserializeOwned,
|
|
|
|
{
|
2019-12-18 21:26:11 -08:00
|
|
|
assert!(params.is_array() || params.is_null());
|
2020-05-10 08:51:53 -07:00
|
|
|
let response = self
|
2020-05-20 18:40:45 -07:00
|
|
|
.sender
|
2020-05-22 08:53:53 -07:00
|
|
|
.send(request, params)
|
2020-05-10 08:51:53 -07:00
|
|
|
.map_err(|err| err.into_with_request(request))?;
|
|
|
|
serde_json::from_value(response)
|
|
|
|
.map_err(|err| ClientError::new_with_request(err.into(), request))
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 09:42:17 -07:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct GetConfirmedSignaturesForAddress2Config {
|
|
|
|
pub before: Option<Signature>,
|
|
|
|
pub until: Option<Signature>,
|
|
|
|
pub limit: Option<usize>,
|
|
|
|
}
|
|
|
|
|
2020-03-17 16:58:02 -07:00
|
|
|
fn new_spinner_progress_bar() -> ProgressBar {
|
|
|
|
let progress_bar = ProgressBar::new(42);
|
|
|
|
progress_bar
|
|
|
|
.set_style(ProgressStyle::default_spinner().template("{spinner:.green} {wide_msg}"));
|
|
|
|
progress_bar.enable_steady_tick(100);
|
|
|
|
progress_bar
|
|
|
|
}
|
|
|
|
|
2019-03-16 22:37:20 -07:00
|
|
|
pub fn get_rpc_request_str(rpc_addr: SocketAddr, tls: bool) -> String {
|
|
|
|
if tls {
|
|
|
|
format!("https://{}", rpc_addr)
|
|
|
|
} else {
|
|
|
|
format!("http://{}", rpc_addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 13:53:49 -07:00
|
|
|
fn parse_keyed_accounts(
|
|
|
|
accounts: Vec<RpcKeyedAccount>,
|
|
|
|
request: RpcRequest,
|
|
|
|
) -> ClientResult<Vec<(Pubkey, Account)>> {
|
|
|
|
let mut pubkey_accounts: Vec<(Pubkey, Account)> = Vec::new();
|
|
|
|
for RpcKeyedAccount { pubkey, account } in accounts.into_iter() {
|
|
|
|
let pubkey = pubkey.parse().map_err(|_| {
|
|
|
|
ClientError::new_with_request(
|
|
|
|
RpcError::ParseError("Pubkey".to_string()).into(),
|
|
|
|
request,
|
|
|
|
)
|
|
|
|
})?;
|
2020-08-04 10:11:30 -07:00
|
|
|
pubkey_accounts.push((
|
|
|
|
pubkey,
|
|
|
|
account.decode().ok_or_else(|| {
|
|
|
|
ClientError::new_with_request(
|
|
|
|
RpcError::ParseError("Account from rpc".to_string()).into(),
|
|
|
|
request,
|
|
|
|
)
|
|
|
|
})?,
|
|
|
|
));
|
2020-07-31 13:53:49 -07:00
|
|
|
}
|
|
|
|
Ok(pubkey_accounts)
|
|
|
|
}
|
|
|
|
|
2019-03-16 22:37:20 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-05-20 18:40:45 -07:00
|
|
|
use crate::{client_error::ClientErrorKind, mock_sender::PUBKEY};
|
2019-12-09 19:35:34 -08:00
|
|
|
use assert_matches::assert_matches;
|
2019-03-16 22:37:20 -07:00
|
|
|
use jsonrpc_core::{Error, IoHandler, Params};
|
|
|
|
use jsonrpc_http_server::{AccessControlAllowOrigin, DomainsValidation, ServerBuilder};
|
|
|
|
use serde_json::Number;
|
2019-11-06 13:15:00 -08:00
|
|
|
use solana_sdk::{
|
2020-02-12 13:15:12 -08:00
|
|
|
instruction::InstructionError, signature::Keypair, system_transaction,
|
2019-11-06 13:15:00 -08:00
|
|
|
transaction::TransactionError,
|
|
|
|
};
|
2020-03-12 23:20:49 -07:00
|
|
|
use std::{io, sync::mpsc::channel, thread};
|
2019-03-16 22:37:20 -07:00
|
|
|
|
|
|
|
#[test]
|
2019-11-13 08:43:15 -08:00
|
|
|
fn test_send() {
|
2019-03-16 22:37:20 -07:00
|
|
|
let (sender, receiver) = channel();
|
|
|
|
thread::spawn(move || {
|
|
|
|
let rpc_addr = "0.0.0.0:0".parse().unwrap();
|
|
|
|
let mut io = IoHandler::default();
|
|
|
|
// Successful request
|
|
|
|
io.add_method("getBalance", |_params: Params| {
|
|
|
|
Ok(Value::Number(Number::from(50)))
|
|
|
|
});
|
|
|
|
// Failed request
|
|
|
|
io.add_method("getRecentBlockhash", |params: Params| {
|
|
|
|
if params != Params::None {
|
|
|
|
Err(Error::invalid_request())
|
|
|
|
} else {
|
|
|
|
Ok(Value::String(
|
|
|
|
"deadbeefXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNHhx".to_string(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let server = ServerBuilder::new(io)
|
|
|
|
.threads(1)
|
|
|
|
.cors(DomainsValidation::AllowOnly(vec![
|
|
|
|
AccessControlAllowOrigin::Any,
|
|
|
|
]))
|
|
|
|
.start_http(&rpc_addr)
|
|
|
|
.expect("Unable to start RPC server");
|
|
|
|
sender.send(*server.address()).unwrap();
|
|
|
|
server.wait();
|
|
|
|
});
|
|
|
|
|
|
|
|
let rpc_addr = receiver.recv().unwrap();
|
|
|
|
let rpc_client = RpcClient::new_socket(rpc_addr);
|
|
|
|
|
2020-05-10 08:51:53 -07:00
|
|
|
let balance: u64 = rpc_client
|
|
|
|
.send(
|
|
|
|
RpcRequest::GetBalance,
|
|
|
|
json!(["deadbeefXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNHhx"]),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(balance, 50);
|
2019-03-16 22:37:20 -07:00
|
|
|
|
2020-05-10 08:51:53 -07:00
|
|
|
let blockhash: String = rpc_client
|
2020-05-22 08:53:53 -07:00
|
|
|
.send(RpcRequest::GetRecentBlockhash, Value::Null)
|
2020-05-10 08:51:53 -07:00
|
|
|
.unwrap();
|
|
|
|
assert_eq!(blockhash, "deadbeefXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNHhx");
|
2019-03-16 22:37:20 -07:00
|
|
|
|
|
|
|
// Send erroneous parameter
|
2020-05-10 08:51:53 -07:00
|
|
|
let blockhash: ClientResult<String> =
|
2020-05-22 08:53:53 -07:00
|
|
|
rpc_client.send(RpcRequest::GetRecentBlockhash, json!(["parameter"]));
|
2019-03-16 22:37:20 -07:00
|
|
|
assert_eq!(blockhash.is_err(), true);
|
|
|
|
}
|
|
|
|
|
2019-03-16 17:17:44 -07:00
|
|
|
#[test]
|
|
|
|
fn test_send_transaction() {
|
|
|
|
let rpc_client = RpcClient::new_mock("succeeds".to_string());
|
|
|
|
|
|
|
|
let key = Keypair::new();
|
2020-10-19 12:12:08 -07:00
|
|
|
let to = solana_sdk::pubkey::new_rand();
|
2019-03-16 17:17:44 -07:00
|
|
|
let blockhash = Hash::default();
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx = system_transaction::transfer(&key, &to, 50, blockhash);
|
2019-03-16 17:17:44 -07:00
|
|
|
|
|
|
|
let signature = rpc_client.send_transaction(&tx);
|
2020-04-06 18:27:37 -07:00
|
|
|
assert_eq!(signature.unwrap(), tx.signatures[0]);
|
2019-03-16 17:17:44 -07:00
|
|
|
|
|
|
|
let rpc_client = RpcClient::new_mock("fails".to_string());
|
|
|
|
|
|
|
|
let signature = rpc_client.send_transaction(&tx);
|
|
|
|
assert!(signature.is_err());
|
2020-04-06 18:27:37 -07:00
|
|
|
|
|
|
|
// Test bad signature returned from rpc node
|
|
|
|
let rpc_client = RpcClient::new_mock("malicious".to_string());
|
|
|
|
let signature = rpc_client.send_transaction(&tx);
|
|
|
|
assert!(signature.is_err());
|
2019-03-16 17:17:44 -07:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_get_recent_blockhash() {
|
|
|
|
let rpc_client = RpcClient::new_mock("succeeds".to_string());
|
|
|
|
|
2019-05-16 21:43:18 -07:00
|
|
|
let expected_blockhash: Hash = PUBKEY.parse().unwrap();
|
2019-03-16 17:17:44 -07:00
|
|
|
|
2019-05-13 12:49:37 -07:00
|
|
|
let (blockhash, _fee_calculator) = rpc_client.get_recent_blockhash().expect("blockhash ok");
|
2019-03-16 17:17:44 -07:00
|
|
|
assert_eq!(blockhash, expected_blockhash);
|
|
|
|
|
|
|
|
let rpc_client = RpcClient::new_mock("fails".to_string());
|
|
|
|
|
2019-05-13 12:49:37 -07:00
|
|
|
assert!(rpc_client.get_recent_blockhash().is_err());
|
2019-03-16 17:17:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_signature_status() {
|
2020-03-27 15:46:00 -07:00
|
|
|
let signature = Signature::default();
|
|
|
|
|
2019-03-16 17:17:44 -07:00
|
|
|
let rpc_client = RpcClient::new_mock("succeeds".to_string());
|
2019-04-05 19:56:17 -07:00
|
|
|
let status = rpc_client.get_signature_status(&signature).unwrap();
|
|
|
|
assert_eq!(status, Some(Ok(())));
|
2019-03-16 17:17:44 -07:00
|
|
|
|
2019-04-05 19:56:17 -07:00
|
|
|
let rpc_client = RpcClient::new_mock("sig_not_found".to_string());
|
|
|
|
let status = rpc_client.get_signature_status(&signature).unwrap();
|
|
|
|
assert_eq!(status, None);
|
2019-03-16 17:17:44 -07:00
|
|
|
|
2019-04-05 19:56:17 -07:00
|
|
|
let rpc_client = RpcClient::new_mock("account_in_use".to_string());
|
|
|
|
let status = rpc_client.get_signature_status(&signature).unwrap();
|
|
|
|
assert_eq!(status, Some(Err(TransactionError::AccountInUse)));
|
2019-03-16 17:17:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_send_and_confirm_transaction() {
|
|
|
|
let rpc_client = RpcClient::new_mock("succeeds".to_string());
|
|
|
|
|
|
|
|
let key = Keypair::new();
|
2020-10-19 12:12:08 -07:00
|
|
|
let to = solana_sdk::pubkey::new_rand();
|
2019-03-16 17:17:44 -07:00
|
|
|
let blockhash = Hash::default();
|
2020-05-09 09:06:32 -07:00
|
|
|
let tx = system_transaction::transfer(&key, &to, 50, blockhash);
|
|
|
|
let result = rpc_client.send_and_confirm_transaction(&tx);
|
2019-03-16 17:17:44 -07:00
|
|
|
result.unwrap();
|
|
|
|
|
|
|
|
let rpc_client = RpcClient::new_mock("account_in_use".to_string());
|
2020-05-09 09:06:32 -07:00
|
|
|
let result = rpc_client.send_and_confirm_transaction(&tx);
|
2019-03-16 17:17:44 -07:00
|
|
|
assert!(result.is_err());
|
|
|
|
|
2019-12-09 19:35:34 -08:00
|
|
|
let rpc_client = RpcClient::new_mock("instruction_error".to_string());
|
2020-05-09 09:06:32 -07:00
|
|
|
let result = rpc_client.send_and_confirm_transaction(&tx);
|
2019-12-09 19:35:34 -08:00
|
|
|
assert_matches!(
|
2020-03-12 23:20:49 -07:00
|
|
|
result.unwrap_err().kind(),
|
|
|
|
ClientErrorKind::TransactionError(TransactionError::InstructionError(
|
2019-12-09 19:35:34 -08:00
|
|
|
0,
|
|
|
|
InstructionError::UninitializedAccount
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
|
|
|
let rpc_client = RpcClient::new_mock("sig_not_found".to_string());
|
2020-05-09 09:06:32 -07:00
|
|
|
let result = rpc_client.send_and_confirm_transaction(&tx);
|
2020-03-12 23:20:49 -07:00
|
|
|
if let ClientErrorKind::Io(err) = result.unwrap_err().kind() {
|
2019-12-09 19:35:34 -08:00
|
|
|
assert_eq!(err.kind(), io::ErrorKind::Other);
|
|
|
|
}
|
2019-03-16 17:17:44 -07:00
|
|
|
}
|
|
|
|
|
2019-04-19 07:54:21 -07:00
|
|
|
#[test]
|
|
|
|
fn test_rpc_client_thread() {
|
|
|
|
let rpc_client = RpcClient::new_mock("succeeds".to_string());
|
|
|
|
thread::spawn(move || rpc_client);
|
|
|
|
}
|
2019-03-16 22:37:20 -07:00
|
|
|
}
|