-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<WIP> feat: Proxy Hook #7
Draft
chef-omelette
wants to merge
1
commit into
main
Choose a base branch
from
feat/proxy-hook
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,399 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
|
||
pragma solidity ^0.8.26; | ||
|
||
import { | ||
ICLHooks, | ||
HOOKS_BEFORE_INITIALIZE_OFFSET, | ||
HOOKS_AFTER_INITIALIZE_OFFSET, | ||
HOOKS_BEFORE_ADD_LIQUIDITY_OFFSET, | ||
HOOKS_AFTER_ADD_LIQUIDITY_OFFSET, | ||
HOOKS_BEFORE_REMOVE_LIQUIDITY_OFFSET, | ||
HOOKS_AFTER_REMOVE_LIQUIDITY_OFFSET, | ||
HOOKS_BEFORE_SWAP_OFFSET, | ||
HOOKS_AFTER_SWAP_OFFSET, | ||
HOOKS_BEFORE_DONATE_OFFSET, | ||
HOOKS_AFTER_DONATE_OFFSET, | ||
HOOKS_BEFORE_SWAP_RETURNS_DELTA_OFFSET, | ||
HOOKS_AFTER_SWAP_RETURNS_DELTA_OFFSET, | ||
HOOKS_AFTER_ADD_LIQUIDIY_RETURNS_DELTA_OFFSET, | ||
HOOKS_AFTER_REMOVE_LIQUIDIY_RETURNS_DELTA_OFFSET | ||
} from "@pancakeswap/v4-core/src/pool-cl/interfaces/ICLHooks.sol"; | ||
import {ICLPoolManager} from "@pancakeswap/v4-core/src/pool-cl/interfaces/ICLPoolManager.sol"; | ||
import {BalanceDelta, BalanceDeltaLibrary} from "@pancakeswap/v4-core/src/types/BalanceDelta.sol"; | ||
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@pancakeswap/v4-core/src/types/BeforeSwapDelta.sol"; | ||
import {PoolKey} from "@pancakeswap/v4-core/src/types/PoolKey.sol"; | ||
import {Hooks} from "@pancakeswap/v4-core/src/libraries/Hooks.sol"; | ||
import {LPFeeLibrary} from "@pancakeswap/v4-core/src/libraries/LPFeeLibrary.sol"; | ||
import {CLHooks} from "@pancakeswap/v4-core/src/pool-cl/libraries/CLHooks.sol"; | ||
|
||
import {CLBaseHook} from "../CLBaseHook.sol"; | ||
|
||
/// @notice A proxy hook for combining multiple hooks | ||
contract CLProxyHook is CLBaseHook { | ||
using Hooks for bytes32; | ||
using LPFeeLibrary for uint24; | ||
|
||
ICLHooks[] public hooks; | ||
|
||
uint16 private _hooksRegistrationBitmap; | ||
|
||
constructor(ICLPoolManager poolManager, ICLHooks[] memory hooks_) CLBaseHook(poolManager) { | ||
hooks = hooks_; | ||
|
||
uint16 hooksRegistrationBitmap; | ||
for (uint256 i = 0; i < hooks_.length; ++i) { | ||
hooksRegistrationBitmap |= hooks[i].getHooksRegistrationBitmap(); | ||
} | ||
_validatePermissionsConflict(hooksRegistrationBitmap); | ||
_hooksRegistrationBitmap = hooksRegistrationBitmap; | ||
} | ||
|
||
function getHooksRegistrationBitmap() external view override returns (uint16) { | ||
return _hooksRegistrationBitmap; | ||
} | ||
|
||
/// @notice Validate hook permission, eg. if before_swap_return_delta is set, before_swap_delta must be set | ||
function _validatePermissionsConflict(uint16 hooksRegistrationBitmap) internal pure { | ||
bytes32 parameters = bytes32(uint256(hooksRegistrationBitmap)); | ||
if ( | ||
parameters.hasOffsetEnabled(HOOKS_BEFORE_SWAP_RETURNS_DELTA_OFFSET) | ||
&& !parameters.hasOffsetEnabled(HOOKS_BEFORE_SWAP_OFFSET) | ||
) { | ||
revert Hooks.HookPermissionsValidationError(); | ||
} | ||
|
||
if ( | ||
parameters.hasOffsetEnabled(HOOKS_AFTER_SWAP_RETURNS_DELTA_OFFSET) | ||
&& !parameters.hasOffsetEnabled(HOOKS_AFTER_SWAP_OFFSET) | ||
) { | ||
revert Hooks.HookPermissionsValidationError(); | ||
} | ||
|
||
if ( | ||
parameters.hasOffsetEnabled(HOOKS_AFTER_ADD_LIQUIDIY_RETURNS_DELTA_OFFSET) | ||
&& !parameters.hasOffsetEnabled(HOOKS_AFTER_ADD_LIQUIDITY_OFFSET) | ||
) { | ||
revert Hooks.HookPermissionsValidationError(); | ||
} | ||
|
||
if ( | ||
parameters.hasOffsetEnabled(HOOKS_AFTER_REMOVE_LIQUIDIY_RETURNS_DELTA_OFFSET) | ||
&& !parameters.hasOffsetEnabled(HOOKS_AFTER_REMOVE_LIQUIDITY_OFFSET) | ||
) { | ||
revert Hooks.HookPermissionsValidationError(); | ||
} | ||
} | ||
|
||
function beforeInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96, bytes calldata hookData) | ||
external | ||
override | ||
returns (bytes4) | ||
{ | ||
bytes32 parameters = bytes32(uint256(_hooksRegistrationBitmap)); | ||
if (!parameters.hasOffsetEnabled(HOOKS_BEFORE_INITIALIZE_OFFSET)) { | ||
revert HookNotImplemented(); | ||
} | ||
|
||
for (uint256 i = 0; i < hooks.length; ++i) { | ||
ICLHooks hook = hooks[i]; | ||
bytes32 hookParameters = bytes32(uint256(hook.getHooksRegistrationBitmap())); | ||
|
||
if (!hookParameters.hasOffsetEnabled(HOOKS_BEFORE_INITIALIZE_OFFSET)) { | ||
continue; | ||
} | ||
|
||
if (hook.beforeInitialize(sender, key, sqrtPriceX96, hookData) != ICLHooks.beforeInitialize.selector) { | ||
revert Hooks.InvalidHookResponse(); | ||
} | ||
} | ||
|
||
return this.beforeInitialize.selector; | ||
} | ||
|
||
function afterInitialize( | ||
address sender, | ||
PoolKey calldata key, | ||
uint160 sqrtPriceX96, | ||
int24 tick, | ||
bytes calldata hookData | ||
) external override returns (bytes4) { | ||
bytes32 parameters = bytes32(uint256(_hooksRegistrationBitmap)); | ||
if (!parameters.hasOffsetEnabled(HOOKS_AFTER_INITIALIZE_OFFSET)) { | ||
revert HookNotImplemented(); | ||
} | ||
|
||
for (uint256 i = 0; i < hooks.length; ++i) { | ||
ICLHooks hook = hooks[i]; | ||
bytes32 hookParameters = bytes32(uint256(hook.getHooksRegistrationBitmap())); | ||
|
||
if (!hookParameters.hasOffsetEnabled(HOOKS_AFTER_INITIALIZE_OFFSET)) { | ||
continue; | ||
} | ||
|
||
if (hook.afterInitialize(sender, key, sqrtPriceX96, tick, hookData) != ICLHooks.afterInitialize.selector) { | ||
revert Hooks.InvalidHookResponse(); | ||
} | ||
} | ||
|
||
return this.afterInitialize.selector; | ||
} | ||
|
||
function beforeAddLiquidity( | ||
address sender, | ||
PoolKey calldata key, | ||
ICLPoolManager.ModifyLiquidityParams calldata params, | ||
bytes calldata hookData | ||
) external override returns (bytes4) { | ||
bytes32 parameters = bytes32(uint256(_hooksRegistrationBitmap)); | ||
if (!parameters.hasOffsetEnabled(HOOKS_BEFORE_ADD_LIQUIDITY_OFFSET)) { | ||
revert HookNotImplemented(); | ||
} | ||
|
||
for (uint256 i = 0; i < hooks.length; ++i) { | ||
ICLHooks hook = hooks[i]; | ||
bytes32 hookParameters = bytes32(uint256(hook.getHooksRegistrationBitmap())); | ||
|
||
if (!hookParameters.hasOffsetEnabled(HOOKS_BEFORE_ADD_LIQUIDITY_OFFSET)) { | ||
continue; | ||
} | ||
|
||
if (hook.beforeAddLiquidity(sender, key, params, hookData) != ICLHooks.beforeAddLiquidity.selector) { | ||
revert Hooks.InvalidHookResponse(); | ||
} | ||
} | ||
|
||
return this.beforeAddLiquidity.selector; | ||
} | ||
|
||
function afterAddLiquidity( | ||
address sender, | ||
PoolKey calldata key, | ||
ICLPoolManager.ModifyLiquidityParams calldata params, | ||
BalanceDelta delta, | ||
bytes calldata hookData | ||
) external override returns (bytes4, BalanceDelta) { | ||
bytes32 parameters = bytes32(uint256(_hooksRegistrationBitmap)); | ||
if (!parameters.hasOffsetEnabled(HOOKS_AFTER_ADD_LIQUIDITY_OFFSET)) { | ||
revert HookNotImplemented(); | ||
} | ||
|
||
BalanceDelta hookDelta = BalanceDeltaLibrary.ZERO_DELTA; | ||
for (uint256 i = 0; i < hooks.length; ++i) { | ||
ICLHooks hook = hooks[i]; | ||
bytes32 hookParameters = bytes32(uint256(hook.getHooksRegistrationBitmap())); | ||
|
||
if (!hookParameters.hasOffsetEnabled(HOOKS_AFTER_ADD_LIQUIDITY_OFFSET)) { | ||
continue; | ||
} | ||
|
||
(bytes4 s, BalanceDelta d) = hook.afterAddLiquidity(sender, key, params, delta, hookData); | ||
if (s != ICLHooks.afterAddLiquidity.selector) { | ||
revert Hooks.InvalidHookResponse(); | ||
} | ||
|
||
if ( | ||
hookParameters.hasOffsetEnabled(HOOKS_AFTER_ADD_LIQUIDIY_RETURNS_DELTA_OFFSET) | ||
&& d != BalanceDeltaLibrary.ZERO_DELTA | ||
) { | ||
delta = delta - d; | ||
hookDelta = hookDelta + d; | ||
} | ||
} | ||
|
||
return (this.afterAddLiquidity.selector, hookDelta); | ||
} | ||
|
||
function beforeRemoveLiquidity( | ||
address sender, | ||
PoolKey calldata key, | ||
ICLPoolManager.ModifyLiquidityParams calldata params, | ||
bytes calldata hookData | ||
) external override returns (bytes4) { | ||
bytes32 parameters = bytes32(uint256(_hooksRegistrationBitmap)); | ||
if (!parameters.hasOffsetEnabled(HOOKS_BEFORE_REMOVE_LIQUIDITY_OFFSET)) { | ||
revert HookNotImplemented(); | ||
} | ||
|
||
for (uint256 i = 0; i < hooks.length; ++i) { | ||
ICLHooks hook = hooks[i]; | ||
bytes32 hookParameters = bytes32(uint256(hook.getHooksRegistrationBitmap())); | ||
|
||
if (!hookParameters.hasOffsetEnabled(HOOKS_BEFORE_REMOVE_LIQUIDITY_OFFSET)) { | ||
continue; | ||
} | ||
|
||
if (hook.beforeRemoveLiquidity(sender, key, params, hookData) != ICLHooks.beforeRemoveLiquidity.selector) { | ||
revert Hooks.InvalidHookResponse(); | ||
} | ||
} | ||
|
||
return this.beforeRemoveLiquidity.selector; | ||
} | ||
|
||
function afterRemoveLiquidity( | ||
address sender, | ||
PoolKey calldata key, | ||
ICLPoolManager.ModifyLiquidityParams calldata params, | ||
BalanceDelta delta, | ||
bytes calldata hookData | ||
) external override returns (bytes4, BalanceDelta) { | ||
bytes32 parameters = bytes32(uint256(_hooksRegistrationBitmap)); | ||
if (!parameters.hasOffsetEnabled(HOOKS_AFTER_REMOVE_LIQUIDITY_OFFSET)) { | ||
revert HookNotImplemented(); | ||
} | ||
|
||
BalanceDelta hookDelta = BalanceDeltaLibrary.ZERO_DELTA; | ||
for (uint256 i = 0; i < hooks.length; ++i) { | ||
ICLHooks hook = hooks[i]; | ||
bytes32 hookParameters = bytes32(uint256(hook.getHooksRegistrationBitmap())); | ||
|
||
if (!hookParameters.hasOffsetEnabled(HOOKS_AFTER_REMOVE_LIQUIDITY_OFFSET)) { | ||
continue; | ||
} | ||
|
||
(bytes4 s, BalanceDelta d) = hook.afterRemoveLiquidity(sender, key, params, delta, hookData); | ||
if (s != ICLHooks.afterRemoveLiquidity.selector) { | ||
revert Hooks.InvalidHookResponse(); | ||
} | ||
|
||
if ( | ||
hookParameters.hasOffsetEnabled(HOOKS_AFTER_REMOVE_LIQUIDIY_RETURNS_DELTA_OFFSET) | ||
&& d != BalanceDeltaLibrary.ZERO_DELTA | ||
) { | ||
delta = delta - d; | ||
hookDelta = hookDelta + d; | ||
} | ||
} | ||
|
||
return (this.afterRemoveLiquidity.selector, hookDelta); | ||
} | ||
|
||
function beforeSwap( | ||
address sender, | ||
PoolKey calldata key, | ||
ICLPoolManager.SwapParams calldata params, | ||
bytes calldata hookData | ||
) external override returns (bytes4, BeforeSwapDelta, uint24) { | ||
bytes32 parameters = bytes32(uint256(_hooksRegistrationBitmap)); | ||
if (!parameters.hasOffsetEnabled(HOOKS_BEFORE_SWAP_OFFSET)) { | ||
revert HookNotImplemented(); | ||
} | ||
|
||
BeforeSwapDelta beforeSwapDelta = BeforeSwapDeltaLibrary.ZERO_DELTA; | ||
uint24 lpFeeOverride; | ||
for (uint256 i = 0; i < hooks.length; ++i) { | ||
ICLHooks hook = hooks[i]; | ||
bytes32 hookParameters = bytes32(uint256(hook.getHooksRegistrationBitmap())); | ||
|
||
if (!hookParameters.hasOffsetEnabled(HOOKS_BEFORE_SWAP_OFFSET)) { | ||
continue; | ||
} | ||
|
||
(bytes4 s, BeforeSwapDelta d, uint24 f) = hook.beforeSwap(sender, key, params, hookData); | ||
if (s != ICLHooks.beforeSwap.selector) { | ||
revert Hooks.InvalidHookResponse(); | ||
} | ||
|
||
if (key.fee.isDynamicLPFee()) { | ||
lpFeeOverride += f; | ||
} | ||
|
||
// if (hookParameters.hasOffsetEnabled(HOOKS_BEFORE_SWAP_RETURNS_DELTA_OFFSET)) { | ||
// beforeSwapDelta += d; | ||
// } | ||
} | ||
|
||
return (this.beforeSwap.selector, beforeSwapDelta, lpFeeOverride); | ||
} | ||
|
||
function afterSwap( | ||
address sender, | ||
PoolKey calldata key, | ||
ICLPoolManager.SwapParams calldata params, | ||
BalanceDelta delta, | ||
bytes calldata hookData | ||
) external override returns (bytes4, int128) { | ||
bytes32 parameters = bytes32(uint256(_hooksRegistrationBitmap)); | ||
if (!parameters.hasOffsetEnabled(HOOKS_AFTER_SWAP_OFFSET)) { | ||
revert HookNotImplemented(); | ||
} | ||
|
||
int128 hookDeltaUnspecified; | ||
for (uint256 i = 0; i < hooks.length; ++i) { | ||
ICLHooks hook = hooks[i]; | ||
bytes32 hookParameters = bytes32(uint256(hook.getHooksRegistrationBitmap())); | ||
|
||
if (!hookParameters.hasOffsetEnabled(HOOKS_AFTER_SWAP_OFFSET)) { | ||
continue; | ||
} | ||
|
||
(bytes4 s, int128 d) = hook.afterSwap(sender, key, params, delta, hookData); | ||
if (s != ICLHooks.afterSwap.selector) { | ||
revert Hooks.InvalidHookResponse(); | ||
} | ||
|
||
if (hookParameters.hasOffsetEnabled(HOOKS_AFTER_SWAP_RETURNS_DELTA_OFFSET)) { | ||
hookDeltaUnspecified += d; | ||
} | ||
} | ||
|
||
return (this.afterSwap.selector, hookDeltaUnspecified); | ||
} | ||
|
||
function beforeDonate( | ||
address sender, | ||
PoolKey calldata key, | ||
uint256 amount0, | ||
uint256 amount1, | ||
bytes calldata hookData | ||
) external override returns (bytes4) { | ||
bytes32 parameters = bytes32(uint256(_hooksRegistrationBitmap)); | ||
if (!parameters.hasOffsetEnabled(HOOKS_BEFORE_DONATE_OFFSET)) { | ||
revert HookNotImplemented(); | ||
} | ||
|
||
for (uint256 i = 0; i < hooks.length; ++i) { | ||
ICLHooks hook = hooks[i]; | ||
bytes32 hookParameters = bytes32(uint256(hook.getHooksRegistrationBitmap())); | ||
|
||
if (!hookParameters.hasOffsetEnabled(HOOKS_BEFORE_DONATE_OFFSET)) { | ||
continue; | ||
} | ||
|
||
if (hook.beforeDonate(sender, key, amount0, amount1, hookData) != ICLHooks.beforeDonate.selector) { | ||
revert Hooks.InvalidHookResponse(); | ||
} | ||
} | ||
|
||
return this.beforeDonate.selector; | ||
} | ||
|
||
function afterDonate( | ||
address sender, | ||
PoolKey calldata key, | ||
uint256 amount0, | ||
uint256 amount1, | ||
bytes calldata hookData | ||
) external override returns (bytes4) { | ||
bytes32 parameters = bytes32(uint256(_hooksRegistrationBitmap)); | ||
if (!parameters.hasOffsetEnabled(HOOKS_AFTER_DONATE_OFFSET)) { | ||
revert HookNotImplemented(); | ||
} | ||
|
||
for (uint256 i = 0; i < hooks.length; ++i) { | ||
ICLHooks hook = hooks[i]; | ||
bytes32 hookParameters = bytes32(uint256(hook.getHooksRegistrationBitmap())); | ||
|
||
if (!hookParameters.hasOffsetEnabled(HOOKS_AFTER_DONATE_OFFSET)) { | ||
continue; | ||
} | ||
|
||
if (hook.afterDonate(sender, key, amount0, amount1, hookData) != ICLHooks.afterDonate.selector) { | ||
revert Hooks.InvalidHookResponse(); | ||
} | ||
} | ||
|
||
return this.afterDonate.selector; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if there was test cases with hooks using hookDelta, we will notice an issue around:
how does those hook take/settle with the vault
-- something we need to figure out