Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement 'balance' command to fetch wallet balance #152

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub use batch::Batch;

use self::query_me::WalletCurrency;

use crate::types::Wallet;

pub mod server;

pub struct GaloyClient {
Expand Down Expand Up @@ -97,6 +99,57 @@ impl GaloyClient {
Ok(me)
}

pub fn fetch_balance(&self, wallet: Option<Wallet>) -> anyhow::Result<String> {
let me = self.me()?;

let default_wallet_id = me.default_account.default_wallet_id;

let wallets = &me.default_account.wallets;
let mut wallet_balances = String::new();

for wallet_info in wallets {
match &wallet {
Some(Wallet::Usd) if wallet_info.wallet_currency == WalletCurrency::USD => {
return Ok(format!("USD wallet: {} cents", wallet_info.balance));
}
Some(Wallet::Btc) if wallet_info.wallet_currency == WalletCurrency::BTC => {
return Ok(format!("BTC wallet: {} sats", wallet_info.balance));
}
None => {
if wallet_info.wallet_currency == WalletCurrency::USD {
wallet_balances += &format!(
"USD wallet{}: {} cents\n",
Copy link
Member

@nicolasburtey nicolasburtey Jun 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we exporting the value always as a string? would be good to have the option to expose it as a json as well (can be done in another PR later in the project)

a key goal behind galoy-cli is to enable scripting. which is why considering json output as much as friendly output is important

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the time being, the main focus of this command is to output the balance in the CLI, hence the string format. However, as you've suggested, as we further develop the cli, the necessity for the function to return a number or a JSON will definitely arise as this function will be used in other functions or tests as well.

if wallet_info.id == default_wallet_id {
" (default)"
} else {
""
},
wallet_info.balance
);
}
if wallet_info.wallet_currency == WalletCurrency::BTC {
wallet_balances += &format!(
"BTC wallet{}: {} sats\n",
if wallet_info.id == default_wallet_id {
" (default)"
} else {
""
},
wallet_info.balance
);
}
}
_ => {}
}
}

if wallet_balances.is_empty() {
Err(anyhow::anyhow!("No matching wallet found"))
} else {
Ok(wallet_balances)
}
}

pub fn request_phone_code(&self, phone: String, nocaptcha: bool) -> std::io::Result<()> {
match nocaptcha {
false => {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
mod client;

pub use client::*;

pub mod types;
27 changes: 21 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use std::fs::{self};
mod constants;
mod token;

use galoy_cli::types::*;

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
struct Cli {
Expand Down Expand Up @@ -65,6 +67,13 @@ enum Commands {
},
/// Execute Me query
Me,
/// Fetch the balance of a wallet
Balance {
#[clap(long)]
btc: bool,
#[clap(long)]
usd: bool,
},
/// Execute a Payment
Pay {
#[clap(short, long)]
Expand All @@ -82,12 +91,6 @@ enum Commands {
Batch { filename: String, price: Decimal },
}

#[derive(Debug, Clone, clap::ValueEnum, PartialEq, Eq)]
enum Wallet {
Btc,
Usd,
}

fn main() -> anyhow::Result<()> {
log::set_max_level(LevelFilter::Warn);

Expand Down Expand Up @@ -128,6 +131,18 @@ fn main() -> anyhow::Result<()> {
serde_json::to_string_pretty(&result).expect("Can't serialize json")
);
}
Commands::Balance { btc, usd } => {
let wallet_type = match (btc, usd) {
(true, true) | (false, false) => None,
(true, false) => Some(Wallet::Btc),
(false, true) => Some(Wallet::Usd),
};

let balance = galoy_cli
.fetch_balance(wallet_type)
.context("can't fetch balance")?;
println!("{}", balance);
}
Commands::Pay {
username,
wallet,
Expand Down
5 changes: 5 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[derive(Debug, Clone, clap::ValueEnum, PartialEq, Eq)]
pub enum Wallet {
Btc,
Usd,
}