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 all 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
56 changes: 54 additions & 2 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use reqwest::blocking::Client;

use log::info;
use rust_decimal::Decimal;
use std::net::TcpListener;
use std::{collections::HashSet, net::TcpListener};

pub mod queries;
pub use queries::*;
Expand All @@ -15,10 +15,22 @@ pub use error::*;
pub mod batch;
pub use batch::Batch;

use self::query_me::WalletCurrency;
pub use self::query_me::WalletCurrency;

use crate::types::*;

pub mod server;

impl From<&WalletCurrency> for Wallet {
fn from(currency: &WalletCurrency) -> Self {
match currency {
WalletCurrency::USD => Wallet::Usd,
WalletCurrency::BTC => Wallet::Btc,
_ => panic!("Unsupported currency"),
}
}
}

pub struct GaloyClient {
graphql_client: Client,
api: String,
Expand Down Expand Up @@ -97,6 +109,46 @@ impl GaloyClient {
Ok(me)
}

pub fn fetch_balance(
&self,
wallet_type: Option<Wallet>,
wallet_ids: Vec<String>,
) -> anyhow::Result<Vec<WalletBalance>> {
let me = self.me()?;
let default_wallet_id = me.default_account.default_wallet_id;
let wallets = &me.default_account.wallets;

let wallet_ids_set: HashSet<_> = wallet_ids.into_iter().collect();

let balances: Vec<_> = wallets
.iter()
.filter(|wallet_info| {
wallet_ids_set.contains(&wallet_info.id)
|| wallet_type.as_ref().map_or(wallet_ids_set.is_empty(), |w| {
*w == Wallet::from(&wallet_info.wallet_currency)
})
})
.map(|wallet_info| WalletBalance {
currency: format!("{:?}", Wallet::from(&wallet_info.wallet_currency)),
balance: wallet_info.balance,
id: if wallet_info.wallet_currency == WalletCurrency::USD
|| wallet_info.wallet_currency == WalletCurrency::BTC
{
None
} else {
Some(wallet_info.id.clone())
},
default: wallet_info.id == default_wallet_id,
})
.collect();

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

pub fn request_phone_code(&self, phone: String, nocaptcha: bool) -> std::io::Result<()> {
match nocaptcha {
false => {
Expand Down
2 changes: 1 addition & 1 deletion src/client/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub use self::query_globals::QueryGlobalsGlobals;
query_path = "src/client/graphql/queries/me.graphql",
response_derives = "Debug, Serialize, PartialEq"
)]
pub(super) struct QueryMe;
pub struct QueryMe;
pub use self::query_me::QueryMeMe;

// mutations
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;
35 changes: 29 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,15 @@ enum Commands {
},
/// Execute Me query
Me,
/// Fetch the balance of a wallet
Balance {
#[clap(long)]
btc: bool,
#[clap(long)]
usd: bool,
#[clap(long, use_value_delimiter = true)]
wallet_ids: Vec<String>,
},
/// Execute a Payment
Pay {
#[clap(short, long)]
Expand All @@ -82,12 +93,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 +133,24 @@ fn main() -> anyhow::Result<()> {
serde_json::to_string_pretty(&result).expect("Can't serialize json")
);
}
Commands::Balance {
btc,
usd,
wallet_ids,
} => {
let wallet_type = match (btc, usd) {
(true, true) | (false, false) => None,
(true, false) => Some(Wallet::Btc),
(false, true) => Some(Wallet::Usd),
};

let balances = galoy_cli
.fetch_balance(wallet_type, wallet_ids)
.context("can't fetch balance")?;
let balances_json =
serde_json::to_string_pretty(&balances).context("Can't serialize json")?;
println!("{}", balances_json);
}
Commands::Pay {
username,
wallet,
Expand Down
16 changes: 16 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use rust_decimal::Decimal;
use serde::Serialize;

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

#[derive(Debug, Serialize)]
pub struct WalletBalance {
pub currency: String,
pub balance: Decimal,
pub id: Option<String>,
pub default: bool,
}