Skip to content

Revert via BaseHook #83

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

Merged
merged 4 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion script/DeployPool.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ contract DeployPool is ScriptUtil {
eulerAccount,
uint160(
Hooks.BEFORE_INITIALIZE_FLAG | Hooks.BEFORE_SWAP_FLAG | Hooks.BEFORE_SWAP_RETURNS_DELTA_FLAG
| Hooks.BEFORE_ADD_LIQUIDITY_FLAG
| Hooks.BEFORE_DONATE_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG
),
creationCode
);
Expand Down
37 changes: 16 additions & 21 deletions src/UniswapHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ contract UniswapHook is BaseHook {

PoolKey internal _poolKey;

error AlreadyInitialized();
error NativeConcentratedLiquidityUnsupported();
error LockedHook();

constructor(address evc_, address _poolManager) BaseHook(IPoolManager(_poolManager)) {
Expand Down Expand Up @@ -143,25 +141,22 @@ contract UniswapHook is BaseHook {
return (BaseHook.beforeSwap.selector, returnDelta, 0);
}

/// @dev Each deployed hook only services one pair and prevent subsequent initializations
function _beforeInitialize(address, PoolKey calldata, uint160) internal view override returns (bytes4) {
// when the hook is deployed for the first time, the internal _poolKey is empty
// upon activation, the internal _poolKey is initialized and set
// once the hook contract is activated, do not allow subsequent initializations
require(_poolKey.tickSpacing == 0, AlreadyInitialized());
return BaseHook.beforeInitialize.selector;
}

function _beforeAddLiquidity(address, PoolKey calldata, IPoolManager.ModifyLiquidityParams calldata, bytes calldata)
internal
pure
override
returns (bytes4)
{
revert NativeConcentratedLiquidityUnsupported();
}

function getHookPermissions() public pure override returns (Hooks.Permissions memory) {
/**
* @dev Hook Permissions without overrides:
* - beforeInitialize, beforeDoate, beforeAddLiquidity
* We use BaseHook's original reverts to *intentionally* revert
*
* beforeInitialize: the hook reverts for initializations NOT going through EulerSwap.activateHook()
* we want to prevent users from initializing other pairs with the same hook address
*
* beforeDonate: because the hook does not support native concentrated liquidity, any
* donations are permanently irrecoverable. The hook reverts on beforeDonate to prevent accidental misusage
*
* beforeAddLiquidity: the hook reverts to prevent v3-CLAMM positions
* because the hook is a "custom curve", any concentrated liquidity position sits idle and entirely unused
* to protect users from accidentally creating non-productive positions, the hook reverts on beforeAddLiquidity
*/
return Hooks.Permissions({
beforeInitialize: true,
afterInitialize: false,
Expand All @@ -171,7 +166,7 @@ contract UniswapHook is BaseHook {
afterRemoveLiquidity: false,
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
beforeDonate: true,
afterDonate: false,
beforeSwapReturnDelta: true,
afterSwapReturnDelta: false,
Expand Down
2 changes: 1 addition & 1 deletion test/EulerSwapTestBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ contract EulerSwapTestBase is EVaultTestBase {
holder,
uint160(
Hooks.BEFORE_INITIALIZE_FLAG | Hooks.BEFORE_SWAP_FLAG | Hooks.BEFORE_SWAP_RETURNS_DELTA_FLAG
| Hooks.BEFORE_ADD_LIQUIDITY_FLAG
| Hooks.BEFORE_DONATE_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG
),
creationCode
);
Expand Down
8 changes: 5 additions & 3 deletions test/FactoryTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract FactoryTest is EulerSwapTestBase {
function mineSalt(IEulerSwap.Params memory poolParams) internal view returns (address hookAddress, bytes32 salt) {
uint160 flags = uint160(
Hooks.BEFORE_INITIALIZE_FLAG | Hooks.BEFORE_SWAP_FLAG | Hooks.BEFORE_SWAP_RETURNS_DELTA_FLAG
| Hooks.BEFORE_ADD_LIQUIDITY_FLAG
| Hooks.BEFORE_DONATE_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG
);
bytes memory creationCode = MetaProxyDeployer.creationCodeMetaProxy(eulerSwapImpl, abi.encode(poolParams));
(hookAddress, salt) = HookMiner.find(address(eulerSwapFactory), holder, flags, creationCode);
Expand All @@ -47,8 +47,10 @@ contract FactoryTest is EulerSwapTestBase {
returns (address hookAddress, bytes32 salt)
{
// missing BEFORE_ADD_LIQUIDITY_FLAG
uint160 flags =
uint160(Hooks.BEFORE_INITIALIZE_FLAG | Hooks.BEFORE_SWAP_FLAG | Hooks.BEFORE_SWAP_RETURNS_DELTA_FLAG);
uint160 flags = uint160(
Hooks.BEFORE_INITIALIZE_FLAG | Hooks.BEFORE_SWAP_FLAG | Hooks.BEFORE_SWAP_RETURNS_DELTA_FLAG
| Hooks.BEFORE_DONATE_FLAG
);
bytes memory creationCode = MetaProxyDeployer.creationCodeMetaProxy(eulerSwapImpl, abi.encode(poolParams));
(hookAddress, salt) = HookMiner.find(address(eulerSwapFactory), holder, flags, creationCode);
}
Expand Down
27 changes: 24 additions & 3 deletions test/HookSwaps.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {UniswapHook} from "../src/UniswapHook.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {IPoolManager, PoolManagerDeployer} from "./utils/PoolManagerDeployer.sol";
import {PoolSwapTest} from "@uniswap/v4-core/src/test/PoolSwapTest.sol";
import {PoolDonateTest} from "@uniswap/v4-core/src/test/PoolDonateTest.sol";
import {MinimalRouter} from "./utils/MinimalRouter.sol";
import {PoolModifyLiquidityTest} from "@uniswap/v4-core/src/test/PoolModifyLiquidityTest.sol";
import {Currency, CurrencyLibrary} from "@uniswap/v4-core/src/types/Currency.sol";
Expand All @@ -18,6 +19,7 @@ import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {CustomRevert} from "@uniswap/v4-core/src/libraries/CustomRevert.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {BaseHook} from "v4-periphery/src/utils/BaseHook.sol";

contract HookSwapsTest is EulerSwapTestBase {
using StateLibrary for IPoolManager;
Expand All @@ -28,6 +30,7 @@ contract HookSwapsTest is EulerSwapTestBase {
PoolSwapTest public swapRouter;
MinimalRouter public minimalRouter;
PoolModifyLiquidityTest public liquidityManager;
PoolDonateTest public donateRouter;

PoolSwapTest.TestSettings public settings = PoolSwapTest.TestSettings({takeClaims: false, settleUsingBurn: false});

Expand All @@ -38,6 +41,7 @@ contract HookSwapsTest is EulerSwapTestBase {
swapRouter = new PoolSwapTest(poolManager);
minimalRouter = new MinimalRouter(poolManager);
liquidityManager = new PoolModifyLiquidityTest(poolManager);
donateRouter = new PoolDonateTest(poolManager);

deployEulerSwap(address(poolManager));

Expand Down Expand Up @@ -133,13 +137,13 @@ contract HookSwapsTest is EulerSwapTestBase {
assertTrue(perms.beforeAddLiquidity);
assertTrue(perms.beforeSwap);
assertTrue(perms.beforeSwapReturnDelta);
assertTrue(perms.beforeDonate);

assertFalse(perms.afterInitialize);
assertFalse(perms.afterAddLiquidity);
assertFalse(perms.beforeRemoveLiquidity);
assertFalse(perms.afterRemoveLiquidity);
assertFalse(perms.afterSwap);
assertFalse(perms.beforeDonate);
assertFalse(perms.afterDonate);
assertFalse(perms.afterSwapReturnDelta);
assertFalse(perms.afterAddLiquidityReturnDelta);
Expand All @@ -163,7 +167,7 @@ contract HookSwapsTest is EulerSwapTestBase {
CustomRevert.WrappedError.selector,
address(eulerSwap),
IHooks.beforeAddLiquidity.selector,
abi.encodeWithSelector(UniswapHook.NativeConcentratedLiquidityUnsupported.selector),
abi.encodeWithSelector(BaseHook.HookNotImplemented.selector),
abi.encodeWithSelector(Hooks.HookCallFailed.selector)
)
);
Expand All @@ -186,13 +190,30 @@ contract HookSwapsTest is EulerSwapTestBase {
CustomRevert.WrappedError.selector,
address(eulerSwap),
IHooks.beforeInitialize.selector,
abi.encodeWithSelector(UniswapHook.AlreadyInitialized.selector),
abi.encodeWithSelector(BaseHook.HookNotImplemented.selector),
abi.encodeWithSelector(Hooks.HookCallFailed.selector)
)
);
poolManager.initialize(newPoolKey, 79228162514264337593543950336);
}

/// @dev revert on donations as they are irrecoverable if they were supported
function test_revertDonate(uint256 amount0, uint256 amount1) public {
PoolKey memory poolKey = eulerSwap.poolKey();

// hook intentionally reverts to prevent irrecoverable donations
vm.expectRevert(
abi.encodeWithSelector(
CustomRevert.WrappedError.selector,
address(eulerSwap),
IHooks.beforeDonate.selector,
abi.encodeWithSelector(BaseHook.HookNotImplemented.selector),
abi.encodeWithSelector(Hooks.HookCallFailed.selector)
)
);
donateRouter.donate(poolKey, amount0, amount1, "");
}

function _swap(PoolKey memory key, bool zeroForOne, bool exactInput, uint256 amount) internal {
IPoolManager.SwapParams memory swapParams = IPoolManager.SwapParams({
zeroForOne: zeroForOne,
Expand Down