Skip to content
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

add hook helpers library #116

Closed
wants to merge 1 commit into from
Closed
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
36 changes: 20 additions & 16 deletions contracts/hooks/examples/FullRange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@uniswap/v4-core/src/type

import "../../libraries/LiquidityAmounts.sol";

function hookPermissions() pure returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: true,
afterInitialize: false,
beforeAddLiquidity: true,
beforeRemoveLiquidity: false,
afterAddLiquidity: false,
afterRemoveLiquidity: false,
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}

contract FullRange is BaseHook, IUnlockCallback {
using CurrencyLibrary for Currency;
using CurrencySettleTake for Currency;
Expand Down Expand Up @@ -93,22 +112,7 @@ contract FullRange is BaseHook, IUnlockCallback {
}

function getHookPermissions() public pure override returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: true,
afterInitialize: false,
beforeAddLiquidity: true,
beforeRemoveLiquidity: false,
afterAddLiquidity: false,
afterRemoveLiquidity: false,
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
return hookPermissions();
}

function addLiquidity(AddLiquidityParams calldata params)
Expand Down
36 changes: 20 additions & 16 deletions contracts/hooks/examples/GeomeanOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol";

function hookPermissions() pure returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: true,
afterInitialize: true,
beforeAddLiquidity: true,
beforeRemoveLiquidity: true,
afterAddLiquidity: false,
afterRemoveLiquidity: false,
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}

/// @notice A hook for a pool that allows a Uniswap pool to act as an oracle. Pools that use this hook must have full range
/// tick spacing and liquidity is always permanently locked in these pools. This is the suggested configuration
/// for protocols that wish to use a V3 style geomean oracle.
Expand Down Expand Up @@ -64,22 +83,7 @@ contract GeomeanOracle is BaseHook {
constructor(IPoolManager _poolManager) BaseHook(_poolManager) {}

function getHookPermissions() public pure override returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: true,
afterInitialize: true,
beforeAddLiquidity: true,
beforeRemoveLiquidity: true,
afterAddLiquidity: false,
afterRemoveLiquidity: false,
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
return hookPermissions();
}

function beforeInitialize(address, PoolKey calldata key, uint160, bytes calldata)
Expand Down
36 changes: 20 additions & 16 deletions contracts/hooks/examples/LimitOrder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ library EpochLibrary {
}
}

function hookPermissions() pure returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: false,
afterInitialize: true,
beforeAddLiquidity: false,
beforeRemoveLiquidity: false,
afterAddLiquidity: false,
afterRemoveLiquidity: false,
beforeSwap: false,
afterSwap: true,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}

contract LimitOrder is BaseHook {
using EpochLibrary for Epoch;
using PoolIdLibrary for PoolKey;
Expand Down Expand Up @@ -78,22 +97,7 @@ contract LimitOrder is BaseHook {
constructor(IPoolManager _poolManager) BaseHook(_poolManager) {}

function getHookPermissions() public pure override returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: false,
afterInitialize: true,
beforeAddLiquidity: false,
beforeRemoveLiquidity: false,
afterAddLiquidity: false,
afterRemoveLiquidity: false,
beforeSwap: false,
afterSwap: true,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
return hookPermissions();
}

function getTickLowerLast(PoolId poolId) public view returns (int24) {
Expand Down
36 changes: 20 additions & 16 deletions contracts/hooks/examples/TWAMM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ import {CurrencySettleTake} from "@uniswap/v4-core/src/libraries/CurrencySettleT
import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol";

function hookPermissions() pure returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: true,
afterInitialize: false,
beforeAddLiquidity: true,
beforeRemoveLiquidity: false,
afterAddLiquidity: false,
afterRemoveLiquidity: false,
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}

contract TWAMM is BaseHook, ITWAMM {
using TransferHelper for IERC20Minimal;
using CurrencyLibrary for Currency;
Expand Down Expand Up @@ -66,22 +85,7 @@ contract TWAMM is BaseHook, ITWAMM {
}

function getHookPermissions() public pure override returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: true,
afterInitialize: false,
beforeAddLiquidity: true,
beforeRemoveLiquidity: false,
afterAddLiquidity: false,
afterRemoveLiquidity: false,
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
return hookPermissions();
}

function beforeInitialize(address, PoolKey calldata key, uint160, bytes calldata)
Expand Down
36 changes: 20 additions & 16 deletions contracts/hooks/examples/VolatilityOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ import {LPFeeLibrary} from "@uniswap/v4-core/src/libraries/LPFeeLibrary.sol";
import {BaseHook} from "../../BaseHook.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";

function hookPermissions() pure returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: true,
afterInitialize: true,
beforeAddLiquidity: false,
beforeRemoveLiquidity: false,
afterAddLiquidity: false,
afterRemoveLiquidity: false,
beforeSwap: false,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}

