Skip to content

Commit

Permalink
[feature] Allow balance querying at specified block height
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-u410 committed Aug 22, 2024
1 parent daf77c6 commit 32a8280
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 7 deletions.
8 changes: 8 additions & 0 deletions crates/apps_lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6084,6 +6084,7 @@ pub mod args {
owner: chain_ctx.get_cached(&self.owner),
token: chain_ctx.get(&self.token),
no_conversions: self.no_conversions,
height: self.height,
})
}
}
Expand All @@ -6094,11 +6095,13 @@ pub mod args {
let owner = BALANCE_OWNER.parse(matches);
let token = TOKEN.parse(matches);
let no_conversions = NO_CONVERSIONS.parse(matches);
let height = BLOCK_HEIGHT_OPT.parse(matches);
Self {
query,
owner,
token,
no_conversions,
height,
}
}

Expand All @@ -6117,6 +6120,10 @@ pub mod args {
.arg(NO_CONVERSIONS.def().help(wrap!(
"Whether not to automatically perform conversions."
)))
.arg(BLOCK_HEIGHT_OPT.def().help(wrap!(
"The block height at which to query the balance. \
(Optional)"
)))
}
}

Expand Down Expand Up @@ -6996,6 +7003,7 @@ pub mod args {
type AddrOrNativeToken = WalletAddrOrNativeToken;
type Address = WalletAddress;
type BalanceOwner = WalletBalanceOwner;
type BlockHeight = BlockHeight;
type BpConversionTable = PathBuf;
type ConfigRpcTendermintAddress = ConfigRpcAddress;
type Data = PathBuf;
Expand Down
12 changes: 9 additions & 3 deletions crates/apps_lib/src/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ async fn query_transparent_balance(
owner,
// The token to query
token,
// Optional block height
height,
..
} = args;

Expand All @@ -213,9 +215,13 @@ async fn query_transparent_balance(
.expect("Balance owner should have been a transparent address");

let token_alias = lookup_token_alias(context, &token, &owner).await;
let token_balance_result =
namada_sdk::rpc::get_token_balance(context.client(), &token, &owner)
.await;
let token_balance_result = namada_sdk::rpc::get_token_balance(
context.client(),
&token,
&owner,
height,
)
.await;

match token_balance_result {
Ok(balance) => {
Expand Down
2 changes: 2 additions & 0 deletions crates/apps_lib/src/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ where
namada.client(),
&namada.native_token(),
&proposal.proposal.author,
None,
)
.await
.unwrap();
Expand Down Expand Up @@ -956,6 +957,7 @@ where
namada.client(),
&namada.native_token(),
&proposal.proposal.author,
None,
)
.await
.unwrap();
Expand Down
4 changes: 3 additions & 1 deletion crates/light_sdk/src/reading/asynchronous/account.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use namada_sdk::account::Account;
use namada_sdk::key::common;
use namada_sdk::storage::BlockHeight;

use super::*;

Expand All @@ -8,13 +9,14 @@ pub async fn get_token_balance(
tendermint_addr: &str,
token: &Address,
owner: &Address,
height: Option<BlockHeight>, // Specify block height or None for latest
) -> Result<token::Amount, Error> {
let client = HttpClient::new(
TendermintAddress::from_str(tendermint_addr)
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
rpc::get_token_balance(&client, token, owner).await
rpc::get_token_balance(&client, token, owner, height).await
}

/// Check if the address exists on chain. Established address exists if it
Expand Down
7 changes: 7 additions & 0 deletions crates/sdk/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ pub trait NamadaTypes: Clone + std::fmt::Debug {
type BpConversionTable: Clone + std::fmt::Debug;
/// Address of a `namada-masp-indexer` live instance
type MaspIndexerAddress: Clone + std::fmt::Debug;
/// Represents a block height
type BlockHeight: Clone + std::fmt::Debug;
}

/// The concrete types being used in Namada SDK
Expand All @@ -109,6 +111,7 @@ impl NamadaTypes for SdkTypes {
type AddrOrNativeToken = Address;
type Address = Address;
type BalanceOwner = namada_core::masp::BalanceOwner;
type BlockHeight = namada_core::storage::BlockHeight;
type BpConversionTable = HashMap<Address, BpConversionTableEntry>;
type ConfigRpcTendermintAddress = tendermint_rpc::Url;
type Data = Vec<u8>;
Expand Down Expand Up @@ -698,6 +701,7 @@ impl InitProposal {
context.client(),
&nam_address,
&proposal.proposal.author,
None,
)
.await?;
let proposal = proposal
Expand Down Expand Up @@ -726,6 +730,7 @@ impl InitProposal {
context.client(),
&nam_address,
&proposal.proposal.author,
None,
)
.await?;
let proposal = proposal
Expand Down Expand Up @@ -1584,6 +1589,8 @@ pub struct QueryBalance<C: NamadaTypes = SdkTypes> {
pub token: C::Address,
/// Whether not to convert balances
pub no_conversions: bool,
/// Optional height to query balances at
pub height: Option<C::BlockHeight>,
}

/// Query historical transfer(s)
Expand Down
7 changes: 5 additions & 2 deletions crates/sdk/src/queries/vp/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,30 @@ where
pub mod client_only_methods {
use borsh::BorshDeserialize;
use namada_core::address::Address;
use namada_core::storage::BlockHeight;
use namada_core::token;
use namada_token::storage_key::{balance_key, masp_total_rewards};

use super::Token;
use crate::queries::{Client, RPC};

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

let balance = if response.data.is_empty() {
Expand Down
3 changes: 2 additions & 1 deletion crates/sdk/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,10 @@ pub async fn get_token_balance<C: crate::queries::Client + Sync>(
client: &C,
token: &Address,
owner: &Address,
height: Option<namada_storage::BlockHeight>,
) -> Result<token::Amount, error::Error> {
convert_response::<C, _>(
RPC.vp().token().balance(client, token, owner).await,
RPC.vp().token().balance(client, token, owner, height).await,
)
}

Expand Down

0 comments on commit 32a8280

Please sign in to comment.