Skip to content

Commit

Permalink
additions to have tokens for dispute (#346)
Browse files Browse the repository at this point in the history
* additions to have tokens for dispute

* bumped mostro-core ver in cargo.toml

* additions to save to db tokens of dispute

* removed overwrite from toml

* 3 digit token 100-999
  • Loading branch information
arkanoider authored Sep 2, 2024
1 parent 29e6945 commit 41f182c
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 27 deletions.
4 changes: 1 addition & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ uuid = { version = "1.8.0", features = [
"serde",
] }
reqwest = { version = "0.12.1", features = ["json"] }
mostro-core = { version = "0.6.5", features = ["sqlx"] }
mostro-core = { version = "0.6.6", features = ["sqlx"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
config = "0.14.0"
Expand Down
4 changes: 3 additions & 1 deletion migrations/20230928145530_disputes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ CREATE TABLE IF NOT EXISTS disputes (
status varchar(10) not null,
solver_pubkey char(64),
created_at integer not null,
taken_at integer default 0
taken_at integer default 0,
buyer_token integer not null,
seller_token integer not null
);
13 changes: 11 additions & 2 deletions src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ pub async fn add_invoice_action(
{
Ok(_) => payment_request,
Err(_) => {
send_new_order_msg(None, Action::IncorrectInvoiceAmount, None, &event.pubkey).await;
send_new_order_msg(None, Action::IncorrectInvoiceAmount, None, &event.pubkey)
.await;
return Ok(());
}
}
Expand All @@ -93,7 +94,13 @@ pub async fn add_invoice_action(
return Ok(());
}
_ => {
send_new_order_msg(Some(order.id), Action::NotAllowedByStatus, None, &event.pubkey).await;
send_new_order_msg(
Some(order.id),
Action::NotAllowedByStatus,
None,
&event.pubkey,
)
.await;
return Ok(());
}
}
Expand Down Expand Up @@ -121,6 +128,8 @@ pub async fn add_invoice_action(
None,
None,
None,
None,
None,
);
// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
Expand Down
16 changes: 14 additions & 2 deletions src/app/admin_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ pub async fn admin_cancel_action(

match is_assigned_solver(pool, &event.pubkey.to_string(), order_id).await {
Ok(false) => {
send_new_order_msg(Some(order_id), Action::IsNotYourDispute, None, &event.pubkey).await;
send_new_order_msg(
Some(order_id),
Action::IsNotYourDispute,
None,
&event.pubkey,
)
.await;
return Ok(());
}
Err(e) => {
Expand Down Expand Up @@ -63,7 +69,13 @@ pub async fn admin_cancel_action(
}

if order.status != Status::Dispute.to_string() {
send_new_order_msg(Some(order.id), Action::NotAllowedByStatus, None, &event.pubkey).await;
send_new_order_msg(
Some(order.id),
Action::NotAllowedByStatus,
None,
&event.pubkey,
)
.await;
return Ok(());
}

Expand Down
16 changes: 14 additions & 2 deletions src/app/admin_settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ pub async fn admin_settle_action(

match is_assigned_solver(pool, &event.pubkey.to_string(), order_id).await {
Ok(false) => {
send_new_order_msg(Some(order_id), Action::IsNotYourDispute, None, &event.pubkey).await;
send_new_order_msg(
Some(order_id),
Action::IsNotYourDispute,
None,
&event.pubkey,
)
.await;
return Ok(());
}
Err(e) => {
Expand Down Expand Up @@ -64,7 +70,13 @@ pub async fn admin_settle_action(
}

if order.status != Status::Dispute.to_string() {
send_new_order_msg(Some(order.id), Action::NotAllowedByStatus, None, &event.pubkey).await;
send_new_order_msg(
Some(order.id),
Action::NotAllowedByStatus,
None,
&event.pubkey,
)
.await;
return Ok(());
}

Expand Down
21 changes: 16 additions & 5 deletions src/app/admin_take_dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,14 @@ pub async fn admin_take_dispute_action(
dispute.status = Status::InProgress.to_string();
dispute.solver_pubkey = Some(event.pubkey.to_string());
dispute.taken_at = Timestamp::now().as_u64() as i64;

info!("Dispute {} taken by {}", dispute_id, event.pubkey);
// Assign token for admin message
new_order.seller_token = dispute.seller_token;
new_order.buyer_token = dispute.buyer_token;
// Save it to DB
dispute.update(pool).await?;
info!("Dispute {} taken by {}", dispute_id, event.pubkey);

// We create a Message for admin
let message = Message::new_dispute(
Some(dispute_id),
Expand All @@ -102,7 +107,14 @@ pub async fn admin_take_dispute_action(
// Now we create a message to both parties of the order
// to them know who will assist them on the dispute
let solver_pubkey = Peer::new(event.pubkey.to_hex());
let message = Message::new_order(
let msg_to_buyer = Message::new_order(
Some(order.id),
None,
Action::AdminTookDispute,
Some(Content::Peer(solver_pubkey.clone())),
);

let msg_to_seller = Message::new_order(
Some(order.id),
None,
Action::AdminTookDispute,
Expand All @@ -118,9 +130,8 @@ pub async fn admin_take_dispute_action(
(_, None) => return Err(Error::msg("Missing buyer pubkey")),
};

let message = message.as_json()?;
send_dm(&buyer_pubkey, message.clone()).await?;
send_dm(&seller_pubkey, message).await?;
send_dm(&buyer_pubkey, msg_to_buyer.as_json()?).await?;
send_dm(&seller_pubkey, msg_to_seller.as_json()?).await?;
// We create a tag to show status of the dispute
let tags: Vec<Tag> = vec![
Tag::custom(
Expand Down
19 changes: 16 additions & 3 deletions src/app/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use mostro_core::dispute::Dispute;
use mostro_core::message::{Action, Content, Message};
use mostro_core::order::{Order, Status};
use nostr_sdk::prelude::*;
use rand::Rng;
use sqlx::{Pool, Sqlite};
use sqlx_crud::traits::Crud;
use tracing::{error, info};
Expand Down Expand Up @@ -105,7 +106,18 @@ pub async fn dispute_action(
// Need to update dispute status
order.update(pool).await?;
}
let dispute = Dispute::new(order_id);

let mut dispute = Dispute::new(order_id);
// Generate tokens for the users to avoid fake resolver
let mut rng = rand::thread_rng();
dispute.buyer_token = Some(rng.gen_range(100..=999));
dispute.seller_token = Some(rng.gen_range(100..=999));

let (initiator_token, counterpart_token) = match seller_dispute {
true => (dispute.seller_token, dispute.buyer_token),
false => (dispute.buyer_token, dispute.seller_token),
};

// Use CRUD create method
let dispute = dispute.create(pool).await?;

Expand All @@ -117,10 +129,11 @@ pub async fn dispute_action(
return Ok(());
}
};

send_new_order_msg(
Some(order_id),
Action::DisputeInitiatedByYou,
Some(Content::Dispute(dispute.clone().id)),
Some(Content::Dispute(dispute.clone().id, initiator_token)),
&initiator_pubkey,
)
.await;
Expand All @@ -136,7 +149,7 @@ pub async fn dispute_action(
send_new_order_msg(
Some(order_id),
Action::DisputeInitiatedByPeer,
Some(Content::Dispute(dispute.clone().id)),
Some(Content::Dispute(dispute.clone().id, counterpart_token)),
&counterpart_pubkey,
)
.await;
Expand Down
8 changes: 7 additions & 1 deletion src/app/fiat_sent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ pub async fn fiat_sent_action(
};
// Send to user a DM with the error
if order.status != Status::Active.to_string() {
send_new_order_msg(Some(order.id), Action::NotAllowedByStatus, None, &event.pubkey).await;
send_new_order_msg(
Some(order.id),
Action::NotAllowedByStatus,
None,
&event.pubkey,
)
.await;
return Ok(());
}
// Check if the pubkey is the buyer
Expand Down
10 changes: 8 additions & 2 deletions src/app/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub async fn check_failure_retries(order: &Order) -> Result<Order> {
order.payment_attempts = 0;
} else if order.payment_attempts < retries_number {
order.payment_attempts += 1;
}
}
let buyer_pubkey = match &order.buyer_pubkey {
Some(buyer) => PublicKey::from_str(buyer.as_str())?,
None => return Err(Error::msg("Missing buyer pubkey")),
Expand Down Expand Up @@ -90,7 +90,13 @@ pub async fn release_action(
&& current_status != Status::FiatSent
&& current_status != Status::Dispute
{
send_new_order_msg(Some(order.id), Action::NotAllowedByStatus, None, &event.pubkey).await;
send_new_order_msg(
Some(order.id),
Action::NotAllowedByStatus,
None,
&event.pubkey,
)
.await;
return Ok(());
}

Expand Down
16 changes: 14 additions & 2 deletions src/app/take_buy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ pub async fn take_buy_action(
if let Some(am) = get_fiat_amount_requested(&order, &msg) {
order.fiat_amount = am;
} else {
send_new_order_msg(Some(order.id), Action::OutOfRangeFiatAmount, None, &event.pubkey).await;
send_new_order_msg(
Some(order.id),
Action::OutOfRangeFiatAmount,
None,
&event.pubkey,
)
.await;
return Ok(());
}

Expand Down Expand Up @@ -81,7 +87,13 @@ pub async fn take_buy_action(
// Seller can take pending orders only
if order_status != Status::Pending {
// We create a Message
send_new_order_msg(Some(order.id), Action::NotAllowedByStatus, None, &seller_pubkey).await;
send_new_order_msg(
Some(order.id),
Action::NotAllowedByStatus,
None,
&seller_pubkey,
)
.await;
return Ok(());
}

Expand Down
24 changes: 21 additions & 3 deletions src/app/take_sell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ pub async fn take_sell_action(
if let Some(am) = get_fiat_amount_requested(&order, &msg) {
order.fiat_amount = am;
} else {
send_new_order_msg(Some(order.id), Action::OutOfRangeFiatAmount, None, &event.pubkey).await;
send_new_order_msg(
Some(order.id),
Action::OutOfRangeFiatAmount,
None,
&event.pubkey,
)
.await;
return Ok(());
}

Expand Down Expand Up @@ -98,7 +104,13 @@ pub async fn take_sell_action(
match order_status {
Status::Pending | Status::WaitingBuyerInvoice => {}
_ => {
send_new_order_msg(Some(order.id), Action::NotAllowedByStatus, None, &buyer_pubkey).await;
send_new_order_msg(
Some(order.id),
Action::NotAllowedByStatus,
None,
&buyer_pubkey,
)
.await;
return Ok(());
}
}
Expand All @@ -117,7 +129,13 @@ pub async fn take_sell_action(
if order_status == Status::WaitingBuyerInvoice {
if let Some(ref buyer) = order.buyer_pubkey {
if buyer != &buyer_pubkey.to_string() {
send_new_order_msg(Some(order.id), Action::NotAllowedByStatus, None, &buyer_pubkey).await;
send_new_order_msg(
Some(order.id),
Action::NotAllowedByStatus,
None,
&buyer_pubkey,
)
.await;
return Ok(());
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub async fn hold_invoice_paid(hash: &str) -> Result<()> {
None,
Some(order.created_at),
Some(order.expires_at),
None,
None,
);
let status;

Expand Down
2 changes: 2 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ pub async fn set_waiting_invoice_status(order: &mut Order, buyer_pubkey: PublicK
None,
None,
None,
None,
None,
);
// We create a Message
send_new_order_msg(
Expand Down

0 comments on commit 41f182c

Please sign in to comment.