Skip to content

Commit

Permalink
Refactoring and fixes (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch authored Jan 5, 2024
1 parent 17272e7 commit d54f593
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 68 deletions.
4 changes: 3 additions & 1 deletion src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use sqlx_crud::Crud;
use std::str::FromStr;
use std::thread;
use tracing::error;

pub async fn add_invoice_action(
Expand Down Expand Up @@ -119,6 +120,7 @@ pub async fn add_invoice_action(
// We save the invoice on db
order.buyer_invoice = Some(pr.clone());
let order = order.update(pool).await?;
thread::sleep(std::time::Duration::from_secs(1));

if order.preimage.is_some() {
// We send this data related to the order to the parties
Expand All @@ -138,7 +140,7 @@ pub async fn add_invoice_action(
);
// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
crate::util::update_order_event(pool, client, my_keys, Status::Active, &order, None)
crate::util::update_order_event(pool, client, my_keys, Status::Active, &order)
.await
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion src/app/admin_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub async fn admin_cancel_action(

// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
update_order_event(pool, client, my_keys, Status::CanceledByAdmin, &order, None).await?;
update_order_event(pool, client, my_keys, Status::CanceledByAdmin, &order).await?;
// We create a Message
let message = Message::new_dispute(Some(order.id), None, Action::AdminCancel, None);
let message = message.as_json()?;
Expand Down
2 changes: 1 addition & 1 deletion src/app/admin_settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub async fn admin_settle_action(
tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;
// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
update_order_event(&pool, &client, &my_keys, status, &order, None)
update_order_event(&pool, &client, &my_keys, status, &order)
.await
.unwrap();

Expand Down
19 changes: 5 additions & 14 deletions src/app/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub async fn cancel_action(
} else {
// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
update_order_event(pool, client, my_keys, Status::Canceled, &order, None).await?;
update_order_event(pool, client, my_keys, Status::Canceled, &order).await?;
// We create a Message for cancel
let message = Message::new_order(Some(order.id), None, Action::Cancel, None);
let message = message.as_json()?;
Expand Down Expand Up @@ -102,7 +102,6 @@ pub async fn cancel_action(
my_keys,
Status::CooperativelyCanceled,
&order,
None,
)
.await?;
// We create a Message for an accepted cooperative cancel and send it to both parties
Expand Down Expand Up @@ -183,15 +182,7 @@ pub async fn cancel_add_invoice(
if &order.creator_pubkey == buyer_pubkey {
// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
update_order_event(
pool,
client,
my_keys,
Status::CooperativelyCanceled,
order,
None,
)
.await?;
update_order_event(pool, client, my_keys, Status::CooperativelyCanceled, order).await?;
// We create a Message for cancel
let message = Message::new_order(Some(order.id), None, Action::Cancel, None);
let message = message.as_json()?;
Expand All @@ -207,7 +198,7 @@ pub async fn cancel_add_invoice(
}
edit_buyer_pubkey_order(pool, order.id, None).await?;
update_order_to_initial_state(pool, order.id, order.amount, order.fee).await?;
update_order_event(pool, client, my_keys, Status::Pending, order, None).await?;
update_order_event(pool, client, my_keys, Status::Pending, order).await?;
info!(
"{}: Canceled order Id {} republishing order",
buyer_pubkey, order.id
Expand Down Expand Up @@ -252,7 +243,7 @@ pub async fn cancel_pay_hold_invoice(
if order.creator_pubkey == seller_pubkey.to_string() {
// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
update_order_event(pool, client, my_keys, Status::Canceled, order, None).await?;
update_order_event(pool, client, my_keys, Status::Canceled, order).await?;
// We create a Message for cancel
let message = Message::new_order(Some(order.id), None, Action::Cancel, None);
let message = message.as_json()?;
Expand All @@ -268,7 +259,7 @@ pub async fn cancel_pay_hold_invoice(
}
edit_seller_pubkey_order(pool, order.id, None).await?;
update_order_to_initial_state(pool, order.id, order.amount, order.fee).await?;
update_order_event(pool, client, my_keys, Status::Pending, order, None).await?;
update_order_event(pool, client, my_keys, Status::Pending, order).await?;
info!(
"{}: Canceled order Id {} republishing order",
buyer_pubkey, order.id
Expand Down
2 changes: 1 addition & 1 deletion src/app/fiat_sent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub async fn fiat_sent_action(

// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
update_order_event(pool, client, my_keys, Status::FiatSent, &order, None).await?;
update_order_event(pool, client, my_keys, Status::FiatSent, &order).await?;

let seller_pubkey = match order.seller_pubkey.as_ref() {
Some(pk) => XOnlyPublicKey::from_str(pk)?,
Expand Down
2 changes: 1 addition & 1 deletion src/app/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub async fn release_action(
tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;
// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
update_order_event(&pool, &client, &my_keys, status, &order, None)
update_order_event(&pool, &client, &my_keys, status, &order)
.await
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion src/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub async fn hold_invoice_paid(hash: &str) {
}
// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
crate::util::update_order_event(&pool, &client, &my_keys, status, &order, None)
crate::util::update_order_event(&pool, &client, &my_keys, status, &order)
.await
.unwrap();
// Update the invoice_held_at field
Expand Down
23 changes: 6 additions & 17 deletions src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@ async fn job_cancel_orders(client: Client) {
let _ = ln_client.cancel_hold_invoice(hash).await;
info!("Order Id {}: Funds returned to seller - buyer did not sent regular invoice in time", &order.id);
};
let mut order = order.clone();
// We re-publish the event with Pending status
// and update on local database
let mut updated_order_amount = order.amount;
let mut updated_order_fee = order.fee;

if order.price_from_api {
updated_order_amount = 0;
updated_order_fee = 0;
order.amount = 0;
order.fee = 0;
}

// Initialize reset status to pending, change in case of specifici needs of order
Expand Down Expand Up @@ -137,8 +135,8 @@ async fn job_cancel_orders(client: Client) {
let _ = update_order_to_initial_state(
&pool,
order.id,
updated_order_amount,
updated_order_fee,
order.amount,
order.fee,
)
.await;
info!(
Expand All @@ -151,15 +149,7 @@ async fn job_cancel_orders(client: Client) {
order.id
);
}
let _ = update_order_event(
&pool,
&client,
&keys,
new_status,
&order,
Some(updated_order_amount),
)
.await;
let _ = update_order_event(&pool, &client, &keys, new_status, &order).await;
}
}
}
Expand Down Expand Up @@ -193,7 +183,6 @@ async fn job_expire_pending_older_orders(client: Client) {
&keys,
Status::Expired,
order,
None,
)
.await;
}
Expand Down
48 changes: 17 additions & 31 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@ pub async fn publish_order(
master_pubkey: &str,
ack_pubkey: XOnlyPublicKey,
) -> Result<()> {
let mostro_settings = Settings::get_mostro();
// We calculate the bot fee
let fee = mostro_settings.fee;
let fee = (fee * new_order.amount as f64) / 2.0;
let fee = fee.round() as i64;
let mut fee = 0;
if new_order.amount > 0 {
let mostro_settings = Settings::get_mostro();
// We calculate the bot fee
let split_fee = (mostro_settings.fee * new_order.amount as f64) / 2.0;
fee = split_fee.round() as i64;
}
// Prepare a new default order
let mut new_order_db = Order {
id: Uuid::new_v4(),
Expand Down Expand Up @@ -231,9 +233,7 @@ pub async fn update_order_event(
keys: &Keys,
status: Status,
order: &Order,
amount: Option<i64>,
) -> Result<()> {
let amount = amount.unwrap_or(order.amount);
let mut order = order.clone();
// update order.status with new status
order.status = status.to_string();
Expand All @@ -245,7 +245,6 @@ pub async fn update_order_event(
info!("Sending replaceable event: {event:#?}");
// We update the order with the new event_id
order.event_id = event.id.to_string();
order.amount = amount;
order.update(pool).await?;
info!(
"Order Id: {} updated Nostr new Status: {}",
Expand Down Expand Up @@ -294,13 +293,8 @@ pub async fn show_hold_invoice(
}
};
let mut ln_client = lightning::LndConnector::new().await;
let mostro_settings = Settings::get_mostro();
// Add fee of seller to hold invoice
let seller_fee = mostro_settings.fee / 2.0;
let add_fee = seller_fee * order.amount as f64;
let rounded_fee = add_fee.round();
let new_amount = order.amount + rounded_fee as i64;
let seller_total_amount = new_amount;
let new_amount = order.amount + order.fee;

// Now we generate the hold invoice that seller should pay
let (invoice_response, preimage, hash) = ln_client
Expand All @@ -311,7 +305,7 @@ pub async fn show_hold_invoice(
&order.fiat_code,
&order.fiat_amount.to_string(),
)?,
seller_total_amount,
new_amount,
)
.await?;
if let Some(invoice) = payment_request {
Expand All @@ -327,7 +321,7 @@ pub async fn show_hold_invoice(
let order = order.update(pool).await?;

// We need to publish a new event with the new status
update_order_event(pool, client, my_keys, Status::WaitingPayment, &order, None).await?;
update_order_event(pool, client, my_keys, Status::WaitingPayment, &order).await?;
let mut new_order = order.as_new_order();
new_order.status = Some(Status::WaitingPayment);
// We create a Message to send the hold invoice to seller
Expand Down Expand Up @@ -399,13 +393,12 @@ pub async fn set_market_order_sats_amount(
get_market_quote(&order.fiat_amount, &order.fiat_code, &order.premium).await?;

// We calculate the bot fee
let fee = mostro_settings.fee / 2.0;
let sub_fee = fee * new_sats_amount as f64;
let rounded_fee = sub_fee.round();
let sub_fee = (mostro_settings.fee * new_sats_amount as f64) / 2.0;
let rounded_fee = sub_fee.round() as i64;

let buyer_final_amount = new_sats_amount - rounded_fee as i64;
let buyer_final_amount = new_sats_amount - rounded_fee;
let kind = OrderKind::from_str(&order.kind).unwrap();
let status = Status::from_str(&order.status).unwrap();
let status = Status::WaitingBuyerInvoice;

// We send this data related to the buyer
let order_data = SmallOrder::new(
Expand Down Expand Up @@ -435,15 +428,8 @@ pub async fn set_market_order_sats_amount(

// Update order with new sats value
order.amount = new_sats_amount;
update_order_event(
pool,
client,
my_keys,
Status::WaitingBuyerInvoice,
order,
None,
)
.await?;
order.fee = rounded_fee;
update_order_event(pool, client, my_keys, status, order).await?;

Ok(order.amount)
}
Expand Down Expand Up @@ -505,7 +491,7 @@ pub async fn settle_seller_hold_invoice(
info!("{action}: Order Id {}: hold invoice settled", order.id);
// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
update_order_event(pool, client, my_keys, status, order, None).await?;
update_order_event(pool, client, my_keys, status, order).await?;

Ok(())
}
Expand Down

0 comments on commit d54f593

Please sign in to comment.