Skip to content

Commit

Permalink
app/client/rpc: use enriched bonds and unbonds query
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Jun 12, 2023
1 parent d3b9635 commit 7b74871
Showing 1 changed file with 29 additions and 46 deletions.
75 changes: 29 additions & 46 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1252,29 +1254,18 @@ pub async fn query_bonds<C: namada::ledger::queries::Client + Sync>(
_wallet: &mut Wallet<CliWalletUtils>,
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;

let stdout = io::stdout();
let mut w = stdout.lock();

let bonds_and_unbonds: pos::types::BondsAndUnbondsDetails =
unwrap_client_response::<C, pos::types::BondsAndUnbondsDetails>(
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 {
Expand All @@ -1284,74 +1275,66 @@ pub async fn query_bonds<C: namada::ledger::queries::Client + Sync>(
)
};
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(())
}

Expand Down

0 comments on commit 7b74871

Please sign in to comment.