Enable SOL or lamports for create-vote-account, show-{stake,vote}-account commands (#6114)

automerge
This commit is contained in:
Michael Vines 2019-09-26 10:26:47 -07:00 committed by Grimes
parent 35365974bf
commit 74a648accb
2 changed files with 87 additions and 27 deletions

View File

@ -25,11 +25,13 @@ pub fn parse_vote_create_account(matches: &ArgMatches<'_>) -> Result<WalletComma
let authorized_voter = pubkey_of(matches, "authorized_voter").unwrap_or(vote_account_pubkey); let authorized_voter = pubkey_of(matches, "authorized_voter").unwrap_or(vote_account_pubkey);
let authorized_withdrawer = let authorized_withdrawer =
pubkey_of(matches, "authorized_withdrawer").unwrap_or(vote_account_pubkey); pubkey_of(matches, "authorized_withdrawer").unwrap_or(vote_account_pubkey);
let lamports = matches
.value_of("lamports") let lamports = crate::wallet::parse_amount_lamports(
.unwrap() matches.value_of("amount").unwrap(),
.parse() matches.value_of("unit"),
.map_err(|err| WalletError::BadParameter(format!("Invalid lamports: {:?}", err)))?; )
.map_err(|err| WalletError::BadParameter(format!("Invalid amount: {:?}", err)))?;
Ok(WalletCommand::CreateVoteAccount( Ok(WalletCommand::CreateVoteAccount(
vote_account_pubkey, vote_account_pubkey,
VoteInit { VoteInit {
@ -62,7 +64,11 @@ pub fn parse_vote_get_account_command(
matches: &ArgMatches<'_>, matches: &ArgMatches<'_>,
) -> Result<WalletCommand, WalletError> { ) -> Result<WalletCommand, WalletError> {
let vote_account_pubkey = pubkey_of(matches, "vote_account_pubkey").unwrap(); let vote_account_pubkey = pubkey_of(matches, "vote_account_pubkey").unwrap();
Ok(WalletCommand::ShowVoteAccount(vote_account_pubkey)) let use_lamports_unit = matches.is_present("lamports");
Ok(WalletCommand::ShowVoteAccount {
pubkey: vote_account_pubkey,
use_lamports_unit,
})
} }
pub fn process_create_vote_account( pub fn process_create_vote_account(
@ -144,6 +150,7 @@ pub fn process_show_vote_account(
rpc_client: &RpcClient, rpc_client: &RpcClient,
_config: &WalletConfig, _config: &WalletConfig,
vote_account_pubkey: &Pubkey, vote_account_pubkey: &Pubkey,
use_lamports_unit: bool,
) -> ProcessResult { ) -> ProcessResult {
let vote_account = rpc_client.get_account(vote_account_pubkey)?; let vote_account = rpc_client.get_account(vote_account_pubkey)?;
@ -159,7 +166,10 @@ pub fn process_show_vote_account(
) )
})?; })?;
println!("account lamports: {}", vote_account.lamports); println!(
"account balance: {}",
crate::wallet::build_balance_message(vote_account.lamports, use_lamports_unit)
);
println!("node id: {}", vote_state.node_pubkey); println!("node id: {}", vote_state.node_pubkey);
println!("authorized voter: {}", vote_state.authorized_voter); println!("authorized voter: {}", vote_state.authorized_voter);
println!( println!(
@ -319,6 +329,7 @@ mod tests {
"50", "50",
"--commission", "--commission",
"10", "10",
"lamports",
]); ]);
assert_eq!( assert_eq!(
parse_command(&pubkey, &test_create_vote_account).unwrap(), parse_command(&pubkey, &test_create_vote_account).unwrap(),
@ -350,7 +361,7 @@ mod tests {
authorized_withdrawer: pubkey, authorized_withdrawer: pubkey,
commission: 0 commission: 0
}, },
50 858993459200
) )
); );
// test init with an authed voter // test init with an authed voter
@ -361,6 +372,7 @@ mod tests {
&pubkey_string, &pubkey_string,
&node_pubkey_string, &node_pubkey_string,
"50", "50",
"SOL",
"--authorized-voter", "--authorized-voter",
&authed.to_string(), &authed.to_string(),
]); ]);
@ -374,7 +386,7 @@ mod tests {
authorized_withdrawer: pubkey, authorized_withdrawer: pubkey,
commission: 0 commission: 0
}, },
50 858993459200
) )
); );
// test init with an authed withdrawer // test init with an authed withdrawer
@ -383,7 +395,7 @@ mod tests {
"create-vote-account", "create-vote-account",
&pubkey_string, &pubkey_string,
&node_pubkey_string, &node_pubkey_string,
"50", "0.5",
"--authorized-withdrawer", "--authorized-withdrawer",
&authed.to_string(), &authed.to_string(),
]); ]);
@ -397,7 +409,7 @@ mod tests {
authorized_withdrawer: authed, authorized_withdrawer: authed,
commission: 0 commission: 0
}, },
50 8589934592
) )
); );

