diff --git a/gql/gva_queries.gql b/gql/gva_queries.gql index 7a97ba49d754d58960822ad01c0b85ccfd2cbaf2..2c1ae9f68672e71584cf71d8da4f259d86e3d0dd 100644 --- a/gql/gva_queries.gql +++ b/gql/gva_queries.gql @@ -1,13 +1,14 @@ -query BalanceQuery($script: String!) { +query BalanceQuery($script: String!, $withUd: Boolean!) { balance(script: $script) { amount - base + } + currentUd @include(if: $withUd) { + amount } } query CurrentUdQuery { currentUd { amount - base } } diff --git a/src/main.rs b/src/main.rs index 13710cdefa5143a5a89e09cc73361f17b42795dd..49380ccbb1f192e5874b6d04463b986c2cbb5101 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,6 +57,8 @@ enum SubCommand { /// Get account balance Balance { pubkey_or_script: String, + #[structopt(short, long)] + ud_unit: bool, }, /// Get current UD value CurrentUd, @@ -68,27 +70,41 @@ fn main() -> anyhow::Result<()> { let client = Client::new(); match cli_args.command { - SubCommand::Balance { pubkey_or_script } => { + SubCommand::Balance { + pubkey_or_script, + ud_unit, + } => { let request_body = BalanceQuery::build_query(balance_query::Variables { script: pubkey_or_script.clone(), + with_ud: ud_unit, }); let balance_query::ResponseData { - balance: balance_query::BalanceQueryBalance { amount, .. }, + balance: balance_query::BalanceQueryBalance { amount }, + current_ud: current_ud_opt, } = client.send_gql_query(&request_body, &cli_args.server)?; - let int_part = amount / 100; - let dec_part = amount % 100; - println!( - "The balance of account '{}' is {}.{} Ğ1 !", - pubkey_or_script, int_part, dec_part - ); + if let Some(balance_query::BalanceQueryCurrentUd { amount: ud_amount }) = current_ud_opt + { + println!( + "The balance of account '{}' is {:.2} UDĞ1 !", + pubkey_or_script, + amount as f64 / ud_amount as f64, + ); + } else { + println!( + "The balance of account '{}' is {}.{} Ğ1 !", + pubkey_or_script, + amount / 100, + amount % 100 + ); + } } SubCommand::CurrentUd => { let request_body = CurrentUdQuery::build_query(current_ud_query::Variables); if let current_ud_query::ResponseData { - current_ud: Some(current_ud_query::CurrentUdQueryCurrentUd { amount, .. }), + current_ud: Some(current_ud_query::CurrentUdQueryCurrentUd { amount }), } = client.send_gql_query(&request_body, &cli_args.server)? { let int_part = amount / 100;