Skip to content

Commit

Permalink
Update various examples, BaseHook, Quoter and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qiwu7 committed Mar 11, 2024
1 parent ba45e0f commit bff6075
Show file tree
Hide file tree
Showing 22 changed files with 59 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddInitialLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
392772
385077
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
187139
179444
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeFirstSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136542
128741
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeInitialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1041060
1017451
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175903
169775
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
363995
347304
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSecondSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
97295
89494
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134817
127016
2 changes: 1 addition & 1 deletion .forge-snapshots/TWAMMSubmitOrder.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122753
122687
7 changes: 1 addition & 6 deletions contracts/BaseHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ abstract contract BaseHook is IHooks {
Hooks.validateHookPermissions(_this, getHookPermissions());
}

function lockAcquired(address, /*sender*/ bytes calldata data)
external
virtual
poolManagerOnly
returns (bytes memory)
{
function lockAcquired(bytes calldata data) external virtual poolManagerOnly returns (bytes memory) {
(bool success, bytes memory returnData) = address(this).call(data);
if (success) return returnData;
if (returnData.length == 0) revert LockFailure();
Expand Down
12 changes: 3 additions & 9 deletions contracts/hooks/examples/FullRange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ contract FullRange is BaseHook, ILockCallback {
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
noOp: false,
accessLock: false
afterDonate: false
});
}

Expand Down Expand Up @@ -251,9 +249,7 @@ contract FullRange is BaseHook, ILockCallback {
internal
returns (BalanceDelta delta)
{
delta = abi.decode(
poolManager.lock(address(this), abi.encode(CallbackData(msg.sender, key, params))), (BalanceDelta)
);
delta = abi.decode(poolManager.lock(abi.encode(CallbackData(msg.sender, key, params))), (BalanceDelta));
}

function _settleDeltas(address sender, PoolKey memory key, BalanceDelta delta) internal {
Expand Down Expand Up @@ -301,14 +297,12 @@ contract FullRange is BaseHook, ILockCallback {
pool.hasAccruedFees = false;
}

function lockAcquired(address sender, bytes calldata rawData)
function lockAcquired(bytes calldata rawData)
external
override(ILockCallback, BaseHook)
poolManagerOnly
returns (bytes memory)
{
// Now that manager can be called by EOAs with a lock target, it's necessary for lockAcquired to check the original sender if it wants to trust the data passed through.
if (sender != address(this)) revert SenderMustBeHook();
CallbackData memory data = abi.decode(rawData, (CallbackData));
BalanceDelta delta;

Expand Down
4 changes: 1 addition & 3 deletions contracts/hooks/examples/GeomeanOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ contract GeomeanOracle is BaseHook {
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
noOp: false,
accessLock: false
afterDonate: false
});
}

Expand Down
8 changes: 1 addition & 7 deletions contracts/hooks/examples/LimitOrder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ contract LimitOrder is BaseHook {
beforeSwap: false,
afterSwap: true,
beforeDonate: false,
afterDonate: false,
noOp: false,
accessLock: false
afterDonate: false
});
}

