pub mod add_invoice;
pub mod get_dm;
pub mod list_orders;
pub mod new_order;
pub mod rate_user;
pub mod send_msg;
pub mod take_buy;
pub mod take_sell;
use crate::cli::add_invoice::execute_add_invoice;
use crate::cli::get_dm::execute_get_dm;
use crate::cli::list_orders::execute_list_orders;
use crate::cli::new_order::execute_new_order;
use crate::cli::rate_user::execute_rate_user;
use crate::cli::send_msg::execute_send_msg;
use crate::cli::take_buy::execute_take_buy;
use crate::cli::take_sell::execute_take_sell;
use crate::util;
use anyhow::Result;
use clap::{Parser, Subcommand};
use mostro_core::{Kind, Status};
use nostr_sdk::prelude::FromBech32;
use nostr_sdk::secp256k1::XOnlyPublicKey;
use std::env::{set_var, var};
use uuid::Uuid;
#[derive(Parser)]
#[command(
name = "mostro-cli",
about = "A simple CLI to use Mostro P2P",
author,
help_template = "\
{before-help}{name} 🧌
{about-with-newline}
{author-with-newline}
{usage-heading} {usage}
{all-args}{after-help}
",
version
)]
#[command(propagate_version = true)]
#[command(arg_required_else_help(true))]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Commands>,
#[arg(short, long)]
pub verbose: bool,
}
#[derive(Subcommand, Clone)]
#[clap(rename_all = "lower")]
pub enum Commands {
ListOrders {
#[arg(short, long)]
#[clap(default_value = "pending")]
status: Option<Status>,
#[arg(short, long)]
currency: Option<String>,
#[arg(value_enum)]
#[arg(short, long)]
kind: Option<Kind>,
},
Neworder {
#[arg(value_enum)]
#[arg(short, long)]
kind: Kind,
#[arg(short, long)]
#[clap(default_value_t = 0)]
amount: i64,
#[arg(short = 'c', long)]
fiat_code: String,
#[arg(short, long)]
#[clap(value_parser=check_fiat_range)]
fiat_amount: i64,
#[arg(short = 'm', long)]
payment_method: String,
#[arg(short, long)]
#[clap(default_value_t = 0)]
premium: i64,
#[arg(short, long)]
invoice: Option<String>,
},
TakeSell {
#[arg(short, long)]
order_id: Uuid,
#[arg(short, long)]
invoice: Option<String>,
},
TakeBuy {
#[arg(short, long)]
order_id: Uuid,
},
AddInvoice {
#[arg(short, long)]
order_id: Uuid,
#[arg(short, long)]
invoice: String,
},
GetDm {
#[arg(short, long)]
#[clap(default_value_t = 30)]
since: i64,
},
FiatSent {
#[arg(short, long)]
order_id: Uuid,
},
Release {
#[arg(short, long)]
order_id: Uuid,
},
Cancel {
#[arg(short, long)]
order_id: Uuid,
},
Rate {
#[arg(short, long)]
order_id: Uuid,
#[arg(short, long)]
rating: u8,
},
Dispute {
#[arg(short, long)]
order_id: Uuid,
},
AdminCancel {
#[arg(short, long)]
order_id: Uuid,
},
AdminSettle {
#[arg(short, long)]
order_id: Uuid,
},
}
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")),
}
}
pub async fn run() -> Result<()> {
let cli = Cli::parse();
if cli.verbose {
set_var("RUST_LOG", "info");
}
let pubkey = var("MOSTRO_PUBKEY").expect("$MOSTRO_PUBKEY env var needs to be set");
let mostro_key = XOnlyPublicKey::from_bech32(pubkey)?;
let my_key = util::get_keys()?;
let client = util::connect_nostr().await?;
if let Some(cmd) = cli.command {
match &cmd {
Commands::ListOrders {
status,
currency,
kind,
} => execute_list_orders(kind, currency, status, mostro_key, &client).await?,
Commands::TakeSell { order_id, invoice } => {
execute_take_sell(order_id, invoice, &my_key, mostro_key, &client).await?
}
Commands::TakeBuy { order_id } => {
execute_take_buy(order_id, &my_key, mostro_key, &client).await?
}
Commands::AddInvoice { order_id, invoice } => {
execute_add_invoice(order_id, invoice, &my_key, mostro_key, &client).await?
}
Commands::GetDm { since } => {
execute_get_dm(since, &my_key, mostro_key, &client).await?
}
Commands::FiatSent { order_id }
| Commands::Release { order_id }
| Commands::Dispute { order_id }
| Commands::AdminCancel { order_id }
| Commands::AdminSettle { order_id }
| Commands::Cancel { order_id } => {
execute_send_msg(cmd.clone(), order_id, &my_key, mostro_key, &client).await?
}
Commands::Neworder {
kind,
fiat_code,
amount,
fiat_amount,
payment_method,
premium,
invoice,
} => {
execute_new_order(
kind,
fiat_code,
fiat_amount,
amount,
payment_method,
premium,
invoice,
&my_key,
mostro_key,
&client,
)
.await?
}
Commands::Rate { order_id, rating } => {
execute_rate_user(order_id, rating, &my_key, mostro_key, &client).await?;
}
};
}
println!("Bye Bye!");
Ok(())
}