solana/client/src/mock_rpc_client_request.rs

76 lines
2.6 KiB
Rust
Raw Normal View History

use crate::{
client_error::ClientError, generic_rpc_client_request::GenericRpcClientRequest,
rpc_request::RpcRequest,
};
use serde_json::{Number, Value};
use solana_sdk::{
commitment_config::CommitmentConfig,
fee_calculator::FeeCalculator,
transaction::{self, TransactionError},
};
2019-03-12 17:26:07 -07:00
pub const PUBKEY: &str = "7RoSF9fUmdphVCpabEoefH81WwrW7orsWonXWqTXkKV8";
pub const SIGNATURE: &str =
"43yNSFC6fYTuPgTNFFhF4axw7AfWxB2BPdurme8yrsWEYwm8299xh8n6TAHjGymiSub1XtyxTNyd9GBfY2hxoBw8";
pub struct MockRpcClientRequest {
url: String,
2019-03-12 17:26:07 -07:00
}
impl MockRpcClientRequest {
2019-03-15 22:42:36 -07:00
pub fn new(url: String) -> Self {
Self { url }
2019-03-12 17:26:07 -07:00
}
}
2019-03-12 17:26:07 -07:00
impl GenericRpcClientRequest for MockRpcClientRequest {
fn send(
2019-03-12 17:26:07 -07:00
&self,
request: &RpcRequest,
params: Option<serde_json::Value>,
_retries: usize,
_commitment_config: Option<CommitmentConfig>,
2019-04-25 10:29:44 -07:00
) -> Result<serde_json::Value, ClientError> {
2019-03-15 22:42:36 -07:00
if self.url == "fails" {
2019-03-12 17:26:07 -07:00
return Ok(Value::Null);
}
let val = match request {
RpcRequest::ConfirmTransaction => {
if let Some(Value::Array(param_array)) = params {
if let Value::String(param_string) = &param_array[0] {
Value::Bool(param_string == SIGNATURE)
} else {
Value::Null
}
} else {
Value::Null
}
}
RpcRequest::GetBalance => {
2019-03-15 22:42:36 -07:00
let n = if self.url == "airdrop" { 0 } else { 50 };
2019-03-12 17:26:07 -07:00
Value::Number(Number::from(n))
}
RpcRequest::GetRecentBlockhash => Value::Array(vec![
Value::String(PUBKEY.to_string()),
serde_json::to_value(FeeCalculator::default()).unwrap(),
]),
2019-03-12 17:26:07 -07:00
RpcRequest::GetSignatureStatus => {
2019-04-05 19:56:17 -07:00
let response: Option<transaction::Result<()>> = if self.url == "account_in_use" {
Some(Err(TransactionError::AccountInUse))
} else if self.url == "sig_not_found" {
None
2019-03-12 17:26:07 -07:00
} else {
2019-04-05 19:56:17 -07:00
Some(Ok(()))
2019-03-12 17:26:07 -07:00
};
2019-04-05 19:56:17 -07:00
serde_json::to_value(response).unwrap()
2019-03-12 17:26:07 -07:00
}
RpcRequest::GetTransactionCount => Value::Number(Number::from(1234)),
RpcRequest::GetSlot => Value::Number(Number::from(0)),
2019-03-12 17:26:07 -07:00
RpcRequest::SendTransaction => Value::String(SIGNATURE.to_string()),
RpcRequest::GetMinimumBalanceForRentExemption => Value::Number(Number::from(1234)),
2019-03-12 17:26:07 -07:00
_ => Value::Null,
};
Ok(val)
}
}