We use Uniswap V4’s hook to implement a new batch-auction style MEV-resilient mechanism for AMM. Our approach expands the recent theoretical work of https://dl.acm.org/doi/10.1145/3564246.3585233, and mitigates MEVs by imposing a specific transaction ordering rule so that transactions in different directions (buy or sell) are matched as much as possible. A technical challenge in our hook implementation is that we need to impose constraints (the transaction ordering rule) on the block level instead of individual transaction levels. Our MEV-resilient AMM has a nice property in that an MEV-maximizing builder will order transactions in such a way that no MEV opportunities remain.
Batch auctions have been advocated for preventing manipulative behaviors either in traditional limit-order markets (e.g., Budish, Crampton, and Shin 2015 against HFT rat race) or on AMMs (e.g., Ferreira and Parks 2023 against MEV). In this project, we demonstrate how Uniswap V4 hooks can implement batch auctions natively on constant-product AMMs. We overcome the technical challenge in our hook implementation in that we need to impose constraints (the transaction ordering rule) on the block level instead of individual transaction levels. Our resulting MEV-resilient AMM has a nice property in that an MEV-maximizing builder will order transactions in such a way that no MEV opportunities remain.
- AsyncCSMM - hook contract
- Router - add liquidity, swap & fill async orders
- Live Demo Frontend
- Video Walkthrough
To install dependencies in all our packages in packages/*
bun install
Install foundry dependencies (v4-periphery)
forge install
Tip
We suggest you set up local anvil account with cast.
cast wallet import --mnemonic "test test test test test test test test test test test junk" anvil
- This will allow you to use
--account anvil
in the deploys scripts in./start_script.sh
Run local anvil node
anvil
# or simulate block mining and finality
anvil --block-time 13
Run deployment script
./start_script.sh # scripts that you use --account setup of you choice
Note
The start scripts will do the following:
- Deploy local PoolManger
./script/00_DeployPoolManager.s.sol
- Deploy Hook & Router contracts
./script/01_DeployHook.s.sol
- Initialize a pool with your hook attached
./script/02_InitilizePool.s.sol
- Add liqudity to previously initialized pool
./script/03_AddLiquidity.s.sol
- Submit an async swap transaction through custom router
./script/04_Swap.s.sol
- Fill previously submitted swap transaction
./script/05_ExecuteOrder.s.sol
Run tests
forge test -vvvv
Start local indexer
bun run dev
Tip
- If you need typescript abi for your contracts on frontend or indexer use this script
./generateAbi.sh
./generateAbi.sh
Go to http://localhost:42069 to query orders from hook events
Thanks to Atrium Academy, over the past 2 months we build this project during Uniswap Hook incubator program.
Team Socials: