From ddfcfaf6580420be399f3c81eee6b513e61e11e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Calder=C3=B3n?= Date: Thu, 5 Oct 2023 16:27:08 -0300 Subject: [PATCH] New function to get order fields as tags Fix bugs on nip33 order creation --- src/nip33.rs | 56 ++++++++++++++++++++++++++++++---------------------- src/util.rs | 27 +++++++++++-------------- 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/nip33.rs b/src/nip33.rs index 905a19e6..bd9a97ed 100644 --- a/src/nip33.rs +++ b/src/nip33.rs @@ -1,3 +1,4 @@ +use mostro_core::order::Order; use mostro_core::NOSTR_REPLACEABLE_EVENT_KIND; use nostr::event::builder::Error; use nostr_sdk::prelude::*; @@ -8,38 +9,45 @@ use nostr_sdk::prelude::*; /// /// * `keys` - The keys used to sign the event /// * `content` - The content of the event -/// * `d_str` - The nip33 d tag used to replaced the event with a new one -/// * `k_str` - The nip33 k tag used to subscribe to sell/buy order type notifications -/// * `f_str` - The nip33 f tag used to subscribe to fiat currency code type notifications -/// * `s_str` - The nip33 s tag used to subscribe to order status type notifications +/// * `identifier` - The nip33 d tag used to replaced the event with a new one +/// * `extra_tags` - The nip33 other tags used to subscribe order type notifications +/// +/// # Returns +/// Returns a new event +/// pub fn new_event( keys: &Keys, content: String, - d_str: String, - k_str: Option, - f_str: Option, - s_str: Option, + identifier: String, + extra_tags: Vec<(String, String)>, ) -> Result { // This tag (nip33) allows us to change this event in particular in the future - let d_tag = Tag::Generic(TagKind::Custom("d".to_string()), vec![d_str]); + let d_tag = Tag::Generic(TagKind::Custom("d".to_string()), vec![identifier]); let mut tags = vec![d_tag]; - if let Some(k) = k_str { - // This tag helps client to subscribe to sell/buy order type notifications - let k_tag = Tag::Generic(TagKind::Custom("k".to_string()), vec![k]); - tags.push(k_tag); + for tag in extra_tags { + let tag = Tag::Generic(TagKind::Custom(tag.0), vec![tag.1]); + tags.push(tag); } - if let Some(f) = f_str { - // This tag helps client to subscribe to fiat(shit) coin name - let f_tag = Tag::Generic(TagKind::Custom("f".to_string()), vec![f]); - tags.push(f_tag); - } + EventBuilder::new(Kind::Custom(NOSTR_REPLACEABLE_EVENT_KIND), content, &tags).to_event(keys) +} - if let Some(s) = s_str { - // This tag helps client to subscribe to order status - let s_tag = Tag::Generic(TagKind::Custom("s".to_string()), vec![s]); - tags.push(s_tag); - } +/// Transform an order fields to tags +/// +/// # Arguments +/// +/// * `order` - The order to transform +/// +pub fn order_to_tags(order: &Order) -> Vec<(String, String)> { + let tags = vec![ + ("k".to_string(), order.kind.to_string()), + ("f".to_string(), order.fiat_code.to_string()), + ("s".to_string(), order.status.to_string()), + ("amt".to_string(), order.amount.to_string()), + ("fa".to_string(), order.fiat_amount.to_string()), + ("pm".to_string(), order.payment_method.to_string()), + ("premium".to_string(), order.premium.to_string()), + ]; - EventBuilder::new(Kind::Custom(NOSTR_REPLACEABLE_EVENT_KIND), content, &tags).to_event(keys) + tags } diff --git a/src/util.rs b/src/util.rs index 2fdf3dac..87e1f008 100644 --- a/src/util.rs +++ b/src/util.rs @@ -4,7 +4,7 @@ use crate::lightning; use crate::lightning::LndConnector; use crate::messages; use crate::models::Yadio; -use crate::nip33::new_event; +use crate::nip33::{new_event, order_to_tags}; use crate::{db, flow}; use anyhow::{Context, Result}; @@ -90,6 +90,8 @@ pub async fn publish_order( let order = crate::db::add_order(pool, new_order, "", initiator_pubkey, master_pubkey).await?; let order_id = order.id; info!("New order saved Id: {}", order_id); + // 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( Some(order_id), @@ -105,18 +107,10 @@ pub async fn publish_order( None, Some(order.created_at), ); - let order_string = order.as_json().unwrap(); info!("serialized order: {order_string}"); - // nip33 kind with d, k, f, s tags - let event = new_event( - keys, - order_string, - order_id.to_string(), - Some(order.kind.to_string()), - Some(order.fiat_code.clone()), - Some(order.status.to_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:#?}"); let event_id = event.id.to_string(); info!("Publishing Event Id: {event_id} for Order Id: {order_id}"); @@ -184,8 +178,8 @@ pub async fn update_user_rating_event( pool: &SqlitePool, rate_list: Arc>>, ) -> Result<()> { - // nip33 kind and d tag - let event = new_event(keys, reputation, user.to_string(), None, None, None)?; + // nip33 kind with user as identifier + let event = new_event(keys, reputation, user.to_string(), vec![])?; info!("Sending replaceable event: {event:#?}"); // We update the order vote status if buyer_sent_rate { @@ -226,9 +220,10 @@ pub async fn update_order_event( Some(order.created_at), ); let order_content = publish_order.as_json()?; - // nip33 kind and d tag - // FIXME: check if we need to send k, f and s tags here too - let event = new_event(keys, order_content, order.id.to_string(), None, None, None)?; + // We transform the order fields to tags to use in the event + let tags = order_to_tags(order); + // nip33 kind with order id as identifier and order fields as tags + let event = new_event(keys, order_content, order.id.to_string(), tags)?; let event_id = event.id.to_string(); let status_str = status.to_string(); info!("Sending replaceable event: {event:#?}");