diff --git a/.forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap b/.forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap index 0047034f..286d19b7 100644 --- a/.forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap +++ b/.forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap @@ -1 +1 @@ -178819 \ No newline at end of file +178871 \ No newline at end of file diff --git a/.forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap b/.forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap index 35b55257..b1a30062 100644 --- a/.forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap +++ b/.forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap @@ -1 +1 @@ -328598 \ No newline at end of file +328650 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerBytecodeSize.snap b/.forge-snapshots/BinPoolManagerBytecodeSize.snap index cf151c52..cf2a49b9 100644 --- a/.forge-snapshots/BinPoolManagerBytecodeSize.snap +++ b/.forge-snapshots/BinPoolManagerBytecodeSize.snap @@ -1 +1 @@ -23620 \ No newline at end of file +23691 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap b/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap index 605c3b5a..95111a71 100644 --- a/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap +++ b/.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap @@ -1 +1 @@ -133834 \ No newline at end of file +133876 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap b/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap index a11299ea..9d1d0de9 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasBurnHalfBin.snap @@ -1 +1 @@ -142745 \ No newline at end of file +142797 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap b/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap index 317dbb45..391d41e1 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasBurnNineBins.snap @@ -1 +1 @@ -288502 \ No newline at end of file +288876 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap b/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap index 840fba64..5c4f7a43 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap @@ -1 +1 @@ -127005 \ No newline at end of file +127046 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-1.snap b/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-1.snap index 3ad701fd..560ec845 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-1.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-1.snap @@ -1 +1 @@ -967714 \ No newline at end of file +968182 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-2.snap b/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-2.snap index 913fe209..b17c5273 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-2.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasMintNneBins-2.snap @@ -1 +1 @@ -325931 \ No newline at end of file +326399 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-1.snap b/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-1.snap index 2feb5e3e..b8b0965c 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-1.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-1.snap @@ -1 +1 @@ -337317 \ No newline at end of file +337369 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-2.snap b/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-2.snap index 07206f3d..8b87f9b2 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-2.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasMintOneBin-2.snap @@ -1 +1 @@ -139719 \ No newline at end of file +139771 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testMintNativeCurrency.snap b/.forge-snapshots/BinPoolManagerTest#testMintNativeCurrency.snap index b56a4263..ec46c4bf 100644 --- a/.forge-snapshots/BinPoolManagerTest#testMintNativeCurrency.snap +++ b/.forge-snapshots/BinPoolManagerTest#testMintNativeCurrency.snap @@ -1 +1 @@ -304287 \ No newline at end of file +304339 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerBytecodeSize.snap b/.forge-snapshots/CLPoolManagerBytecodeSize.snap index e317435e..c70bcfef 100644 --- a/.forge-snapshots/CLPoolManagerBytecodeSize.snap +++ b/.forge-snapshots/CLPoolManagerBytecodeSize.snap @@ -1 +1 @@ -22712 \ No newline at end of file +22811 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap index 04ed6b65..91208801 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromEmpty.snap @@ -1 +1 @@ -352371 \ No newline at end of file +352438 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap index d8e16a6c..8bd422ed 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_fromNonEmpty.snap @@ -1 +1 @@ -167812 \ No newline at end of file +167879 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap b/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap index 8896fe97..93db1d73 100644 --- a/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap +++ b/.forge-snapshots/CLPoolManagerTest#addLiquidity_nativeToken.snap @@ -1 +1 @@ -239219 \ No newline at end of file +239286 \ No newline at end of file diff --git a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap index bb825a9a..547ca60c 100644 --- a/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap +++ b/.forge-snapshots/CLPoolManagerTest#removeLiquidity_toNonEmpty.snap @@ -1 +1 @@ -115846 \ No newline at end of file +115913 \ No newline at end of file diff --git a/.forge-snapshots/ExtsloadTest#extsloadInBatch.snap b/.forge-snapshots/ExtsloadTest#extsloadInBatch.snap index c20afdd0..30d78bbc 100644 --- a/.forge-snapshots/ExtsloadTest#extsloadInBatch.snap +++ b/.forge-snapshots/ExtsloadTest#extsloadInBatch.snap @@ -1 +1 @@ -11050 \ No newline at end of file +11109 \ No newline at end of file diff --git a/src/Extsload.sol b/src/Extsload.sol index b5492913..243d7c58 100644 --- a/src/Extsload.sol +++ b/src/Extsload.sol @@ -20,24 +20,25 @@ abstract contract Extsload is IExtsload { /// @inheritdoc IExtsload function extsload(bytes32[] calldata slots) external view returns (bytes32[] memory) { - // since the function is external and enters a new call context and exits right - // after execution, Solidity's memory management convention can be disregarded - // and a direct slice of memory can be returned assembly ("memory-safe") { - // Copy the abi offset of dynamic array and the length of the array to memory. - calldatacopy(0, 0x04, 0x40) + let memptr := mload(0x40) + let start := memptr + // for abi encoding the response - the array will be found at 0x20 + mstore(memptr, 0x20) + // next we store the length of the return array + mstore(add(memptr, 0x20), slots.length) + // update memptr to the first location to hold an array entry + memptr := add(memptr, 0x40) // A left bit-shift of 5 is equivalent to multiplying by 32 but costs less gas. - let end := add(0x40, shl(5, slots.length)) + let end := add(memptr, shl(5, slots.length)) let calldataptr := slots.offset - // Return values will start at 64 while calldata offset is 68. - for { let memptr := 0x40 } 1 {} { + for {} 1 {} { mstore(memptr, sload(calldataload(calldataptr))) memptr := add(memptr, 0x20) calldataptr := add(calldataptr, 0x20) if iszero(lt(memptr, end)) { break } } - // The end offset is also the length of the returndata. - return(0, end) + return(start, sub(end, start)) } } } diff --git a/src/pool-bin/libraries/BinPosition.sol b/src/pool-bin/libraries/BinPosition.sol index 1db77e5d..64002679 100644 --- a/src/pool-bin/libraries/BinPosition.sol +++ b/src/pool-bin/libraries/BinPosition.sol @@ -31,13 +31,16 @@ library BinPosition { // ref: https://github.com/Vectorized/solady/blob/main/src/tokens/ERC20.sol#L95 // memory will be 12 bytes of zeros, the 20 bytes of address, 3 bytes for uint24 assembly ("memory-safe") { - mstore(0x23, salt) - mstore(0x03, binId) - mstore(0x00, owner) - key := keccak256(0x0c, 0x37) - // 0x00 - 0x3f is scratch space - // 0x40 ~ 0x46 should be clear to avoid polluting free pointer - mstore(0x23, 0) + let fmp := mload(0x40) + mstore(add(fmp, 0x23), salt) // [0x23, 0x43) + mstore(add(fmp, 0x03), binId) // [0x03, 0x23) + mstore(fmp, owner) // [0x0c, 0x20) + key := keccak256(add(fmp, 0x0c), 0x37) // len is 55 bytes + + // now clean the memory we used + mstore(add(fmp, 0x40), 0) // fmp+0x40 held salt + mstore(add(fmp, 0x20), 0) // fmp+0x20 held binId, salt + mstore(fmp, 0) // fmp held owner } position = self[key]; } diff --git a/src/pool-cl/libraries/CLPosition.sol b/src/pool-cl/libraries/CLPosition.sol index 02c41bec..4ad39fc3 100644 --- a/src/pool-cl/libraries/CLPosition.sol +++ b/src/pool-cl/libraries/CLPosition.sol @@ -39,14 +39,17 @@ library CLPosition { // make use of memory scratch space // ref: https://github.com/Vectorized/solady/blob/main/src/tokens/ERC20.sol#L95 assembly ("memory-safe") { - mstore(0x26, salt) - mstore(0x06, tickUpper) - mstore(0x03, tickLower) - mstore(0x00, owner) - key := keccak256(0x0c, 0x3a) - // 0x00 - 0x3f is scratch space - // 0x40 ~ 0x46 should be clear to avoid polluting free pointer - mstore(0x26, 0) + let fmp := mload(0x40) + mstore(add(fmp, 0x26), salt) // [0x26, 0x46) + mstore(add(fmp, 0x06), tickUpper) // [0x23, 0x26) + mstore(add(fmp, 0x03), tickLower) // [0x20, 0x23) + mstore(fmp, owner) // [0x0c, 0x20) + key := keccak256(add(fmp, 0x0c), 0x3a) // len is 58 bytes + + // now clean the memory we used + mstore(add(fmp, 0x40), 0) // fmp+0x40 held salt + mstore(add(fmp, 0x20), 0) // fmp+0x20 held tickLower, tickUpper, salt + mstore(fmp, 0) // fmp held owner } position = self[key]; } diff --git a/src/pool-cl/libraries/TickBitmap.sol b/src/pool-cl/libraries/TickBitmap.sol index 4fc6af48..472dcb32 100644 --- a/src/pool-cl/libraries/TickBitmap.sol +++ b/src/pool-cl/libraries/TickBitmap.sol @@ -53,10 +53,11 @@ library TickBitmap { assembly ("memory-safe") { // ensure that the tick is spaced if smod(tick, tickSpacing) { - mstore(0, 0xd4d8f3e6) // selector for TickMisaligned(int24,int24) - mstore(0x20, tick) - mstore(0x40, tickSpacing) - revert(0x1c, 0x44) + let fmp := mload(0x40) + mstore(fmp, 0xd4d8f3e6) // selector for TickMisaligned(int24,int24) + mstore(add(fmp, 0x20), tick) + mstore(add(fmp, 0x40), tickSpacing) + revert(add(fmp, 0x1c), 0x44) } tick := sdiv(tick, tickSpacing) // calculate the storage slot corresponding to the tick