Skip to content

Uniswap v4 hook with limit order integration on orderbook

License

Notifications You must be signed in to change notification settings

standardweb3/v4-orderbook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

v4-orderbook

Image

Ultimate on-chain trading with both AMM and orderbook working together

"if ETH drops below $1500, market sell my bags"

"Only sell ETH when the price goes up to $1600"

"Allow slippage of 0.5% on market price, store amount which exceeds slippage threshold to limit order"

Integrated directly into the Uniswap V4 pools, limit/market orders are posted onchain and executed via the afterSwap() hook. No external bots or actors are required to guarantee execution.


How the hook works

Image

Hook lifecycle of v4-orderbook

The hook has two processes to customize in trading digital assets.

  1. Committing Capital: User first commits capital to trade in UniswapV4 to trade in AMM. beforeSwap() can modify amount to trade in amm for a certain tick or price by decoding it. This phase utilizes AMM to create price actions or instantly finalize trade.

  2. Hedging Capital: This phase uses orderbook to hedge user's digital assets to by only traded at certain limit price. afterSwap() customizes the amount and price to hedge user's left amount after trading in AMM. if there are lower orders placed than the limit price, the hedging capital is matched. If not, it is stored at the limit price to wait for matching orders. This protects slippage or price actions from MEV attacks.

Use Cases

  • Spot traders: protect slippage or theft by MEVs

  • Leverage traders: use stop loss proceeds to repay loans. Please see examples/README.md for usage

  • Lending Protocols (advanced): use limit/market/stop orders to liquidate collateral without significant price actions. Instead of liquidation bots and external participants, stop losses offer guaranteed execution

    • Note: additional safety is required to ensure that large market orders do not result in bad debt.

Features

  • Contribute to price action, protect on price drop: Traders now can avoid rug pull or dangerously volatile price actions by hedging a certain amount of capital in orderbook to only trade when price satisfies with their limit.

  • Safe and versatile liquidation process for decentralized lending/borrowing protocols: lending protocols do not need to worry about price actions on AMM when liquidating assets. They can take available profit in AMM where slippage is tolerant, then protect position by storing atomic swap order in orderbook in future.

  • Customizing trading strategy: By carefully adjusting AMM and orderbook trade amount, hook developers can make rug pull guard trading hook where it exposes certain capital to price increases on AMM, and hedge other capital to be traded at certain price to be only traded when the price keeps increasing. Good customized hooks can be sold at a hook marketplace just as trading algorithms are sold in financial market.

Building

forge build

Testing

requires foundry

Due to breaking changes in Uniswap v4 PoolManager, test is under development.

# tests require a local mainnet fork
forge test --fork-url https://eth.llamarpc.com --match-test test_placeOrder

Acknowledgements

this project was inspired by v4-stoploss, and the project was inspired by uniswap v4 example.

The difference of this project is to make the hook more scalable by implementing orderbook storage and matching engine with greedy algorithm, replacing key-value storage order without sorting or handling multiple orders. Hook contract has license of MIT. The orderbook exchange contracts have license of BUSL.

About

Uniswap v4 hook with limit order integration on orderbook

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published