Repository navigation
Add Bitget buy-order support via reverse-quote endpoint - #4378
Conversation
Routes Side::Buy orders to /bgw-pro/swapx/pro/swapr with requestMode=minAmountOut. Bitget runs the recursion server-side, so we just translate the response into a dex::Swap. Output is capped to order.amount.get() to satisfy CoW exact-out fulfillment, any on-chain overshoot accrues to the settlement buffer as positive surplus. Unlocks Bitget as a native-price source for niche tokens, where the autopilot probes prices via OrderKind::Buy with a fixed native amount.
Off by default. When disabled, Side::Buy short-circuits to OrderNotSupported without touching the API. Enabled, buy orders go through the existing reverse-quote path. The reverse-quote path costs the user up to `slippage` of surplus on real auctions (overshoot accrues to the settlement buffer rather than the user), so we want a per-deployment switch before turning it on.
There was a problem hiding this comment.
Code Review
This pull request introduces support for buy orders in the Bitget solver by integrating their reverse-quote API endpoint (/swapr). It includes new data transfer objects for reverse-swap requests and responses, configuration options to enable or disable buy orders, and updated logic in the Bitget struct to handle both sell and buy order paths. My feedback focuses on two critical issues: the current decimal-to-wei conversion uses default rounding which is unsafe for financial settlements, and the buy order handler only processes the first transaction from the API response, potentially ignoring necessary multi-hop steps or setup calls.
There was a problem hiding this comment.
Code Review
This pull request introduces support for buy orders in the Bitget DEX solver by implementing the reverse-quote endpoint. Key changes include the addition of an enable_buy_orders configuration flag, new DTOs for reverse-swap requests, and specialized rounding logic for decimal-to-wei conversions to maintain settlement integrity. Feedback focused on a logic improvement for the sanity check of expected output amounts, suggesting that rounding down is safer to ensure consistency with on-chain enforcement.
There was a problem hiding this comment.
Code Review
This pull request implements buy order support for the Bitget DEX solver using the reverse-quote endpoint. The implementation includes new configuration settings, DTOs for reverse swaps, and a rounding-aware decimal-to-wei conversion utility. The review feedback identifies a logic error in the sanity check for expected output, recommending a change from rounding up to rounding down to prevent the acceptance of trades that could revert due to insufficient output.
There was a problem hiding this comment.
Code Review
This pull request implements buy order support for the Bitget DEX by integrating the reverse-quote API endpoint. The changes include a new configuration flag, DTOs for reverse-swap requests, and a rounding utility for converting API decimal responses to wei. Feedback was provided regarding the sanity check for expected output, suggesting the use of downward rounding to ensure the received amount strictly satisfies the order requirements and avoids on-chain reverts.
| // Compare the raw BigDecimals so the check doesn't depend on | ||
| // rounding direction. | ||
| let requested_out = wei_to_decimal(order.amount.get(), buy_decimals); | ||
| if response.expected_amount_out < requested_out { | ||
| return Err(Error::NotFound); | ||
| } |
There was a problem hiding this comment.
It's less than because "more than" (as stated in the docs) reverts to the buffer, right?
There was a problem hiding this comment.
Exactly. expected_amount_out > requested_out is the normal case (Bitget plans to deliver more than asked, the overshoot lands in the buffer). < requested_out means Bitget itself thinks it'll undershoot, in which case the on-chain minAmountOut check would revert. So we bail on the strict-less-than path and let the strict-greater-or-equal path through.
MartinquaXD
left a comment
There was a problem hiding this comment.
Change and tests look good to me. Only nits.
Description
Bitget's
/swaprendpoint supports buy quotes (requestMode = "minAmountOut") by running a recursion server-side on top of sell quotes. Wiring this up unlocks Bitget as a native-price source for the long tail of tokens that other estimators cover poorly. Today the integration only supports sell orders.The reverse-quote on real auction buy orders is correct end-to-end (positive overshoot accrues to the settlement buffer, the user receives exactly their signed buy amount, GPv2Settlement does no expected-vs-actual interaction-output check), but it can cost the user up to the configured slippage in surplus, so it's gated behind a config flag and off by default.
Changes
Side::Buycode path in the Bitget integration, calling/bgw-pro/swapx/pro/swaprwithrequestMode = "minAmountOut"order.buy.amountso CoW exact-out fulfillment passesenable-buy-ordersconfig flag, off by default, gating the buy pathdecimal_to_weinow rounds to the token's precision (Bitget can return more decimal digits than the token has)How to test
Existing tests plus new mock + ignored live tests. Run the live one with credentials: