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

Code refactoring removing old actions #409

Merged
merged 3 commits into from
Dec 28, 2024
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
4 changes: 2 additions & 2 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.21", features = ["sqlx"] }
mostro-core = { version = "0.6.22", features = ["sqlx"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
config = "0.14.0"
Expand Down
57 changes: 57 additions & 0 deletions epa.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
diff --git a/src/app/release.rs b/src/app/release.rs
index 2513540..ffdefed 100644
--- a/src/app/release.rs
+++ b/src/app/release.rs
@@ -328,6 +328,7 @@ pub async fn get_child_order(
}
Ordering::Less => {
notify_invalid_amount(order, request_id).await;
+ return Ok((false, order.clone()));
}
}
}
@@ -337,6 +338,8 @@ pub async fn get_child_order(

fn create_base_order(order: &Order) -> Order {
let mut new_order = order.clone();
+ new_order.id = uuid::Uuid::new_v4();
+ new_order.status = Status::Pending.to_string();
new_order.amount = 0;
new_order.hash = None;
new_order.preimage = None;
@@ -363,8 +366,6 @@ async fn update_order_for_equal(new_max: i64, new_order: &mut Order, my_keys: &K
new_order.fiat_amount = new_max;
new_order.max_amount = None;
new_order.min_amount = None;
- new_order.status = Status::Pending.to_string();
- new_order.id = uuid::Uuid::new_v4();

let tags = crate::nip33::order_to_tags(new_order, None);
let event = crate::nip33::new_event(my_keys, "", new_order.id.to_string(), tags)?;
@@ -372,7 +373,7 @@ async fn update_order_for_equal(new_max: i64, new_order: &mut Order, my_keys: &K
new_order.clone().create(&pool).await?;
NOSTR_CLIENT
.get()
- .unwrap()
+ .ok_or_else(|| anyhow::Error::msg("NOSTR_CLIENT not initialized"))?
.send_event(event)
.await
.map_err(|err| anyhow::Error::msg(err.to_string()))?;
@@ -388,8 +389,6 @@ async fn update_order_for_greater(
let pool = db::connect().await?;
new_order.max_amount = Some(new_max);
new_order.fiat_amount = 0;
- new_order.id = uuid::Uuid::new_v4();
- new_order.status = Status::Pending.to_string();

let tags = crate::nip33::order_to_tags(new_order, None);
let event = crate::nip33::new_event(my_keys, "", new_order.id.to_string(), tags)?;
@@ -397,7 +396,7 @@ async fn update_order_for_greater(
new_order.clone().create(&pool).await?;
NOSTR_CLIENT
.get()
- .unwrap()
+ .ok_or_else(|| anyhow::Error::msg("NOSTR_CLIENT not initialized"))?
.send_event(event)
.await
.map_err(|err| anyhow::Error::msg(err.to_string()))?;
12 changes: 4 additions & 8 deletions src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ pub async fn add_invoice_action(
{
Ok(_) => payment_request,
Err(_) => {
send_new_order_msg(
send_cant_do_msg(
request_id,
Some(order.id),
Action::IncorrectInvoiceAmount,
None,
Some(CantDoReason::InvalidAmount),
&event.rumor.pubkey,
None,
)
.await;
return Ok(());
Expand Down Expand Up @@ -120,13 +118,11 @@ pub async fn add_invoice_action(
return Ok(());
}
_ => {
send_new_order_msg(
send_cant_do_msg(
request_id,
Some(order.id),
Action::NotAllowedByStatus,
None,
Some(CantDoReason::NotAllowedByStatus),
&event.rumor.pubkey,
None,
)
.await;
return Ok(());
Expand Down
22 changes: 10 additions & 12 deletions src/app/admin_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use std::str::FromStr;
use crate::db::{find_dispute_by_order_id, is_assigned_solver};
use crate::lightning::LndConnector;
use crate::nip33::new_event;
use crate::util::{get_nostr_client, send_dm, send_new_order_msg, update_order_event};
use crate::util::{get_nostr_client, send_cant_do_msg, send_dm, update_order_event};

use anyhow::{Error, Result};
use mostro_core::dispute::Status as DisputeStatus;
use mostro_core::message::{Action, Message, MessageKind};
use mostro_core::message::{Action, CantDoReason, Message, MessageKind};
use mostro_core::order::{Order, Status};
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
Expand All @@ -35,15 +35,14 @@ pub async fn admin_cancel_action(

match is_assigned_solver(pool, &event.rumor.pubkey.to_string(), order_id).await {
Ok(false) => {
send_new_order_msg(
inner_message.request_id,
send_cant_do_msg(
request_id,
Some(order_id),
Action::IsNotYourDispute,
None,
Some(CantDoReason::IsNotYourDispute),
&event.rumor.pubkey,
inner_message.trade_index,
)
.await;

return Ok(());
}
Err(e) => {
Expand Down Expand Up @@ -78,15 +77,14 @@ pub async fn admin_cancel_action(
}

if order.status != Status::Dispute.to_string() {
send_new_order_msg(
inner_message.request_id,
send_cant_do_msg(
request_id,
Some(order.id),
Action::NotAllowedByStatus,
None,
Some(CantDoReason::NotAllowedByStatus),
&event.rumor.pubkey,
inner_message.trade_index,
)
.await;

return Ok(());
}

Expand Down
22 changes: 10 additions & 12 deletions src/app/admin_settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use crate::db::{find_dispute_by_order_id, is_assigned_solver};
use crate::lightning::LndConnector;
use crate::nip33::new_event;
use crate::util::{
get_nostr_client, send_dm, send_new_order_msg, settle_seller_hold_invoice, update_order_event,
get_nostr_client, send_cant_do_msg, send_dm, settle_seller_hold_invoice, update_order_event,
};

use anyhow::{Error, Result};
use mostro_core::dispute::Status as DisputeStatus;
use mostro_core::message::{Action, Message, MessageKind};
use mostro_core::message::{Action, CantDoReason, Message, MessageKind};
use mostro_core::order::{Order, Status};
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
Expand Down Expand Up @@ -37,15 +37,14 @@ pub async fn admin_settle_action(

match is_assigned_solver(pool, &event.rumor.pubkey.to_string(), order_id).await {
Ok(false) => {
send_new_order_msg(
msg.get_inner_message_kind().request_id,
send_cant_do_msg(
request_id,
Some(order_id),
Action::IsNotYourDispute,
None,
Some(CantDoReason::IsNotYourDispute),
&event.rumor.pubkey,
inner_message.trade_index,
)
.await;

return Ok(());
}
Err(e) => {
Expand Down Expand Up @@ -80,15 +79,14 @@ pub async fn admin_settle_action(
}

if order.status != Status::Dispute.to_string() {
send_new_order_msg(
inner_message.request_id,
send_cant_do_msg(
request_id,
Some(order.id),
Action::NotAllowedByStatus,
None,
Some(CantDoReason::NotAllowedByStatus),
&event.rumor.pubkey,
inner_message.trade_index,
)
.await;

return Ok(());
}

Expand Down
10 changes: 4 additions & 6 deletions src/app/admin_take_dispute.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::db::find_solver_pubkey;
use crate::nip33::new_event;
use crate::util::{get_nostr_client, send_cant_do_msg, send_dm, send_new_order_msg};
use crate::util::{get_nostr_client, send_cant_do_msg, send_dm};

use anyhow::{Error, Result};
use mostro_core::dispute::{Dispute, Status};
Expand Down Expand Up @@ -56,16 +56,14 @@ pub async fn admin_take_dispute_action(
let mut dispute = match Dispute::by_id(pool, dispute_id).await? {
Some(dispute) => dispute,
None => {
// We create a Message
send_new_order_msg(
send_cant_do_msg(
request_id,
Some(dispute_id),
Action::NotFound,
None,
Some(CantDoReason::NotFound),
&event.rumor.pubkey,
None,
)
.await;

return Ok(());
}
};
Expand Down
7 changes: 2 additions & 5 deletions src/app/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,11 @@ pub async fn cancel_action(
if order.status == Status::Pending.to_string() {
// Validates if this user is the order creator
if user_pubkey != order.creator_pubkey {
// We create a Message
send_new_order_msg(
send_cant_do_msg(
request_id,
Some(order.id),
Action::IsNotYourOrder,
None,
Some(CantDoReason::IsNotYourOrder),
&event.rumor.pubkey,
None,
)
.await;
} else {
Expand Down
7 changes: 3 additions & 4 deletions src/app/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,14 @@ async fn get_valid_order(
// Only allow disputes for Active or FiatSent orders
if !matches!(status, Status::Active | Status::FiatSent) {
// Notify the sender that the action is not allowed for this status
send_new_order_msg(
send_cant_do_msg(
request_id,
Some(order.id),
Action::NotAllowedByStatus,
None,
Some(CantDoReason::NotAllowedByStatus),
&event.rumor.pubkey,
None,
)
.await;

return Err(Error::msg(format!(
"Order {} with status {} does not allow disputes. Must be Active or FiatSent",
order.id, order.status
Expand Down
7 changes: 3 additions & 4 deletions src/app/fiat_sent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ 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(
send_cant_do_msg(
request_id,
Some(order.id),
Action::NotAllowedByStatus,
None,
Some(CantDoReason::NotAllowedByStatus),
&event.rumor.pubkey,
None,
)
.await;

return Ok(());
}
// Check if the pubkey is the buyer
Expand Down
31 changes: 13 additions & 18 deletions src/app/order.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::cli::settings::Settings;
use crate::lightning::invoice::is_valid_invoice;
use crate::util::{get_bitcoin_price, publish_order, send_cant_do_msg, send_new_order_msg};
use crate::util::{get_bitcoin_price, publish_order, send_cant_do_msg};
use anyhow::Result;
use mostro_core::message::{Action, CantDoReason, Message};
use mostro_core::message::{CantDoReason, Message};
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
use nostr_sdk::Keys;
Expand All @@ -28,15 +28,14 @@ pub async fn order_action(
match is_valid_invoice(invoice.clone(), None, None).await {
Ok(_) => (),
Err(_) => {
send_new_order_msg(
send_cant_do_msg(
request_id,
order.id,
Action::IncorrectInvoiceAmount,
None,
Some(CantDoReason::InvalidAmount),
&event.rumor.pubkey,
None,
)
.await;

return Ok(());
}
}
Expand All @@ -59,15 +58,14 @@ pub async fn order_action(
return Ok(());
}
if order.amount != 0 {
send_new_order_msg(
send_cant_do_msg(
request_id,
None,
Action::InvalidSatsAmount,
None,
Some(CantDoReason::InvalidAmount),
&event.rumor.pubkey,
None,
)
.await;

return Ok(());
}
amount_vec.clear();
Expand Down Expand Up @@ -106,28 +104,25 @@ pub async fn order_action(

// Check amount is positive - extra safety check
if quote < 0 {
send_new_order_msg(
send_cant_do_msg(
request_id,
None,
Action::InvalidSatsAmount,
None,
Some(CantDoReason::InvalidAmount),
&event.rumor.pubkey,
None,
)
.await;

return Ok(());
}

if quote > mostro_settings.max_order_amount as i64
|| quote < mostro_settings.min_payment_amount as i64
{
send_new_order_msg(
send_cant_do_msg(
request_id,
None,
Action::OutOfRangeSatsAmount,
None,
Some(CantDoReason::OutOfRangeSatsAmount),
&event.rumor.pubkey,
None,
)
.await;
return Ok(());
Expand Down
Loading
Loading