Skip to content

Commit

Permalink
chore: ask for confirmation before executing batch payment
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasburtey committed Sep 1, 2022
1 parent 1385595 commit d6398cd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
19 changes: 17 additions & 2 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use batch::Batch;

use rust_decimal::Decimal;

use std::io;

pub struct GaloyClient {
graphql_client: Client,
api: String,
Expand Down Expand Up @@ -179,8 +181,7 @@ impl GaloyClient {
}
}

// TODO: check if we can do self without &
pub fn batch(self, filename: String, price: Decimal) -> anyhow::Result<()> {
pub fn batch(self, filename: String, price: Decimal, force: bool) -> anyhow::Result<()> {
let mut batch = Batch::new(self, price);

batch.add_csv(filename).context("can't load file")?;
Expand All @@ -196,6 +197,20 @@ impl GaloyClient {
println!("going to execute:");
batch.show();

if !force {
println!("do you want to confirm? y/n");

let mut input = String::new();

io::stdin()
.read_line(&mut input)
.expect("error: unable to read user input");

if !(input.trim() == "y" || input.trim() == "yes") {
bail!("didn't confirm batch payment. exiting")
}
}

batch.execute().context("can't make payment successfully")?;

Ok(())
Expand Down
56 changes: 39 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use url::Url;

use galoy_client::GaloyClient;

use anyhow::Context;
use anyhow::{bail, Context};

use jsonwebtoken::decode_header;

Expand Down Expand Up @@ -61,7 +61,12 @@ enum Commands {
/// get JWT of an account
Login { phone: String, code: String },
/// execute a batch payment
Batch { filename: String, price: Decimal },
Batch {
filename: String,
price: Decimal,
#[clap(short, long)]
force: bool,
},
}

fn main() -> anyhow::Result<()> {
Expand All @@ -83,7 +88,7 @@ fn main() -> anyhow::Result<()> {
}

info!("using api: {api} and jwt: {:?}", &jwt);
let galoy_client = GaloyClient::new(api, jwt);
let galoy_client = GaloyClient::new(api, jwt.clone());

match cli.command {
Commands::Getinfo {} => {
Expand All @@ -94,7 +99,24 @@ fn main() -> anyhow::Result<()> {
let result = galoy_client.default_wallet(username)?;
println!("{:#?}", result);
}
Commands::RequestPhoneCode { phone } => {
let result = galoy_client
.request_auth_code(phone)
.context("issue getting code")?;
println!("{:#?}", result);
}
Commands::Login { phone, code } => {
let result = galoy_client
.user_login(phone, code)
.context("issue logging in")?;
println!("{:#?}", result);
}

Commands::Me => {
if jwt.is_none() {
bail!("need JWT");
}

let result = galoy_client.me().context("can't get me")?;
println!("{:#?}", result);
}
Expand All @@ -103,26 +125,26 @@ fn main() -> anyhow::Result<()> {
amount,
memo,
} => {
if jwt.is_none() {
bail!("need JWT");
}

let result = galoy_client
.intraleger_send(username, amount, memo)
.context("issue sending intraledger")?;
println!("{:#?}", result);
}
Commands::RequestPhoneCode { phone } => {
let result = galoy_client
.request_auth_code(phone)
.context("issue getting code")?;
println!("{:#?}", result);
}
Commands::Login { phone, code } => {
let result = galoy_client
.user_login(phone, code)
.context("issue logging in")?;
println!("{:#?}", result);
}
Commands::Batch { filename, price } => {
Commands::Batch {
filename,
price,
force,
} => {
if jwt.is_none() {
bail!("need JWT");
}

let result = galoy_client
.batch(filename, price)
.batch(filename, price, force)
.context("issue batching payment");
println!("{:#?}", result);
}
Expand Down

0 comments on commit d6398cd

Please sign in to comment.