Skip to content
Business logic Audit finding Intermediate

FOK orders wrongly revert on dust residual amounts below lot size

The CLOB contract reverts fill-or-kill orders when the remaining unmatched amount is greater than zero but smaller than the lot size, contrary to documented behaviour. This causes multi-hop swaps to fail when Uniswap output amounts cannot be adjusted to match the CLOB lot size.

montecristo Follow with 0x15, 0xterrah and 25 others

Originally disclosed on Code4rena

Submitted by montecristo, also found by 0x15, 0xterrah, Adotsam, ahahaHard1k, Angry_Mustache_Man, anonymousjoe, befree3x, BenRai, boredpukar, dhank, dmdg321, edoscoba, KineticsOfWeb3, lodelux, lonelybones, Ollam, Olugbenga, princekay, random1106, Shahil_Hussain, Soosh, touristS, udogodwin, volifoet, Web3Hunters, willycode20, and Yanx

clob/CLOB.sol #L440

The CLOB contract unnecessarily reverts FOK orders when the remaining unmatched amount is gte (pun intended) 0, but smaller than lotSizeInBase, contrary to the documented behavior. This leads to reverts of multi-hop swaps where exact output amounts from UniswapV2 cannot be adjusted to match CLOB’s lot size.

According to the README:

On CLOB fill, filled amounts are rounded down to the nearest lot. FOK fill orders should not revert if only the amount rounded off is left unfilled, and the user is not charged for the dust.

It can be understood in either one of the following ways:

  • If the FOK fill order amount is slightly less than the matching limit order, the transaction should not revert
  • If the FOK fill order amount is slightly greater than the matching limit order, the transaction should not revert

However, the current implementation does not behave as described.

For example, let’s consider there is a limit ask order and a fill bid order, with lotSizeInBase = 1e6:

Case 1: If fill bid order amount is slightly less than limit ask order:

  • Limit ask order

    • amountInBase: 1e18 (1 WETH)
    • price: 3000e6 (3000 USD)
  • Fill bid order

    • amountInBase: 1e18 - 1
    • price: 3000e6
    • fillOrderType: FOK
  • After matching:

    • matchData.baseDelta: 1e18 - 1e6
    • matchData.quoteDelta: (1e18 - 1e6) * 3000e6 / 1e18 = 2999999999
    • newOrder.amount: 1e18 - 1 - (1e18 - 1e6) = 1e6 - 1

Thus, the transaction reverts due to the following check:

File: contracts/clob/CLOB.sol

440:         if (args.fillOrderType == FillOrderType.FILL_OR_KILL && newOrder.amount > 0) revert FOKOrderNotFilled();

Case 2: If fill bid order amount is slightly greater than limit ask order:

  • Limit ask order 1

    • amountInBase: 1e18 (1 WETH)
    • price: 3000e6 (3000 USD)
  • Limit ask order 2 (ensure there are sufficient matching orders)

    • amountInBase: 1e18 (1 WETH)
    • price: 3000e6 (3000 USD)
  • Fill bid order

    • amountInBase: 1e18 + 1
    • price: 3000e6
    • fillOrderType: FOK
  • After matching ask order 1:

    • matchData.baseDelta: 1e18
    • matchData.quoteDelta: 3000e6
    • newOrder.amount: 1
  • After matching ask order 2:

    • matchData.baseDelta: 0
    • matchData.quoteDelta: 0
    • newOrder.amount: 1

Thus, order fill will again revert with FOKOrderNotFilled error.

Indeed, order fill should not revert if newOrder.amount < ds.settings().lotSizeInBase

Impact

One might argue that this is a user mistake, as the user should not input quirky amounts like 1e18 - 1 or 1e18 + 1 in the first place.

However, this can happen with no user mistake in a practical scenario:

  • Assume user wants to do a multihop swap in GTERouter
  • The first hop will be done on Uniswap
  • The second hop will be done on GTE

In this case, Uniswap’s output amount will be set as GTE FOK fill order’s amount:

File: contracts/router/GTERouter.sol

310:         fillArgs.amount = route.prevAmountOut; // @audit this is UniswapV2's output amount
311:         fillArgs.fillOrderType = ICLOB.FillOrderType.FILL_OR_KILL; // @audit FOK type is enforced

The user cannot adjust Uniswap’s output to match GTE’s lot size. So if Uniswap’s output amount is slightly greater or less than matching order’s amount, the whole swap will revert with FOKOrderNotFilled error.

Recommended Mitigation Steps

diff --git a/contracts/clob/CLOB.sol b/contracts/clob/CLOB.sol
index 2131fcc..0f9f2a3 100644
--- a/contracts/clob/CLOB.sol
+++ b/contracts/clob/CLOB.sol
@@ -437,7 +437,7 @@ contract CLOB is ICLOB, Ownable2StepUpgradeable {
         (uint256 totalQuoteSent, uint256 totalBaseReceived) = _matchIncomingBid(ds, newOrder, args.amountIsBase);

         if (totalQuoteSent == 0 || totalBaseReceived == 0) revert ZeroCostTrade();
-        if (args.fillOrderType == FillOrderType.FILL_OR_KILL && newOrder.amount > 0) revert FOKOrderNotFilled();
+        if (args.fillOrderType == FillOrderType.FILL_OR_KILL && newOrder.amount >= ds.settings().lotSizeInBase) revert FOKOrderNotFilled();

         // slither-disable-next-line reentrancy-events This external call is to the factory
         uint256 takerFee = _settleIncomingOrder(ds, account, Side.BUY, totalQuoteSent, totalBaseReceived);

View detailed Proof of Concept


The original report as published on Code4rena, by montecristo. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The check for fill-or-kill orders reverts whenever any amount remains unfilled, instead of only reverting when the remaining amount is at least the lot size.

Impact

Multi-hop swaps through GTERouter can revert when Uniswap output amounts are slightly above or below the matching order amount, because the user cannot adjust Uniswap's output to match the CLOB lot size.

What the attacker needed

Multi-hop swap via GTERouter; Uniswap output not matching lot size

The fix

The check should revert only when the remaining amount is greater than or equal to the lot size, not whenever it is greater than zero.

How it was found

  1. Set up a limit ask order with amountInBase 1e18 and price 3000e6.
  2. Create a fill bid order with amountInBase 1e18 - 1, same price, and fillOrderType FOK.
  3. After matching, the remaining amount is 1e6 - 1, which is greater than zero but less than lotSizeInBase (1e6).
  4. The transaction reverts with FOKOrderNotFilled.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A fill-or-kill order must be filled completely or not at all. The contract checks if any amount is left unfilled and cancels the order, but it should ignore tiny leftover amounts smaller than the minimum trade size (lot size). This makes some swaps fail even though the user did nothing wrong.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by montecristo

View all

10 more disclosed reports, best first.

More from GTE Spot CLOB and Router

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.