View File

@ -71,7 +71,10 @@ pub enum WalletCommand {
output_file: Option<String>, output_file: Option<String>,
use_lamports_unit: bool, use_lamports_unit: bool,
}, },
ShowVoteAccount(Pubkey), ShowVoteAccount {
pubkey: Pubkey,
use_lamports_unit: bool,
},
Uptime { Uptime {
pubkey: Pubkey, pubkey: Pubkey,
aggregate: bool, aggregate: bool,
@ -81,7 +84,10 @@ pub enum WalletCommand {
WithdrawStake(Keypair, Pubkey, u64), WithdrawStake(Keypair, Pubkey, u64),
DeactivateStake(Keypair, Pubkey), DeactivateStake(Keypair, Pubkey),
RedeemVoteCredits(Pubkey, Pubkey), RedeemVoteCredits(Pubkey, Pubkey),
ShowStakeAccount(Pubkey), ShowStakeAccount {
pubkey: Pubkey,
use_lamports_unit: bool,
},
CreateReplicatorStorageAccount(Pubkey, Pubkey), CreateReplicatorStorageAccount(Pubkey, Pubkey),
CreateValidatorStorageAccount(Pubkey, Pubkey), CreateValidatorStorageAccount(Pubkey, Pubkey),
ClaimStorageReward(Pubkey, Pubkey), ClaimStorageReward(Pubkey, Pubkey),
@ -291,7 +297,11 @@ pub fn parse_command(
} }
("show-stake-account", Some(matches)) => { ("show-stake-account", Some(matches)) => {
let stake_account_pubkey = pubkey_of(matches, "stake_account_pubkey").unwrap(); let stake_account_pubkey = pubkey_of(matches, "stake_account_pubkey").unwrap();
Ok(WalletCommand::ShowStakeAccount(stake_account_pubkey)) let use_lamports_unit = matches.is_present("lamports");
Ok(WalletCommand::ShowStakeAccount {
pubkey: stake_account_pubkey,
use_lamports_unit,
})
} }
("create-replicator-storage-account", Some(matches)) => { ("create-replicator-storage-account", Some(matches)) => {
let account_owner = pubkey_of(matches, "storage_account_owner").unwrap(); let account_owner = pubkey_of(matches, "storage_account_owner").unwrap();
@ -721,6 +731,7 @@ fn process_show_stake_account(
rpc_client: &RpcClient, rpc_client: &RpcClient,
_config: &WalletConfig, _config: &WalletConfig,
stake_account_pubkey: &Pubkey, stake_account_pubkey: &Pubkey,
use_lamports_unit: bool,
) -> ProcessResult { ) -> ProcessResult {
use solana_stake_api::stake_state::StakeState; use solana_stake_api::stake_state::StakeState;
let stake_account = rpc_client.get_account(stake_account_pubkey)?; let stake_account = rpc_client.get_account(stake_account_pubkey)?;
@ -731,9 +742,15 @@ fn process_show_stake_account(
} }
match stake_account.state() { match stake_account.state() {
Ok(StakeState::Stake(stake)) => { Ok(StakeState::Stake(stake)) => {
println!("total stake: {}", stake_account.lamports); println!(
"total stake: {}",
build_balance_message(stake_account.lamports, use_lamports_unit)
);
println!("credits observed: {}", stake.credits_observed); println!("credits observed: {}", stake.credits_observed);
println!("delegated stake: {}", stake.stake); println!(
"delegated stake: {}",
build_balance_message(stake.stake, use_lamports_unit)
);
if stake.voter_pubkey != Pubkey::default() { if stake.voter_pubkey != Pubkey::default() {
println!("delegated voter pubkey: {}", stake.voter_pubkey); println!("delegated voter pubkey: {}", stake.voter_pubkey);
} }
@ -1310,9 +1327,15 @@ pub fn process_command(config: &WalletConfig) -> ProcessResult {
*use_lamports_unit, *use_lamports_unit,
), ),
WalletCommand::ShowVoteAccount(vote_account_pubkey) => { WalletCommand::ShowVoteAccount {
process_show_vote_account(&rpc_client, config, &vote_account_pubkey) pubkey: vote_account_pubkey,
} use_lamports_unit,
} => process_show_vote_account(
&rpc_client,
config,
&vote_account_pubkey,
*use_lamports_unit,
),
WalletCommand::Uptime { WalletCommand::Uptime {
pubkey: vote_account_pubkey, pubkey: vote_account_pubkey,
@ -1365,9 +1388,15 @@ pub fn process_command(config: &WalletConfig) -> ProcessResult {
) )
} }
WalletCommand::ShowStakeAccount(stake_account_pubkey) => { WalletCommand::ShowStakeAccount {
process_show_stake_account(&rpc_client, config, &stake_account_pubkey) pubkey: stake_account_pubkey,
} use_lamports_unit,
} => process_show_stake_account(
&rpc_client,
config,
&stake_account_pubkey,
*use_lamports_unit,
),
WalletCommand::CreateReplicatorStorageAccount( WalletCommand::CreateReplicatorStorageAccount(
storage_account_owner, storage_account_owner,
@ -1541,7 +1570,7 @@ where
} }
} }
fn build_balance_message(lamports: u64, use_lamports_unit: bool) -> String { pub(crate) fn build_balance_message(lamports: u64, use_lamports_unit: bool) -> String {
if use_lamports_unit { if use_lamports_unit {
let ess = if lamports == 1 { "" } else { "s" }; let ess = if lamports == 1 { "" } else { "s" };
format!("{:?} lamport{}", lamports, ess) format!("{:?} lamport{}", lamports, ess)
@ -1553,7 +1582,7 @@ fn build_balance_message(lamports: u64, use_lamports_unit: bool) -> String {
} }
} }
fn parse_amount_lamports( pub(crate) fn parse_amount_lamports(
amount: &str, amount: &str,
use_lamports_unit: Option<&str>, use_lamports_unit: Option<&str>,
) -> Result<u64, Box<dyn error::Error>> { ) -> Result<u64, Box<dyn error::Error>> {
@ -1732,12 +1761,19 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
.help("Validator that will vote with this account"), .help("Validator that will vote with this account"),
) )
.arg( .arg(
Arg::with_name("lamports") Arg::with_name("amount")
.index(3) .index(3)
.value_name("LAMPORTS") .value_name("AMOUNT")
.takes_value(true) .takes_value(true)
.required(true) .required(true)
.help("The amount of lamports to send to the vote account"), .help("The amount of send to the vote account (default unit SOL)"),
)
.arg(
Arg::with_name("unit")
.index(4)
.takes_value(true)
.possible_values(&["SOL", "lamports"])
.help("Specify unit to use for request"),
) )
.arg( .arg(
Arg::with_name("commission") Arg::with_name("commission")
@ -1803,6 +1839,12 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
.required(true) .required(true)
.validator(is_pubkey_or_keypair) .validator(is_pubkey_or_keypair)
.help("Vote account pubkey"), .help("Vote account pubkey"),
)
.arg(
Arg::with_name("lamports")
.long("lamports")
.takes_value(false)
.help("Display balance in lamports instead of SOL"),
), ),
) )
.subcommand( .subcommand(
@ -1966,6 +2008,12 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
.validator(is_pubkey_or_keypair) .validator(is_pubkey_or_keypair)
.help("Stake account pubkey"), .help("Stake account pubkey"),
) )
.arg(
Arg::with_name("lamports")
.long("lamports")
.takes_value(false)
.help("Display balance in lamports instead of SOL"),
),
) )
.subcommand( .subcommand(
SubCommand::with_name("create-storage-mining-pool-account") SubCommand::with_name("create-storage-mining-pool-account")