Skip to content

classcool/async-swap

Repository files navigation

Async Swap CSMM Hook

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.

Install

To install dependencies in all our packages in packages/*

bun install

Install foundry dependencies (v4-periphery)

forge install

Setup

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

Local Deployment

Run deployment script

./start_script.sh # scripts that you use --account setup of you choice

Note

The start scripts will do the following:

  1. Deploy local PoolManger ./script/00_DeployPoolManager.s.sol
  2. Deploy Hook & Router contracts ./script/01_DeployHook.s.sol
  3. Initialize a pool with your hook attached ./script/02_InitilizePool.s.sol
  4. Add liqudity to previously initialized pool ./script/03_AddLiquidity.s.sol
  5. Submit an async swap transaction through custom router ./script/04_Swap.s.sol
  6. Fill previously submitted swap transaction ./script/05_ExecuteOrder.s.sol

Testing

Run tests

forge test -vvvv

Offchain Indexer

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

Acknowledgment

Thanks to Atrium Academy, over the past 2 months we build this project during Uniswap Hook incubator program.

Team Socials: