Skip to content

Conversation

@squadgazzz
Copy link
Contributor

@squadgazzz squadgazzz commented Jan 9, 2026

Description

Adds a configurable margin-bps parameter per solver that adjusts order limits sent to solvers. This protects against negative slippage from external pricing APIs (e.g., OKX) that may quote optimistic prices that don't materialize on-chain.

The margin adjusts order amounts before sending to solvers:

  • Sell orders: Increases the minimum buy amount requirement (buy_amount / (1 - margin_factor))
  • Buy orders: Decreases the maximum sell amount allowed (sell_amount / (1 + margin_factor))

This forces solvers to find solutions with enough buffer to absorb expected negative slippage. The difference is handled through the protocol's existing buffer mechanism.

Example

With margin-bps = 500 (5%):

  • User sells 100 USDC for DAI with min buy of 95 DAI
  • Solver sees: sell 100 USDC, min buy 99.74 DAI (95 / 0.95) - Even if the solution experiences ~5% negative slippage, the user still receives at least their minimum

How to test

New driver and e2e tests.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @squadgazzz, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a 'quote haircut' mechanism designed to enhance the conservatism of competition bids. By allowing a configurable reduction in solver-reported executed amounts, the system can submit more cautious bids. This adjustment is performed without altering the underlying interaction calldata, ensuring that bids remain within acceptable limits and respect the order's minimum requirements.

Highlights

  • New Haircut Logic Module: A new module haircut.rs has been introduced under crates/driver/src/domain/competition/solution/ to encapsulate the logic for calculating 'haircutted' executed amounts. This function applies a configurable basis points reduction to solver-reported economics to make competition bids more conservative.
  • Configurable Haircut Parameter: The SolverConfig struct now includes a quote_haircut_bps field, allowing the haircut percentage (in basis points) to be configured via the driver's configuration file. This parameter defaults to 0, meaning no haircut is applied unless explicitly set.
  • Integration into Solution Processing: The haircut calculation has been integrated into the solution processing pipeline within crates/driver/src/infra/solver/dto/solution.rs. If quote_haircut_bps is greater than zero, the calculate_executed_amount function is called to adjust the executed amount of an order before the competition solution is finalized.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a 'quote haircut' feature, applying a configurable basis points reduction to solver-reported economics to make competition bids more conservative. The core logic is implemented in haircut.rs and includes plumbing the quote_haircut_bps configuration. A medium-severity Denial of Service vulnerability has been identified where a misconfiguration of the quote_haircut_bps parameter can cause a panic due to an arithmetic underflow, crashing the driver. It is recommended to use saturating_sub to prevent this. Additionally, a minor redundancy in the code was noted.

@squadgazzz squadgazzz changed the title Quote out amount haircut Solver solution out amount haircut Jan 9, 2026
@squadgazzz squadgazzz changed the title Solver solution out amount haircut Add solver margin configuration to protect against negative slippage Jan 13, 2026
@squadgazzz
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request introduces a configurable margin-bps parameter per solver to adjust order limits, protecting against negative slippage. This is a valuable addition for making bids more conservative. The changes involve adding the margin_bps field to relevant configuration structs, passing it through the system, and implementing the logic to adjust order amounts in the auction DTO and mock solver. A new test case has been added to verify the functionality. The review comments highlight important considerations regarding floating-point precision in financial calculations and the status of ignored tests, which should be addressed for robustness and comprehensive testing.

// This forces solvers to find solutions with enough surplus to cover
// the margin applied during competition scoring.
if margin_bps > 0 {
let factor = margin_bps as f64 / 10_000.0;
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Using f64 for financial calculations like factor = margin_bps as f64 / 10_000.0 can introduce floating-point precision errors. It's generally safer and more accurate to perform these calculations using integer arithmetic with U256 or BigRational to avoid unexpected rounding issues, especially when dealing with token amounts.

                    let factor = eth::U256::from(margin_bps);
                    let denominator = eth::U256::from(10_000);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The apply_factor accepts f64, so it should be fine I guess.

}
// Apply margin: reduce sell amount for buy orders
if config.margin_bps > 0 {
let factor = config.margin_bps as f64 / 10_000.0;
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Similar to the issue identified in dto/auction.rs, using f64 for factor calculation in the mock solver can introduce precision problems. For consistency and accuracy in financial simulations, it's best to use integer or BigRational arithmetic.

Suggested change
let factor = config.margin_bps as f64 / 10_000.0;
let factor = eth::U256::from(config.margin_bps);
let denominator = eth::U256::from(10_000);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ditto

}
// Apply margin: increase buy amount for sell orders
if config.margin_bps > 0 {
let factor = config.margin_bps as f64 / 10_000.0;
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Again, using f64 for factor calculation in the mock solver can lead to precision issues. It's recommended to use integer or BigRational arithmetic for financial calculations.

Suggested change
let factor = config.margin_bps as f64 / 10_000.0;
let factor = eth::U256::from(config.margin_bps);
let denominator = eth::U256::from(10_000);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ditto

@squadgazzz squadgazzz marked this pull request as ready for review January 13, 2026 12:50
@squadgazzz squadgazzz requested a review from a team as a code owner January 13, 2026 12:50
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request introduces a configurable margin-bps parameter, which is a sound approach. However, a correctness issue was identified where a 100% margin could lead to division by zero and incorrect order handling, which needs to be addressed. The overall approach is otherwise well-documented.

Comment on lines +137 to +140
if let Some(adjusted) =
available.buy.amount.apply_factor(1.0 / (1.0 - factor))
{
available.buy.amount = adjusted;
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The calculation 1.0 / (1.0 - factor) for sell orders can lead to division by zero if margin_bps is 10000 (i.e., factor is 1.0). In this scenario, apply_factor will return None due to floating-point division by zero resulting in inf, which cannot be converted to U256. The current implementation then logs a warning and proceeds with the original available.buy.amount, effectively ignoring the 100% margin. This is a correctness issue, as a 100% margin should make the order unfillable or require an impossibly large buy amount, not simply bypass the margin.

Consider adding a check to ensure margin_bps is strictly less than 10000 to prevent this edge case, or explicitly handle the None result from apply_factor as an unfillable order rather than silently using the unadjusted amount.

                            if margin_bps >= 10000 { // Handle 100% margin as an impossible order
                                tracing::warn!(
                                    "applying margin bps {} led to infinite buy amount for order \n                                     {:?}, treating as unfillable",
                                    margin_bps,
                                    order.uid
                                );
                                available.buy.amount = eth::U256::MAX; // Or some other indicator of unfillable
                            } else if let Some(adjusted) =
                                available.buy.amount.apply_factor(1.0 / (1.0 - factor))
                            {
                                available.buy.amount = adjusted;
                            } else {
                                tracing::warn!(
                                    "applying margin bps {} led to buy amount overflow for order \n                                     {:?}",
                                    margin_bps,
                                    order.uid
                                );
                            }

@squadgazzz squadgazzz marked this pull request as draft January 13, 2026 14:50
@squadgazzz squadgazzz closed this Jan 14, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Jan 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants