Skip to content

Commit

Permalink
change the balance format
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 authored and james-chf committed Sep 2, 2022
1 parent 8789392 commit df76c3c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 120 deletions.
168 changes: 61 additions & 107 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,141 +131,95 @@ pub async fn query_balance(ctx: Context, args: args::QueryBalance) {
}
(None, Some(owner)) => {
let owner = ctx.get(&owner);
let mut found_any = false;
for (token, currency_code) in tokens {
for (token, _) in tokens {
let prefix = token.to_db_key().into();
let balances = query_storage_prefix::<token::Amount>(
client.clone(),
prefix,
)
.await;
if let Some(balances) = balances {
let stdout = io::stdout();
let mut w = stdout.lock();
for (key, balance) in balances {
match token::is_any_multitoken_balance_key(&key) {
Some((sub_prefix, o)) if *o == owner => {
writeln!(
w,
"{} with {}: {}",
currency_code, sub_prefix, balance
)
.unwrap();
found_any = true;
}
Some(_) => {}
None => {
if let Some(o) =
token::is_any_token_balance_key(&key)
{
if *o == owner {
writeln!(
w,
"{}: {}",
currency_code, balance
)
.unwrap();
found_any = true;
}
}
}
}
}
print_balances(balances, &token, Some(&owner));
}
}
if !found_any {
println!("No balance found for {}", owner);
}
}
(Some(token), None) => {
let token = ctx.get(&token);
let prefix = token.to_db_key().into();
let balances =
query_storage_prefix::<token::Amount>(client, prefix).await;
match balances {
Some(balances) => {
let currency_code = tokens
.get(&token)
.map(|c| Cow::Borrowed(*c))
.unwrap_or_else(|| Cow::Owned(token.to_string()));
let stdout = io::stdout();
let mut w = stdout.lock();
writeln!(w, "Token {}", currency_code).unwrap();
for (key, balance) in balances {
match token::is_any_multitoken_balance_key(&key) {
Some((sub_prefix, owner)) => {
writeln!(
w,
" with {}: {}, owned by {}",
sub_prefix, balance, owner
)
.unwrap();
}
None => {
if let Some(owner) =
token::is_any_token_balance_key(&key)
{
writeln!(
w,
": {}, owned by {}",
balance, owner
)
.unwrap();
}
}
}
}
}
None => {
println!("No balances for token {}", token.encode())
}
if let Some(balances) = balances {
print_balances(balances, &token, None);
}
}
(None, None) => {
let stdout = io::stdout();
let mut w = stdout.lock();
for (token, currency_code) in tokens {
for (token, _) in tokens {
let key = token::balance_prefix(&token);
let balances =
query_storage_prefix::<token::Amount>(client.clone(), key)
.await;
match balances {
Some(balances) => {
writeln!(w, "Token {}", currency_code).unwrap();
for (key, balance) in balances {
match token::is_any_multitoken_balance_key(&key) {
Some((sub_prefix, owner)) => {
writeln!(
w,
" with {}: {}, owned by {}",
sub_prefix, balance, owner
)
.unwrap();
}
None => {
if let Some(owner) =
token::is_any_token_balance_key(&key)
{
writeln!(
w,
": {}, owned by {}",
balance, owner
)
.unwrap()
}
}
}
}
}
None => {
println!("No balances for token {}", token.encode())
}
if let Some(balances) = balances {
print_balances(balances, &token, None);
}
}
}
}
}

fn print_balances(
balances: impl Iterator<Item = (storage::Key, token::Amount)>,
token: &Address,
target: Option<&Address>,
) {
let stdout = io::stdout();
let mut w = stdout.lock();

// Token
let tokens = address::tokens();
let currency_code = tokens
.get(token)
.map(|c| Cow::Borrowed(*c))
.unwrap_or_else(|| Cow::Owned(token.to_string()));
writeln!(w, "Token {}", currency_code).unwrap();

let print_num = balances
.filter_map(
|(key, balance)| match token::is_any_multitoken_balance_key(&key) {
Some((sub_prefix, owner)) => Some((
owner.clone(),
format!(
"with {}: {}, owned by {}",
sub_prefix, balance, owner
),
)),
None => token::is_any_token_balance_key(&key).map(|owner| {
(
owner.clone(),
format!(": {}, owned by {}", balance, owner),
)
}),
},
)
.filter_map(|(o, s)| match target {
Some(t) if o == *t => Some(s),
Some(_) => None,
None => Some(s),
})
.map(|s| {
writeln!(w, "{}", s).unwrap();
})
.count();

if print_num == 0 {
match target {
Some(t) => writeln!(w, "No balances owned by {}", t).unwrap(),
None => {
writeln!(w, "No balances for token {}", currency_code).unwrap()
}
}
}
}

/// Query Proposals
pub async fn query_proposal(_ctx: Context, args: args::QueryProposal) {
async fn print_proposal(
Expand Down
23 changes: 10 additions & 13 deletions shared/src/types/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,19 +344,16 @@ fn multitoken_balance_owner(key: &Key) -> Option<(Key, &Address)> {
// token, balance, owner
return None;
}
match key.get_at(len - 2) {
Some(DbKeySeg::StringSeg(balance))
if balance == BALANCE_STORAGE_KEY =>
{
match key.segments.last() {
Some(DbKeySeg::AddressSeg(owner)) => {
let sub_prefix = Key {
segments: key.segments[1..(len - 2)].to_vec(),
};
Some((sub_prefix, owner))
}
_ => None,
}
match &key.segments[..] {
[
..,
DbKeySeg::StringSeg(balance),
DbKeySeg::AddressSeg(owner),
] if balance == BALANCE_STORAGE_KEY => {
let sub_prefix = Key {
segments: key.segments[1..(len - 2)].to_vec(),
};
Some((sub_prefix, owner))
}
_ => None,
}
Expand Down

0 comments on commit df76c3c

Please sign in to comment.