Skip to content

Commit

Permalink
Fix --no-conversions flag
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes committed Oct 18, 2022
1 parent 85bb366 commit e24c13e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
60 changes: 42 additions & 18 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use masp_primitives::merkle_tree::MerklePath;
use masp_primitives::primitives::ViewingKey;
use masp_primitives::sapling::Node;
use masp_primitives::zip32::ExtendedFullViewingKey;
use masp_primitives::transaction::components::Amount;
use namada::ledger::governance::parameters::GovParams;
use namada::ledger::governance::storage as gov_storage;
use namada::ledger::governance::utils::Votes;
Expand Down Expand Up @@ -162,7 +163,6 @@ pub async fn query_tx_deltas(
query_token: &Option<Address>,
) -> BTreeMap<(BlockHeight, TxIndex), (Epoch, TransferDelta, TransactionDelta)>
{
use masp_primitives::transaction::components::Amount;
const TXS_PER_PAGE: u8 = 100;
// Connect to the Tendermint server holding the transactions
let client = HttpClient::new(ledger_address.clone()).unwrap();
Expand Down Expand Up @@ -1009,7 +1009,6 @@ pub async fn query_shielded_balance(
let viewing_key =
ExtendedFullViewingKey::from(viewing_keys[0]).fvk.vk;
let balance;
let decoded_balance;
if no_conversions {
balance = ctx
.shielded
Expand All @@ -1018,40 +1017,65 @@ pub async fn query_shielded_balance(
)
.expect("context should contain viewing key");
// Print balances by human-readable token names
decoded_balance = ctx
let decoded_balance = ctx
.shielded
.decode_all_amounts(client.clone(), balance)
.await;
print_decoded_balance_with_epoch(decoded_balance);
} else {
balance = ctx
.shielded
.compute_exchanged_balance(client.clone(), &viewing_key, epoch)
.await
.expect("context should contain viewing key");
// Print balances by human-readable token names
decoded_balance = ctx
let decoded_balance = ctx
.shielded
.decode_amount(client.clone(), balance, epoch)
.await;
}
let mut found_any = false;
for (addr, value) in decoded_balance.components() {
let asset_value = token::Amount::from(*value as u64);
let addr_enc = addr.encode();
println!(
"{}: {}",
tokens.get(addr).cloned().unwrap_or(addr_enc.as_str()),
asset_value
);
found_any = true;
}
if !found_any {
println!("No shielded balance found for given key");
print_decoded_balance(decoded_balance);
}
}
}
}

pub fn print_decoded_balance(decoded_balance: Amount<Address>) {
let tokens = address::tokens();
let mut found_any = false;
for (addr, value) in decoded_balance.components() {
let asset_value = token::Amount::from(*value as u64);
let addr_enc = addr.encode();
println!(
"{} : {}",
tokens.get(addr).cloned().unwrap_or(addr_enc.as_str()),
asset_value
);
found_any = true;
}
if !found_any {
println!("No shielded balance found for given key");
}
}

pub fn print_decoded_balance_with_epoch(decoded_balance: Amount<(Address, Epoch)>) {
let tokens = address::tokens();
let mut found_any = false;
for ((addr, epoch), value) in decoded_balance.components() {
let asset_value = token::Amount::from(*value as u64);
let addr_enc = addr.encode();
println!(
"{} | {} : {}",
tokens.get(addr).cloned().unwrap_or(addr_enc.as_str()),
epoch,
asset_value
);
found_any = true;
}
if !found_any {
println!("No shielded balance found for given key");
}
}

/// Query token amount of owner.
pub async fn get_token_balance(
client: &HttpClient,
Expand Down
6 changes: 3 additions & 3 deletions apps/src/lib/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,16 +1280,16 @@ impl ShieldedContext {
&mut self,
client: HttpClient,
amt: Amount,
) -> Amount<Address> {
) -> Amount<(Address, Epoch)> {
let mut res = Amount::zero();
for (asset_type, val) in amt.components() {
// Decode the asset type
let decoded =
self.decode_asset_type(client.clone(), *asset_type).await;
// Only assets with the target timestamp count
match decoded {
Some((addr, _)) => {
res += &Amount::from_pair(addr, *val).unwrap()
Some((addr, epoch)) => {
res += &Amount::from_pair((addr, epoch), *val).unwrap()
}
_ => {}
}
Expand Down

0 comments on commit e24c13e

Please sign in to comment.