Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New function to get order fields as tags #124

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 32 additions & 24 deletions src/nip33.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -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<String>,
f_str: Option<String>,
s_str: Option<String>,
identifier: String,
extra_tags: Vec<(String, String)>,
) -> Result<Event, Error> {
// 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
}
27 changes: 11 additions & 16 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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),
Expand All @@ -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}");
Expand Down Expand Up @@ -184,8 +178,8 @@ pub async fn update_user_rating_event(
pool: &SqlitePool,
rate_list: Arc<Mutex<Vec<Event>>>,
) -> 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 {
Expand Down Expand Up @@ -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:#?}");
Expand Down