From 7b74871c47fd87cb302ea27c67ad668eed5dc8cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 7 Jun 2023 10:03:40 +0200 Subject: [PATCH] app/client/rpc: use enriched bonds and unbonds query --- apps/src/lib/client/rpc.rs | 75 +++++++++++++++----------------------- 1 file changed, 29 insertions(+), 46 deletions(-) diff --git a/apps/src/lib/client/rpc.rs b/apps/src/lib/client/rpc.rs index 70167f59e2..6b8967f136 100644 --- a/apps/src/lib/client/rpc.rs +++ b/apps/src/lib/client/rpc.rs @@ -32,7 +32,9 @@ use namada::ledger::pos::{ self, BondId, BondsAndUnbondsDetail, CommissionPair, PosParams, Slash, }; use namada::ledger::queries::RPC; -use namada::ledger::rpc::{query_epoch, TxResponse}; +use namada::ledger::rpc::{ + enriched_bonds_and_unbonds, query_epoch, TxResponse, +}; use namada::ledger::storage::ConversionState; use namada::ledger::wallet::{AddressVpType, Wallet}; use namada::proof_of_stake::types::WeightedValidator; @@ -1252,7 +1254,7 @@ pub async fn query_bonds( _wallet: &mut Wallet, args: args::QueryBonds, ) -> std::io::Result<()> { - let _epoch = query_and_print_epoch(client).await; + let epoch = query_and_print_epoch(client).await; let source = args.owner; let validator = args.validator; @@ -1260,21 +1262,10 @@ pub async fn query_bonds( let stdout = io::stdout(); let mut w = stdout.lock(); - let bonds_and_unbonds: pos::types::BondsAndUnbondsDetails = - unwrap_client_response::( - RPC.vp() - .pos() - .bonds_and_unbonds(client, &source, &validator) - .await, - ); - let mut bonds_total: token::Amount = 0.into(); - let mut bonds_total_slashed: token::Amount = 0.into(); - let mut unbonds_total: token::Amount = 0.into(); - let mut unbonds_total_slashed: token::Amount = 0.into(); - let mut total_withdrawable: token::Amount = 0.into(); - for (bond_id, details) in bonds_and_unbonds { - let mut total: token::Amount = 0.into(); - let mut total_slashed: token::Amount = 0.into(); + let bonds_and_unbonds = + enriched_bonds_and_unbonds(client, epoch, &source, &validator).await; + + for (bond_id, details) in &bonds_and_unbonds.data { let bond_type = if bond_id.source == bond_id.validator { format!("Self-bonds from {}", bond_id.validator) } else { @@ -1284,74 +1275,66 @@ pub async fn query_bonds( ) }; writeln!(w, "{}:", bond_type)?; - for bond in details.bonds { + for bond in &details.data.bonds { writeln!( w, " Remaining active bond from epoch {}: Δ {}", bond.start, bond.amount )?; - total += bond.amount; - total_slashed += bond.slashed_amount.unwrap_or_default(); } - if total_slashed != token::Amount::default() { + if details.bonds_total_slashed != token::Amount::default() { writeln!( w, "Active (slashed) bonds total: {}", - total - total_slashed + details.bonds_total_active() )?; } - writeln!(w, "Bonds total: {}", total)?; + writeln!(w, "Bonds total: {}", details.bonds_total)?; writeln!(w)?; - bonds_total += total; - bonds_total_slashed += total_slashed; - let mut withdrawable = token::Amount::default(); - if !details.unbonds.is_empty() { - let mut total: token::Amount = 0.into(); - let mut total_slashed: token::Amount = 0.into(); + if !details.data.unbonds.is_empty() { let bond_type = if bond_id.source == bond_id.validator { format!("Unbonded self-bonds from {}", bond_id.validator) } else { format!("Unbonded delegations from {}", bond_id.source) }; writeln!(w, "{}:", bond_type)?; - for unbond in details.unbonds { - total += unbond.amount; - total_slashed += unbond.slashed_amount.unwrap_or_default(); + for unbond in &details.data.unbonds { writeln!( w, " Withdrawable from epoch {} (active from {}): Δ {}", unbond.withdraw, unbond.start, unbond.amount )?; } - withdrawable = total - total_slashed; - writeln!(w, "Unbonded total: {}", total)?; - - unbonds_total += total; - unbonds_total_slashed += total_slashed; - total_withdrawable += withdrawable; + writeln!(w, "Unbonded total: {}", details.unbonds_total)?; } - writeln!(w, "Withdrawable total: {}", withdrawable)?; + writeln!(w, "Withdrawable total: {}", details.total_withdrawable)?; writeln!(w)?; } - if bonds_total != bonds_total_slashed { + if bonds_and_unbonds.bonds_total != bonds_and_unbonds.bonds_total_slashed { writeln!( w, "All bonds total active: {}", - bonds_total - bonds_total_slashed + bonds_and_unbonds.bonds_total_active() )?; } - writeln!(w, "All bonds total: {}", bonds_total)?; + writeln!(w, "All bonds total: {}", bonds_and_unbonds.bonds_total)?; - if unbonds_total != unbonds_total_slashed { + if bonds_and_unbonds.unbonds_total + != bonds_and_unbonds.unbonds_total_slashed + { writeln!( w, "All unbonds total active: {}", - unbonds_total - unbonds_total_slashed + bonds_and_unbonds.unbonds_total_active() )?; } - writeln!(w, "All unbonds total: {}", unbonds_total)?; - writeln!(w, "All unbonds total withdrawable: {}", total_withdrawable)?; + writeln!(w, "All unbonds total: {}", bonds_and_unbonds.unbonds_total)?; + writeln!( + w, + "All unbonds total withdrawable: {}", + bonds_and_unbonds.total_withdrawable + )?; Ok(()) }