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

Avoid panic on getting price of not found code #145

Merged
merged 1 commit into from
Nov 13, 2023
Merged
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
9 changes: 8 additions & 1 deletion src/app/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::cli::settings::Settings;
use crate::util::{get_market_quote, publish_order, send_dm};

use anyhow::Result;
use log::error;
use mostro_core::{Action, Message};
use nostr_sdk::prelude::ToBech32;
use nostr_sdk::{Client, Event, Keys};
Expand All @@ -16,7 +17,13 @@ pub async fn order_action(
) -> Result<()> {
if let Some(order) = msg.get_order() {
let mostro_settings = Settings::get_mostro();
let quote = get_market_quote(&order.fiat_amount, &order.fiat_code, &0).await?;
let quote = match get_market_quote(&order.fiat_amount, &order.fiat_code, &0).await {
Ok(amount) => amount,
Err(e) => {
error!("{:?}", e.to_string());
return Ok(());
}
};
if quote > mostro_settings.max_order_amount as i64 {
let message = Message::new(0, order.id, None, Action::CantDo, None);
let message = message.as_json()?;
Expand Down
16 changes: 11 additions & 5 deletions src/app/take_buy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub async fn take_buy_action(
let mut order = match Order::by_id(pool, order_id).await? {
Some(order) => order,
None => {
error!("TakeBuy: Order Id {order_id} not found!");
error!("Order Id {order_id} not found!");
return Ok(());
}
};
Expand All @@ -37,21 +37,21 @@ pub async fn take_buy_action(
}

if order.kind != "Buy" {
error!("TakeBuy: Order Id {order_id} wrong kind");
error!("Order Id {order_id} wrong kind");
return Ok(());
}

let order_status = match Status::from_str(&order.status) {
Ok(s) => s,
Err(e) => {
error!("TakeBuy: Order Id {order_id} wrong status: {e:?}");
error!("Order Id {order_id} wrong status: {e:?}");
return Ok(());
}
};
let buyer_pubkey = match order.buyer_pubkey.as_ref() {
Some(pk) => XOnlyPublicKey::from_bech32(pk)?,
None => {
error!("TakeBuy: Buyer pubkey not found for order {}!", order.id);
error!("Buyer pubkey not found for order {}!", order.id);
return Ok(());
}
};
Expand Down Expand Up @@ -92,7 +92,13 @@ pub async fn take_buy_action(
// Check market price value in sats - if order was with market price then calculate
if order.amount == 0 {
order.amount =
get_market_quote(&order.fiat_amount, &order.fiat_code, &order.premium).await?;
match get_market_quote(&order.fiat_amount, &order.fiat_code, &order.premium).await {
Ok(amount) => amount,
Err(e) => {
error!("{:?}", e);
return Ok(());
}
};
}

show_hold_invoice(
Expand Down
4 changes: 2 additions & 2 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Yadio {
request: Request,
pub result: f64,
rate: f64,
timestamp: i64,
}

#[derive(Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Request {
amount: i64,
from: String,
Expand Down
8 changes: 6 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub async fn get_market_quote(
"https://api.yadio.io/convert/{}/{}/BTC",
fiat_amount, fiat_code
);
info!("Requesting API price: {}", req_string);

let mut req = None;
// Retry for 4 times
Expand All @@ -64,11 +65,14 @@ pub async fn get_market_quote(

// Case no answers from Yadio
if req.is_none() {
println!("Send dm to user to signal no API response");
return Err(MostroError::NoAPIResponse);
}

let quote = req.unwrap().json::<Yadio>().await?;
let quote = req.unwrap().json::<Yadio>().await;
if quote.is_err() {
return Err(MostroError::NoAPIResponse);
}
let quote = quote.unwrap();

let mut sats = quote.result * 100_000_000_f64;

Expand Down