-
Notifications
You must be signed in to change notification settings - Fork 35
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
cancel in waiting-buyer-invoice or waiting-payment #432
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe pull request modifies the Changes
Assessment against linked issues
Possibly related PRs
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/app/cancel.rs (1)
104-135
: Refactor to eliminate code duplication in cancellation handlingThe blocks of code handling order cancellation for creators (lines 104–135) and takers (lines 136–184) contain similar logic, such as updating the order status, canceling hold invoices, and sending notifications. Refactoring these blocks to extract common functionality into helper functions can improve maintainability and reduce repetition.
Also applies to: 136-184
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/app/cancel.rs
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
♻️ Duplicate comments (1)
src/app/cancel.rs (1)
136-186
:⚠️ Potential issueFix logical error: Notifications may not be sent due to premature status update.
When the taker cancels the order, the status is updated to
Pending
at line 173. The subsequent status checks at lines 145-147 and 159-161 will never be true because the status has already been changed, causing the creator to miss cancellation notifications.Store the original status before updating it:
+ let original_status = order.status.clone(); if let Ok(order_updated) = update_order_event(my_keys, Status::Pending, &order).await { let _ = order_updated.update(pool).await; } if order.kind == OrderKind::Buy.to_string() - && order.status == Status::WaitingBuyerInvoice.to_string() + && original_status == Status::WaitingBuyerInvoice.to_string() { // ... notification code ... } if order.kind == OrderKind::Sell.to_string() - && order.status == Status::WaitingPayment.to_string() + && original_status == Status::WaitingPayment.to_string() { // ... notification code ... }
🧹 Nitpick comments (1)
src/app/cancel.rs (1)
91-102
: Consider simplifying taker identification logic.While the current implementation is correct, it can be more concise by using a ternary operation.
- let taker_pubkey: String; - if seller_pubkey == &order.creator_pubkey { - taker_pubkey = buyer_pubkey.to_string(); - } else { - taker_pubkey = seller_pubkey.to_string(); - } + let taker_pubkey = if seller_pubkey == &order.creator_pubkey { + buyer_pubkey.to_string() + } else { + seller_pubkey.to_string() + };
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/app/cancel.rs
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: tests
🔇 Additional comments (2)
src/app/cancel.rs (2)
88-90
: LGTM! Status checks align with requirements.The condition correctly handles both
WaitingPayment
andWaitingBuyerInvoice
statuses as specified in the PR objectives.
104-135
: LGTM! Creator cancellation flow is well-structured.The implementation correctly:
- Updates order status
- Handles hold invoice cancellation
- Sends notifications to both parties
and remove unused functions cancel_add_invoice cancel_pay_hold_invoice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/app/cancel.rs (2)
91-102
: Consider simplifying taker identification logic.While the current implementation is correct, it could be more concise.
Consider this more idiomatic approach:
- let taker_pubkey: String; - if seller_pubkey == &order.creator_pubkey { - taker_pubkey = buyer_pubkey.to_string(); - } else { - taker_pubkey = seller_pubkey.to_string(); - } + let taker_pubkey = if seller_pubkey == &order.creator_pubkey { + buyer_pubkey.to_string() + } else { + seller_pubkey.to_string() + };
144-168
: Consider reducing code duplication in taker cancellation logic.The price reset and order update logic is duplicated between Buy and Sell order handling.
Consider extracting the common logic:
+ fn reset_order_state( + order: &mut Order, + pool: &Pool<Sqlite>, + my_keys: &Keys, + ) -> Result<()> { + if order.price_from_api { + order.amount = 0; + order.fee = 0; + } + update_order_to_initial_state(pool, order.id, order.amount, order.fee).await?; + update_order_event(my_keys, Status::Pending, order).await?; + Ok(()) + } + if order.kind == OrderKind::Buy.to_string() { if order.status == Status::WaitingBuyerInvoice.to_string() { send_new_order_msg( request_id, Some(order.id), Action::Canceled, None, &creator_pubkey, None, ) .await; } - if order.price_from_api { - order.amount = 0; - order.fee = 0; - } edit_seller_pubkey_order(pool, order.id, None).await?; edit_master_seller_pubkey_order(pool, order.id, None).await?; - update_order_to_initial_state(pool, order.id, order.amount, order.fee).await?; - update_order_event(my_keys, Status::Pending, &order).await?; + reset_order_state(&mut order, pool, my_keys)?; info!( "{}: Canceled order Id {} republishing order", buyer_pubkey, order.id ); } if order.kind == OrderKind::Sell.to_string() { if order.status == Status::WaitingPayment.to_string() { send_new_order_msg( request_id, Some(order.id), Action::Canceled, None, &creator_pubkey, None, ) .await; } - if order.price_from_api { - order.amount = 0; - order.fee = 0; - } edit_buyer_pubkey_order(pool, order.id, None).await?; edit_master_buyer_pubkey_order(pool, order.id, None).await?; - update_order_to_initial_state(pool, order.id, order.amount, order.fee).await?; - update_order_event(my_keys, Status::Pending, &order).await?; + reset_order_state(&mut order, pool, my_keys)?; info!( "{}: Canceled order Id {} republishing order", buyer_pubkey, order.id ); }Also applies to: 170-194
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/app/cancel.rs
(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: tests
🔇 Additional comments (3)
src/app/cancel.rs (3)
88-90
: LGTM! Status checks align with requirements.The condition correctly handles both
WaitingPayment
andWaitingBuyerInvoice
statuses as specified in the PR objectives.
104-134
: LGTM! Creator cancellation logic is robust.The implementation correctly:
- Updates order status to
Canceled
- Handles hold invoice cancellation
- Sends notifications to both parties
144-194
: Verify the impact of resetting price and fee.The code resets
amount
andfee
to 0 whenprice_from_api
is true. Please ensure this doesn't cause issues with order republishing.Run this script to check the impact:
✅ Verification successful
Price and fee reset is a verified pattern for market price orders
The code intentionally uses amount = 0 as a marker to trigger fresh market price fetching when orders are taken. This ensures market prices are current when orders are republished. The system has proper validation in place to prevent orders with invalid amounts from being processed.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Check if there are any assumptions about non-zero amount/fee in the codebase # Search for amount/fee validation or checks rg -A 5 'amount.*[=><!].*0|fee.*[=><!].*0' # Search for order republishing logic ast-grep --pattern 'update_order_to_initial_state($$$)'Length of output: 8783
please @grunch @arkanoider take a look |
closes #379
Added:
When the maker cancels an order in status
waiting-buyer-invoice
orwaiting-payment
, the order goes to statuscanceled
and the taker will be notified that it was cancelledWhen the taker cancels an order in the status
waiting-buyer-invoice
orwaiting-payment
, the order goes to statuspending
. The maker will be notified that it was cancelled only if the maker has already been notified to submit their invoice or pay the hold invoiceSummary by CodeRabbit
New Features
Bug Fixes