StarUNIHOOKUNIHOOKUNIHOOK

Market Maker Hook 旨在在 Aggregator Hook 的基础上拓展一个实用案例:使用一个 MatchEngine 维护做市商们提供的 orderbook,当用户发起交易时,使用 Aggregator Hook 的技术填充流动性,使得用户能直接使用 orderbook 的报价。这种设计也使得做市商无缝接入 Uniswap V4 生态成为可能。

Aggregator // Order Limit // Tick Mapping

Quote

$ Query the best price for a swap...

> Searching liqudity:

> Comparing prices:

> Best price found:

> How to fill liquidity in Uniswap Tick from third party quote?

Mapping LP positions from various DEXs to Uniswap V4 ticks, ensuring precise alignment and optimal pricing for seamless cross-platform liquidity provision.
uint160 sqrtPriceX96 = TickMath.getSqrtRatioAtTick(curTick);

if(zeroForOne) {
  uint256 tmp1 = fromAmount * uint256(sqrtPriceX96) / Q96 *uint256(sqrtPriceX96) / Q96- toAmount;
  uint256 tmp2 = fromAmount * uint256(sqrtPriceX96) * toAmount / Q96;
  liquidity = uint128(tmp2 / tmp1);
} else {
  uint256 tmp1 = fromAmount - toAmount * uint256(sqrtPriceX96) / Q96 * uint256(sqrtPriceX96) / Q96;
  uint256 tmp2 = fromAmount * uint256(sqrtPriceX96) * toAmount / Q96;
  liquidity = uint128(tmp2 / tmp1);
}
Liquidity

Before Swap

$ Add liquidity to the pool...

> Before swap, remove liquidity last left...

> Calc a new tickLower, tickUpper and slot0 tick from quote

Optimizing liquidity provision by adjusting tick ranges to ensure 50% of the price range meets single-sided trading needs, with dynamic corrections to maintain target prices within the optimal zone.
Liquidity

Swap

$ Execute swap...

> Swap 2423.80 USDC to 1 ETH

> 👏 Swap successful!

After Swap

$ Check the price diff...

> Pool stats updated:

Liquidity is now `poolManager.getLiquidity(poolId)`, current Tick is `poolManager.getTick(poolId)`, and Delta amounts are `delta.amount0()` and `delta.amount1()`
if (delta.amount0() > 0 && delta.amount1() < 0) {
  priceDiff = int256(targetAmount + delta.amount1()) * 1e18 / targetAmount;
}

if (delta.amount0() < 0 && delta.amount1() > 0) {
  priceDiff = int256(targetAmount + delta.amount0()) * 1e18 / targetAmount;
}   

San Francisco, CA // ETHGlobal Hackathon 2024/10/19