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

disallow premium with fixed order #400

Merged
merged 4 commits into from
Dec 11, 2024

Conversation

Catrya
Copy link
Collaborator

@Catrya Catrya commented Dec 10, 2024

fix #397
Now Mostro will not allow the creation of orders that have a fixed price and a premium at the same time

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced error handling for order processing by adding validation checks to prevent further processing of invalid orders.

Copy link
Contributor

coderabbitai bot commented Dec 10, 2024

Walkthrough

The changes made in src/app/order.rs involve modifications to the order_action function. A new validation check has been introduced to ensure that the fields order.premium, order.amount, and order.fiat_amount are all non-zero before processing an order. If this condition is met, the function sends a "can't do" message and exits early, enhancing error handling for order processing.

Changes

File Change Summary
src/app/order.rs Modified order_action function to include validation checks for order.premium, order.amount, and order.fiat_amount. Added early exit with a message if all three fields are non-zero.

Assessment against linked issues

Objective Addressed Explanation
Do not allow the creation of orders with premium/discount if it is not at market price (#397)

🐇 In the meadow, orders hop,
With checks in place, they won't flop.
Premiums and amounts must align,
Or a "can't do" message will shine!
Validation's here, a joyful cheer,
For orders now, there's nothing to fear! 🌼


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 0a94986 and ecb5835.

📒 Files selected for processing (1)
  • src/app/order.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/app/order.rs

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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e019e8a and f5ded67.

📒 Files selected for processing (1)
  • src/app/order.rs (1 hunks)
🔇 Additional comments (2)
src/app/order.rs (2)

70-74: Implementation successfully addresses PR objectives.

The validation check successfully prevents the creation of orders with both fixed price and premium, directly addressing issue #397. The placement of the check is appropriate within the order processing flow.


70-70: Consider additional validation for premium orders.

The current check only validates when all three fields are non-zero, but we might want to prevent any premium when fixed prices are set.

Let's verify if there are other premium-related validations in the codebase:

Consider a more strict validation:

-        if order.premium != 0 && order.amount !=0 && order.fiat_amount !=0 {
+        if order.premium != 0 && (order.amount != 0 || order.fiat_amount != 0) {

This would prevent any premium when either fixed amount is set, which might better align with the business logic.

src/app/order.rs Outdated
Comment on lines 70 to 74
if order.premium != 0 && order.amount !=0 && order.fiat_amount !=0 {
send_cant_do_msg(None, None, &event.sender).await;
return Ok(());
}

Copy link
Contributor

@coderabbitai coderabbitai bot Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling for premium validation.

While the validation logic correctly implements the business rule, the error handling could be more informative and maintain context.

Consider applying these improvements:

-        if order.premium != 0 && order.amount !=0 && order.fiat_amount !=0 {
-            send_cant_do_msg(None, None, &event.sender).await;
+        if order.premium != 0 && order.amount != 0 && order.fiat_amount != 0 {
+            send_new_order_msg(
+                request_id,
+                order.id,
+                Action::InvalidOrderCombination,
+                Some("Orders cannot have both fixed price and premium".to_string()),
+                &event.sender,
+            )
             return Ok(());
         }

This change:

  1. Preserves the request_id and order.id context
  2. Uses a more specific error message
  3. Uses send_new_order_msg for consistent error handling

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waiting for it to be decided whether to do #393 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Catrya, understood. I'll await the outcome of the discussion in PR #393.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from cosmetics of the code it makes sense to me.
Waiting for @grunch opinion

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

@arkanoider arkanoider self-requested a review December 10, 2024 16:35
@grunch
Copy link
Member

grunch commented Dec 10, 2024

@coderabbitai can you refactor the new code of this PR to be more rust idiomatic?

Copy link
Contributor

coderabbitai bot commented Dec 10, 2024

Tip

For best results, initiate chat on the files or code changes.

@grunch: Certainly! Here’s how you can refactor the new code to be more idiomatic in Rust:

Encapsulate the condition in a method

By creating a method within the Order struct, you enhance readability and adhere to Rust's idiomatic practices.

impl Order {
    fn has_premium_and_fixed_amounts(&self) -> bool {
        self.premium != 0 && self.amount != 0 && self.fiat_amount != 0
    }
}

Then, update the condition in your order_action function:

if order.has_premium_and_fixed_amounts() {
    send_cant_do_msg(request_id, None, None, &event.sender).await;
    return Ok(());
}

Use pattern matching with Option types

If premium, amount, and fiat_amount are of Option types, you can leverage pattern matching for a more idiomatic approach:

if let (Some(premium), Some(amount), Some(fiat_amount)) = (order.premium, order.amount, order.fiat_amount) {
    send_cant_do_msg(request_id, None, None, &event.sender).await;
    return Ok(());
}

This ensures the code only executes when all fields are Some(value), improving safety and readability.

Leverage the is_some method

Alternatively, you can use the is_some method for clarity:

if order.premium.is_some() && order.amount.is_some() && order.fiat_amount.is_some() {
    send_cant_do_msg(request_id, None, None, &event.sender).await;
    return Ok(());
}

This approach is clean and directly expresses the intent to check for the presence of values.

Let me know if you need further assistance!

🔗 Analysis chain
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Display the changes made to src/app/order.rs in this PR
git diff origin/main src/app/order.rs

Length of output: 613

Copy link
Member

@grunch grunch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@grunch grunch merged commit 3e5f7fa into MostroP2P:main Dec 11, 2024
2 checks passed
@Catrya Catrya deleted the new_order_premium_price branch December 11, 2024 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do not allow the creation of orders with premium/discount if it is not at market price
3 participants