From 843f318bce05bd6f3f8d7190a5280dc96ba689ce Mon Sep 17 00:00:00 2001 From: arkanoider Date: Thu, 30 Nov 2023 16:47:39 +0100 Subject: [PATCH] First draft with only smallorder --- src/app/add_invoice.rs | 10 +++++++--- src/db.rs | 10 +++++----- src/flow.rs | 12 ++++++++---- src/main.rs | 4 ++-- src/util.rs | 32 ++++++++++++++++++-------------- 5 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/app/add_invoice.rs b/src/app/add_invoice.rs index 7fa425ea..1746ada8 100644 --- a/src/app/add_invoice.rs +++ b/src/app/add_invoice.rs @@ -113,7 +113,9 @@ pub async fn add_invoice_action( if order.preimage.is_some() { // We send this data related to the order to the parties let order_data = SmallOrder::new( - order.id, + Some(order.id), + None, + None, order.amount, order.fiat_code.clone(), order.fiat_amount, @@ -121,13 +123,15 @@ pub async fn add_invoice_action( order.premium, order.buyer_pubkey.as_ref().cloned(), order.seller_pubkey.as_ref().cloned(), + None, + None, ); // We send a confirmation message to seller let message = Message::new_order( Some(order.id), None, Action::BuyerTookOrder, - Some(Content::SmallOrder(order_data.clone())), + Some(Content::Order(order_data.clone())), ); let message = message.as_json().unwrap(); @@ -137,7 +141,7 @@ pub async fn add_invoice_action( Some(order.id), None, Action::HoldInvoicePaymentAccepted, - Some(Content::SmallOrder(order_data)), + Some(Content::Order(order_data)), ); let message = message.as_json().unwrap(); send_dm(client, my_keys, &buyer_pubkey, message) diff --git a/src/db.rs b/src/db.rs index f8939ebc..8041b9c5 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,5 +1,5 @@ use mostro_core::dispute::{Dispute, Status as DisputeStatus}; -use mostro_core::order::{Kind, NewOrder, Order, Status}; +use mostro_core::order::{Kind, Order, SmallOrder, Status}; use mostro_core::user::User; use nostr_sdk::prelude::*; use sqlx::migrate::MigrateDatabase; @@ -80,7 +80,7 @@ pub async fn add_dispute(dispute: &Dispute, pool: &SqlitePool) -> anyhow::Result pub async fn add_order( pool: &SqlitePool, - order: &NewOrder, + order: &SmallOrder, event_id: &str, initiator_pubkey: &str, master_pubkey: &str, @@ -92,15 +92,15 @@ pub async fn add_order( let mut seller_pubkey: Option = None; let mut master_seller_pubkey: Option = None; let created_at = Timestamp::now(); - if order.kind == Kind::Buy { + if order.kind == Some(Kind::Buy) { buyer_pubkey = Some(initiator_pubkey.to_string()); master_buyer_pubkey = Some(master_pubkey.to_string()); } else { seller_pubkey = Some(initiator_pubkey.to_string()); master_seller_pubkey = Some(master_pubkey.to_string()); } - let kind = order.kind.to_string(); - let status = order.status.to_string(); + let kind = order.kind.unwrap().to_string(); + let status = order.status.unwrap().to_string(); let price_from_api = order.amount == 0; let order = sqlx::query_as::<_, Order>( diff --git a/src/flow.rs b/src/flow.rs index 16e894d1..7f661de4 100644 --- a/src/flow.rs +++ b/src/flow.rs @@ -28,7 +28,9 @@ pub async fn hold_invoice_paid(hash: &str) { // We send this data related to the order to the parties let mut order_data = SmallOrder::new( - order.id, + Some(order.id), + None, + None, order.amount, order.fiat_code.clone(), order.fiat_amount, @@ -36,6 +38,8 @@ pub async fn hold_invoice_paid(hash: &str) { order.premium, master_buyer_pubkey, master_seller_pubkey, + None, + None, ); let status; @@ -45,7 +49,7 @@ pub async fn hold_invoice_paid(hash: &str) { Some(order.id), None, Action::BuyerTookOrder, - Some(Content::SmallOrder(order_data.clone())), + Some(Content::Order(order_data.clone())), ); let message = message.as_json().unwrap(); send_dm(&client, &my_keys, &seller_pubkey, message) @@ -56,7 +60,7 @@ pub async fn hold_invoice_paid(hash: &str) { Some(order.id), None, Action::HoldInvoicePaymentAccepted, - Some(Content::SmallOrder(order_data)), + Some(Content::Order(order_data)), ); let message = message.as_json().unwrap(); send_dm(&client, &my_keys, &buyer_pubkey, message) @@ -75,7 +79,7 @@ pub async fn hold_invoice_paid(hash: &str) { Some(order.id), None, Action::AddInvoice, - Some(Content::SmallOrder(order_data.clone())), + Some(Content::Order(order_data.clone())), ); let message = message.as_json().unwrap(); send_dm(&client, &my_keys, &buyer_pubkey, message) diff --git a/src/main.rs b/src/main.rs index 10b40349..7162d79b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,13 +65,13 @@ async fn main() -> Result<()> { #[cfg(test)] mod tests { use mostro_core::message::Message; - use mostro_core::order::NewOrder; + use mostro_core::order::SmallOrder; use std::time::{SystemTime, UNIX_EPOCH}; #[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,"created_at":0}"#; - let order = NewOrder::from_json(sample_order).unwrap(); + let order = SmallOrder::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 62924b4f..6517e952 100644 --- a/src/util.rs +++ b/src/util.rs @@ -10,7 +10,7 @@ use crate::{db, flow}; use anyhow::{Context, Result}; use log::{error, info}; use mostro_core::message::{Action, Content, Message}; -use mostro_core::order::{Kind as OrderKind, NewOrder, Order, SmallOrder, Status}; +use mostro_core::order::{Kind as OrderKind, Order, SmallOrder, Status}; use nostr_sdk::prelude::*; use sqlx::types::chrono::Utc; use sqlx::SqlitePool; @@ -88,7 +88,7 @@ pub async fn publish_order( pool: &SqlitePool, client: &Client, keys: &Keys, - new_order: &NewOrder, + new_order: &SmallOrder, initiator_pubkey: &str, master_pubkey: &str, ack_pubkey: XOnlyPublicKey, @@ -99,10 +99,10 @@ pub async fn publish_order( // We transform the order fields to tags to use in the event let tags = order_to_tags(&order); // Now we have the order id, we can create a new event adding this id to the Order object - let order = NewOrder::new( + let order = SmallOrder::new( Some(order_id), - OrderKind::from_str(&order.kind).unwrap(), - Status::Pending, + Some(OrderKind::from_str(&order.kind).unwrap()), + Some(Status::Pending), order.amount, order.fiat_code, order.fiat_amount, @@ -111,7 +111,7 @@ pub async fn publish_order( None, None, None, - Utc::now().timestamp(), + Some(Utc::now().timestamp()), ); let order_string = order.as_json().unwrap(); info!("serialized order: {order_string}"); @@ -210,10 +210,10 @@ pub async fn update_order_event( ) -> Result<()> { let kind = OrderKind::from_str(&order.kind).unwrap(); let amount = amount.unwrap_or(order.amount); - let publish_order = NewOrder::new( + let publish_order = SmallOrder::new( Some(order.id), - kind, - status, + Some(kind), + Some(status), amount, order.fiat_code.to_owned(), order.fiat_amount, @@ -222,7 +222,7 @@ pub async fn update_order_event( None, None, None, - order.created_at, + Some(order.created_at), ); let order_content = publish_order.as_json()?; let mut order = order.clone(); @@ -315,7 +315,7 @@ pub async fn show_hold_invoice( // We need to publish a new event with the new status update_order_event(pool, client, my_keys, Status::WaitingPayment, order, None).await?; let mut new_order = order.as_new_order(); - new_order.status = Status::WaitingPayment; + new_order.status = Some(Status::WaitingPayment); // We create a Message to send the hold invoice to seller let message = Message::new_order( Some(order.id), @@ -393,7 +393,9 @@ pub async fn set_market_order_sats_amount( // We send this data related to the buyer let order_data = SmallOrder::new( - order.id, + Some(order.id), + None, + None, buyer_final_amount, order.fiat_code.clone(), order.fiat_amount, @@ -401,13 +403,15 @@ pub async fn set_market_order_sats_amount( order.premium, None, None, + None, + None, ); // We create a Message let message = Message::new_order( Some(order.id), None, Action::AddInvoice, - Some(Content::SmallOrder(order_data)), + Some(Content::Order(order_data)), ); let message = message.as_json()?; @@ -434,7 +438,7 @@ pub async fn rate_counterpart( buyer_pubkey: &XOnlyPublicKey, seller_pubkey: &XOnlyPublicKey, my_keys: &Keys, - order: NewOrder, + order: SmallOrder, ) -> Result<()> { // Send dm to counterparts // to buyer