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

Range order fix #70

Merged
merged 5 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mostro-cli"
version = "0.9.3"
version = "0.9.4"
edition = "2021"
license = "MIT"
authors = [
Expand Down Expand Up @@ -39,7 +39,7 @@ uuid = { version = "1.3.0", features = [
dotenvy = "0.15.6"
lightning-invoice = "0.23.0"
reqwest = { version = "0.12.4", features = ["json"] }
mostro-core = { version = "0.5.8", features = ["sqlx"] }
mostro-core = { version = "0.5.10", features = ["sqlx"] }
bitcoin_hashes = "0.12.0"
lnurl-rs = "0.4.0"
pretty_env_logger = "0.5.0"
pretty_env_logger = "0.5.0"
46 changes: 39 additions & 7 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::cli::take_dispute::execute_take_dispute;
use crate::cli::take_sell::execute_take_sell;
use crate::util;

use anyhow::Result;
use anyhow::{Error, Result};
use clap::{Parser, Subcommand};
use nostr_sdk::prelude::*;
use std::env::{set_var, var};
Expand Down Expand Up @@ -88,7 +88,7 @@ pub enum Commands {
/// Fiat amount
#[arg(short, long)]
#[clap(value_parser=check_fiat_range)]
fiat_amount: i64,
fiat_amount: (i64, Option<i64>),
/// Payment method
#[arg(short = 'm', long)]
payment_method: String,
Expand Down Expand Up @@ -196,11 +196,43 @@ pub enum Commands {
},
}

/// Check range simple version for just a single value
pub fn check_fiat_range(s: &str) -> Result<i64, String> {
match s.parse::<i64>() {
Ok(val) => Ok(val),
Err(_e) => Err(String::from("Error on parsing sats value")),
// Check range with two values value
fn check_fiat_range(s: &str) -> Result<(i64, Option<i64>)> {
if s.contains('-') {
let min: i64;
let max: i64;

// Get values from CLI
let values: Vec<&str> = s.split('-').collect();

// Check if more than two values
if values.len() > 2 {
return Err(Error::msg("Wrong amount syntax"));
};

// Get ranged command
if let Err(e) = values[0].parse::<i64>() {
return Err(e.into());
} else {
min = values[0].parse().unwrap();
}

if let Err(e) = values[1].parse::<i64>() {
return Err(e.into());
} else {
max = values[1].parse().unwrap();
}

// Check min below max
if min >= max {
return Err(Error::msg("Range of values must be 100-200 for example..."));
};
Ok((min, Some(max)))
} else {
match s.parse::<i64>() {
Ok(s) => Ok((s, None)),
Err(e) => Err(e.into()),
}
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/cli/new_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub type FiatNames = HashMap<String, String>;
pub async fn execute_new_order(
kind: &str,
fiat_code: &str,
fiat_amount: &i64,
fiat_amount: &(i64, Option<i64>),
amount: &i64,
payment_method: &String,
premium: &i64,
Expand Down Expand Up @@ -56,14 +56,25 @@ pub async fn execute_new_order(
}
};

// Get the type of neworder
// if both tuple field are valid than it's a range order
// otherwise use just fiat amount value as before
let amt = if fiat_amount.1.is_some() {
(0, Some(fiat_amount.0), Some(fiat_amount.1.unwrap()))
arkanoider marked this conversation as resolved.
Show resolved Hide resolved
} else {
(fiat_amount.0, None, None)
};

// Create new order for mostro
let order_content = Content::Order(SmallOrder::new(
None,
Some(kind_checked),
Some(Status::Pending),
*amount,
fiat_code,
*fiat_amount,
amt.1,
amt.2,
amt.0,
payment_method.to_owned(),
*premium,
None,
Expand Down
12 changes: 11 additions & 1 deletion src/pretty_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ pub fn print_order_preview(ord: Content) -> Result<String, String> {
Cell::new(single_order.amount).set_alignment(CellAlignment::Center)
},
Cell::new(single_order.fiat_code.to_string()).set_alignment(CellAlignment::Center),
Cell::new(single_order.fiat_amount.to_string()).set_alignment(CellAlignment::Center),
// No range order print row
if single_order.min_amount.is_none() && single_order.max_amount.is_none() {
Cell::new(single_order.fiat_amount.to_string()).set_alignment(CellAlignment::Center)
} else {
let range_str = format!(
"{}-{}",
single_order.min_amount.unwrap(),
single_order.max_amount.unwrap()
);
Cell::new(range_str).set_alignment(CellAlignment::Center)
},
Cell::new(single_order.payment_method.to_string()).set_alignment(CellAlignment::Center),
Cell::new(single_order.premium.to_string()).set_alignment(CellAlignment::Center),
]);
Expand Down
Loading