diff --git a/src/app/add_invoice.rs b/src/app/add_invoice.rs index 845c701d..d0e84bc2 100644 --- a/src/app/add_invoice.rs +++ b/src/app/add_invoice.rs @@ -10,6 +10,7 @@ use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; use std::str::FromStr; +use std::thread; use tracing::error; pub async fn add_invoice_action( @@ -119,6 +120,7 @@ pub async fn add_invoice_action( // We save the invoice on db order.buyer_invoice = Some(pr.clone()); let order = order.update(pool).await?; + thread::sleep(std::time::Duration::from_secs(1)); if order.preimage.is_some() { // We send this data related to the order to the parties @@ -138,7 +140,7 @@ pub async fn add_invoice_action( ); // We publish a new replaceable kind nostr event with the status updated // and update on local database the status and new event id - crate::util::update_order_event(pool, client, my_keys, Status::Active, &order, None) + crate::util::update_order_event(pool, client, my_keys, Status::Active, &order) .await .unwrap(); diff --git a/src/app/admin_cancel.rs b/src/app/admin_cancel.rs index 757ca4d2..e641afdb 100644 --- a/src/app/admin_cancel.rs +++ b/src/app/admin_cancel.rs @@ -70,7 +70,7 @@ pub async fn admin_cancel_action( // We publish a new replaceable kind nostr event with the status updated // and update on local database the status and new event id - update_order_event(pool, client, my_keys, Status::CanceledByAdmin, &order, None).await?; + update_order_event(pool, client, my_keys, Status::CanceledByAdmin, &order).await?; // We create a Message let message = Message::new_dispute(Some(order.id), None, Action::AdminCancel, None); let message = message.as_json()?; diff --git a/src/app/admin_settle.rs b/src/app/admin_settle.rs index 61250cd5..f826a196 100644 --- a/src/app/admin_settle.rs +++ b/src/app/admin_settle.rs @@ -116,7 +116,7 @@ pub async fn admin_settle_action( tokio::time::sleep(tokio::time::Duration::from_secs(10)).await; // We publish a new replaceable kind nostr event with the status updated // and update on local database the status and new event id - update_order_event(&pool, &client, &my_keys, status, &order, None) + update_order_event(&pool, &client, &my_keys, status, &order) .await .unwrap(); diff --git a/src/app/cancel.rs b/src/app/cancel.rs index 478d7666..6afc8b7e 100644 --- a/src/app/cancel.rs +++ b/src/app/cancel.rs @@ -41,7 +41,7 @@ pub async fn cancel_action( } else { // We publish a new replaceable kind nostr event with the status updated // and update on local database the status and new event id - update_order_event(pool, client, my_keys, Status::Canceled, &order, None).await?; + update_order_event(pool, client, my_keys, Status::Canceled, &order).await?; // We create a Message for cancel let message = Message::new_order(Some(order.id), None, Action::Cancel, None); let message = message.as_json()?; @@ -102,7 +102,6 @@ pub async fn cancel_action( my_keys, Status::CooperativelyCanceled, &order, - None, ) .await?; // We create a Message for an accepted cooperative cancel and send it to both parties @@ -183,15 +182,7 @@ pub async fn cancel_add_invoice( if &order.creator_pubkey == buyer_pubkey { // We publish a new replaceable kind nostr event with the status updated // and update on local database the status and new event id - update_order_event( - pool, - client, - my_keys, - Status::CooperativelyCanceled, - order, - None, - ) - .await?; + update_order_event(pool, client, my_keys, Status::CooperativelyCanceled, order).await?; // We create a Message for cancel let message = Message::new_order(Some(order.id), None, Action::Cancel, None); let message = message.as_json()?; @@ -207,7 +198,7 @@ pub async fn cancel_add_invoice( } edit_buyer_pubkey_order(pool, order.id, None).await?; update_order_to_initial_state(pool, order.id, order.amount, order.fee).await?; - update_order_event(pool, client, my_keys, Status::Pending, order, None).await?; + update_order_event(pool, client, my_keys, Status::Pending, order).await?; info!( "{}: Canceled order Id {} republishing order", buyer_pubkey, order.id @@ -252,7 +243,7 @@ pub async fn cancel_pay_hold_invoice( if order.creator_pubkey == seller_pubkey.to_string() { // We publish a new replaceable kind nostr event with the status updated // and update on local database the status and new event id - update_order_event(pool, client, my_keys, Status::Canceled, order, None).await?; + update_order_event(pool, client, my_keys, Status::Canceled, order).await?; // We create a Message for cancel let message = Message::new_order(Some(order.id), None, Action::Cancel, None); let message = message.as_json()?; @@ -268,7 +259,7 @@ pub async fn cancel_pay_hold_invoice( } edit_seller_pubkey_order(pool, order.id, None).await?; update_order_to_initial_state(pool, order.id, order.amount, order.fee).await?; - update_order_event(pool, client, my_keys, Status::Pending, order, None).await?; + update_order_event(pool, client, my_keys, Status::Pending, order).await?; info!( "{}: Canceled order Id {} republishing order", buyer_pubkey, order.id diff --git a/src/app/fiat_sent.rs b/src/app/fiat_sent.rs index 166a5f5e..35e3c76b 100644 --- a/src/app/fiat_sent.rs +++ b/src/app/fiat_sent.rs @@ -47,7 +47,7 @@ pub async fn fiat_sent_action( // We publish a new replaceable kind nostr event with the status updated // and update on local database the status and new event id - update_order_event(pool, client, my_keys, Status::FiatSent, &order, None).await?; + update_order_event(pool, client, my_keys, Status::FiatSent, &order).await?; let seller_pubkey = match order.seller_pubkey.as_ref() { Some(pk) => XOnlyPublicKey::from_str(pk)?, diff --git a/src/app/release.rs b/src/app/release.rs index eb4b12f9..0f20fdfb 100644 --- a/src/app/release.rs +++ b/src/app/release.rs @@ -101,7 +101,7 @@ pub async fn release_action( tokio::time::sleep(tokio::time::Duration::from_secs(10)).await; // We publish a new replaceable kind nostr event with the status updated // and update on local database the status and new event id - update_order_event(&pool, &client, &my_keys, status, &order, None) + update_order_event(&pool, &client, &my_keys, status, &order) .await .unwrap(); diff --git a/src/flow.rs b/src/flow.rs index a81173ee..98ef8e59 100644 --- a/src/flow.rs +++ b/src/flow.rs @@ -107,7 +107,7 @@ pub async fn hold_invoice_paid(hash: &str) { } // We publish a new replaceable kind nostr event with the status updated // and update on local database the status and new event id - crate::util::update_order_event(&pool, &client, &my_keys, status, &order, None) + crate::util::update_order_event(&pool, &client, &my_keys, status, &order) .await .unwrap(); // Update the invoice_held_at field diff --git a/src/scheduler.rs b/src/scheduler.rs index fddee1b8..dc2c9c77 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -84,14 +84,12 @@ async fn job_cancel_orders(client: Client) { let _ = ln_client.cancel_hold_invoice(hash).await; info!("Order Id {}: Funds returned to seller - buyer did not sent regular invoice in time", &order.id); }; + let mut order = order.clone(); // We re-publish the event with Pending status // and update on local database - let mut updated_order_amount = order.amount; - let mut updated_order_fee = order.fee; - if order.price_from_api { - updated_order_amount = 0; - updated_order_fee = 0; + order.amount = 0; + order.fee = 0; } // Initialize reset status to pending, change in case of specifici needs of order @@ -137,8 +135,8 @@ async fn job_cancel_orders(client: Client) { let _ = update_order_to_initial_state( &pool, order.id, - updated_order_amount, - updated_order_fee, + order.amount, + order.fee, ) .await; info!( @@ -151,15 +149,7 @@ async fn job_cancel_orders(client: Client) { order.id ); } - let _ = update_order_event( - &pool, - &client, - &keys, - new_status, - &order, - Some(updated_order_amount), - ) - .await; + let _ = update_order_event(&pool, &client, &keys, new_status, &order).await; } } } @@ -193,7 +183,6 @@ async fn job_expire_pending_older_orders(client: Client) { &keys, Status::Expired, order, - None, ) .await; } diff --git a/src/util.rs b/src/util.rs index 1a04d7b7..ce64f44a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -94,11 +94,13 @@ pub async fn publish_order( master_pubkey: &str, ack_pubkey: XOnlyPublicKey, ) -> Result<()> { - let mostro_settings = Settings::get_mostro(); - // We calculate the bot fee - let fee = mostro_settings.fee; - let fee = (fee * new_order.amount as f64) / 2.0; - let fee = fee.round() as i64; + let mut fee = 0; + if new_order.amount > 0 { + let mostro_settings = Settings::get_mostro(); + // We calculate the bot fee + let split_fee = (mostro_settings.fee * new_order.amount as f64) / 2.0; + fee = split_fee.round() as i64; + } // Prepare a new default order let mut new_order_db = Order { id: Uuid::new_v4(), @@ -231,9 +233,7 @@ pub async fn update_order_event( keys: &Keys, status: Status, order: &Order, - amount: Option, ) -> Result<()> { - let amount = amount.unwrap_or(order.amount); let mut order = order.clone(); // update order.status with new status order.status = status.to_string(); @@ -245,7 +245,6 @@ pub async fn update_order_event( info!("Sending replaceable event: {event:#?}"); // We update the order with the new event_id order.event_id = event.id.to_string(); - order.amount = amount; order.update(pool).await?; info!( "Order Id: {} updated Nostr new Status: {}", @@ -294,13 +293,8 @@ pub async fn show_hold_invoice( } }; let mut ln_client = lightning::LndConnector::new().await; - let mostro_settings = Settings::get_mostro(); // Add fee of seller to hold invoice - let seller_fee = mostro_settings.fee / 2.0; - let add_fee = seller_fee * order.amount as f64; - let rounded_fee = add_fee.round(); - let new_amount = order.amount + rounded_fee as i64; - let seller_total_amount = new_amount; + let new_amount = order.amount + order.fee; // Now we generate the hold invoice that seller should pay let (invoice_response, preimage, hash) = ln_client @@ -311,7 +305,7 @@ pub async fn show_hold_invoice( &order.fiat_code, &order.fiat_amount.to_string(), )?, - seller_total_amount, + new_amount, ) .await?; if let Some(invoice) = payment_request { @@ -327,7 +321,7 @@ pub async fn show_hold_invoice( let order = order.update(pool).await?; // We need to publish a new event with the new status - update_order_event(pool, client, my_keys, Status::WaitingPayment, &order, None).await?; + update_order_event(pool, client, my_keys, Status::WaitingPayment, &order).await?; let mut new_order = order.as_new_order(); new_order.status = Some(Status::WaitingPayment); // We create a Message to send the hold invoice to seller @@ -399,13 +393,12 @@ pub async fn set_market_order_sats_amount( get_market_quote(&order.fiat_amount, &order.fiat_code, &order.premium).await?; // We calculate the bot fee - let fee = mostro_settings.fee / 2.0; - let sub_fee = fee * new_sats_amount as f64; - let rounded_fee = sub_fee.round(); + let sub_fee = (mostro_settings.fee * new_sats_amount as f64) / 2.0; + let rounded_fee = sub_fee.round() as i64; - let buyer_final_amount = new_sats_amount - rounded_fee as i64; + let buyer_final_amount = new_sats_amount - rounded_fee; let kind = OrderKind::from_str(&order.kind).unwrap(); - let status = Status::from_str(&order.status).unwrap(); + let status = Status::WaitingBuyerInvoice; // We send this data related to the buyer let order_data = SmallOrder::new( @@ -435,15 +428,8 @@ pub async fn set_market_order_sats_amount( // Update order with new sats value order.amount = new_sats_amount; - update_order_event( - pool, - client, - my_keys, - Status::WaitingBuyerInvoice, - order, - None, - ) - .await?; + order.fee = rounded_fee; + update_order_event(pool, client, my_keys, status, order).await?; Ok(order.amount) } @@ -505,7 +491,7 @@ pub async fn settle_seller_hold_invoice( info!("{action}: Order Id {}: hold invoice settled", order.id); // We publish a new replaceable kind nostr event with the status updated // and update on local database the status and new event id - update_order_event(pool, client, my_keys, status, order, None).await?; + update_order_event(pool, client, my_keys, status, order).await?; Ok(()) }