Skip to content

Commit

Permalink
cancel in waiting-buyer-invoice or waiting-payment
Browse files Browse the repository at this point in the history
  • Loading branch information
Catrya committed Jan 18, 2025
1 parent 01710f4 commit ec08b81
Showing 1 changed file with 95 additions and 8 deletions.
103 changes: 95 additions & 8 deletions src/app/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,103 @@ pub async fn cancel_action(
return Ok(());
}

if order.kind == OrderKind::Sell.to_string()
&& order.status == Status::WaitingBuyerInvoice.to_string()
if order.status == Status::WaitingPayment.to_string()
|| order.status == Status::WaitingBuyerInvoice.to_string()
{
cancel_add_invoice(ln_client, &mut order, event, pool, my_keys, request_id).await?;
}
let (seller_pubkey, buyer_pubkey) = match (&order.seller_pubkey, &order.buyer_pubkey) {
(Some(seller), Some(buyer)) => (seller, buyer),
(None, _) => return Err(Error::msg("Missing seller pubkey")),
(_, None) => return Err(Error::msg("Missing buyer pubkey")),
};

if order.kind == OrderKind::Buy.to_string()
&& order.status == Status::WaitingPayment.to_string()
{
cancel_pay_hold_invoice(ln_client, &mut order, event, pool, my_keys, request_id).await?;
let taker_pubkey: String;
if seller_pubkey == &order.creator_pubkey {
taker_pubkey = buyer_pubkey.to_string();
} else {
taker_pubkey = seller_pubkey.to_string();
}

if user_pubkey == order.creator_pubkey {
if let Ok(order_updated) = update_order_event(my_keys, Status::Canceled, &order).await {
let _ = order_updated.update(pool).await;
}

if let Some(hash) = &order.hash {
ln_client.cancel_hold_invoice(hash).await?;
info!("Order Id {}: Funds returned to seller", &order.id);
}

send_new_order_msg(
request_id,
Some(order.id),
Action::Canceled,
None,
&event.rumor.pubkey,
None,
)
.await;

let taker_pubkey = PublicKey::from_str(&taker_pubkey)?;
//We notify the taker that the order was cancelled
send_new_order_msg(
None,
Some(order.id),
Action::Canceled,
None,
&taker_pubkey,
None,
)
.await;
} else if user_pubkey == taker_pubkey {
if let Ok(order_updated) = update_order_event(my_keys, Status::Pending, &order).await {
let _ = order_updated.update(pool).await;
}
if let Some(hash) = &order.hash {
ln_client.cancel_hold_invoice(hash).await?;
info!("Order Id {}: Funds returned to seller", &order.id);
}

send_new_order_msg(
request_id,
Some(order.id),
Action::Canceled,
None,
&event.rumor.pubkey,
None,
)
.await;

let creator_pubkey = PublicKey::from_str(&order.creator_pubkey)?;
//We notify the creator that the order was cancelled only if the taker had already done his part before

if order.kind == OrderKind::Buy.to_string()
&& order.status == Status::WaitingBuyerInvoice.to_string()
{
send_new_order_msg(
request_id,
Some(order.id),
Action::Canceled,
None,
&creator_pubkey,
None,
)
.await;
}

if order.kind == OrderKind::Sell.to_string()
&& order.status == Status::WaitingPayment.to_string()
{
send_new_order_msg(
request_id,
Some(order.id),
Action::Canceled,
None,
&creator_pubkey,
None,
)
.await;
}
}
}

if order.status == Status::Active.to_string()
Expand Down

0 comments on commit ec08b81

Please sign in to comment.