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 9f0188d
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 64 deletions.
2 changes: 1 addition & 1 deletion contracts/BaseHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ abstract contract BaseHook is IHooks {
Hooks.validateHookPermissions(_this, getHookPermissions());
}

function lockAcquired(address, /*sender*/ bytes calldata data)
function lockAcquired(bytes calldata data)
external
virtual
poolManagerOnly
Expand Down
6 changes: 2 additions & 4 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 @@ -252,7 +250,7 @@ contract FullRange is BaseHook, ILockCallback {
returns (BalanceDelta delta)
{
delta = abi.decode(
poolManager.lock(address(this), abi.encode(CallbackData(msg.sender, key, params))), (BalanceDelta)
poolManager.lock(abi.encode(CallbackData(msg.sender, key, params))), (BalanceDelta)
);
}

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
6 changes: 2 additions & 4 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 @@ -145,7 +143,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))
abi.encode(key, IPoolManager.SwapParams(zeroForOne, type(int256).max, sqrtPriceLimitX96))
);
}
}
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
20 changes: 10 additions & 10 deletions test/GeomeanOracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ contract TestGeomeanOracle is Test, Deployers {
}

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

function testBeforeInitializeRevertsIfFee() public {
vm.expectRevert(GeomeanOracle.OnlyOneOraclePoolAllowed.selector);
initializeRouter.initialize(
manager.initialize(
PoolKey(Currency.wrap(address(token0)), Currency.wrap(address(token1)), 1, MAX_TICK_SPACING, geomeanOracle),
SQRT_RATIO_1_1,
ZERO_BYTES
Expand All @@ -79,23 +79,23 @@ contract TestGeomeanOracle is Test, Deployers {

function testBeforeInitializeRevertsIfNotMaxTickSpacing() public {
vm.expectRevert(GeomeanOracle.OnlyOneOraclePoolAllowed.selector);
initializeRouter.initialize(
manager.initialize(
PoolKey(Currency.wrap(address(token0)), Currency.wrap(address(token1)), 0, 60, geomeanOracle),
SQRT_RATIO_1_1,
ZERO_BYTES
);
}

function testAfterInitializeState() public {
initializeRouter.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
GeomeanOracle.ObservationState memory observationState = geomeanOracle.getState(key);
assertEq(observationState.index, 0);
assertEq(observationState.cardinality, 1);
assertEq(observationState.cardinalityNext, 1);
}

function testAfterInitializeObservation() public {
initializeRouter.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
Oracle.Observation memory observation = geomeanOracle.getObservation(key, 0);
assertTrue(observation.initialized);
assertEq(observation.blockTimestamp, 1);
Expand All @@ -104,7 +104,7 @@ contract TestGeomeanOracle is Test, Deployers {
}

function testAfterInitializeObserve0() public {
initializeRouter.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
uint32[] memory secondsAgo = new uint32[](1);
secondsAgo[0] = 0;
(int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s) =
Expand All @@ -116,7 +116,7 @@ contract TestGeomeanOracle is Test, Deployers {
}

function testBeforeModifyPositionNoObservations() public {
initializeRouter.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
modifyLiquidityRouter.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams(
Expand All @@ -138,7 +138,7 @@ contract TestGeomeanOracle is Test, Deployers {
}

function testBeforeModifyPositionObservation() public {
initializeRouter.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
geomeanOracle.setTime(3); // advance 2 seconds
modifyLiquidityRouter.modifyLiquidity(
key,
Expand All @@ -161,7 +161,7 @@ contract TestGeomeanOracle is Test, Deployers {
}

function testBeforeModifyPositionObservationAndCardinality() public {
initializeRouter.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
geomeanOracle.setTime(3); // advance 2 seconds
geomeanOracle.increaseCardinalityNext(key, 2);
GeomeanOracle.ObservationState memory observationState = geomeanOracle.getState(key);
Expand Down Expand Up @@ -199,7 +199,7 @@ contract TestGeomeanOracle is Test, Deployers {
}

function testPermanentLiquidity() public {
initializeRouter.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
manager.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
geomeanOracle.setTime(3); // advance 2 seconds
modifyLiquidityRouter.modifyLiquidity(
key,
Expand Down
Loading

0 comments on commit 9f0188d

Please sign in to comment.