Skip to content

Commit

Permalink
Merge branch 'fraccaman/rework-balance' (#3171)
Browse files Browse the repository at this point in the history
* origin/fraccaman/rework-balance:
  Changelog for #3171
  Fix integration tests
  Standardize no balance response
  Improve error msg when no balance owner is found
  Remove `get_token_balance` from apps
  Remove IBC token query command
  Refactor the CLI's balance command
  • Loading branch information
brentstone committed May 9, 2024
2 parents 888532c + 65ce1d7 commit 51cc9a2
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 745 deletions.
3 changes: 3 additions & 0 deletions .changelog/unreleased/improvements/3171-rework-balance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Remove unbounded `token` and `owner` balance queries from the CLI, in
an attempt to reduce strain on the RPC servers of full/validator nodes.
([\#3171](https://github.com/anoma/namada/pull/3171))
82 changes: 6 additions & 76 deletions crates/apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ pub mod cmds {
.subcommand(QueryMaspRewardTokens::def().display_order(5))
.subcommand(QueryBlock::def().display_order(5))
.subcommand(QueryBalance::def().display_order(5))
.subcommand(QueryIbcToken::def().display_order(5))
.subcommand(QueryBonds::def().display_order(5))
.subcommand(QueryBondedStake::def().display_order(5))
.subcommand(QuerySlashes::def().display_order(5))
Expand Down Expand Up @@ -323,7 +322,6 @@ pub mod cmds {
Self::parse_with_ctx(matches, QueryMaspRewardTokens);
let query_block = Self::parse_with_ctx(matches, QueryBlock);
let query_balance = Self::parse_with_ctx(matches, QueryBalance);
let query_ibc_token = Self::parse_with_ctx(matches, QueryIbcToken);
let query_bonds = Self::parse_with_ctx(matches, QueryBonds);
let query_bonded_stake =
Self::parse_with_ctx(matches, QueryBondedStake);
Expand Down Expand Up @@ -384,7 +382,6 @@ pub mod cmds {
.or(query_masp_reward_tokens)
.or(query_block)
.or(query_balance)
.or(query_ibc_token)
.or(query_bonds)
.or(query_bonded_stake)
.or(query_slashes)
Expand Down Expand Up @@ -474,7 +471,6 @@ pub mod cmds {
QueryMaspRewardTokens(QueryMaspRewardTokens),
QueryBlock(QueryBlock),
QueryBalance(QueryBalance),
QueryIbcToken(QueryIbcToken),
QueryBonds(QueryBonds),
QueryBondedStake(QueryBondedStake),
QueryCommissionRate(QueryCommissionRate),
Expand Down Expand Up @@ -1713,30 +1709,11 @@ pub mod cmds {

fn def() -> App {
App::new(Self::CMD)
.about("Query balance(s) of tokens.")
.about("Query the token balance of some account.")
.add_args::<args::QueryBalance<args::CliTypes>>()
}
}

#[derive(Clone, Debug)]
pub struct QueryIbcToken(pub args::QueryIbcToken<args::CliTypes>);

impl SubCmd for QueryIbcToken {
const CMD: &'static str = "ibc-token";

fn parse(matches: &ArgMatches) -> Option<Self> {
matches.subcommand_matches(Self::CMD).map(|matches| {
QueryIbcToken(args::QueryIbcToken::parse(matches))
})
}

fn def() -> App {
App::new(Self::CMD)
.about("Query IBC token(s).")
.add_args::<args::QueryIbcToken<args::CliTypes>>()
}
}

#[derive(Clone, Debug)]
pub struct QueryBonds(pub args::QueryBonds<args::CliTypes>);

Expand Down Expand Up @@ -3026,7 +3003,7 @@ pub mod args {
pub const AMOUNT: Arg<token::DenominatedAmount> = arg("amount");
pub const ARCHIVE_DIR: ArgOpt<PathBuf> = arg_opt("archive-dir");
pub const AVATAR_OPT: ArgOpt<String> = arg_opt("avatar");
pub const BALANCE_OWNER: ArgOpt<WalletBalanceOwner> = arg_opt("owner");
pub const BALANCE_OWNER: Arg<WalletBalanceOwner> = arg("owner");
pub const BASE_DIR: ArgDefault<PathBuf> = arg_default(
"base-dir",
DefaultFn(|| match env::var("NAMADA_BASE_DIR") {
Expand Down Expand Up @@ -5472,10 +5449,9 @@ pub mod args {

Ok(QueryBalance::<SdkTypes> {
query,
owner: self.owner.map(|x| chain_ctx.get_cached(&x)),
token: self.token.map(|x| chain_ctx.get(&x)),
owner: chain_ctx.get_cached(&self.owner),
token: chain_ctx.get(&self.token),
no_conversions: self.no_conversions,
show_ibc_tokens: self.show_ibc_tokens,
})
}
}
Expand All @@ -5484,15 +5460,13 @@ pub mod args {
fn parse(matches: &ArgMatches) -> Self {
let query = Query::parse(matches);
let owner = BALANCE_OWNER.parse(matches);
let token = TOKEN_OPT.parse(matches);
let token = TOKEN.parse(matches);
let no_conversions = NO_CONVERSIONS.parse(matches);
let show_ibc_tokens = SHOW_IBC_TOKENS.parse(matches);
Self {
query,
owner,
token,
no_conversions,
show_ibc_tokens,
}
}

Expand All @@ -5504,7 +5478,7 @@ pub mod args {
.help("The account address whose balance to query."),
)
.arg(
TOKEN_OPT
TOKEN
.def()
.help("The token's address whose balance to query."),
)
Expand All @@ -5513,50 +5487,6 @@ pub mod args {
"Whether not to automatically perform conversions.",
),
)
.arg(SHOW_IBC_TOKENS.def().help(
"Show IBC tokens. When the given token is an IBC denom, \
IBC tokens will be shown even if this flag is false.",
))
}
}

impl CliToSdk<QueryIbcToken<SdkTypes>> for QueryIbcToken<CliTypes> {
type Error = std::convert::Infallible;

fn to_sdk(
self,
ctx: &mut Context,
) -> Result<QueryIbcToken<SdkTypes>, Self::Error> {
let query = self.query.to_sdk(ctx)?;
let chain_ctx = ctx.borrow_mut_chain_or_exit();
Ok(QueryIbcToken::<SdkTypes> {
query,
token: self.token,
owner: self.owner.map(|x| chain_ctx.get_cached(&x)),
})
}
}

impl Args for QueryIbcToken<CliTypes> {
fn parse(matches: &ArgMatches) -> Self {
let query = Query::parse(matches);
let token = TOKEN_STR_OPT.parse(matches);
let owner = BALANCE_OWNER.parse(matches);
Self {
query,
owner,
token,
}
}

fn def(app: App) -> App {
app.add_args::<Query<CliTypes>>()
.arg(TOKEN_STR_OPT.def().help("The base token to query."))
.arg(
BALANCE_OWNER
.def()
.help("The account address whose token to query."),
)
}
}

Expand Down
12 changes: 0 additions & 12 deletions crates/apps/src/lib/cli/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,18 +511,6 @@ impl CliApi {
let namada = ctx.to_sdk(client, io);
rpc::query_balance(&namada, args).await;
}
Sub::QueryIbcToken(QueryIbcToken(args)) => {
let chain_ctx = ctx.borrow_mut_chain_or_exit();
let ledger_address =
chain_ctx.get(&args.query.ledger_address);
let client = client.unwrap_or_else(|| {
C::from_tendermint_address(&ledger_address)
});
client.wait_until_node_is_synced(&io).await?;
let args = args.to_sdk(&mut ctx)?;
let namada = ctx.to_sdk(client, io);
rpc::query_ibc_tokens(&namada, args).await;
}
Sub::QueryBonds(QueryBonds(args)) => {
let chain_ctx = ctx.borrow_mut_chain_or_exit();
let ledger_address =
Expand Down
6 changes: 6 additions & 0 deletions crates/apps/src/lib/cli/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,5 +621,11 @@ impl ArgFromMutContext for BalanceOwner {
ExtendedViewingKey::arg_from_mut_ctx(ctx, raw)
.map(Self::FullViewingKey)
})
.map_err(|_| {
format!(
"Could not find {raw} in the wallet, nor parse it as a \
transparent address or as a MASP viewing key"
)
})
}
}
Loading

0 comments on commit 51cc9a2

Please sign in to comment.