Skip to content

Commit

Permalink
Hot fix on message 1 implementation (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch authored Dec 6, 2023
1 parent 34cea67 commit 52f03bf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use anyhow::Result;
use log::error;
use mostro_core::message::{Action, Content, Message};
use mostro_core::order::SmallOrder;
use mostro_core::order::{Order, Status};
use mostro_core::order::{Kind, Order, Status};
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use sqlx_crud::Crud;
Expand Down Expand Up @@ -73,6 +73,15 @@ pub async fn add_invoice_action(
return Ok(());
}
};

let order_kind = match Kind::from_str(&order.kind) {
Ok(k) => k,
Err(e) => {
error!("Order Id {order_id} wrong kind: {e:?}");
return Ok(());
}
};

let buyer_pubkey = match order.buyer_pubkey.as_ref() {
Some(pk) => XOnlyPublicKey::from_bech32(pk)?,
None => {
Expand Down Expand Up @@ -114,8 +123,8 @@ pub async fn add_invoice_action(
// We send this data related to the order to the parties
let order_data = SmallOrder::new(
Some(order.id),
None,
None,
Some(order_kind),
Some(order_status),
order.amount,
order.fiat_code.clone(),
order.fiat_amount,
Expand Down
20 changes: 15 additions & 5 deletions src/flow.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::cli::settings::Settings;
use crate::util::send_dm;

use log::error;
use log::info;
use mostro_core::message::{Action, Content, Message};
use mostro_core::order::{SmallOrder, Status};
use mostro_core::order::{Kind, SmallOrder, Status};
use nostr_sdk::prelude::*;
use std::str::FromStr;

pub async fn hold_invoice_paid(hash: &str) {
let pool = crate::db::connect().await.unwrap();
Expand All @@ -25,11 +27,18 @@ pub async fn hold_invoice_paid(hash: &str) {
master_buyer_pubkey = order.master_buyer_pubkey.clone();
master_seller_pubkey = order.master_seller_pubkey.clone();
}
let order_kind = match Kind::from_str(&order.kind) {
Ok(k) => k,
Err(e) => {
error!("Order Id {} wrong kind: {:?}", order.id, e);
return;
}
};

// We send this data related to the order to the parties
let mut order_data = SmallOrder::new(
Some(order.id),
None,
Some(order_kind),
None,
order.amount,
order.fiat_code.clone(),
Expand All @@ -51,6 +60,8 @@ pub async fn hold_invoice_paid(hash: &str) {
Action::BuyerTookOrder,
Some(Content::Order(order_data.clone())),
);
status = Status::Active;
order_data.status = Some(status);
let message = message.as_json().unwrap();
send_dm(&client, &my_keys, &seller_pubkey, message)
.await
Expand All @@ -66,14 +77,14 @@ pub async fn hold_invoice_paid(hash: &str) {
send_dm(&client, &my_keys, &buyer_pubkey, message)
.await
.unwrap();
status = Status::Active;
} else {
let mostro_settings = Settings::get_mostro();
let sub_fee = mostro_settings.fee * order_data.amount as f64;
let rounded_fee = sub_fee.round();
let new_amount = order_data.amount - rounded_fee as i64;
order_data.amount = new_amount;

status = Status::WaitingBuyerInvoice;
order_data.status = Some(status);
// We ask to buyer for a new invoice
let message = Message::new_order(
Some(order.id),
Expand All @@ -91,7 +102,6 @@ pub async fn hold_invoice_paid(hash: &str) {
send_dm(&client, &my_keys, &seller_pubkey, message)
.await
.unwrap();
status = Status::WaitingBuyerInvoice;
}
// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
Expand Down

0 comments on commit 52f03bf

Please sign in to comment.