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

Publish dispute event #129

Merged
merged 3 commits into from
Oct 26, 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
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "0.3.9"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
config = "0.13.3"
Expand Down
1 change: 1 addition & 0 deletions migrations/20230928145530_disputes.sql
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
21 changes: 13 additions & 8 deletions src/app/dispute.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -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
Expand All @@ -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(())
}
4 changes: 3 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,13 +106,13 @@ pub async fn publish_order(
None,
None,
None,
Some(order.created_at),
Utc::now().timestamp(),
);
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
Expand Down Expand Up @@ -217,7 +218,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();
Expand Down