diff --git a/Cargo.toml b/Cargo.toml index 5693c515..942f5c02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ sqlx = { version = "0.6.2", features = [ "uuid", "offline", ] } -sqlx-crud = { version = "0.3.2", features = ["runtime-tokio-rustls"] } +sqlx-crud = { version = "0.4.0", features = ["runtime-tokio-rustls"] } tokio = { version = "1.23.0", features = ["full"] } tonic_openssl_lnd = "0.2.0" uuid = { version = "1.3.0", features = [ @@ -36,7 +36,7 @@ uuid = { version = "1.3.0", features = [ "serde", ] } reqwest = { version = "0.11", features = ["json"] } -mostro-core = "0.4.0" +mostro-core = "0.4.2" tracing = "0.1.37" tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } config = "0.13.3" diff --git a/src/app/add_invoice.rs b/src/app/add_invoice.rs index 9679644c..86dc407e 100644 --- a/src/app/add_invoice.rs +++ b/src/app/add_invoice.rs @@ -118,7 +118,7 @@ pub async fn add_invoice_action( let seller_pubkey = XOnlyPublicKey::from_bech32(seller_pubkey)?; // We save the invoice on db order.buyer_invoice = Some(pr.clone()); - order.update(pool).await?; + let order = order.update(pool).await?; if order.preimage.is_some() { // We send this data related to the order to the parties @@ -171,7 +171,7 @@ pub async fn add_invoice_action( None, &buyer_pubkey, &seller_pubkey, - &mut order, + order.id, ) .await?; } diff --git a/src/app/admin_add_solver.rs b/src/app/admin_add_solver.rs index 5a8afaeb..9e8c51b4 100644 --- a/src/app/admin_add_solver.rs +++ b/src/app/admin_add_solver.rs @@ -6,7 +6,7 @@ use mostro_core::user::User; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; -use tracing::error; +use tracing::{error, info}; pub async fn admin_add_solver_action( msg: Message, @@ -41,13 +41,15 @@ pub async fn admin_add_solver_action( } let user = User::new(npubkey.to_string(), 0, 1, 0, 0); // Use CRUD to create user - user.create(pool).await?; - + match user.create(pool).await { + Ok(r) => info!("Solver added: {:#?}", r), + Err(ee) => error!("Error creating solver: {:#?}", ee), + } // We create a Message for admin let message = Message::new_dispute(None, None, Action::AdminAddSolver, None); let message = message.as_json()?; // Send the message - send_dm(client, my_keys, &event.pubkey, message.clone()).await?; + send_dm(client, my_keys, &event.pubkey, message).await?; Ok(()) } diff --git a/src/app/cancel.rs b/src/app/cancel.rs index 8ce2c0b2..1cd0fedc 100644 --- a/src/app/cancel.rs +++ b/src/app/cancel.rs @@ -87,7 +87,7 @@ pub async fn cancel_action( ); } // Update db - order.update(pool).await?; + let mut order = order.update(pool).await?; order.status = "CooperativelyCanceled".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 @@ -117,7 +117,7 @@ pub async fn cancel_action( None => { order.cancel_initiator_pubkey = Some(user_pubkey.clone()); // update db - order.update(pool).await?; + let order = order.update(pool).await?; // We create a Message to start a cooperative cancel and send it to both parties let message = Message::new_order( Some(order.id), diff --git a/src/app/dispute.rs b/src/app/dispute.rs index 8be8e6f5..308ca663 100644 --- a/src/app/dispute.rs +++ b/src/app/dispute.rs @@ -7,7 +7,7 @@ use mostro_core::message::{Action, Message}; use mostro_core::order::Order; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; -use sqlx_crud::Crud; +use sqlx_crud::traits::Crud; use tracing::{error, info}; pub async fn dispute_action( @@ -71,18 +71,18 @@ pub async fn dispute_action( // Need to update dispute status order.update(pool).await?; } - let dispute = Dispute::new(order.id); + let dispute = Dispute::new(order_id); // Use CRUD create method - dispute.create(pool).await?; + let dispute = dispute.create(pool).await?; // We create a Message for the initiator - let message = Message::new_order(Some(order.id), None, Action::DisputeInitiatedByYou, None); + let message = Message::new_order(Some(order_id), None, Action::DisputeInitiatedByYou, None); let message = message.as_json()?; let initiator_pubkey = XOnlyPublicKey::from_bech32(message_sender)?; send_dm(client, my_keys, &initiator_pubkey, message).await?; // We create a Message for the counterpart - let message = Message::new_order(Some(order.id), None, Action::DisputeInitiatedByPeer, None); + let message = Message::new_order(Some(order_id), None, Action::DisputeInitiatedByPeer, None); let message = message.as_json()?; let counterpart_pubkey = XOnlyPublicKey::from_bech32(counterpart)?; send_dm(client, my_keys, &counterpart_pubkey, message).await?; diff --git a/src/app/take_buy.rs b/src/app/take_buy.rs index 170c2db4..dbf6a1a2 100644 --- a/src/app/take_buy.rs +++ b/src/app/take_buy.rs @@ -84,12 +84,6 @@ pub async fn take_buy_action( return Ok(()); } - // Timestamp order take time - if order.taken_at == 0 { - order.taken_at = Timestamp::now().as_i64(); - order.update(pool).await?; - } - // Check market price value in sats - if order was with market price then calculate if order.amount == 0 { order.amount = @@ -102,6 +96,11 @@ pub async fn take_buy_action( }; } + // Timestamp order take time + order.taken_at = Timestamp::now().as_i64(); + let order_id = order.id; + order.update(pool).await?; + show_hold_invoice( pool, client, @@ -109,7 +108,7 @@ pub async fn take_buy_action( None, &buyer_pubkey, &seller_pubkey, - &mut order, + order_id, ) .await?; Ok(()) diff --git a/src/app/take_sell.rs b/src/app/take_sell.rs index d801072c..bfcf6118 100644 --- a/src/app/take_sell.rs +++ b/src/app/take_sell.rs @@ -125,10 +125,9 @@ pub async fn take_sell_action( // Add buyer pubkey to order edit_buyer_pubkey_order(pool, order_id, buyer_pubkey_bech32).await?; // Timestamp take order time - if order.taken_at == 0 { - order.taken_at = Timestamp::now().as_i64(); - order.update(pool).await?; - } + order.taken_at = Timestamp::now().as_i64(); + let order_id = order.id; + let mut order = order.update(pool).await?; // Check market price value in sats - if order was with market price then calculate it and send a DM to buyer if order.amount == 0 { set_market_order_sats_amount(&mut order, buyer_pubkey, my_keys, pool, client).await?; @@ -140,7 +139,7 @@ pub async fn take_sell_action( pr, &buyer_pubkey, &seller_pubkey, - &mut order, + order_id, ) .await?; } diff --git a/src/util.rs b/src/util.rs index a5626e73..ff10f5fd 100644 --- a/src/util.rs +++ b/src/util.rs @@ -184,12 +184,11 @@ pub async fn update_user_rating_event( // We update the order vote status if buyer_sent_rate { order.buyer_sent_rate = buyer_sent_rate; - order.update(pool).await?; } if seller_sent_rate { order.seller_sent_rate = seller_sent_rate; - order.update(pool).await?; } + order.update(pool).await?; // Add event message to global list rate_list.lock().await.push(event); @@ -254,8 +253,15 @@ pub async fn show_hold_invoice( payment_request: Option, buyer_pubkey: &XOnlyPublicKey, seller_pubkey: &XOnlyPublicKey, - order: &mut Order, + order_id: Uuid, ) -> anyhow::Result<()> { + let mut order = match Order::by_id(pool, order_id).await? { + Some(order) => order, + None => { + error!("Order Id {order_id} not found!"); + return Ok(()); + } + }; let mut ln_client = lightning::LndConnector::new().await; let mostro_settings = Settings::get_mostro(); // Add fee of seller to hold invoice @@ -279,7 +285,6 @@ pub async fn show_hold_invoice( .await?; if let Some(invoice) = payment_request { order.buyer_invoice = Some(invoice); - order.update(pool).await?; }; // Using CRUD to update all fiels @@ -288,10 +293,10 @@ pub async fn show_hold_invoice( order.status = Status::WaitingPayment.to_string(); order.buyer_pubkey = Some(buyer_pubkey.to_bech32()?); order.seller_pubkey = Some(seller_pubkey.to_bech32()?); - order.update(pool).await?; + 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, None).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