Skip to content

Commit

Permalink
hot fix on range order implementation (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch committed Jun 21, 2024
1 parent a06d3ae commit 11363e0
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/app/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,28 +236,43 @@ async fn payment_success(
if order.max_amount.is_some() && order.min_amount.is_some() {
if let Some(max) = order.max_amount {
if let Some(new_max) = max.checked_sub(order.fiat_amount) {
let mut new_order = order.clone();
new_order.amount = 0;
new_order.hash = None;
new_order.preimage = None;
new_order.buyer_invoice = None;
new_order.taken_at = 0;
new_order.invoice_held_at = 0;
new_order.range_parent_id = Some(order.id);
if new_order.kind == "sell" {
new_order.buyer_pubkey = None;
new_order.master_buyer_pubkey = None;
} else {
new_order.seller_pubkey = None;
new_order.master_seller_pubkey = None;
}
match new_max.cmp(&order.min_amount.unwrap()) {
Ordering::Equal => {
// Update order in case max == min
let pool = db::connect().await?;
let mut new_order = order.clone();
new_order.fiat_amount = new_max;
new_order.max_amount = None;
new_order.min_amount = None;
new_order.amount = 0;
new_order.fiat_amount = new_max;
new_order.status = Status::Pending.to_string();
new_order.id = uuid::Uuid::new_v4();
new_order.status = Status::Pending.to_string();
// CRUD order creation
new_order.clone().create(&pool).await?;
// We transform the order fields to tags to use in the event
let tags = crate::nip33::order_to_tags(&new_order, None);

info!("range order tags to be republished: {:#?}", tags);
// nip33 kind with order fields as tags and order id as identifier
let event =
crate::nip33::new_event(my_keys, "", new_order.id.to_string(), tags)?;

let event_id = event.id.to_string();
// We update the order with the new event_id
new_order.event_id = event_id;
// CRUD order creation
new_order.clone().create(&pool).await?;
let _ = NOSTR_CLIENT
.get()
.unwrap()
Expand All @@ -269,22 +284,23 @@ async fn payment_success(
Ordering::Greater => {
// Update order in case new max is still greater the min amount
let pool = db::connect().await?;
let mut new_order = order.clone();
// let mut new_order = order.clone();
new_order.max_amount = Some(new_max);
new_order.range_parent_id = Some(order.id);
new_order.amount = 0;
new_order.fiat_amount = 0;
new_order.id = uuid::Uuid::new_v4();
new_order.status = Status::Pending.to_string();
// CRUD order creation
new_order.clone().create(&pool).await?;
// We transform the order fields to tags to use in the event
let tags = crate::nip33::order_to_tags(&new_order, None);

info!("range order tags to be republished: {:#?}", tags);
// nip33 kind with order fields as tags and order id as identifier
let event =
crate::nip33::new_event(my_keys, "", new_order.id.to_string(), tags)?;

let event_id = event.id.to_string();
// We update the order with the new event_id
new_order.event_id = event_id;
new_order.clone().create(&pool).await?;
let _ = NOSTR_CLIENT
.get()
.unwrap()
Expand Down

0 comments on commit 11363e0

Please sign in to comment.