Skip to content

Commit 1b0420c

Browse files
drcpu-githubaesedepece
authored andcommitted
feat(cli): pretty format stakes CLI function
1 parent 335f66f commit 1b0420c

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/cli/node/json_rpc_client.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
cmp::Reverse,
23
collections::{BTreeSet, HashMap, HashSet},
34
convert::TryFrom,
45
fmt,
@@ -36,13 +37,14 @@ use witnet_data_structures::{
3637
versioning::{ProtocolInfo, ProtocolVersion},
3738
ProtobufConvert,
3839
},
40+
staking::prelude::StakeEntry,
3941
transaction::{
4042
DRTransaction, StakeTransaction, Transaction, UnstakeTransaction, VTTransaction,
4143
},
4244
transaction_factory::NodeBalance,
4345
types::SequentialId,
4446
utxo_pool::{UtxoInfo, UtxoSelectionStrategy},
45-
wit::Wit,
47+
wit::{Wit, WIT_DECIMAL_PLACES},
4648
};
4749
use witnet_node::actors::{
4850
chain_manager::run_dr_locally,
@@ -1949,6 +1951,7 @@ pub fn query_stakes(
19491951
validator: Option<String>,
19501952
withdrawer: Option<String>,
19511953
all: bool,
1954+
long: bool,
19521955
) -> Result<(), failure::Error> {
19531956
let mut stream = start_client(addr)?;
19541957
let params = if all {
@@ -1971,7 +1974,37 @@ pub fn query_stakes(
19711974
serde_json::to_string(&params).unwrap()
19721975
),
19731976
)?;
1974-
log::info!("{}", response);
1977+
1978+
let mut stakes: Vec<StakeEntry<WIT_DECIMAL_PLACES, PublicKeyHash, Wit, Epoch, u64, u64>> =
1979+
parse_response(&response)?;
1980+
stakes.sort_by_key(|stake| Reverse(stake.value.coins));
1981+
1982+
let mut stakes_table = Table::new();
1983+
stakes_table.set_format(*prettytable::format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
1984+
if long {
1985+
stakes_table.set_titles(row![c->"Validator", c->"Withdrawer", c->"Staked", c->"Mining Epoch", c->"Witnessing Epoch", c->"Nonce"]);
1986+
for stake in stakes {
1987+
stakes_table.add_row(row![
1988+
stake.key.validator,
1989+
stake.key.withdrawer,
1990+
r->whole_wits(stake.value.coins.into()).to_formatted_string(&Locale::en),
1991+
r->stake.value.epochs.mining.to_formatted_string(&Locale::en),
1992+
r->stake.value.epochs.witnessing.to_formatted_string(&Locale::en),
1993+
r->stake.value.nonce.to_formatted_string(&Locale::en),
1994+
]);
1995+
}
1996+
} else {
1997+
stakes_table.set_titles(row![c->"Validator", c->"Withdrawer", c->"Staked"]);
1998+
for stake in stakes {
1999+
stakes_table.add_row(row![
2000+
stake.key.validator,
2001+
stake.key.withdrawer,
2002+
r->whole_wits(stake.value.coins.into()).to_formatted_string(&Locale::en),
2003+
]);
2004+
}
2005+
}
2006+
stakes_table.printstd();
2007+
println!();
19752008

19762009
Ok(())
19772010
}

src/cli/node/with_node.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,14 @@ pub fn exec_cmd(
296296
validator,
297297
withdrawer,
298298
all,
299-
} => rpc::query_stakes(node.unwrap_or(default_jsonrpc), validator, withdrawer, all),
299+
long,
300+
} => rpc::query_stakes(
301+
node.unwrap_or(default_jsonrpc),
302+
validator,
303+
withdrawer,
304+
all,
305+
long,
306+
),
300307
Command::Unstake {
301308
node,
302309
operator,
@@ -830,6 +837,8 @@ pub enum Command {
830837
withdrawer: Option<String>,
831838
#[structopt(short = "a", long = "all")]
832839
all: bool,
840+
#[structopt(short = "l", long = "long")]
841+
long: bool,
833842
},
834843
#[structopt(name = "unstake", about = "Create an unstake transaction")]
835844
Unstake {

0 commit comments

Comments
 (0)