Normally, the contract owner can rescue non-core ERC20 tokens held within the protocol by calling the OrderBook::emergencyWithdrawERC20
function. This is typically done when tokens are mistakenly sent or need to be recovered due to a misconfiguration. However, if a MEV bot has a sell order open for such a token, it can scan the mempool and front-run the emergency withdrawal by cancelling its sell order and retrieving the tokens before the withdrawal transaction is mined.
This causes the contract’s token balance to drop, and the subsequent safeTransfer
in emergencyWithdrawERC20
fails and reverts. As a result, the protocol owner is unable to rescue the non-core tokens, and the MEV bot successfully retains them.
This breaks the intended emergency fund recovery functionality and could block governance, protocol safety mechanisms, or fund management capabilities.
Reason 1: emergencyWithdrawERC20
can be front-run easily if sent through public RPC or non-private mempool.
Reason 2: The contract does not lock or reserve tokens being withdrawn during the pending mempool period.
Reason 3: Canceling an order returns the tokens immediately, allowing the bot to reclaim funds before the owner can.
Impact 1: The protocol owner is unable to withdraw non-core tokens even with correct conditions.
Impact 2: The MEV bot retrieves its tokens and prevents recovery of mistakenly sent or recoverable tokens.
Impact 3: Emergency control mechanisms of the protocol are bypassed, posing security and governance risks.
The MevBotAttack
contract simulates a bot that front-runs the owner’s emergency withdrawal transaction by canceling its sell order in the same mempool window. This reclaims the token from the contract, causing the emergency withdrawal to fail due to insufficient balance.
Add this contract to your TestOrderBook.t.sol
file:
âś… test_MevAttackOnEmergencyFundWithdraw
– Front-running Owner’s Emergency Rescue
Use private RPC providers like Flashbots RPC to protect governance and admin transactions from public mempool exposure.
Consider adding:
Pre-lock mechanism: Lock tokens for withdrawal during pending transactions.
Slippage or fallback conditions: Check that token balances meet minimum thresholds before execution.
Time-based grace period: Prevent order cancellation within N
seconds of emergency withdrawal attempts.
Commit-reveal schemes for sensitive admin operations like emergencyWithdrawERC20
.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.