-
Notifications
You must be signed in to change notification settings - Fork 401
Expand file tree
/
Copy pathbalance.rs
More file actions
190 lines (166 loc) · 6.24 KB
/
balance.rs
File metadata and controls
190 lines (166 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::fmt::Write;
use abscissa_core::clap::Parser;
use abscissa_core::{Command, Runnable};
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::config::ChainConfig;
use ibc_relayer_types::core::ics24_host::identifier::ChainId;
use crate::application::app_config;
use crate::cli_utils::spawn_chain_runtime;
use crate::conclude::{exit_with_unrecoverable_error, json, Output};
/// The data structure that represents the arguments when invoking the `keys balance` CLI command.
///
/// The command has one argument and one optional flag:
///
/// `keys balance --chain <chain_id> --key-name <KEY_NAME>`
///
/// If no key name is given, it will be taken from the configuration file.
/// If successful the balance and denominator of the account, associated with the key name
/// on the given chain, will be displayed.
#[derive(Clone, Command, Debug, Parser, PartialEq, Eq)]
pub struct KeyBalanceCmd {
#[clap(
long = "chain",
required = true,
value_name = "CHAIN_ID",
help_heading = "REQUIRED",
help = "Identifier of the chain"
)]
chain_id: ChainId,
#[clap(
long = "key-name",
value_name = "KEY_NAME",
help = "(optional) name of the key (defaults to the `key_name` defined in the config)"
)]
key_name: Option<String>,
#[clap(
long = "denom",
value_name = "DENOM",
help = "(optional) query the balance for the given denom (defaults to the `denom` defined in the config for the gas price)"
)]
denom: Option<String>,
#[clap(
long = "all",
help = "(optional) query the balance for all denom. This flag overwrites the `--denom` flag (defaults to false)"
)]
all: bool,
}
impl Runnable for KeyBalanceCmd {
fn run(&self) {
let config = app_config();
let chain = spawn_chain_runtime(&config, &self.chain_id)
.unwrap_or_else(exit_with_unrecoverable_error);
let key_name = self.key_name.clone();
if self.all {
get_balances(chain, key_name)
} else {
get_balance(chain, key_name, self.denom.clone());
}
}
}
fn get_balance(chain: impl ChainHandle, key_name: Option<String>, denom: Option<String>) {
match chain.query_balance(key_name.clone(), denom) {
Ok(balance) if json() => Output::success(balance).exit(),
Ok(balance) => {
// Retrieve the key name string to output.
let key_name = key_name.unwrap_or_else(|| {
let chain_config = chain.config().unwrap_or_else(exit_with_unrecoverable_error);
match chain_config {
ChainConfig::CosmosSdk(chain_config) | ChainConfig::Namada(chain_config) => {
chain_config.key_name
}
ChainConfig::Penumbra(_) => unimplemented!("not yet supported for penumbra"),
ChainConfig::Cardano(chain_config) => chain_config.key_name,
}
});
Output::success_msg(format!(
"balance for key `{}`: {} {}",
key_name, balance.amount, balance.denom
))
.exit()
}
Err(e) => Output::error(format!("there was a problem querying the balance: {e}")).exit(),
}
}
fn get_balances(chain: impl ChainHandle, key_name: Option<String>) {
match chain.query_all_balances(key_name.clone()) {
Ok(balances) if json() => Output::success(balances).exit(),
Ok(balances) => {
// Retrieve the key name string to output.
let key_name = key_name.unwrap_or_else(|| {
let chain_config = chain.config().unwrap_or_else(exit_with_unrecoverable_error);
match chain_config {
ChainConfig::CosmosSdk(chain_config) | ChainConfig::Namada(chain_config) => {
chain_config.key_name
}
ChainConfig::Penumbra(_) => unimplemented!("not yet supported for penumbra"),
ChainConfig::Cardano(chain_config) => chain_config.key_name,
}
});
let mut pretty_output = format!("Balances for key `{key_name}`:");
for balance in balances {
write!(pretty_output, "\n\t{} {}", balance.amount, balance.denom)
.unwrap_or_else(exit_with_unrecoverable_error);
}
Output::success_msg(pretty_output).exit()
}
Err(e) => Output::error(format!("there was a problem querying the balance: {e}")).exit(),
}
}
#[cfg(test)]
mod tests {
use super::KeyBalanceCmd;
use abscissa_core::clap::Parser;
use ibc_relayer_types::core::ics24_host::identifier::ChainId;
#[test]
fn test_keys_balance_required_only() {
assert_eq!(
KeyBalanceCmd {
chain_id: ChainId::from_string("chain_id"),
key_name: None,
denom: None,
all: false,
},
KeyBalanceCmd::parse_from(["test", "--chain", "chain_id"])
)
}
#[test]
fn test_keys_balance_name() {
assert_eq!(
KeyBalanceCmd {
chain_id: ChainId::from_string("chain_id"),
key_name: Some("kname".to_owned()),
denom: None,
all: false,
},
KeyBalanceCmd::parse_from(["test", "--chain", "chain_id", "--key-name", "kname"])
)
}
#[test]
fn test_keys_balance_denom() {
assert_eq!(
KeyBalanceCmd {
chain_id: ChainId::from_string("chain_id"),
key_name: None,
denom: Some("samoleans".to_owned()),
all: false,
},
KeyBalanceCmd::parse_from(["test", "--chain", "chain_id", "--denom", "samoleans"])
)
}
#[test]
fn test_keys_balance_all_denom() {
assert_eq!(
KeyBalanceCmd {
chain_id: ChainId::from_string("chain_id"),
key_name: None,
denom: None,
all: true,
},
KeyBalanceCmd::parse_from(["test", "--chain", "chain_id", "--all"])
)
}
#[test]
fn test_keys_balance_no_chain() {
assert!(KeyBalanceCmd::try_parse_from(["test"]).is_err())
}
}