Print account balances in SOL by default (#5857)
* Print account balances in SOL by default * Review comments * Fix wallet-sanity
This commit is contained in:
parent
fc4aa71193
commit
e05f8faa74
|
@ -6,3 +6,7 @@ pub mod display;
|
||||||
pub mod input_validators;
|
pub mod input_validators;
|
||||||
pub mod validator_info;
|
pub mod validator_info;
|
||||||
pub mod wallet;
|
pub mod wallet;
|
||||||
|
|
||||||
|
pub(crate) fn lamports_to_sol(lamports: u64) -> f64 {
|
||||||
|
lamports as f64 / 2u64.pow(34) as f64
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::{display::println_name_value, input_validators::*, validator_info::*};
|
use crate::{display::println_name_value, input_validators::*, lamports_to_sol, validator_info::*};
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use clap::{value_t_or_exit, App, AppSettings, Arg, ArgMatches, SubCommand};
|
use clap::{value_t_or_exit, App, AppSettings, Arg, ArgMatches, SubCommand};
|
||||||
use console::{style, Emoji};
|
use console::{style, Emoji};
|
||||||
|
@ -57,12 +57,19 @@ pub enum WalletCommand {
|
||||||
drone_port: u16,
|
drone_port: u16,
|
||||||
lamports: u64,
|
lamports: u64,
|
||||||
},
|
},
|
||||||
Balance(Pubkey),
|
Balance {
|
||||||
|
pubkey: Pubkey,
|
||||||
|
use_lamports_unit: bool,
|
||||||
|
},
|
||||||
Cancel(Pubkey),
|
Cancel(Pubkey),
|
||||||
Confirm(Signature),
|
Confirm(Signature),
|
||||||
AuthorizeVoter(Pubkey, Keypair, Pubkey),
|
AuthorizeVoter(Pubkey, Keypair, Pubkey),
|
||||||
CreateVoteAccount(Pubkey, Pubkey, u8, u64),
|
CreateVoteAccount(Pubkey, Pubkey, u8, u64),
|
||||||
ShowAccount(Pubkey, Option<String>),
|
ShowAccount {
|
||||||
|
pubkey: Pubkey,
|
||||||
|
output_file: Option<String>,
|
||||||
|
use_lamports_unit: bool,
|
||||||
|
},
|
||||||
ShowVoteAccount(Pubkey),
|
ShowVoteAccount(Pubkey),
|
||||||
DelegateStake(Keypair, Pubkey, u64, bool),
|
DelegateStake(Keypair, Pubkey, u64, bool),
|
||||||
WithdrawStake(Keypair, Pubkey, u64),
|
WithdrawStake(Keypair, Pubkey, u64),
|
||||||
|
@ -136,7 +143,10 @@ impl Default for WalletConfig {
|
||||||
keypair_path.extend(&[".config", "solana", "id.json"]);
|
keypair_path.extend(&[".config", "solana", "id.json"]);
|
||||||
|
|
||||||
WalletConfig {
|
WalletConfig {
|
||||||
command: WalletCommand::Balance(Pubkey::default()),
|
command: WalletCommand::Balance {
|
||||||
|
pubkey: Pubkey::default(),
|
||||||
|
use_lamports_unit: false,
|
||||||
|
},
|
||||||
json_rpc_url: "http://127.0.0.1:8899".to_string(),
|
json_rpc_url: "http://127.0.0.1:8899".to_string(),
|
||||||
keypair: Keypair::new(),
|
keypair: Keypair::new(),
|
||||||
keypair_path: keypair_path.to_str().unwrap().to_string(),
|
keypair_path: keypair_path.to_str().unwrap().to_string(),
|
||||||
|
@ -222,7 +232,11 @@ pub fn parse_command(
|
||||||
}
|
}
|
||||||
("balance", Some(balance_matches)) => {
|
("balance", Some(balance_matches)) => {
|
||||||
let pubkey = pubkey_of(&balance_matches, "pubkey").unwrap_or(*pubkey);
|
let pubkey = pubkey_of(&balance_matches, "pubkey").unwrap_or(*pubkey);
|
||||||
Ok(WalletCommand::Balance(pubkey))
|
let use_lamports_unit = balance_matches.is_present("lamports");
|
||||||
|
Ok(WalletCommand::Balance {
|
||||||
|
pubkey,
|
||||||
|
use_lamports_unit,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
("cancel", Some(cancel_matches)) => {
|
("cancel", Some(cancel_matches)) => {
|
||||||
let process_id = value_of(cancel_matches, "process_id").unwrap();
|
let process_id = value_of(cancel_matches, "process_id").unwrap();
|
||||||
|
@ -269,10 +283,12 @@ pub fn parse_command(
|
||||||
("show-account", Some(matches)) => {
|
("show-account", Some(matches)) => {
|
||||||
let account_pubkey = pubkey_of(matches, "account_pubkey").unwrap();
|
let account_pubkey = pubkey_of(matches, "account_pubkey").unwrap();
|
||||||
let output_file = matches.value_of("output_file");
|
let output_file = matches.value_of("output_file");
|
||||||
Ok(WalletCommand::ShowAccount(
|
let use_lamports_unit = matches.is_present("lamports");
|
||||||
account_pubkey,
|
Ok(WalletCommand::ShowAccount {
|
||||||
output_file.map(ToString::to_string),
|
pubkey: account_pubkey,
|
||||||
))
|
output_file: output_file.map(ToString::to_string),
|
||||||
|
use_lamports_unit,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
("show-vote-account", Some(matches)) => {
|
("show-vote-account", Some(matches)) => {
|
||||||
let vote_account_pubkey = pubkey_of(matches, "vote_account_pubkey").unwrap();
|
let vote_account_pubkey = pubkey_of(matches, "vote_account_pubkey").unwrap();
|
||||||
|
@ -526,16 +542,21 @@ fn process_airdrop(
|
||||||
.retry_get_balance(&config.keypair.pubkey(), 5)?
|
.retry_get_balance(&config.keypair.pubkey(), 5)?
|
||||||
.unwrap_or(previous_balance);
|
.unwrap_or(previous_balance);
|
||||||
|
|
||||||
Ok(format!("Your balance is: {:?}", current_balance))
|
let ess = if current_balance == 1 { "" } else { "s" };
|
||||||
|
Ok(format!(
|
||||||
|
"Your balance is: {:?} lamport{}",
|
||||||
|
current_balance, ess
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_balance(pubkey: &Pubkey, rpc_client: &RpcClient) -> ProcessResult {
|
fn process_balance(
|
||||||
|
pubkey: &Pubkey,
|
||||||
|
rpc_client: &RpcClient,
|
||||||
|
use_lamports_unit: bool,
|
||||||
|
) -> ProcessResult {
|
||||||
let balance = rpc_client.retry_get_balance(pubkey, 5)?;
|
let balance = rpc_client.retry_get_balance(pubkey, 5)?;
|
||||||
match balance {
|
match balance {
|
||||||
Some(lamports) => {
|
Some(lamports) => Ok(build_balance_message(lamports, use_lamports_unit)),
|
||||||
let ess = if lamports == 1 { "" } else { "s" };
|
|
||||||
Ok(format!("{:?} lamport{}", lamports, ess))
|
|
||||||
}
|
|
||||||
None => Err(WalletError::RpcRequestError(
|
None => Err(WalletError::RpcRequestError(
|
||||||
"Received result of an unexpected type".to_string(),
|
"Received result of an unexpected type".to_string(),
|
||||||
))?,
|
))?,
|
||||||
|
@ -629,12 +650,16 @@ fn process_show_account(
|
||||||
_config: &WalletConfig,
|
_config: &WalletConfig,
|
||||||
account_pubkey: &Pubkey,
|
account_pubkey: &Pubkey,
|
||||||
output_file: &Option<String>,
|
output_file: &Option<String>,
|
||||||
|
use_lamports_unit: bool,
|
||||||
) -> ProcessResult {
|
) -> ProcessResult {
|
||||||
let account = rpc_client.get_account(account_pubkey)?;
|
let account = rpc_client.get_account(account_pubkey)?;
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
println!("Public Key: {}", account_pubkey);
|
println!("Public Key: {}", account_pubkey);
|
||||||
println!("Lamports: {}", account.lamports);
|
println!(
|
||||||
|
"{:?}",
|
||||||
|
build_balance_message(account.lamports, use_lamports_unit)
|
||||||
|
);
|
||||||
println!("Owner: {}", account.owner);
|
println!("Owner: {}", account.owner);
|
||||||
println!("Executable: {}", account.executable);
|
println!("Executable: {}", account.executable);
|
||||||
|
|
||||||
|
@ -1403,7 +1428,10 @@ pub fn process_command(config: &WalletConfig) -> ProcessResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check client balance
|
// Check client balance
|
||||||
WalletCommand::Balance(pubkey) => process_balance(&pubkey, &rpc_client),
|
WalletCommand::Balance {
|
||||||
|
pubkey,
|
||||||
|
use_lamports_unit,
|
||||||
|
} => process_balance(&pubkey, &rpc_client, *use_lamports_unit),
|
||||||
|
|
||||||
// Cancel a contract by contract Pubkey
|
// Cancel a contract by contract Pubkey
|
||||||
WalletCommand::Cancel(pubkey) => process_cancel(&rpc_client, config, &pubkey),
|
WalletCommand::Cancel(pubkey) => process_cancel(&rpc_client, config, &pubkey),
|
||||||
|
@ -1438,9 +1466,17 @@ pub fn process_command(config: &WalletConfig) -> ProcessResult {
|
||||||
&new_authorized_voter_pubkey,
|
&new_authorized_voter_pubkey,
|
||||||
),
|
),
|
||||||
|
|
||||||
WalletCommand::ShowAccount(account_pubkey, output_file) => {
|
WalletCommand::ShowAccount {
|
||||||
process_show_account(&rpc_client, config, &account_pubkey, &output_file)
|
pubkey,
|
||||||
}
|
output_file,
|
||||||
|
use_lamports_unit,
|
||||||
|
} => process_show_account(
|
||||||
|
&rpc_client,
|
||||||
|
config,
|
||||||
|
&pubkey,
|
||||||
|
&output_file,
|
||||||
|
*use_lamports_unit,
|
||||||
|
),
|
||||||
|
|
||||||
WalletCommand::ShowVoteAccount(vote_account_pubkey) => {
|
WalletCommand::ShowVoteAccount(vote_account_pubkey) => {
|
||||||
process_show_vote_account(&rpc_client, config, &vote_account_pubkey)
|
process_show_vote_account(&rpc_client, config, &vote_account_pubkey)
|
||||||
|
@ -1667,6 +1703,16 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn build_balance_message(lamports: u64, use_lamports_unit: bool) -> String {
|
||||||
|
if use_lamports_unit {
|
||||||
|
let ess = if lamports == 1 { "" } else { "s" };
|
||||||
|
format!("{:?} lamport{}", lamports, ess)
|
||||||
|
} else {
|
||||||
|
let sol = lamports_to_sol(lamports);
|
||||||
|
format!("{:.8} SOL", sol)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, 'v> {
|
pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, 'v> {
|
||||||
App::new(name)
|
App::new(name)
|
||||||
.about(about)
|
.about(about)
|
||||||
|
@ -1711,6 +1757,12 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.validator(is_pubkey_or_keypair)
|
.validator(is_pubkey_or_keypair)
|
||||||
.help("The public key of the balance to check"),
|
.help("The public key of the balance to check"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("lamports")
|
||||||
|
.long("lamports")
|
||||||
|
.takes_value(false)
|
||||||
|
.help("Display balance in lamports instead of SOL"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
|
@ -1825,6 +1877,12 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
|
||||||
.value_name("FILE")
|
.value_name("FILE")
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.help("Write the account data to this file"),
|
.help("Write the account data to this file"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("lamports")
|
||||||
|
.long("lamports")
|
||||||
|
.takes_value(false)
|
||||||
|
.help("Display balance in lamports instead of SOL"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
|
@ -2336,15 +2394,23 @@ mod tests {
|
||||||
]);
|
]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse_command(&pubkey, &test_balance).unwrap(),
|
parse_command(&pubkey, &test_balance).unwrap(),
|
||||||
WalletCommand::Balance(keypair.pubkey())
|
WalletCommand::Balance {
|
||||||
|
pubkey: keypair.pubkey(),
|
||||||
|
use_lamports_unit: false
|
||||||
|
}
|
||||||
);
|
);
|
||||||
let test_balance =
|
let test_balance = test_commands.clone().get_matches_from(vec![
|
||||||
test_commands
|
"test",
|
||||||
.clone()
|
"balance",
|
||||||
.get_matches_from(vec!["test", "balance", &keypair_file]);
|
&keypair_file,
|
||||||
|
"--lamports",
|
||||||
|
]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse_command(&pubkey, &test_balance).unwrap(),
|
parse_command(&pubkey, &test_balance).unwrap(),
|
||||||
WalletCommand::Balance(keypair.pubkey())
|
WalletCommand::Balance {
|
||||||
|
pubkey: keypair.pubkey(),
|
||||||
|
use_lamports_unit: true
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// Test Cancel Subcommand
|
// Test Cancel Subcommand
|
||||||
|
@ -2629,9 +2695,18 @@ mod tests {
|
||||||
config.command = WalletCommand::Address;
|
config.command = WalletCommand::Address;
|
||||||
assert_eq!(process_command(&config).unwrap(), pubkey);
|
assert_eq!(process_command(&config).unwrap(), pubkey);
|
||||||
|
|
||||||
config.command = WalletCommand::Balance(config.keypair.pubkey());
|
config.command = WalletCommand::Balance {
|
||||||
|
pubkey: config.keypair.pubkey(),
|
||||||
|
use_lamports_unit: true,
|
||||||
|
};
|
||||||
assert_eq!(process_command(&config).unwrap(), "50 lamports");
|
assert_eq!(process_command(&config).unwrap(), "50 lamports");
|
||||||
|
|
||||||
|
config.command = WalletCommand::Balance {
|
||||||
|
pubkey: config.keypair.pubkey(),
|
||||||
|
use_lamports_unit: false,
|
||||||
|
};
|
||||||
|
assert_eq!(process_command(&config).unwrap(), "0.00000000 SOL");
|
||||||
|
|
||||||
let process_id = Pubkey::new_rand();
|
let process_id = Pubkey::new_rand();
|
||||||
config.command = WalletCommand::Cancel(process_id);
|
config.command = WalletCommand::Cancel(process_id);
|
||||||
assert_eq!(process_command(&config).unwrap(), SIGNATURE);
|
assert_eq!(process_command(&config).unwrap(), SIGNATURE);
|
||||||
|
@ -2784,7 +2859,10 @@ mod tests {
|
||||||
};
|
};
|
||||||
assert!(process_command(&config).is_err());
|
assert!(process_command(&config).is_err());
|
||||||
|
|
||||||
config.command = WalletCommand::Balance(config.keypair.pubkey());
|
config.command = WalletCommand::Balance {
|
||||||
|
pubkey: config.keypair.pubkey(),
|
||||||
|
use_lamports_unit: false,
|
||||||
|
};
|
||||||
assert!(process_command(&config).is_err());
|
assert!(process_command(&config).is_err());
|
||||||
|
|
||||||
config.command = WalletCommand::CreateVoteAccount(bob_pubkey, node_pubkey, 0, 10);
|
config.command = WalletCommand::CreateVoteAccount(bob_pubkey, node_pubkey, 0, 10);
|
||||||
|
|
|
@ -23,7 +23,7 @@ check_balance_output() {
|
||||||
exec 42>&1
|
exec 42>&1
|
||||||
attempts=3
|
attempts=3
|
||||||
while [[ $attempts -gt 0 ]]; do
|
while [[ $attempts -gt 0 ]]; do
|
||||||
output=$($solana_cli "${entrypoint[@]}" balance | tee >(cat - >&42))
|
output=$($solana_cli "${entrypoint[@]}" balance --lamports | tee >(cat - >&42))
|
||||||
if [[ "$output" =~ $expected_output ]]; then
|
if [[ "$output" =~ $expected_output ]]; then
|
||||||
break
|
break
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in New Issue