Cli: Remove units from various subcommands (#8301) (#8306)

automerge
This commit is contained in:
mergify[bot]
2020-02-15 12:45:02 -08:00
committed by GitHub
parent 897e1fc5d6
commit 8d59bef561
16 changed files with 491 additions and 401 deletions

View File

@@ -408,7 +408,6 @@ pub enum CliCommand {
faucet_host: Option<IpAddr>,
faucet_port: u16,
lamports: u64,
use_lamports_unit: bool,
},
Balance {
pubkey: Option<Pubkey>,
@@ -640,14 +639,12 @@ pub fn parse_command(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, Box<dyn
} else {
None
};
let lamports = required_lamports_from(matches, "amount", "unit")?;
let use_lamports_unit = matches.value_of("unit") == Some("lamports");
let lamports = lamports_of_sol(matches, "amount").unwrap();
Ok(CliCommandInfo {
command: CliCommand::Airdrop {
faucet_host,
faucet_port,
lamports,
use_lamports_unit,
},
require_keypair: true,
})
@@ -680,7 +677,7 @@ pub fn parse_command(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, Box<dyn
}
},
("pay", Some(matches)) => {
let lamports = required_lamports_from(matches, "amount", "unit")?;
let lamports = lamports_of_sol(matches, "amount").unwrap();
let to = pubkey_of(&matches, "to").unwrap();
let timestamp = if matches.is_present("timestamp") {
// Parse input for serde_json
@@ -764,7 +761,7 @@ pub fn parse_command(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, Box<dyn
})
}
("transfer", Some(matches)) => {
let lamports = required_lamports_from(matches, "amount", "unit")?;
let lamports = lamports_of_sol(matches, "amount").unwrap();
let to = pubkey_of(&matches, "to").unwrap();
let sign_only = matches.is_present(SIGN_ONLY_ARG.name);
let signers = pubkeys_sigs_of(&matches, SIGNER_ARG.name);
@@ -943,12 +940,11 @@ fn process_airdrop(
config: &CliConfig,
faucet_addr: &SocketAddr,
lamports: u64,
use_lamports_unit: bool,
) -> ProcessResult {
let pubkey = config.pubkey()?;
println!(
"Requesting airdrop of {} from {}",
build_balance_message(lamports, use_lamports_unit, true),
build_balance_message(lamports, false, true),
faucet_addr
);
let previous_balance = match rpc_client.retry_get_balance(&pubkey, 5)? {
@@ -967,11 +963,7 @@ fn process_airdrop(
.retry_get_balance(&pubkey, 5)?
.unwrap_or(previous_balance);
Ok(build_balance_message(
current_balance,
use_lamports_unit,
true,
))
Ok(build_balance_message(current_balance, false, true))
}
fn process_balance(
@@ -1853,7 +1845,6 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
faucet_host,
faucet_port,
lamports,
use_lamports_unit,
} => {
let faucet_addr = SocketAddr::new(
faucet_host.unwrap_or_else(|| {
@@ -1869,13 +1860,7 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
*faucet_port,
);
process_airdrop(
&rpc_client,
config,
&faucet_addr,
*lamports,
*use_lamports_unit,
)
process_airdrop(&rpc_client, config, &faucet_addr, *lamports)
}
// Check client balance
CliCommand::Balance {
@@ -2044,22 +2029,6 @@ where
}
}
// If clap arg `name` is_required, and specifies an amount of either lamports or SOL, the only way
// `amount_of()` can return None is if `name` is an f64 and `unit`== "lamports". This method
// catches that case and converts it to an Error.
pub(crate) fn required_lamports_from(
matches: &ArgMatches<'_>,
name: &str,
unit: &str,
) -> Result<u64, CliError> {
amount_of(matches, name, unit).ok_or_else(|| {
CliError::BadParameter(format!(
"Lamports cannot be fractional: {}",
matches.value_of("amount").unwrap()
))
})
}
pub(crate) fn build_balance_message(
lamports: u64,
use_lamports_unit: bool,
@@ -2117,15 +2086,7 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
.takes_value(true)
.validator(is_amount)
.required(true)
.help("The airdrop amount to request (default unit SOL)"),
)
.arg(
Arg::with_name("unit")
.index(2)
.value_name("UNIT")
.takes_value(true)
.possible_values(&["SOL", "lamports"])
.help("Specify unit to use for request and balance display"),
.help("The airdrop amount to request, in SOL"),
),
)
.subcommand(
@@ -2234,15 +2195,7 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
.takes_value(true)
.validator(is_amount)
.required(true)
.help("The amount to send (default unit SOL)"),
)
.arg(
Arg::with_name("unit")
.index(3)
.value_name("UNIT")
.takes_value(true)
.possible_values(&["SOL", "lamports"])
.help("Specify unit to use for request"),
.help("The amount to send, in SOL"),
)
.arg(
Arg::with_name("timestamp")
@@ -2347,15 +2300,7 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
.takes_value(true)
.validator(is_amount)
.required(true)
.help("The amount to send (default unit SOL)"),
)
.arg(
Arg::with_name("unit")
.index(3)
.value_name("UNIT")
.takes_value(true)
.possible_values(&["SOL", "lamports"])
.help("Specify unit to use for request"),
.help("The amount to send, in SOL"),
)
.arg(
Arg::with_name("from")
@@ -2457,15 +2402,14 @@ mod tests {
// Test Airdrop Subcommand
let test_airdrop = test_commands
.clone()
.get_matches_from(vec!["test", "airdrop", "50", "lamports"]);
.get_matches_from(vec!["test", "airdrop", "50"]);
assert_eq!(
parse_command(&test_airdrop).unwrap(),
CliCommandInfo {
command: CliCommand::Airdrop {
faucet_host: None,
faucet_port: solana_faucet::faucet::FAUCET_PORT,
lamports: 50,
use_lamports_unit: true,
lamports: 50_000_000_000,
},
require_keypair: true,
}
@@ -2613,18 +2557,15 @@ mod tests {
);
// Test Simple Pay Subcommand
let test_pay = test_commands.clone().get_matches_from(vec![
"test",
"pay",
&pubkey_string,
"50",
"lamports",
]);
let test_pay =
test_commands
.clone()
.get_matches_from(vec!["test", "pay", &pubkey_string, "50"]);
assert_eq!(
parse_command(&test_pay).unwrap(),
CliCommandInfo {
command: CliCommand::Pay(PayCommand {
lamports: 50,
lamports: 50_000_000_000,
to: pubkey,
..PayCommand::default()
}),
@@ -2638,7 +2579,6 @@ mod tests {
"pay",
&pubkey_string,
"50",
"lamports",
"--require-signature-from",
&witness0_string,
"--require-signature-from",
@@ -2648,7 +2588,7 @@ mod tests {
parse_command(&test_pay_multiple_witnesses).unwrap(),
CliCommandInfo {
command: CliCommand::Pay(PayCommand {
lamports: 50,
lamports: 50_000_000_000,
to: pubkey,
witnesses: Some(vec![witness0, witness1]),
..PayCommand::default()
@@ -2661,7 +2601,6 @@ mod tests {
"pay",
&pubkey_string,
"50",
"lamports",
"--require-signature-from",
&witness0_string,
]);
@@ -2669,7 +2608,7 @@ mod tests {
parse_command(&test_pay_single_witness).unwrap(),
CliCommandInfo {
command: CliCommand::Pay(PayCommand {
lamports: 50,
lamports: 50_000_000_000,
to: pubkey,
witnesses: Some(vec![witness0]),
..PayCommand::default()
@@ -2684,7 +2623,6 @@ mod tests {
"pay",
&pubkey_string,
"50",
"lamports",
"--after",
"2018-09-19T17:30:59",
"--require-timestamp-from",
@@ -2694,7 +2632,7 @@ mod tests {
parse_command(&test_pay_timestamp).unwrap(),
CliCommandInfo {
command: CliCommand::Pay(PayCommand {
lamports: 50,
lamports: 50_000_000_000,
to: pubkey,
timestamp: Some(dt),
timestamp_pubkey: Some(witness0),
@@ -2712,7 +2650,6 @@ mod tests {
"pay",
&pubkey_string,
"50",
"lamports",
"--blockhash",
&blockhash_string,
"--sign-only",
@@ -2721,7 +2658,7 @@ mod tests {
parse_command(&test_pay).unwrap(),
CliCommandInfo {
command: CliCommand::Pay(PayCommand {
lamports: 50,
lamports: 50_000_000_000,
to: pubkey,
blockhash_query: BlockhashQuery::None(blockhash, FeeCalculator::default()),
sign_only: true,
@@ -2740,7 +2677,6 @@ mod tests {
"pay",
&pubkey_string,
"50",
"lamports",
"--blockhash",
&blockhash_string,
"--signer",
@@ -2750,7 +2686,7 @@ mod tests {
parse_command(&test_pay).unwrap(),
CliCommandInfo {
command: CliCommand::Pay(PayCommand {
lamports: 50,
lamports: 50_000_000_000,
to: pubkey,
blockhash_query: BlockhashQuery::FeeCalculator(blockhash),
signers: Some(vec![(key1, sig1)]),
@@ -2769,7 +2705,6 @@ mod tests {
"pay",
&pubkey_string,
"50",
"lamports",
"--blockhash",
&blockhash_string,
"--signer",
@@ -2781,7 +2716,7 @@ mod tests {
parse_command(&test_pay).unwrap(),
CliCommandInfo {
command: CliCommand::Pay(PayCommand {
lamports: 50,
lamports: 50_000_000_000,
to: pubkey,
blockhash_query: BlockhashQuery::FeeCalculator(blockhash),
signers: Some(vec![(key1, sig1), (key2, sig2)]),
@@ -2797,7 +2732,6 @@ mod tests {
"pay",
&pubkey_string,
"50",
"lamports",
"--blockhash",
&blockhash_string,
]);
@@ -2805,7 +2739,7 @@ mod tests {
parse_command(&test_pay).unwrap(),
CliCommandInfo {
command: CliCommand::Pay(PayCommand {
lamports: 50,
lamports: 50_000_000_000,
to: pubkey,
blockhash_query: BlockhashQuery::FeeCalculator(blockhash),
..PayCommand::default()
@@ -2822,7 +2756,6 @@ mod tests {
"pay",
&pubkey_string,
"50",
"lamports",
"--blockhash",
&blockhash_string,
"--nonce",
@@ -2832,7 +2765,7 @@ mod tests {
parse_command(&test_pay).unwrap(),
CliCommandInfo {
command: CliCommand::Pay(PayCommand {
lamports: 50,
lamports: 50_000_000_000,
to: pubkey,
blockhash_query: BlockhashQuery::FeeCalculator(blockhash),
nonce_account: Some(pubkey),
@@ -2851,7 +2784,6 @@ mod tests {
"pay",
&pubkey_string,
"50",
"lamports",
"--blockhash",
&blockhash_string,
"--nonce",
@@ -2863,7 +2795,7 @@ mod tests {
parse_command(&test_pay).unwrap(),
CliCommandInfo {
command: CliCommand::Pay(PayCommand {
lamports: 50,
lamports: 50_000_000_000,
to: pubkey,
blockhash_query: BlockhashQuery::FeeCalculator(blockhash),
nonce_account: Some(pubkey),
@@ -2885,7 +2817,6 @@ mod tests {
"pay",
&pubkey_string,
"50",
"lamports",
"--blockhash",
&blockhash_string,
"--nonce",
@@ -2899,7 +2830,7 @@ mod tests {
parse_command(&test_pay).unwrap(),
CliCommandInfo {
command: CliCommand::Pay(PayCommand {
lamports: 50,
lamports: 50_000_000_000,
to: pubkey,
blockhash_query: BlockhashQuery::FeeCalculator(blockhash),
nonce_account: Some(pubkey),
@@ -2923,7 +2854,6 @@ mod tests {
"pay",
&pubkey_string,
"50",
"lamports",
"--blockhash",
&blockhash_string,
"--nonce",
@@ -2954,7 +2884,6 @@ mod tests {
"pay",
&pubkey_string,
"50",
"lamports",
"--after",
"2018-09-19T17:30:59",
"--require-signature-from",
@@ -2968,7 +2897,7 @@ mod tests {
parse_command(&test_pay_multiple_witnesses).unwrap(),
CliCommandInfo {
command: CliCommand::Pay(PayCommand {
lamports: 50,
lamports: 50_000_000_000,
to: pubkey,
timestamp: Some(dt),
timestamp_pubkey: Some(witness0),
@@ -3273,7 +3202,6 @@ mod tests {
faucet_host: None,
faucet_port: 1234,
lamports: 50,
use_lamports_unit: true,
};
assert!(process_command(&config).is_ok());
@@ -3311,7 +3239,6 @@ mod tests {
faucet_host: None,
faucet_port: 1234,
lamports: 50,
use_lamports_unit: true,
};
assert!(process_command(&config).is_err());
@@ -3420,35 +3347,13 @@ mod tests {
fn test_parse_transfer_subcommand() {
let test_commands = app("test", "desc", "version");
//Test Transfer Subcommand, lamports
//Test Transfer Subcommand, SOL
let from_keypair = keypair_from_seed(&[0u8; 32]).unwrap();
let from_pubkey = from_keypair.pubkey();
let from_string = from_pubkey.to_string();
let to_keypair = keypair_from_seed(&[1u8; 32]).unwrap();
let to_pubkey = to_keypair.pubkey();
let to_string = to_pubkey.to_string();
let test_transfer = test_commands
.clone()
.get_matches_from(vec!["test", "transfer", &to_string, "42", "lamports"]);
assert_eq!(
parse_command(&test_transfer).unwrap(),
CliCommandInfo {
command: CliCommand::Transfer {
lamports: 42,
to: to_pubkey,
from: None,
sign_only: false,
signers: None,
blockhash_query: BlockhashQuery::All,
nonce_account: None,
nonce_authority: None,
fee_payer: None,
},
require_keypair: true,
}
);
//Test Transfer Subcommand, SOL
let test_transfer = test_commands
.clone()
.get_matches_from(vec!["test", "transfer", &to_string, "42"]);
@@ -3478,7 +3383,6 @@ mod tests {
"transfer",
&to_string,
"42",
"lamports",
"--blockhash",
&blockhash_string,
"--sign-only",
@@ -3487,7 +3391,7 @@ mod tests {
parse_command(&test_transfer).unwrap(),
CliCommandInfo {
command: CliCommand::Transfer {
lamports: 42,
lamports: 42_000_000_000,
to: to_pubkey,
from: None,
sign_only: true,
@@ -3509,7 +3413,6 @@ mod tests {
"transfer",
&to_string,
"42",
"lamports",
"--from",
&from_string,
"--fee-payer",
@@ -3523,7 +3426,7 @@ mod tests {
parse_command(&test_transfer).unwrap(),
CliCommandInfo {
command: CliCommand::Transfer {
lamports: 42,
lamports: 42_000_000_000,
to: to_pubkey,
from: Some(from_pubkey.into()),
sign_only: false,
@@ -3548,7 +3451,6 @@ mod tests {
"transfer",
&to_string,
"42",
"lamports",
"--blockhash",
&blockhash_string,
"--nonce",
@@ -3560,7 +3462,7 @@ mod tests {
parse_command(&test_transfer).unwrap(),
CliCommandInfo {
command: CliCommand::Transfer {
lamports: 42,
lamports: 42_000_000_000,
to: to_pubkey,
from: None,
sign_only: false,