contract VolatilityOracle is BaseHook {
using LPFeeLibrary for uint24;

Expand All @@ -24,22 +43,7 @@ contract VolatilityOracle is BaseHook {
}

function getHookPermissions() public pure override returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: true,
afterInitialize: true,
beforeAddLiquidity: false,
beforeRemoveLiquidity: false,
afterAddLiquidity: false,
afterRemoveLiquidity: false,
beforeSwap: false,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
return hookPermissions();
}

function beforeInitialize(address, PoolKey calldata key, uint160, bytes calldata)
Expand Down
26 changes: 26 additions & 0 deletions contracts/libraries/HookHelpers.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";

library HookHelpers {
/// @notice Function to calculate flags from permissions
/// @param permissions The permissions to convert to flags
/// @return out The flags that correspond to the permissions
function flags(Hooks.Permissions memory permissions) internal pure returns (uint160 out) {
if (permissions.beforeInitialize) out |= Hooks.BEFORE_INITIALIZE_FLAG;
if (permissions.afterInitialize) out |= Hooks.AFTER_INITIALIZE_FLAG;
if (permissions.beforeAddLiquidity) out |= Hooks.BEFORE_ADD_LIQUIDITY_FLAG;
if (permissions.afterAddLiquidity) out |= Hooks.AFTER_ADD_LIQUIDITY_FLAG;
if (permissions.beforeRemoveLiquidity) out |= Hooks.BEFORE_REMOVE_LIQUIDITY_FLAG;
if (permissions.afterRemoveLiquidity) out |= Hooks.AFTER_REMOVE_LIQUIDITY_FLAG;
if (permissions.beforeSwap) out |= Hooks.BEFORE_SWAP_FLAG;
if (permissions.afterSwap) out |= Hooks.AFTER_SWAP_FLAG;
if (permissions.beforeDonate) out |= Hooks.BEFORE_DONATE_FLAG;
if (permissions.afterDonate) out |= Hooks.AFTER_DONATE_FLAG;
if (permissions.beforeSwapReturnDelta) out |= Hooks.BEFORE_SWAP_RETURNS_DELTA_FLAG;
if (permissions.afterSwapReturnDelta) out |= Hooks.AFTER_SWAP_RETURNS_DELTA_FLAG;
if (permissions.afterAddLiquidityReturnDelta) out |= Hooks.AFTER_ADD_LIQUIDITY_RETURNS_DELTA_FLAG;
if (permissions.afterRemoveLiquidityReturnDelta) out |= Hooks.AFTER_REMOVE_LIQUIDITY_RETURNS_DELTA_FLAG;
}
}
8 changes: 4 additions & 4 deletions test/FullRange.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.19;
import {Test} from "forge-std/Test.sol";
import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {FullRange} from "../contracts/hooks/examples/FullRange.sol";
import {FullRange, hookPermissions} from "../contracts/hooks/examples/FullRange.sol";
import {FullRangeImplementation} from "./shared/implementation/FullRangeImplementation.sol";
import {PoolManager} from "@uniswap/v4-core/src/PoolManager.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
Expand All @@ -21,12 +21,14 @@ import {FullMath} from "@uniswap/v4-core/src/libraries/FullMath.sol";
import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol";
import {HookEnabledSwapRouter} from "./utils/HookEnabledSwapRouter.sol";
import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol";
import {HookHelpers} from "../contracts/libraries/HookHelpers.sol";

