From 5130eebed677673bc1c0e4b3153744781d58dbe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Calder=C3=B3n?= Date: Wed, 25 Oct 2023 18:02:55 -0300 Subject: [PATCH 1/3] Publish new dispute nip33 event --- Cargo.toml | 3 +-- migrations/20230928145530_disputes.sql | 1 + src/app/dispute.rs | 21 +++++++++++++-------- src/db.rs | 4 +++- src/util.rs | 6 +++--- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3f477e06..491a2f5b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,8 +37,7 @@ uuid = { version = "1.3.0", features = [ "serde", ] } reqwest = { version = "0.11", features = ["json"] } -mostro-core = "0.3.7" -#tokio-cron-scheduler = "*" +mostro-core = { path = "../mostro-core" } tracing = "0.1.37" tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } config = "0.13.3" diff --git a/migrations/20230928145530_disputes.sql b/migrations/20230928145530_disputes.sql index ebd01450..b4b46807 100644 --- a/migrations/20230928145530_disputes.sql +++ b/migrations/20230928145530_disputes.sql @@ -1,4 +1,5 @@ CREATE TABLE IF NOT EXISTS disputes ( + id char(36) primary key not null, order_id char(36) unique not null, status varchar(10) not null, solver_pubkey char(64), diff --git a/src/app/dispute.rs b/src/app/dispute.rs index b45cda40..6569b6bf 100644 --- a/src/app/dispute.rs +++ b/src/app/dispute.rs @@ -1,9 +1,11 @@ use crate::db::{add_dispute, update_order_buyer_dispute, update_order_seller_dispute}; +use crate::nip33::new_event; use crate::util::send_dm; use anyhow::Result; use log::error; -use mostro_core::dispute::{Dispute, Status}; +use log::info; +use mostro_core::dispute::Dispute; use mostro_core::order::Order; use mostro_core::{Action, Message}; use nostr_sdk::prelude::*; @@ -65,13 +67,7 @@ pub async fn dispute_action( if !update_buyer_dispute && !update_seller_dispute { return Ok(()); }; - let dispute = Dispute { - order_id, - status: Status::Pending, - solver_pubkey: None, - created_at: 0, - taken_at: 0, - }; + let dispute = Dispute::new(order.id); add_dispute(&dispute, pool).await?; // We create a Message for the initiator @@ -91,6 +87,15 @@ pub async fn dispute_action( let message = message.as_json()?; let counterpart_pubkey = XOnlyPublicKey::from_bech32(counterpart)?; send_dm(client, my_keys, &counterpart_pubkey, message).await?; + // nip33 kind with dispute id as identifier + let event = new_event( + my_keys, + "".to_string(), + dispute.id.to_string(), + ([]).to_vec(), + )?; + info!("Dispute event to be published: {event:#?}"); + client.send_event(event).await?; Ok(()) } diff --git a/src/db.rs b/src/db.rs index 26a10a71..0978f5f8 100644 --- a/src/db.rs +++ b/src/db.rs @@ -26,15 +26,17 @@ pub async fn add_dispute(dispute: &Dispute, pool: &SqlitePool) -> anyhow::Result let dispute = sqlx::query_as::<_, Dispute>( r#" INSERT INTO disputes ( + id, order_id, status, solver_pubkey, created_at, taken_at - ) VALUES (?1, ?2, ?3, ?4, ?5) + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6) RETURNING * "#, ) + .bind(dispute.id) .bind(dispute.order_id) .bind(&dispute.status.to_string()) .bind(&dispute.solver_pubkey) diff --git a/src/util.rs b/src/util.rs index b81da4ab..1fcd4a27 100644 --- a/src/util.rs +++ b/src/util.rs @@ -105,13 +105,13 @@ pub async fn publish_order( None, None, None, - Some(order.created_at), + order.created_at, ); let order_string = order.as_json().unwrap(); info!("serialized order: {order_string}"); // nip33 kind with order fields as tags and order id as identifier let event = new_event(keys, order_string, order_id.to_string(), tags)?; - info!("Event to be published: {event:#?}"); + info!("Order event to be published: {event:#?}"); let event_id = event.id.to_string(); info!("Publishing Event Id: {event_id} for Order Id: {order_id}"); // We update the order id with the new event_id @@ -217,7 +217,7 @@ pub async fn update_order_event( None, None, None, - Some(order.created_at), + order.created_at, ); let order_content = publish_order.as_json()?; let mut order = order.clone(); From e64de1742c6b0d0f9db856cd0abb7a3a9f027d04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Calder=C3=B3n?= Date: Wed, 25 Oct 2023 18:08:35 -0300 Subject: [PATCH 2/3] update mostro core version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 491a2f5b..26f053a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,7 @@ uuid = { version = "1.3.0", features = [ "serde", ] } reqwest = { version = "0.11", features = ["json"] } -mostro-core = { path = "../mostro-core" } +mostro-core = "0.3.9" tracing = "0.1.37" tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } config = "0.13.3" From 865701e366aac659c74b57fdedb4ff7887b64cc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Calder=C3=B3n?= Date: Thu, 26 Oct 2023 10:11:53 -0300 Subject: [PATCH 3/3] Fix test & set correct timestamp on publish order --- src/main.rs | 2 +- src/util.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index c4300ef0..2ec8c06f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,7 +72,7 @@ mod tests { #[test] fn test_order_deserialize_serialize() { - let sample_order = r#"{"kind":"Sell","status":"Pending","amount":100,"fiat_code":"XXX","fiat_amount":10,"payment_method":"belo","premium":1}"#; + let sample_order = r#"{"kind":"Sell","status":"Pending","amount":100,"fiat_code":"XXX","fiat_amount":10,"payment_method":"belo","premium":1,"created_at":0}"#; let order = NewOrder::from_json(sample_order).unwrap(); let json_order = order.as_json().unwrap(); assert_eq!(sample_order, json_order); diff --git a/src/util.rs b/src/util.rs index 1fcd4a27..2456e76d 100644 --- a/src/util.rs +++ b/src/util.rs @@ -12,6 +12,7 @@ use log::{error, info}; use mostro_core::order::{Kind as OrderKind, NewOrder, Order, SmallOrder, Status}; use mostro_core::{Action, Content, Message}; use nostr_sdk::prelude::*; +use sqlx::types::chrono::Utc; use sqlx::SqlitePool; use sqlx::{Pool, Sqlite}; use std::str::FromStr; @@ -105,7 +106,7 @@ pub async fn publish_order( None, None, None, - order.created_at, + Utc::now().timestamp(), ); let order_string = order.as_json().unwrap(); info!("serialized order: {order_string}");