// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; import {IUnlockCallback} from "@uniswap/v4-core/src/interfaces/callback/IUnlockCallback.sol"; import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol"; import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol"; import {ModifyLiquidityParams} from "@uniswap/v4-core/src/types/PoolOperation.sol"; import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol"; import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol"; import {SqrtPriceMath} from "@uniswap/v4-core/src/libraries/SqrtPriceMath.sol"; import {Position} from "@uniswap/v4-core/src/libraries/Position.sol"; import {LiquidityAmounts} from "@uniswap/v4-periphery/src/libraries/LiquidityAmounts.sol"; import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import {KibblePool} from "./libraries/KibblePool.sol"; /// @title LpLocker /// @notice Owns every HYPE / KIBBLE liquidity position of the protocol, forever (docs §3, §6, §9, §11). /// @dev Positions are owned directly in the v4 PoolManager by this contract (owner = address(this), /// full range, salt = bytes32(index)). This contract can only ADD liquidity: it never calls /// modifyLiquidity with a negative delta, has no withdraw, no transfer, no owner and no collect. /// Each add uses a fresh salt so no existing position is ever touched again: in v4, touching a position /// settles its accrued fees to the owner, so with fresh salts swap fees accrued by locked positions are never /// collected and stay in the PoolManager forever. contract LpLocker is IUnlockCallback { using SafeERC20 for IERC20; using PoolIdLibrary for PoolKey; using StateLibrary for IPoolManager; error NotPoolManager(); error RefundFailed(); /// @notice Not a docs event: emitted on every locked add so the LP history can be indexed. event LiquidityLocked(uint256 indexed index, uint128 liquidity, uint256 hypeAmount, uint256 kibbleAmount); IPoolManager public immutable poolManager; address public immutable kibble; PoolId public immutable poolId; /// @notice Number of locked positions (position i has salt bytes32(i)). uint256 public positionCount; /// @notice Sum of the liquidity of all locked positions. Never decreases. uint256 public totalLiquidity; /// @notice Cumulative HYPE deposited into locked positions. uint256 public lockedHype; /// @notice Cumulative KIBBLE deposited into locked positions. uint256 public lockedKibble; /// @notice Liquidity of position i at creation. mapping(uint256 => uint128) public positionLiquidity; constructor(IPoolManager poolManager_, address kibble_) { poolManager = poolManager_; kibble = kibble_; poolId = KibblePool.key(kibble_).toId(); } /// @notice Adds full-range liquidity with `msg.value` HYPE and up to `kibbleAmount` KIBBLE (pulled from the /// caller, approval needed) and locks it forever. Unused HYPE / KIBBLE is refunded to the caller. /// Called by `Genesis` at block 0 and by `Zapper` on every rig purchase after block 0. Anyone may call: /// adding is a donation to locked liquidity. function addLiquidity(uint256 kibbleAmount) external payable returns (uint128 liquidity, uint256 hypeUsed, uint256 kibbleUsed) { if (kibbleAmount > 0) IERC20(kibble).safeTransferFrom(msg.sender, address(this), kibbleAmount); (uint160 sqrtP,,,) = poolManager.getSlot0(poolId); if (sqrtP != 0 && msg.value > 1 && kibbleAmount > 1) { // 1 wei margin on each side absorbs v4's round-up when converting liquidity back to amounts. liquidity = LiquidityAmounts.getLiquidityForAmounts( sqrtP, TickMath.getSqrtPriceAtTick(KibblePool.tickLower()), TickMath.getSqrtPriceAtTick(KibblePool.tickUpper()), msg.value - 1, kibbleAmount - 1 ); } if (liquidity > 0) { uint256 index = positionCount; (hypeUsed, kibbleUsed) = abi.decode(poolManager.unlock(abi.encode(index, liquidity)), (uint256, uint256)); positionCount = index + 1; positionLiquidity[index] = liquidity; totalLiquidity += liquidity; lockedHype += hypeUsed; lockedKibble += kibbleUsed; emit LiquidityLocked(index, liquidity, hypeUsed, kibbleUsed); } uint256 hypeBack = msg.value - hypeUsed; uint256 kibbleBack = kibbleAmount - kibbleUsed; if (kibbleBack > 0) IERC20(kibble).safeTransfer(msg.sender, kibbleBack); if (hypeBack > 0) { (bool ok,) = msg.sender.call{value: hypeBack}(""); if (!ok) revert RefundFailed(); } } /// @inheritdoc IUnlockCallback function unlockCallback(bytes calldata data) external returns (bytes memory) { if (msg.sender != address(poolManager)) revert NotPoolManager(); (uint256 index, uint128 liquidity) = abi.decode(data, (uint256, uint128)); PoolKey memory key = KibblePool.key(kibble); (BalanceDelta delta,) = poolManager.modifyLiquidity( key, ModifyLiquidityParams({ tickLower: KibblePool.tickLower(), tickUpper: KibblePool.tickUpper(), liquidityDelta: int256(uint256(liquidity)), salt: bytes32(index) }), "" ); uint256 hypeOwed = uint256(uint128(-delta.amount0())); uint256 kibbleOwed = uint256(uint128(-delta.amount1())); if (hypeOwed > 0) poolManager.settle{value: hypeOwed}(); if (kibbleOwed > 0) { poolManager.sync(key.currency1); IERC20(kibble).safeTransfer(address(poolManager), kibbleOwed); poolManager.settle(); } return abi.encode(hypeOwed, kibbleOwed); } // ------------------------------------------------------------------ views (docs §14 "LP locked") /// @notice Liquidity of locked position i as stored in the PoolManager (proves it is still there). function onchainPositionLiquidity(uint256 i) public view returns (uint128) { bytes32 key = Position.calculatePositionKey(address(this), KibblePool.tickLower(), KibblePool.tickUpper(), bytes32(i)); return poolManager.getPositionLiquidity(poolId, key); } /// @notice HYPE and KIBBLE currently represented by all locked positions at the current pool price /// (principal only, fees excluded). function lockedAmounts() external view returns (uint256 hype, uint256 kibbleAmt) { (uint160 sqrtP,,,) = poolManager.getSlot0(poolId); uint256 l = totalLiquidity; if (sqrtP == 0 || l == 0) return (0, 0); uint160 sqrtUpper = TickMath.getSqrtPriceAtTick(KibblePool.tickUpper()); uint160 sqrtLower = TickMath.getSqrtPriceAtTick(KibblePool.tickLower()); hype = SqrtPriceMath.getAmount0Delta(sqrtP, sqrtUpper, uint128(l), false); kibbleAmt = SqrtPriceMath.getAmount1Delta(sqrtLower, sqrtP, uint128(l), false); } /// @notice Pool key of the locked liquidity. function poolKey() external view returns (PoolKey memory) { return KibblePool.key(kibble); } }