contract TestFullRange is Test, Deployers, GasSnapshot {
using PoolIdLibrary for PoolKey;
using SafeCast for uint256;
using CurrencyLibrary for Currency;
using StateLibrary for IPoolManager;
using HookHelpers for Hooks.Permissions;

event Initialize(
PoolId poolId,
Expand Down Expand Up @@ -66,9 +68,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
MockERC20 token1;
MockERC20 token2;

FullRangeImplementation fullRange = FullRangeImplementation(
address(uint160(Hooks.BEFORE_INITIALIZE_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG | Hooks.BEFORE_SWAP_FLAG))
);
FullRangeImplementation fullRange = FullRangeImplementation(address(hookPermissions().flags()));

PoolId id;

Expand Down
13 changes: 4 additions & 9 deletions test/GeomeanOracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.19;
import {Test} from "forge-std/Test.sol";
import {GetSender} from "./shared/GetSender.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {GeomeanOracle} from "../contracts/hooks/examples/GeomeanOracle.sol";
import {GeomeanOracle, hookPermissions} from "../contracts/hooks/examples/GeomeanOracle.sol";
import {GeomeanOracleImplementation} from "./shared/implementation/GeomeanOracleImplementation.sol";
import {PoolManager} from "@uniswap/v4-core/src/PoolManager.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
Expand All @@ -16,22 +16,17 @@ import {PoolModifyLiquidityTest} from "@uniswap/v4-core/src/test/PoolModifyLiqui
import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
import {Oracle} from "../contracts/libraries/Oracle.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {HookHelpers} from "../contracts/libraries/HookHelpers.sol";

contract TestGeomeanOracle is Test, Deployers {
using PoolIdLibrary for PoolKey;
using HookHelpers for Hooks.Permissions;

int24 constant MAX_TICK_SPACING = 32767;

TestERC20 token0;
TestERC20 token1;
GeomeanOracleImplementation geomeanOracle = GeomeanOracleImplementation(
address(
uint160(
Hooks.BEFORE_INITIALIZE_FLAG | Hooks.AFTER_INITIALIZE_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG
| Hooks.BEFORE_REMOVE_LIQUIDITY_FLAG | Hooks.BEFORE_SWAP_FLAG
)
)
);
GeomeanOracleImplementation geomeanOracle = GeomeanOracleImplementation(address(hookPermissions().flags()));
PoolId id;

function setUp() public {
Expand Down
6 changes: 4 additions & 2 deletions test/LimitOrder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.19;
import {Test} from "forge-std/Test.sol";
import {GetSender} from "./shared/GetSender.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {LimitOrder, Epoch, EpochLibrary} from "../contracts/hooks/examples/LimitOrder.sol";
import {LimitOrder, Epoch, EpochLibrary, hookPermissions} from "../contracts/hooks/examples/LimitOrder.sol";
import {LimitOrderImplementation} from "./shared/implementation/LimitOrderImplementation.sol";
import {PoolManager} from "@uniswap/v4-core/src/PoolManager.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
Expand All @@ -16,17 +16,19 @@ import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {HookEnabledSwapRouter} from "./utils/HookEnabledSwapRouter.sol";
import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol";
import {HookHelpers} from "../contracts/libraries/HookHelpers.sol";

contract TestLimitOrder is Test, Deployers {
using PoolIdLibrary for PoolKey;
using StateLibrary for IPoolManager;
using HookHelpers for Hooks.Permissions;

uint160 constant SQRT_RATIO_10_1 = 250541448375047931186413801569;

HookEnabledSwapRouter router;
TestERC20 token0;
TestERC20 token1;
LimitOrder limitOrder = LimitOrder(address(uint160(Hooks.AFTER_INITIALIZE_FLAG | Hooks.AFTER_SWAP_FLAG)));
LimitOrder limitOrder = LimitOrder(address(hookPermissions().flags()));
PoolId id;

function setUp() public {
Expand Down
Loading