diff --git a/Cargo.toml b/Cargo.toml index e886b40..b718fdc 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 = "0.2.8" +mostro-core = "0.3.0" tokio-cron-scheduler = "*" tracing = "0.1.37" tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } diff --git a/migrations/20221222153301_orders.sql b/migrations/20221222153301_orders.sql index 66d571b..1c68431 100644 --- a/migrations/20221222153301_orders.sql +++ b/migrations/20221222153301_orders.sql @@ -1,5 +1,5 @@ CREATE TABLE IF NOT EXISTS orders ( - id varchar(36) primary key not null, + id char(36) primary key not null, kind varchar(4) not null, event_id char(64) not null, hash char(64), @@ -11,7 +11,7 @@ CREATE TABLE IF NOT EXISTS orders ( master_buyer_pubkey char(64), seller_pubkey char(64), master_seller_pubkey char(64), - status char(10) not null, + status varchar(10) not null, price_from_api integer not null default 0, premium integer not null, payment_method varchar(500) not null, @@ -27,7 +27,7 @@ CREATE TABLE IF NOT EXISTS orders ( fiat_code varchar(5) not null, fiat_amount integer not null, buyer_invoice text, - range_parent_id varchar(36), + range_parent_id char(36), invoice_held_at integer default 0, taken_at integer default 0, created_at integer not null, diff --git a/migrations/20230928145530_disputes.sql b/migrations/20230928145530_disputes.sql new file mode 100644 index 0000000..ebd0145 --- /dev/null +++ b/migrations/20230928145530_disputes.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS disputes ( + order_id char(36) unique not null, + status varchar(10) not null, + solver_pubkey char(64), + created_at integer not null, + taken_at integer default 0 +); \ No newline at end of file diff --git a/src/app/add_invoice.rs b/src/app/add_invoice.rs index 140b743..8e3b42c 100644 --- a/src/app/add_invoice.rs +++ b/src/app/add_invoice.rs @@ -5,8 +5,8 @@ use crate::util::{send_dm, show_hold_invoice}; use anyhow::Result; use log::error; -use mostro_core::order::Order; -use mostro_core::{order::SmallOrder, Action, Content, Message, Status}; +use mostro_core::order::{Order, Status}; +use mostro_core::{order::SmallOrder, Action, Content, Message}; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; diff --git a/src/app/admin_cancel.rs b/src/app/admin_cancel.rs index d516f00..094b004 100644 --- a/src/app/admin_cancel.rs +++ b/src/app/admin_cancel.rs @@ -3,8 +3,8 @@ use crate::util::{send_dm, update_order_event}; use anyhow::Result; use log::{error, info}; -use mostro_core::order::Order; -use mostro_core::{Action, Message, Status}; +use mostro_core::order::{Order, Status}; +use mostro_core::{Action, Message}; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; diff --git a/src/app/admin_settle.rs b/src/app/admin_settle.rs index df98137..d9ba870 100644 --- a/src/app/admin_settle.rs +++ b/src/app/admin_settle.rs @@ -3,8 +3,8 @@ use crate::util::{send_dm, settle_seller_hold_invoice}; use anyhow::Result; use log::error; -use mostro_core::order::Order; -use mostro_core::{Action, Message, Status}; +use mostro_core::order::{Order, Status}; +use mostro_core::{Action, Message}; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; diff --git a/src/app/cancel.rs b/src/app/cancel.rs index 994fcb1..a823ed2 100644 --- a/src/app/cancel.rs +++ b/src/app/cancel.rs @@ -6,8 +6,8 @@ use crate::lightning::LndConnector; use crate::util::{send_dm, update_order_event}; use anyhow::Result; use log::{error, info}; -use mostro_core::order::Order; -use mostro_core::{Action, Message, Status}; +use mostro_core::order::{Order, Status}; +use mostro_core::{Action, Message}; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; diff --git a/src/app/dispute.rs b/src/app/dispute.rs index 2d432cd..037da45 100644 --- a/src/app/dispute.rs +++ b/src/app/dispute.rs @@ -1,8 +1,9 @@ -use crate::db::{update_order_buyer_dispute, update_order_seller_dispute}; +use crate::db::{add_dispute, update_order_buyer_dispute, update_order_seller_dispute}; use crate::util::send_dm; use anyhow::Result; use log::error; +use mostro_core::dispute::{Dispute, Status}; use mostro_core::order::Order; use mostro_core::{Action, Message}; use nostr_sdk::prelude::*; @@ -64,6 +65,14 @@ 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, + }; + add_dispute(&dispute, &pool).await?; Ok(()) } diff --git a/src/app/fiat_sent.rs b/src/app/fiat_sent.rs index afd3818..3dc7eb7 100644 --- a/src/app/fiat_sent.rs +++ b/src/app/fiat_sent.rs @@ -2,8 +2,8 @@ use crate::util::{send_dm, update_order_event}; use anyhow::Result; use log::error; -use mostro_core::order::Order; -use mostro_core::{Action, Content, Message, Peer, Status}; +use mostro_core::order::{Order, Status}; +use mostro_core::{Action, Content, Message, Peer}; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; diff --git a/src/app/release.rs b/src/app/release.rs index 2aff1af..1caeb4d 100644 --- a/src/app/release.rs +++ b/src/app/release.rs @@ -5,8 +5,8 @@ use crate::util::{rate_counterpart, send_dm, update_order_event}; use anyhow::Result; use log::{error, info}; -use mostro_core::order::Order; -use mostro_core::{Action, Message, Status}; +use mostro_core::order::{Order, Status}; +use mostro_core::{Action, Message}; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; diff --git a/src/app/take_buy.rs b/src/app/take_buy.rs index 737d1b6..906d4f0 100644 --- a/src/app/take_buy.rs +++ b/src/app/take_buy.rs @@ -3,8 +3,8 @@ use crate::util::{get_market_quote, send_dm, show_hold_invoice}; use anyhow::Result; use log::error; -use mostro_core::order::Order; -use mostro_core::{Action, Content, Message, Status}; +use mostro_core::order::{Order, Status}; +use mostro_core::{Action, Content, Message}; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; diff --git a/src/app/take_sell.rs b/src/app/take_sell.rs index 1cafa1f..6e6c2e7 100644 --- a/src/app/take_sell.rs +++ b/src/app/take_sell.rs @@ -5,8 +5,8 @@ use crate::util::{send_dm, set_market_order_sats_amount, show_hold_invoice}; use anyhow::Result; use log::error; -use mostro_core::order::Order; -use mostro_core::{Action, Content, Message, Status}; +use mostro_core::order::{Order, Status}; +use mostro_core::{Action, Content, Message}; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; diff --git a/src/db.rs b/src/db.rs index 304eb82..26a10a7 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,5 +1,5 @@ -use mostro_core::order::{NewOrder, Order}; -use mostro_core::{Kind, Status}; +use mostro_core::dispute::Dispute; +use mostro_core::order::{Kind, NewOrder, Order, Status}; use nostr_sdk::prelude::*; use sqlx::migrate::MigrateDatabase; use sqlx::pool::Pool; @@ -21,6 +21,31 @@ pub async fn connect() -> Result, sqlx::Error> { Ok(pool) } +pub async fn add_dispute(dispute: &Dispute, pool: &SqlitePool) -> anyhow::Result { + let mut conn = pool.acquire().await?; + let dispute = sqlx::query_as::<_, Dispute>( + r#" + INSERT INTO disputes ( + order_id, + status, + solver_pubkey, + created_at, + taken_at + ) VALUES (?1, ?2, ?3, ?4, ?5) + RETURNING * + "#, + ) + .bind(dispute.order_id) + .bind(&dispute.status.to_string()) + .bind(&dispute.solver_pubkey) + .bind(dispute.created_at) + .bind(dispute.taken_at) + .fetch_one(&mut conn) + .await?; + + Ok(dispute) +} + pub async fn add_order( pool: &SqlitePool, order: &NewOrder, diff --git a/src/flow.rs b/src/flow.rs index 8393070..2b5fd47 100644 --- a/src/flow.rs +++ b/src/flow.rs @@ -2,7 +2,8 @@ use crate::cli::settings::Settings; use crate::util::send_dm; use log::info; -use mostro_core::{order::SmallOrder, Action, Content, Message, Status}; +use mostro_core::order::Status; +use mostro_core::{order::SmallOrder, Action, Content, Message}; use nostr_sdk::prelude::*; pub async fn hold_invoice_paid(hash: &str) { diff --git a/src/scheduler.rs b/src/scheduler.rs index 85ab268..90374af 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -4,7 +4,7 @@ use crate::lightning::LndConnector; use crate::util::update_order_event; use anyhow::Result; -use mostro_core::Status; +use mostro_core::order::Status; use nostr_sdk::Event; use std::error::Error; use std::sync::Arc; @@ -45,7 +45,7 @@ pub async fn cron_scheduler( &pool, &client, &keys, - mostro_core::Status::Expired, + Status::Expired, order, None, ) diff --git a/src/util.rs b/src/util.rs index 8d294d2..2a1a6a6 100644 --- a/src/util.rs +++ b/src/util.rs @@ -8,10 +8,8 @@ use crate::{db, flow}; use anyhow::{Context, Result}; use log::{error, info}; -use mostro_core::order::{NewOrder, Order, SmallOrder}; -use mostro_core::{ - Action, Content, Kind as OrderKind, Message, Status, NOSTR_REPLACEABLE_EVENT_KIND, -}; +use mostro_core::order::{Kind as OrderKind, NewOrder, Order, SmallOrder, Status}; +use mostro_core::{Action, Content, Message, NOSTR_REPLACEABLE_EVENT_KIND}; use nostr_sdk::prelude::*; use sqlx::SqlitePool; use sqlx::{Pool, Sqlite};