CLI: Add optional airdrop recipient (#8291) (#8310)

automerge
This commit is contained in:
mergify[bot]
2020-02-16 11:32:06 -08:00
committed by GitHub
parent 8d59bef561
commit e30561f8a0
4 changed files with 27 additions and 6 deletions

View File

@ -320,7 +320,7 @@ solana-airdrop
Request lamports Request lamports
USAGE: USAGE:
solana airdrop [FLAGS] [OPTIONS] <AMOUNT> solana airdrop [FLAGS] [OPTIONS] <AMOUNT> [PUBKEY]
FLAGS: FLAGS:
-h, --help Prints help information -h, --help Prints help information
@ -341,6 +341,7 @@ OPTIONS:
ARGS: ARGS:
<AMOUNT> The airdrop amount to request, in SOL <AMOUNT> The airdrop amount to request, in SOL
<PUBKEY> The pubkey of airdrop recipient
``` ```
#### solana-authorize-nonce-account #### solana-authorize-nonce-account

View File

@ -407,6 +407,7 @@ pub enum CliCommand {
Airdrop { Airdrop {
faucet_host: Option<IpAddr>, faucet_host: Option<IpAddr>,
faucet_port: u16, faucet_port: u16,
pubkey: Option<Pubkey>,
lamports: u64, lamports: u64,
}, },
Balance { Balance {
@ -639,11 +640,13 @@ pub fn parse_command(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, Box<dyn
} else { } else {
None None
}; };
let pubkey = pubkey_of(&matches, "to");
let lamports = lamports_of_sol(matches, "amount").unwrap(); let lamports = lamports_of_sol(matches, "amount").unwrap();
Ok(CliCommandInfo { Ok(CliCommandInfo {
command: CliCommand::Airdrop { command: CliCommand::Airdrop {
faucet_host, faucet_host,
faucet_port, faucet_port,
pubkey,
lamports, lamports,
}, },
require_keypair: true, require_keypair: true,
@ -939,9 +942,10 @@ fn process_airdrop(
rpc_client: &RpcClient, rpc_client: &RpcClient,
config: &CliConfig, config: &CliConfig,
faucet_addr: &SocketAddr, faucet_addr: &SocketAddr,
pubkey: &Option<Pubkey>,
lamports: u64, lamports: u64,
) -> ProcessResult { ) -> ProcessResult {
let pubkey = config.pubkey()?; let pubkey = pubkey.unwrap_or(config.pubkey()?);
println!( println!(
"Requesting airdrop of {} from {}", "Requesting airdrop of {} from {}",
build_balance_message(lamports, false, true), build_balance_message(lamports, false, true),
@ -1844,6 +1848,7 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
CliCommand::Airdrop { CliCommand::Airdrop {
faucet_host, faucet_host,
faucet_port, faucet_port,
pubkey,
lamports, lamports,
} => { } => {
let faucet_addr = SocketAddr::new( let faucet_addr = SocketAddr::new(
@ -1860,7 +1865,7 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
*faucet_port, *faucet_port,
); );
process_airdrop(&rpc_client, config, &faucet_addr, *lamports) process_airdrop(&rpc_client, config, &faucet_addr, pubkey, *lamports)
} }
// Check client balance // Check client balance
CliCommand::Balance { CliCommand::Balance {
@ -2087,6 +2092,14 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
.validator(is_amount) .validator(is_amount)
.required(true) .required(true)
.help("The airdrop amount to request, in SOL"), .help("The airdrop amount to request, in SOL"),
)
.arg(
Arg::with_name("to")
.index(2)
.value_name("PUBKEY")
.takes_value(true)
.validator(is_pubkey_or_keypair)
.help("The pubkey of airdrop recipient"),
), ),
) )
.subcommand( .subcommand(
@ -2400,15 +2413,17 @@ mod tests {
let dt = Utc.ymd(2018, 9, 19).and_hms(17, 30, 59); let dt = Utc.ymd(2018, 9, 19).and_hms(17, 30, 59);
// Test Airdrop Subcommand // Test Airdrop Subcommand
let test_airdrop = test_commands let test_airdrop =
test_commands
.clone() .clone()
.get_matches_from(vec!["test", "airdrop", "50"]); .get_matches_from(vec!["test", "airdrop", "50", &pubkey_string]);
assert_eq!( assert_eq!(
parse_command(&test_airdrop).unwrap(), parse_command(&test_airdrop).unwrap(),
CliCommandInfo { CliCommandInfo {
command: CliCommand::Airdrop { command: CliCommand::Airdrop {
faucet_host: None, faucet_host: None,
faucet_port: solana_faucet::faucet::FAUCET_PORT, faucet_port: solana_faucet::faucet::FAUCET_PORT,
pubkey: Some(pubkey),
lamports: 50_000_000_000, lamports: 50_000_000_000,
}, },
require_keypair: true, require_keypair: true,
@ -3198,9 +3213,11 @@ mod tests {
assert_eq!(signature.unwrap(), SIGNATURE.to_string()); assert_eq!(signature.unwrap(), SIGNATURE.to_string());
// Need airdrop cases // Need airdrop cases
let to = Pubkey::new_rand();
config.command = CliCommand::Airdrop { config.command = CliCommand::Airdrop {
faucet_host: None, faucet_host: None,
faucet_port: 1234, faucet_port: 1234,
pubkey: Some(to),
lamports: 50, lamports: 50,
}; };
assert!(process_command(&config).is_ok()); assert!(process_command(&config).is_ok());
@ -3238,6 +3255,7 @@ mod tests {
config.command = CliCommand::Airdrop { config.command = CliCommand::Airdrop {
faucet_host: None, faucet_host: None,
faucet_port: 1234, faucet_port: 1234,
pubkey: None,
lamports: 50, lamports: 50,
}; };
assert!(process_command(&config).is_err()); assert!(process_command(&config).is_err());

View File

@ -42,6 +42,7 @@ fn test_cli_deploy_program() {
config.command = CliCommand::Airdrop { config.command = CliCommand::Airdrop {
faucet_host: None, faucet_host: None,
faucet_port: faucet_addr.port(), faucet_port: faucet_addr.port(),
pubkey: None,
lamports: minimum_balance_for_rent_exemption + 1, // min balance for rent exemption + leftover for tx processing lamports: minimum_balance_for_rent_exemption + 1, // min balance for rent exemption + leftover for tx processing
}; };
process_command(&config).unwrap(); process_command(&config).unwrap();

View File

@ -18,6 +18,7 @@ fn test_cli_request_airdrop() {
bob_config.command = CliCommand::Airdrop { bob_config.command = CliCommand::Airdrop {
faucet_host: None, faucet_host: None,
faucet_port: faucet_addr.port(), faucet_port: faucet_addr.port(),
pubkey: None,
lamports: 50, lamports: 50,
}; };