From a8a0ed1ffb7c90af1a1c51719d526aac36bc44e0 Mon Sep 17 00:00:00 2001 From: ConjunctiveNormalForm Date: Thu, 16 Nov 2023 11:21:11 -0500 Subject: [PATCH] rename initializedTicksCrossed to initializedTicksLoaded --- contracts/interfaces/IQuoter.sol | 4 +- contracts/lens/Quoter.sol | 49 ++++++++-------- contracts/libraries/PoolTicksCounter.sol | 2 +- test/Quoter.t.sol | 74 ++++++++++++------------ 4 files changed, 66 insertions(+), 63 deletions(-) diff --git a/contracts/interfaces/IQuoter.sol b/contracts/interfaces/IQuoter.sol index 3b3d38f1..03d51076 100644 --- a/contracts/interfaces/IQuoter.sol +++ b/contracts/interfaces/IQuoter.sol @@ -16,13 +16,13 @@ interface IQuoter { function quoteExactInputSingle(ExactInputSingleParams calldata params) external - returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint32 initializedTicksCrossed); + returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint32 initializedTicksLoaded); function quoteExactInput(ExactInputParams memory params) external returns ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ); } diff --git a/contracts/lens/Quoter.sol b/contracts/lens/Quoter.sol index 6805462b..645df2cf 100644 --- a/contracts/lens/Quoter.sol +++ b/contracts/lens/Quoter.sol @@ -31,7 +31,7 @@ contract Quoter is IQuoter { function quoteExactInputSingle(ExactInputSingleParams memory params) external override - returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint32 initializedTicksCrossed) + returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint32 initializedTicksLoaded) { try poolManager.lock(abi.encode(SwapInfo(SwapType.ExactInputSingle, abi.encode(params)))) {} catch (bytes memory reason) { @@ -44,7 +44,7 @@ contract Quoter is IQuoter { returns ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) { try poolManager.lock(abi.encode(SwapInfo(SwapType.ExactInput, abi.encode(params)))) {} @@ -72,26 +72,29 @@ contract Quoter is IQuoter { ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) = _quoteExactInput(abi.decode(swapInfo.params, (ExactInputParams))); + // bytes memory result = abi.encode(deltaAmounts, sqrtPriceX96AfterList, initializedTicksLoadedList); + // uint256 resultLength = result.length; assembly { - // function storeArray(offset, length, array) { - // mstore(offset, length) - // offset := add(offset, 0x20) - // for { let i := 0 } lt(i, length) { i := add(i, 1) } { - // let value := mload(add(array, add(mul(i, 0x20), 0x20))) - // mstore(offset, value) - // offset := add(offset, 0x20) - // } - // } + //revert(result, resultLength) + function storeArray(offset, length, array) { + mstore(offset, length) + offset := add(offset, 0x20) + for { let i := 0 } lt(i, length) { i := add(i, 1) } { + let value := mload(add(array, add(mul(i, 0x20), 0x20))) + mstore(offset, value) + offset := add(offset, 0x20) + } + } let originalPtr := mload(0x40) let ptr := mload(0x40) let deltaLength := mload(deltaAmounts) let sqrtPriceLength := mload(sqrtPriceX96AfterList) - let initializedTicksLength := mload(initializedTicksCrossedList) + let initializedTicksLength := mload(initializedTicksLoadedList) let deltaOffset := 0x60 let sqrtPriceOffset := add(deltaOffset, add(0x20, mul(0x20, deltaLength))) @@ -125,7 +128,7 @@ contract Quoter is IQuoter { mstore(ptr, initializedTicksLength) ptr := add(ptr, 0x20) for { let i := 0 } lt(i, initializedTicksLength) { i := add(i, 1) } { - let value := mload(add(initializedTicksCrossedList, add(mul(i, 0x20), 0x20))) + let value := mload(add(initializedTicksLoadedList, add(mul(i, 0x20), 0x20))) mstore(ptr, value) ptr := add(ptr, 0x20) } @@ -154,7 +157,7 @@ contract Quoter is IQuoter { function _handleRevertExactInputSingle(bytes memory reason, PoolKey memory poolKey) private view - returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint32 initializedTicksCrossed) + returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint32 initializedTicksLoaded) { int24 tickBefore; int24 tickAfter; @@ -166,8 +169,8 @@ contract Quoter is IQuoter { deltaAmounts[0] = deltas.amount0(); deltaAmounts[1] = deltas.amount1(); - initializedTicksCrossed = - PoolTicksCounter.countInitializedTicksCrossed(poolManager, poolKey, tickBefore, tickAfter); + initializedTicksLoaded = + PoolTicksCounter.countInitializedTicksLoaded(poolManager, poolKey, tickBefore, tickAfter); } function _handleRevertExactInput(bytes memory reason) @@ -176,11 +179,11 @@ contract Quoter is IQuoter { returns ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) { reason = validateRevertReason(reason); - (deltaAmounts, sqrtPriceX96AfterList, initializedTicksCrossedList) = + (deltaAmounts, sqrtPriceX96AfterList, initializedTicksLoadedList) = abi.decode(reason, (int128[], uint160[], uint32[])); } @@ -189,14 +192,14 @@ contract Quoter is IQuoter { returns ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) { uint256 pathLength = params.path.length; deltaAmounts = new int128[](pathLength + 1); sqrtPriceX96AfterList = new uint160[](pathLength); - initializedTicksCrossedList = new uint32[](pathLength); + initializedTicksLoadedList = new uint32[](pathLength); Currency prevCurrencyOut; uint128 prevAmountOut; @@ -223,8 +226,8 @@ contract Quoter is IQuoter { prevAmountOut = zeroForOne ? uint128(-curDeltas.amount1()) : uint128(-curDeltas.amount0()); prevCurrencyOut = params.path[i].intermediateCurrency; sqrtPriceX96AfterList[i] = sqrtPriceX96After; - initializedTicksCrossedList[i] = - PoolTicksCounter.countInitializedTicksCrossed(poolManager, poolKey, tickBefore, tickAfter); + initializedTicksLoadedList[i] = + PoolTicksCounter.countInitializedTicksLoaded(poolManager, poolKey, tickBefore, tickAfter); } } diff --git a/contracts/libraries/PoolTicksCounter.sol b/contracts/libraries/PoolTicksCounter.sol index c327fa9b..6c3c2780 100644 --- a/contracts/libraries/PoolTicksCounter.sol +++ b/contracts/libraries/PoolTicksCounter.sol @@ -13,7 +13,7 @@ library PoolTicksCounter { /// When tickBefore and/or tickAfter themselves are initialized, the logic over whether we should count them depends on the /// direction of the swap. If we are swapping upwards (tickAfter > tickBefore) we don't want to count tickBefore but we do /// want to count tickAfter. The opposite is true if we are swapping downwards. - function countInitializedTicksCrossed(IPoolManager self, PoolKey memory key, int24 tickBefore, int24 tickAfter) + function countInitializedTicksLoaded(IPoolManager self, PoolKey memory key, int24 tickBefore, int24 tickAfter) internal view returns (uint32 initializedTicksCrossed) diff --git a/test/Quoter.t.sol b/test/Quoter.t.sol index 73b1d4ff..c55a9454 100644 --- a/test/Quoter.t.sol +++ b/test/Quoter.t.sol @@ -72,7 +72,7 @@ contract QuoterTest is Test, Deployers { uint256 expectedAmountOut = 9871; uint160 expectedSqrtPriceX96After = 78461846509168490764501028180; - (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint32 initializedTicksCrossed) = quoter + (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint32 initializedTicksLoaded) = quoter .quoteExactInputSingle( ExactInputSingleParams({ poolKey: key02, @@ -86,7 +86,7 @@ contract QuoterTest is Test, Deployers { assertEq(uint128(-deltaAmounts[1]), expectedAmountOut); assertEq(sqrtPriceX96After, expectedSqrtPriceX96After); - assertEq(initializedTicksCrossed, 2); + assertEq(initializedTicksLoaded, 2); } function testQuoter_quoteExactInputSingle_OneForZero_MultiplePositions() public { @@ -94,7 +94,7 @@ contract QuoterTest is Test, Deployers { uint256 expectedAmountOut = 9871; uint160 expectedSqrtPriceX96After = 80001962924147897865541384515; - (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint32 initializedTicksCrossed) = quoter + (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint32 initializedTicksLoaded) = quoter .quoteExactInputSingle( ExactInputSingleParams({ poolKey: key02, @@ -108,10 +108,10 @@ contract QuoterTest is Test, Deployers { assertEq(uint128(-deltaAmounts[0]), expectedAmountOut); assertEq(sqrtPriceX96After, expectedSqrtPriceX96After); - assertEq(initializedTicksCrossed, 2); + assertEq(initializedTicksLoaded, 2); } - function testQuoter_quoteExactInput_0to2_2TicksCrossed() public { + function testQuoter_quoteExactInput_0to2_2TicksLoaded() public { tokenPath.push(token0); tokenPath.push(token2); ExactInputParams memory params = getExactInputParams(tokenPath, 10000); @@ -119,15 +119,15 @@ contract QuoterTest is Test, Deployers { ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) = quoter.quoteExactInput(params); assertEq(uint128(-deltaAmounts[1]), 9871); assertEq(sqrtPriceX96AfterList[0], 78461846509168490764501028180); - assertEq(initializedTicksCrossedList[0], 2); + assertEq(initializedTicksLoadedList[0], 2); } - function testQuoter_quoteExactInput_0to2_2TicksCrossed_initialiedAfter() public { + function testQuoter_quoteExactInput_0to2_2TicksLoaded_initialiedAfter() public { tokenPath.push(token0); tokenPath.push(token2); @@ -138,15 +138,15 @@ contract QuoterTest is Test, Deployers { ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) = quoter.quoteExactInput(params); assertEq(uint128(-deltaAmounts[1]), 6143); assertEq(sqrtPriceX96AfterList[0], 78757224507315167622282810783); - assertEq(initializedTicksCrossedList[0], 1); + assertEq(initializedTicksLoadedList[0], 1); } - function testQuoter_quoteExactInput_0to2_1TicksCrossed() public { + function testQuoter_quoteExactInput_0to2_1TicksLoaded() public { tokenPath.push(token0); tokenPath.push(token2); @@ -157,15 +157,15 @@ contract QuoterTest is Test, Deployers { ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) = quoter.quoteExactInput(params); assertEq(uint128(-deltaAmounts[1]), 3971); assertEq(sqrtPriceX96AfterList[0], 78926452400586371254602774705); - assertEq(initializedTicksCrossedList[0], 1); + assertEq(initializedTicksLoadedList[0], 1); } - function testQuoter_quoteExactInput_0to2_0TicksCrossed_startingNotInitialized() public { + function testQuoter_quoteExactInput_0to2_0TicksLoaded_startingNotInitialized() public { tokenPath.push(token0); tokenPath.push(token2); ExactInputParams memory params = getExactInputParams(tokenPath, 10); @@ -173,15 +173,15 @@ contract QuoterTest is Test, Deployers { ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) = quoter.quoteExactInput(params); assertEq(uint128(-deltaAmounts[1]), 8); assertEq(sqrtPriceX96AfterList[0], 79227483487511329217250071027); - assertEq(initializedTicksCrossedList[0], 0); + assertEq(initializedTicksLoadedList[0], 0); } - function testQuoter_quoteExactInput_0to2_0TicksCrossed_startingInitialized() public { + function testQuoter_quoteExactInput_0to2_0TicksLoaded_startingInitialized() public { setupPoolWithZeroTickInitialized(key02); tokenPath.push(token0); tokenPath.push(token2); @@ -190,15 +190,15 @@ contract QuoterTest is Test, Deployers { ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) = quoter.quoteExactInput(params); assertEq(uint128(-deltaAmounts[1]), 8); assertEq(sqrtPriceX96AfterList[0], 79227817515327498931091950511); - assertEq(initializedTicksCrossedList[0], 1); + assertEq(initializedTicksLoadedList[0], 1); } - function testQuoter_quoteExactInput_2to0_2TicksCrossed() public { + function testQuoter_quoteExactInput_2to0_2TicksLoaded() public { tokenPath.push(token2); tokenPath.push(token0); ExactInputParams memory params = getExactInputParams(tokenPath, 10000); @@ -206,15 +206,15 @@ contract QuoterTest is Test, Deployers { ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) = quoter.quoteExactInput(params); assertEq(-deltaAmounts[1], 9871); assertEq(sqrtPriceX96AfterList[0], 80001962924147897865541384515); - assertEq(initializedTicksCrossedList[0], 2); + assertEq(initializedTicksLoadedList[0], 2); } - function testQuoter_quoteExactInput_2to0_2TicksCrossed_initialiedAfter() public { + function testQuoter_quoteExactInput_2to0_2TicksLoaded_initialiedAfter() public { tokenPath.push(token2); tokenPath.push(token0); @@ -225,15 +225,15 @@ contract QuoterTest is Test, Deployers { ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) = quoter.quoteExactInput(params); assertEq(-deltaAmounts[1], 6190); assertEq(sqrtPriceX96AfterList[0], 79705728824507063507279123685); - assertEq(initializedTicksCrossedList[0], 2); + assertEq(initializedTicksLoadedList[0], 2); } - function testQuoter_quoteExactInput_2to0_0TicksCrossed_startingInitialized() public { + function testQuoter_quoteExactInput_2to0_0TicksLoaded_startingInitialized() public { setupPoolWithZeroTickInitialized(key02); tokenPath.push(token2); tokenPath.push(token0); @@ -243,16 +243,16 @@ contract QuoterTest is Test, Deployers { ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) = quoter.quoteExactInput(params); assertEq(-deltaAmounts[1], 198); assertEq(sqrtPriceX96AfterList[0], 79235729830182478001034429156); - assertEq(initializedTicksCrossedList[0], 0); + assertEq(initializedTicksLoadedList[0], 0); } // 2->0 starting not initialized - function testQuoter_quoteExactInput_2to0_0TicksCrossed_startingNotInitialized() public { + function testQuoter_quoteExactInput_2to0_0TicksLoaded_startingNotInitialized() public { tokenPath.push(token2); tokenPath.push(token0); ExactInputParams memory params = getExactInputParams(tokenPath, 103); @@ -260,12 +260,12 @@ contract QuoterTest is Test, Deployers { ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) = quoter.quoteExactInput(params); assertEq(-deltaAmounts[1], 101); assertEq(sqrtPriceX96AfterList[0], 79235858216754624215638319723); - assertEq(initializedTicksCrossedList[0], 0); + assertEq(initializedTicksLoadedList[0], 0); } function testQuoter_quoteExactInput_2to1() public { @@ -276,11 +276,11 @@ contract QuoterTest is Test, Deployers { ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) = quoter.quoteExactInput(params); assertEq(-deltaAmounts[1], 9871); assertEq(sqrtPriceX96AfterList[0], 80018067294531553039351583520); - assertEq(initializedTicksCrossedList[0], 0); + assertEq(initializedTicksLoadedList[0], 0); } function testQuoter_quoteExactInput_0to2to1() public { @@ -292,14 +292,14 @@ contract QuoterTest is Test, Deployers { ( int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, - uint32[] memory initializedTicksCrossedList + uint32[] memory initializedTicksLoadedList ) = quoter.quoteExactInput(params); assertEq(-deltaAmounts[2], 9745); assertEq(sqrtPriceX96AfterList[0], 78461846509168490764501028180); assertEq(sqrtPriceX96AfterList[1], 80007846861567212939802016351); - assertEq(initializedTicksCrossedList[0], 2); - assertEq(initializedTicksCrossedList[1], 0); + assertEq(initializedTicksLoadedList[0], 2); + assertEq(initializedTicksLoadedList[1], 0); } function createPoolKey(MockERC20 tokenA, MockERC20 tokenB, address hookAddr) @@ -418,7 +418,7 @@ contract QuoterTest is Test, Deployers { params.amountIn = uint128(amountIn); } - function logTicksCrossed(uint32[] memory num) private view { + function logTicksLoaded(uint32[] memory num) private view { console.logString("=== Num Ticks Crossed ==="); for (uint256 i = 0; i < num.length; i++) { console.logUint(num[i]);