Expand Down Expand Up @@ -160,7 +158,6 @@ contract LimitOrder is BaseHook {

(uint256 amount0, uint256 amount1) = abi.decode(
poolManager.lock(
address(this),
abi.encodeCall(this.lockAcquiredFill, (key, lower, -int256(uint256(epochInfo.liquidityTotal))))
),
(uint256, uint256)
Expand Down Expand Up @@ -224,7 +221,6 @@ contract LimitOrder is BaseHook {
if (liquidity == 0) revert ZeroLiquidity();

poolManager.lock(
address(this),
abi.encodeCall(this.lockAcquiredPlace, (key, tickLower, zeroForOne, int256(uint256(liquidity)), msg.sender))
);

Expand Down Expand Up @@ -306,7 +302,6 @@ contract LimitOrder is BaseHook {
uint256 amount1Fee;
(amount0, amount1, amount0Fee, amount1Fee) = abi.decode(
poolManager.lock(
address(this),
abi.encodeCall(
this.lockAcquiredKill,
(key, tickLower, -int256(uint256(liquidity)), to, liquidity == epochInfo.liquidityTotal)
Expand Down Expand Up @@ -388,7 +383,6 @@ contract LimitOrder is BaseHook {
epochInfo.liquidityTotal = liquidityTotal - liquidity;

poolManager.lock(
address(this),
abi.encodeCall(this.lockAcquiredWithdraw, (epochInfo.currency0, epochInfo.currency1, amount0, amount1, to))
);

Expand Down
15 changes: 3 additions & 12 deletions contracts/hooks/examples/TWAMM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ contract TWAMM is BaseHook, ITWAMM {
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
noOp: false,
accessLock: false
afterDonate: false
});
}

Expand Down Expand Up @@ -144,9 +142,7 @@ contract TWAMM is BaseHook, ITWAMM {
);

if (sqrtPriceLimitX96 != 0 && sqrtPriceLimitX96 != sqrtPriceX96) {
poolManager.lock(
address(this), abi.encode(key, IPoolManager.SwapParams(zeroForOne, type(int256).max, sqrtPriceLimitX96))
);
poolManager.lock(abi.encode(key, IPoolManager.SwapParams(zeroForOne, type(int256).max, sqrtPriceLimitX96)));
}
}

Expand Down Expand Up @@ -302,12 +298,7 @@ contract TWAMM is BaseHook, ITWAMM {
IERC20Minimal(Currency.unwrap(token)).safeTransfer(to, amountTransferred);
}

function lockAcquired(address, /*sender*/ bytes calldata rawData)
external
override
poolManagerOnly
returns (bytes memory)
{
function lockAcquired(bytes calldata rawData) external override poolManagerOnly returns (bytes memory) {
(PoolKey memory key, IPoolManager.SwapParams memory swapParams) =
abi.decode(rawData, (PoolKey, IPoolManager.SwapParams));

Expand Down
4 changes: 1 addition & 3 deletions contracts/hooks/examples/VolatilityOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ contract VolatilityOracle is BaseHook, IDynamicFeeManager {
beforeSwap: false,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
noOp: false,
accessLock: false
afterDonate: false
});
}

Expand Down
13 changes: 5 additions & 8 deletions contracts/lens/Quoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ contract Quoter is IQuoter, ILockCallback {
override
returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint32 initializedTicksLoaded)
{
try manager.lock(address(this), abi.encodeWithSelector(this._quoteExactInputSingle.selector, params)) {}
try manager.lock(abi.encodeWithSelector(this._quoteExactInputSingle.selector, params)) {}
catch (bytes memory reason) {
return _handleRevertSingle(reason);
}
Expand All @@ -77,7 +77,7 @@ contract Quoter is IQuoter, ILockCallback {
uint32[] memory initializedTicksLoadedList
)
{
try manager.lock(address(this), abi.encodeWithSelector(this._quoteExactInput.selector, params)) {}
try manager.lock(abi.encodeWithSelector(this._quoteExactInput.selector, params)) {}
catch (bytes memory reason) {
return _handleRevert(reason);
}
Expand All @@ -89,7 +89,7 @@ contract Quoter is IQuoter, ILockCallback {
override
returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint32 initializedTicksLoaded)
{
try manager.lock(address(this), abi.encodeWithSelector(this._quoteExactOutputSingle.selector, params)) {}
try manager.lock(abi.encodeWithSelector(this._quoteExactOutputSingle.selector, params)) {}
catch (bytes memory reason) {
if (params.sqrtPriceLimitX96 == 0) delete amountOutCached;
return _handleRevertSingle(reason);
Expand All @@ -106,20 +106,17 @@ contract Quoter is IQuoter, ILockCallback {
uint32[] memory initializedTicksLoadedList
)
{
try manager.lock(address(this), abi.encodeWithSelector(this._quoteExactOutput.selector, params)) {}
try manager.lock(abi.encodeWithSelector(this._quoteExactOutput.selector, params)) {}
catch (bytes memory reason) {
return _handleRevert(reason);
}
}

/// @inheritdoc ILockCallback
function lockAcquired(address lockCaller, bytes calldata data) external returns (bytes memory) {
function lockAcquired(bytes calldata data) external returns (bytes memory) {
if (msg.sender != address(manager)) {
revert InvalidLockAcquiredSender();
}
if (lockCaller != address(this)) {
revert InvalidLockCaller();
}

(bool success, bytes memory returnData) = address(this).call(data);
if (success) return returnData;
Expand Down
34 changes: 17 additions & 17 deletions test/FullRange.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
emit Initialize(id, testKey.currency0, testKey.currency1, testKey.fee, testKey.tickSpacing, testKey.hooks);

snapStart("FullRangeInitialize");
initializeRouter.initialize(testKey, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(testKey, SQRT_RATIO_1_1, ZERO_BYTES);
snapEnd();

(, address liquidityToken) = fullRange.poolInfo(id);
Expand All @@ -139,11 +139,11 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
PoolKey memory wrongKey = PoolKey(key.currency0, key.currency1, 0, TICK_SPACING + 1, fullRange);

vm.expectRevert(FullRange.TickSpacingNotDefault.selector);
initializeRouter.initialize(wrongKey, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(wrongKey, SQRT_RATIO_1_1, ZERO_BYTES);
}

function testFullRange_addLiquidity_InitialAddSucceeds() public {
initializeRouter.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);

uint256 prevBalance0 = key.currency0.balanceOf(address(this));
uint256 prevBalance1 = key.currency1.balanceOf(address(this));
Expand All @@ -169,7 +169,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
}

function testFullRange_addLiquidity_InitialAddFuzz(uint256 amount) public {
initializeRouter.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
if (amount < LOCKED_LIQUIDITY) {
vm.expectRevert(FullRange.LiquidityDoesntMeetMinimum.selector);
fullRange.addLiquidity(
Expand Down Expand Up @@ -244,7 +244,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
}

function testFullRange_addLiquidity_SwapThenAddSucceeds() public {
initializeRouter.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);

uint256 prevBalance0 = key.currency0.balanceOf(address(this));
uint256 prevBalance1 = key.currency1.balanceOf(address(this));
Expand Down Expand Up @@ -298,7 +298,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
}

function testFullRange_addLiquidity_FailsIfTooMuchSlippage() public {
initializeRouter.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);

fullRange.addLiquidity(
FullRange.AddLiquidityParams(
Expand All @@ -323,7 +323,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {

function testFullRange_swap_TwoSwaps() public {
PoolKey memory testKey = key;
initializeRouter.initialize(testKey, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(testKey, SQRT_RATIO_1_1, ZERO_BYTES);

fullRange.addLiquidity(
FullRange.AddLiquidityParams(
Expand Down Expand Up @@ -352,8 +352,8 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
}

function testFullRange_swap_TwoPools() public {
initializeRouter.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
initializeRouter.initialize(key2, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(key2, SQRT_RATIO_1_1, ZERO_BYTES);

fullRange.addLiquidity(
FullRange.AddLiquidityParams(
Expand Down Expand Up @@ -408,7 +408,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
}

function testFullRange_removeLiquidity_InitialRemoveFuzz(uint256 amount) public {
initializeRouter.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);

fullRange.addLiquidity(
FullRange.AddLiquidityParams(
Expand Down Expand Up @@ -456,7 +456,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
}

function testFullRange_removeLiquidity_FailsIfNoLiquidity() public {
initializeRouter.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);

(, address liquidityToken) = fullRange.poolInfo(id);
UniswapV4ERC20(liquidityToken).approve(address(fullRange), type(uint256).max);
Expand All @@ -468,7 +468,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
}

function testFullRange_removeLiquidity_SucceedsWithPartial() public {
initializeRouter.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);

uint256 prevBalance0 = key.currency0.balanceOfSelf();
uint256 prevBalance1 = key.currency1.balanceOfSelf();
Expand Down Expand Up @@ -503,7 +503,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
}

function testFullRange_removeLiquidity_DiffRatios() public {
initializeRouter.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);

uint256 prevBalance0 = key.currency0.balanceOf(address(this));
uint256 prevBalance1 = key.currency1.balanceOf(address(this));
Expand Down Expand Up @@ -571,7 +571,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
}

function testFullRange_removeLiquidity_RemoveAllFuzz(uint256 amount) public {
initializeRouter.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
(, address liquidityToken) = fullRange.poolInfo(id);

if (amount <= LOCKED_LIQUIDITY) {
Expand Down Expand Up @@ -626,7 +626,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
vm.prank(address(2));
token1.approve(address(fullRange), type(uint256).max);

initializeRouter.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
(, address liquidityToken) = fullRange.poolInfo(id);

// Test contract adds liquidity
Expand Down Expand Up @@ -704,7 +704,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
}

function testFullRange_removeLiquidity_SwapRemoveAllFuzz(uint256 amount) public {
initializeRouter.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
(, address liquidityToken) = fullRange.poolInfo(id);

if (amount <= LOCKED_LIQUIDITY) {
Expand Down Expand Up @@ -753,7 +753,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
}

function testFullRange_BeforeModifyPositionFailsWithWrongMsgSender() public {
initializeRouter.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);

vm.expectRevert(FullRange.SenderMustBeHook.selector);
modifyLiquidityRouter.modifyLiquidity(
Expand Down
Loading

0 comments on commit bff6075

Please sign in to comment.