Skip to content

Commit

Permalink
Merge branch 'tomas/token-balance-query' (#1173)
Browse files Browse the repository at this point in the history
* origin/tomas/token-balance-query:
  changelog: add #1173
  client/rpc: use the new token balance method
  shared/ledger/queries: add token balance client-only method
  • Loading branch information
Fraccaman committed Jul 21, 2023
2 parents 93a54c2 + d03df95 commit a72ca1c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Added a reusable token balance query method.
([\#1173](https://github.com/anoma/namada/pull/1173))
2 changes: 1 addition & 1 deletion apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ pub async fn get_token_balance<C: namada::ledger::queries::Client + Sync>(
client: &C,
token: &Address,
owner: &Address,
) -> Option<token::Amount> {
) -> token::Amount {
namada::ledger::rpc::get_token_balance(client, token, owner).await
}

Expand Down
3 changes: 1 addition & 2 deletions apps/src/lib/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,7 @@ where

let balance =
rpc::get_token_balance(client, &ctx.native_token, &proposal.author)
.await
.unwrap_or_default();
.await;
if balance
< token::Amount::from_uint(
governance_parameters.min_proposal_fund,
Expand Down
39 changes: 39 additions & 0 deletions shared/src/ledger/queries/vp/token.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Token validity predicate queries

use namada_core::ledger::storage::{DBIter, StorageHasher, DB};
use namada_core::ledger::storage_api;
use namada_core::ledger::storage_api::token::read_denom;
Expand Down Expand Up @@ -41,3 +43,40 @@ where
{
read_denom(ctx.wl_storage, &addr, None)
}

#[cfg(any(test, feature = "async-client"))]
pub mod client_only_methods {
use borsh::BorshDeserialize;

use super::Token;
use crate::ledger::queries::{Client, RPC};
use crate::types::address::Address;
use crate::types::token;

impl Token {
/// Get the balance of the given `token` belonging to the given `owner`.
pub async fn balance<CLIENT>(
&self,
client: &CLIENT,
token: &Address,
owner: &Address,
) -> Result<token::Amount, <CLIENT as Client>::Error>
where
CLIENT: Client + Sync,
{
let balance_key = token::balance_key(token, owner);
let response = RPC
.shell()
.storage_value(client, None, None, false, &balance_key)
.await?;

let balance = if response.data.is_empty() {
token::Amount::default()
} else {
token::Amount::try_from_slice(&response.data)
.unwrap_or_default()
};
Ok(balance)
}
}
}
8 changes: 4 additions & 4 deletions shared/src/ledger/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use crate::types::governance::{ProposalVote, VotePower};
use crate::types::hash::Hash;
use crate::types::key::*;
use crate::types::storage::{BlockHeight, BlockResults, Epoch, PrefixValue};
use crate::types::token::balance_key;
use crate::types::{storage, token};

/// Query the status of a given transaction.
Expand Down Expand Up @@ -140,9 +139,10 @@ pub async fn get_token_balance<C: crate::ledger::queries::Client + Sync>(
client: &C,
token: &Address,
owner: &Address,
) -> Option<token::Amount> {
let balance_key = balance_key(token, owner);
query_storage_value(client, &balance_key).await
) -> token::Amount {
unwrap_client_response::<C, _>(
RPC.vp().token().balance(client, token, owner).await,
)
}

/// Get account's public key stored in its storage sub-space
Expand Down

0 comments on commit a72ca1c

Please sign in to comment.