|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import { |
| 5 | + toBeforeSwapDelta, BeforeSwapDelta, BeforeSwapDeltaLibrary |
| 6 | +} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol"; |
| 7 | +import {Currency, CurrencyLibrary} from "@uniswap/v4-core/src/types/Currency.sol"; |
| 8 | +import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; |
| 9 | +import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol"; |
| 10 | +import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol"; |
| 11 | +import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol"; |
| 12 | +import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; |
| 13 | +import {BaseHook} from "../../utils/BaseHook.sol"; |
| 14 | +import {DeltaResolver} from "../DeltaResolver.sol"; |
| 15 | +import {ModifyLiquidityParams, SwapParams} from "@uniswap/v4-core/src/types/PoolOperation.sol"; |
| 16 | + |
| 17 | +/// @title BaseLiquidityAdapterHook |
| 18 | +/// @author Uniswap V4 Hooks |
| 19 | +/// @notice Abstract base contract for adapting external liquidity sources to Uniswap V4 pools |
| 20 | +/// @dev Provides core functionality for integrating alternative AMMs and liquidity protocols with V4 |
| 21 | +/// @dev Liquidity operations (add/remove) are disabled - all liquidity is managed externally |
| 22 | +/// @dev Inheritors must implement: _swapExactInput, _swapExactOutput, and _liquidityExists |
| 23 | +abstract contract BaseLiquidityAdapterHook is BaseHook, DeltaResolver { |
| 24 | + using CurrencyLibrary for Currency; |
| 25 | + using SafeCast for int256; |
| 26 | + using SafeCast for uint256; |
| 27 | + |
| 28 | + /// @notice Thrown when attempting to add or remove liquidity |
| 29 | + /// @dev Liquidity operations are disabled as liquidity is managed by the external source |
| 30 | + error LiquidityNotAllowed(); |
| 31 | + |
| 32 | + /// @notice Thrown when initializing a pool that doesn't have corresponding external liquidity |
| 33 | + /// @dev The external liquidity source must support the given token pair |
| 34 | + error InvalidPool(); |
| 35 | + |
| 36 | + /// @notice Initializes the base liquidity adapter hook |
| 37 | + /// @param _manager The Uniswap V4 pool manager contract |
| 38 | + constructor(IPoolManager _manager) BaseHook(_manager) {} |
| 39 | + |
| 40 | + /// @inheritdoc BaseHook |
| 41 | + function getHookPermissions() public pure override returns (Hooks.Permissions memory) { |
| 42 | + return Hooks.Permissions({ |
| 43 | + beforeInitialize: true, |
| 44 | + beforeSwap: true, |
| 45 | + beforeSwapReturnDelta: true, |
| 46 | + beforeAddLiquidity: true, |
| 47 | + afterSwap: false, |
| 48 | + afterInitialize: false, |
| 49 | + beforeRemoveLiquidity: false, |
| 50 | + afterAddLiquidity: false, |
| 51 | + afterRemoveLiquidity: false, |
| 52 | + beforeDonate: false, |
| 53 | + afterDonate: false, |
| 54 | + afterSwapReturnDelta: false, |
| 55 | + afterAddLiquidityReturnDelta: false, |
| 56 | + afterRemoveLiquidityReturnDelta: false |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + /// @notice Hook called before pool initialization to validate compatibility |
| 61 | + /// @dev Ensures the external liquidity source supports the given token pair |
| 62 | + /// @param poolKey The pool configuration containing token pair and fee settings |
| 63 | + /// @return bytes4 The beforeInitialize selector on success |
| 64 | + function _beforeInitialize(address, PoolKey calldata poolKey, uint160) internal view override returns (bytes4) { |
| 65 | + // ensure the pool is supported by the underlying liquidity source |
| 66 | + if (!_liquidityExists(poolKey)) revert InvalidPool(); |
| 67 | + |
| 68 | + return IHooks.beforeInitialize.selector; |
| 69 | + } |
| 70 | + |
| 71 | + /// @notice Hook called before adding liquidity - always reverts |
| 72 | + /// @dev Liquidity provision is disabled as all liquidity comes from external source |
| 73 | + function _beforeAddLiquidity(address, PoolKey calldata, ModifyLiquidityParams calldata, bytes calldata) |
| 74 | + internal |
| 75 | + pure |
| 76 | + override |
| 77 | + returns (bytes4) |
| 78 | + { |
| 79 | + revert LiquidityNotAllowed(); |
| 80 | + } |
| 81 | + |
| 82 | + /// @notice Hook called before swap execution to route through external liquidity |
| 83 | + /// @dev Handles both exact input (amountSpecified < 0) and exact output (amountSpecified > 0) |
| 84 | + /// @param poolKey The pool configuration |
| 85 | + /// @param params Swap parameters including direction, amount, and sqrtPriceLimit |
| 86 | + /// @return bytes4 The beforeSwap selector |
| 87 | + /// @return swapDelta The token deltas for pool accounting |
| 88 | + /// @return uint24 LP fee override (always 0 as fees are handled externally) |
| 89 | + function _beforeSwap(address, PoolKey calldata poolKey, SwapParams calldata params, bytes calldata) |
| 90 | + internal |
| 91 | + override |
| 92 | + returns (bytes4, BeforeSwapDelta swapDelta, uint24) |
| 93 | + { |
| 94 | + bool isExactInput = params.amountSpecified < 0; |
| 95 | + |
| 96 | + if (isExactInput) { |
| 97 | + uint256 amountOut = _swapExactInput(poolKey, params); |
| 98 | + swapDelta = toBeforeSwapDelta(-params.amountSpecified.toInt128(), -int128(int256(amountOut))); |
| 99 | + } else { |
| 100 | + uint256 amountIn = _swapExactOutput(poolKey, params); |
| 101 | + swapDelta = toBeforeSwapDelta(-params.amountSpecified.toInt128(), int128(int256(amountIn))); |
| 102 | + } |
| 103 | + |
| 104 | + return (IHooks.beforeSwap.selector, swapDelta, 0); |
| 105 | + } |
| 106 | + |
| 107 | + /// @inheritdoc DeltaResolver |
| 108 | + /// @notice Settles positive deltas by transferring tokens to the pool manager |
| 109 | + /// @param token The currency to transfer |
| 110 | + /// @param amount The amount to transfer to the pool manager |
| 111 | + function _pay(Currency token, address, uint256 amount) internal override { |
| 112 | + token.transfer(address(poolManager), amount); |
| 113 | + } |
| 114 | + |
| 115 | + /// @notice Executes a swap with exact input amount through external liquidity |
| 116 | + /// @param poolKey The pool configuration |
| 117 | + /// @param params Swap parameters with negative amountSpecified |
| 118 | + /// @return amountOut The amount of output tokens received |
| 119 | + function _swapExactInput(PoolKey calldata poolKey, SwapParams calldata params) |
| 120 | + internal |
| 121 | + virtual |
| 122 | + returns (uint256 amountOut); |
| 123 | + |
| 124 | + /// @notice Executes a swap with exact output amount through external liquidity |
| 125 | + /// @param poolKey The pool configuration |
| 126 | + /// @param params Swap parameters with positive amountSpecified |
| 127 | + /// @return amountIn The amount of input tokens required |
| 128 | + function _swapExactOutput(PoolKey calldata poolKey, SwapParams calldata params) |
| 129 | + internal |
| 130 | + virtual |
| 131 | + returns (uint256 amountIn); |
| 132 | + |
| 133 | + /// @notice Checks if the external liquidity source supports the given pool |
| 134 | + /// @param poolKey The pool configuration to validate |
| 135 | + /// @return exists True if external liquidity exists for this token pair |
| 136 | + function _liquidityExists(PoolKey calldata poolKey) internal view virtual returns (bool exists); |
| 137 | +} |
0 commit comments