Skip to content

Commit

Permalink
Dispute fixes
Browse files Browse the repository at this point in the history
* Updated mostro core
* Refactoring dispute code
* Insert disputes
  • Loading branch information
grunch committed Oct 2, 2023
1 parent 6be0447 commit eb6a38e
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
6 changes: 3 additions & 3 deletions migrations/20221222153301_orders.sql
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -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,
Expand All @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions migrations/20230928145530_disputes.sql
Original file line number Diff line number Diff line change
@@ -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
);
4 changes: 2 additions & 2 deletions src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/app/admin_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/app/admin_settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/app/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion src/app/dispute.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -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(())
}
4 changes: 2 additions & 2 deletions src/app/fiat_sent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/app/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/app/take_buy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/app/take_sell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 27 additions & 2 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,6 +21,31 @@ pub async fn connect() -> Result<Pool<Sqlite>, sqlx::Error> {
Ok(pool)
}

pub async fn add_dispute(dispute: &Dispute, pool: &SqlitePool) -> anyhow::Result<Dispute> {
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,
Expand Down
3 changes: 2 additions & 1 deletion src/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,7 +45,7 @@ pub async fn cron_scheduler(
&pool,
&client,
&keys,
mostro_core::Status::Expired,
Status::Expired,
order,
None,
)
Expand Down
6 changes: 2 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit eb6a38e

Please sign in to comment.