Add retry_get_balance function
clients don't need to know about json
This commit is contained in:
parent
ed4e9febe0
commit
1e58c585d3
|
@ -25,6 +25,19 @@ impl MockRpcClient {
|
||||||
MockRpcClient { addr }
|
MockRpcClient { addr }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn retry_get_balance(
|
||||||
|
&self,
|
||||||
|
id: u64,
|
||||||
|
pubkey: Pubkey,
|
||||||
|
retries: usize,
|
||||||
|
) -> Result<Option<u64>, Box<dyn error::Error>> {
|
||||||
|
let params = json!([format!("{}", pubkey)]);
|
||||||
|
let res = self
|
||||||
|
.retry_make_rpc_request(id, &RpcRequest::GetBalance, Some(params), retries)?
|
||||||
|
.as_u64();
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn retry_make_rpc_request(
|
pub fn retry_make_rpc_request(
|
||||||
&self,
|
&self,
|
||||||
_id: u64,
|
_id: u64,
|
||||||
|
|
|
@ -6,6 +6,8 @@ use std::thread::sleep;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::{error, fmt};
|
use std::{error, fmt};
|
||||||
|
|
||||||
|
use solana_sdk::pubkey::Pubkey;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RpcClient {
|
pub struct RpcClient {
|
||||||
pub client: reqwest::Client,
|
pub client: reqwest::Client,
|
||||||
|
@ -33,6 +35,19 @@ impl RpcClient {
|
||||||
Self::new(get_rpc_request_str(addr, false))
|
Self::new(get_rpc_request_str(addr, false))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn retry_get_balance(
|
||||||
|
&self,
|
||||||
|
id: u64,
|
||||||
|
pubkey: Pubkey,
|
||||||
|
retries: usize,
|
||||||
|
) -> Result<Option<u64>, Box<dyn error::Error>> {
|
||||||
|
let params = json!([format!("{}", pubkey)]);
|
||||||
|
let res = self
|
||||||
|
.retry_make_rpc_request(id, &RpcRequest::GetBalance, Some(params), retries)?
|
||||||
|
.as_u64();
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn retry_make_rpc_request(
|
pub fn retry_make_rpc_request(
|
||||||
&self,
|
&self,
|
||||||
id: u64,
|
id: u64,
|
||||||
|
@ -191,21 +206,13 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_build_request_json() {
|
fn test_build_request_json() {
|
||||||
let test_request = RpcRequest::GetAccountInfo;
|
let test_request = RpcRequest::GetAccountInfo;
|
||||||
let request = test_request.build_request_json(
|
let addr = json!(["deadbeefXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNHhx"]);
|
||||||
1,
|
let request = test_request.build_request_json(1, Some(addr.clone()));
|
||||||
Some(json!(["deadbeefXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNHhx"])),
|
|
||||||
);
|
|
||||||
assert_eq!(request["method"], "getAccountInfo");
|
assert_eq!(request["method"], "getAccountInfo");
|
||||||
assert_eq!(
|
assert_eq!(request["params"], addr,);
|
||||||
request["params"],
|
|
||||||
json!(["deadbeefXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNHhx"])
|
|
||||||
);
|
|
||||||
|
|
||||||
let test_request = RpcRequest::GetBalance;
|
let test_request = RpcRequest::GetBalance;
|
||||||
let request = test_request.build_request_json(
|
let request = test_request.build_request_json(1, Some(addr));
|
||||||
1,
|
|
||||||
Some(json!(["deadbeefXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNHhx"])),
|
|
||||||
);
|
|
||||||
assert_eq!(request["method"], "getBalance");
|
assert_eq!(request["method"], "getBalance");
|
||||||
|
|
||||||
let test_request = RpcRequest::GetConfirmationTime;
|
let test_request = RpcRequest::GetConfirmationTime;
|
||||||
|
|
|
@ -325,11 +325,7 @@ fn process_airdrop(
|
||||||
"Requesting airdrop of {:?} tokens from {}",
|
"Requesting airdrop of {:?} tokens from {}",
|
||||||
tokens, drone_addr
|
tokens, drone_addr
|
||||||
);
|
);
|
||||||
let params = json!([format!("{}", config.id.pubkey())]);
|
let previous_balance = match rpc_client.retry_get_balance(1, config.id.pubkey(), 5)? {
|
||||||
let previous_balance = match rpc_client
|
|
||||||
.retry_make_rpc_request(1, &RpcRequest::GetBalance, Some(params), 5)?
|
|
||||||
.as_u64()
|
|
||||||
{
|
|
||||||
Some(tokens) => tokens,
|
Some(tokens) => tokens,
|
||||||
None => Err(WalletError::RpcRequestError(
|
None => Err(WalletError::RpcRequestError(
|
||||||
"Received result of an unexpected type".to_string(),
|
"Received result of an unexpected type".to_string(),
|
||||||
|
@ -338,10 +334,8 @@ fn process_airdrop(
|
||||||
|
|
||||||
request_and_confirm_airdrop(&rpc_client, &drone_addr, &config.id, tokens)?;
|
request_and_confirm_airdrop(&rpc_client, &drone_addr, &config.id, tokens)?;
|
||||||
|
|
||||||
let params = json!([format!("{}", config.id.pubkey())]);
|
|
||||||
let current_balance = rpc_client
|
let current_balance = rpc_client
|
||||||
.retry_make_rpc_request(1, &RpcRequest::GetBalance, Some(params), 5)?
|
.retry_get_balance(1, config.id.pubkey(), 5)?
|
||||||
.as_u64()
|
|
||||||
.unwrap_or(previous_balance);
|
.unwrap_or(previous_balance);
|
||||||
|
|
||||||
if current_balance < previous_balance {
|
if current_balance < previous_balance {
|
||||||
|
@ -361,10 +355,7 @@ fn process_airdrop(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_balance(config: &WalletConfig, rpc_client: &RpcClient) -> ProcessResult {
|
fn process_balance(config: &WalletConfig, rpc_client: &RpcClient) -> ProcessResult {
|
||||||
let params = json!([format!("{}", config.id.pubkey())]);
|
let balance = rpc_client.retry_get_balance(1, config.id.pubkey(), 5)?;
|
||||||
let balance = rpc_client
|
|
||||||
.retry_make_rpc_request(1, &RpcRequest::GetBalance, Some(params), 5)?
|
|
||||||
.as_u64();
|
|
||||||
match balance {
|
match balance {
|
||||||
Some(0) => Ok("No account found! Request an airdrop to get started.".to_string()),
|
Some(0) => Ok("No account found! Request an airdrop to get started.".to_string()),
|
||||||
Some(tokens) => Ok(format!("Your balance is: {:?}", tokens)),
|
Some(tokens) => Ok(format!("Your balance is: {:?}", tokens)),
|
||||||
|
@ -398,10 +389,7 @@ fn process_deploy(
|
||||||
config: &WalletConfig,
|
config: &WalletConfig,
|
||||||
program_location: &str,
|
program_location: &str,
|
||||||
) -> ProcessResult {
|
) -> ProcessResult {
|
||||||
let params = json!([format!("{}", config.id.pubkey())]);
|
let balance = rpc_client.retry_get_balance(1, config.id.pubkey(), 5)?;
|
||||||
let balance = rpc_client
|
|
||||||
.retry_make_rpc_request(1, &RpcRequest::GetBalance, Some(params), 5)?
|
|
||||||
.as_u64();
|
|
||||||
if let Some(tokens) = balance {
|
if let Some(tokens) = balance {
|
||||||
if tokens < 1 {
|
if tokens < 1 {
|
||||||
Err(WalletError::DynamicProgramError(
|
Err(WalletError::DynamicProgramError(
|
||||||
|
@ -626,10 +614,7 @@ fn process_time_elapsed(
|
||||||
pubkey: Pubkey,
|
pubkey: Pubkey,
|
||||||
dt: DateTime<Utc>,
|
dt: DateTime<Utc>,
|
||||||
) -> ProcessResult {
|
) -> ProcessResult {
|
||||||
let params = json!([format!("{}", config.id.pubkey())]);
|
let balance = rpc_client.retry_get_balance(1, config.id.pubkey(), 5)?;
|
||||||
let balance = rpc_client
|
|
||||||
.retry_make_rpc_request(1, &RpcRequest::GetBalance, Some(params), 5)?
|
|
||||||
.as_u64();
|
|
||||||
|
|
||||||
if let Some(0) = balance {
|
if let Some(0) = balance {
|
||||||
request_and_confirm_airdrop(&rpc_client, &drone_addr, &config.id, 1)?;
|
request_and_confirm_airdrop(&rpc_client, &drone_addr, &config.id, 1)?;
|
||||||
|
@ -650,10 +635,7 @@ fn process_witness(
|
||||||
to: Pubkey,
|
to: Pubkey,
|
||||||
pubkey: Pubkey,
|
pubkey: Pubkey,
|
||||||
) -> ProcessResult {
|
) -> ProcessResult {
|
||||||
let params = json!([format!("{}", config.id.pubkey())]);
|
let balance = rpc_client.retry_get_balance(1, config.id.pubkey(), 5)?;
|
||||||
let balance = rpc_client
|
|
||||||
.retry_make_rpc_request(1, &RpcRequest::GetBalance, Some(params), 5)?
|
|
||||||
.as_u64();
|
|
||||||
|
|
||||||
if let Some(0) = balance {
|
if let Some(0) = balance {
|
||||||
request_and_confirm_airdrop(&rpc_client, &drone_addr, &config.id, 1)?;
|
request_and_confirm_airdrop(&rpc_client, &drone_addr, &config.id, 1)?;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use serde_json::{json, Value};
|
use serde_json::Value;
|
||||||
use solana::rpc_request::{RpcClient, RpcRequest, RpcRequestHandler};
|
use solana::rpc_request::RpcClient;
|
||||||
use solana_drone::drone::run_local_drone;
|
use solana_drone::drone::run_local_drone;
|
||||||
use solana_sdk::pubkey::Pubkey;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||||
|
@ -13,12 +13,8 @@ use std::sync::mpsc::channel;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use solana::thin_client::new_fullnode;
|
use solana::thin_client::new_fullnode;
|
||||||
|
|
||||||
fn check_balance(expected_balance: u64, client: &RpcClient, params: Value) {
|
fn check_balance(expected_balance: u64, client: &RpcClient, pubkey: Pubkey) {
|
||||||
let balance = client
|
let balance = client.retry_get_balance(1, pubkey, 1).unwrap().unwrap();
|
||||||
.make_rpc_request(1, RpcRequest::GetBalance, Some(params))
|
|
||||||
.unwrap()
|
|
||||||
.as_u64()
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(balance, expected_balance);
|
assert_eq!(balance, expected_balance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,8 +41,7 @@ fn test_wallet_timestamp_tx() {
|
||||||
assert_ne!(config_payer.id.pubkey(), config_witness.id.pubkey());
|
assert_ne!(config_payer.id.pubkey(), config_witness.id.pubkey());
|
||||||
|
|
||||||
request_and_confirm_airdrop(&rpc_client, &drone_addr, &config_payer.id, 50).unwrap();
|
request_and_confirm_airdrop(&rpc_client, &drone_addr, &config_payer.id, 50).unwrap();
|
||||||
let params = json!([format!("{}", config_payer.id.pubkey())]);
|
check_balance(50, &rpc_client, config_payer.id.pubkey());
|
||||||
check_balance(50, &rpc_client, params);
|
|
||||||
|
|
||||||
// Make transaction (from config_payer to bob_pubkey) requiring timestamp from config_witness
|
// Make transaction (from config_payer to bob_pubkey) requiring timestamp from config_witness
|
||||||
let date_string = "\"2018-09-19T17:30:59Z\"";
|
let date_string = "\"2018-09-19T17:30:59Z\"";
|
||||||
|
@ -68,23 +63,17 @@ fn test_wallet_timestamp_tx() {
|
||||||
.expect("base58-encoded public key");
|
.expect("base58-encoded public key");
|
||||||
let process_id = Pubkey::new(&process_id_vec);
|
let process_id = Pubkey::new(&process_id_vec);
|
||||||
|
|
||||||
let params = json!([format!("{}", config_payer.id.pubkey())]);
|
check_balance(39, &rpc_client, config_payer.id.pubkey()); // config_payer balance
|
||||||
check_balance(39, &rpc_client, params); // config_payer balance
|
check_balance(11, &rpc_client, process_id); // contract balance
|
||||||
let params = json!([format!("{}", process_id)]);
|
check_balance(0, &rpc_client, bob_pubkey); // recipient balance
|
||||||
check_balance(11, &rpc_client, params); // contract balance
|
|
||||||
let params = json!([format!("{}", bob_pubkey)]);
|
|
||||||
check_balance(0, &rpc_client, params); // recipient balance
|
|
||||||
|
|
||||||
// Sign transaction by config_witness
|
// Sign transaction by config_witness
|
||||||
config_witness.command = WalletCommand::TimeElapsed(bob_pubkey, process_id, dt);
|
config_witness.command = WalletCommand::TimeElapsed(bob_pubkey, process_id, dt);
|
||||||
process_command(&config_witness).unwrap();
|
process_command(&config_witness).unwrap();
|
||||||
|
|
||||||
let params = json!([format!("{}", config_payer.id.pubkey())]);
|
check_balance(39, &rpc_client, config_payer.id.pubkey()); // config_payer balance
|
||||||
check_balance(39, &rpc_client, params); // config_payer balance
|
check_balance(1, &rpc_client, process_id); // contract balance
|
||||||
let params = json!([format!("{}", process_id)]);
|
check_balance(10, &rpc_client, bob_pubkey); // recipient balance
|
||||||
check_balance(1, &rpc_client, params); // contract balance
|
|
||||||
let params = json!([format!("{}", bob_pubkey)]);
|
|
||||||
check_balance(10, &rpc_client, params); // recipient balance
|
|
||||||
|
|
||||||
server_exit();
|
server_exit();
|
||||||
remove_dir_all(ledger_path).unwrap();
|
remove_dir_all(ledger_path).unwrap();
|
||||||
|
@ -132,23 +121,17 @@ fn test_wallet_witness_tx() {
|
||||||
.expect("base58-encoded public key");
|
.expect("base58-encoded public key");
|
||||||
let process_id = Pubkey::new(&process_id_vec);
|
let process_id = Pubkey::new(&process_id_vec);
|
||||||
|
|
||||||
let params = json!([format!("{}", config_payer.id.pubkey())]);
|
check_balance(39, &rpc_client, config_payer.id.pubkey()); // config_payer balance
|
||||||
check_balance(39, &rpc_client, params); // config_payer balance
|
check_balance(11, &rpc_client, process_id); // contract balance
|
||||||
let params = json!([format!("{}", process_id)]);
|
check_balance(0, &rpc_client, bob_pubkey); // recipient balance
|
||||||
check_balance(11, &rpc_client, params); // contract balance
|
|
||||||
let params = json!([format!("{}", bob_pubkey)]);
|
|
||||||
check_balance(0, &rpc_client, params); // recipient balance
|
|
||||||
|
|
||||||
// Sign transaction by config_witness
|
// Sign transaction by config_witness
|
||||||
config_witness.command = WalletCommand::Witness(bob_pubkey, process_id);
|
config_witness.command = WalletCommand::Witness(bob_pubkey, process_id);
|
||||||
process_command(&config_witness).unwrap();
|
process_command(&config_witness).unwrap();
|
||||||
|
|
||||||
let params = json!([format!("{}", config_payer.id.pubkey())]);
|
check_balance(39, &rpc_client, config_payer.id.pubkey()); // config_payer balance
|
||||||
check_balance(39, &rpc_client, params); // config_payer balance
|
check_balance(1, &rpc_client, process_id); // contract balance
|
||||||
let params = json!([format!("{}", process_id)]);
|
check_balance(10, &rpc_client, bob_pubkey); // recipient balance
|
||||||
check_balance(1, &rpc_client, params); // contract balance
|
|
||||||
let params = json!([format!("{}", bob_pubkey)]);
|
|
||||||
check_balance(10, &rpc_client, params); // recipient balance
|
|
||||||
|
|
||||||
server_exit();
|
server_exit();
|
||||||
remove_dir_all(ledger_path).unwrap();
|
remove_dir_all(ledger_path).unwrap();
|
||||||
|
@ -196,23 +179,17 @@ fn test_wallet_cancel_tx() {
|
||||||
.expect("base58-encoded public key");
|
.expect("base58-encoded public key");
|
||||||
let process_id = Pubkey::new(&process_id_vec);
|
let process_id = Pubkey::new(&process_id_vec);
|
||||||
|
|
||||||
let params = json!([format!("{}", config_payer.id.pubkey())]);
|
check_balance(39, &rpc_client, config_payer.id.pubkey()); // config_payer balance
|
||||||
check_balance(39, &rpc_client, params); // config_payer balance
|
check_balance(11, &rpc_client, process_id); // contract balance
|
||||||
let params = json!([format!("{}", process_id)]);
|
check_balance(0, &rpc_client, bob_pubkey); // recipient balance
|
||||||
check_balance(11, &rpc_client, params); // contract balance
|
|
||||||
let params = json!([format!("{}", bob_pubkey)]);
|
|
||||||
check_balance(0, &rpc_client, params); // recipient balance
|
|
||||||
|
|
||||||
// Sign transaction by config_witness
|
// Sign transaction by config_witness
|
||||||
config_payer.command = WalletCommand::Cancel(process_id);
|
config_payer.command = WalletCommand::Cancel(process_id);
|
||||||
process_command(&config_payer).unwrap();
|
process_command(&config_payer).unwrap();
|
||||||
|
|
||||||
let params = json!([format!("{}", config_payer.id.pubkey())]);
|
check_balance(49, &rpc_client, config_payer.id.pubkey()); // config_payer balance
|
||||||
check_balance(49, &rpc_client, params); // config_payer balance
|
check_balance(1, &rpc_client, process_id); // contract balance
|
||||||
let params = json!([format!("{}", process_id)]);
|
check_balance(0, &rpc_client, bob_pubkey); // recipient balance
|
||||||
check_balance(1, &rpc_client, params); // contract balance
|
|
||||||
let params = json!([format!("{}", bob_pubkey)]);
|
|
||||||
check_balance(0, &rpc_client, params); // recipient balance
|
|
||||||
|
|
||||||
server_exit();
|
server_exit();
|
||||||
remove_dir_all(ledger_path).unwrap();
|
remove_dir_all(ledger_path).unwrap();
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use serde_json::json;
|
use solana::rpc_request::RpcClient;
|
||||||
use solana::rpc_request::{RpcClient, RpcRequest, RpcRequestHandler};
|
|
||||||
use solana::thin_client::new_fullnode;
|
use solana::thin_client::new_fullnode;
|
||||||
use solana_drone::drone::run_local_drone;
|
use solana_drone::drone::run_local_drone;
|
||||||
use solana_sdk::signature::KeypairUtil;
|
use solana_sdk::signature::KeypairUtil;
|
||||||
|
@ -25,11 +24,9 @@ fn test_wallet_request_airdrop() {
|
||||||
|
|
||||||
let rpc_client = RpcClient::new_from_socket(leader_data.rpc);
|
let rpc_client = RpcClient::new_from_socket(leader_data.rpc);
|
||||||
|
|
||||||
let params = json!([format!("{}", bob_config.id.pubkey())]);
|
|
||||||
let balance = rpc_client
|
let balance = rpc_client
|
||||||
.make_rpc_request(1, RpcRequest::GetBalance, Some(params))
|
.retry_get_balance(1, bob_config.id.pubkey(), 1)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_u64()
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(balance, 50);
|
assert_eq!(balance, 50);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue