Skip to content

Commit

Permalink
do abi.encodePacked with inline assembly in Position.get (#279)
Browse files Browse the repository at this point in the history
* do `abi.encodePacked` with inline assembly

* Update gas snapshots

* Add test for `Position.get`

* Update gas snapshots

* Update gas snapshots
  • Loading branch information
shuhuiluo authored Apr 8, 2024
1 parent 9f50d5d commit 126ec86
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
264715
264566
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139575
139426
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
144892
144743
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22769
22598
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
55375
55226
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
147561
147412
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149025
148876
11 changes: 10 additions & 1 deletion src/libraries/Position.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ library Position {
view
returns (Info storage position)
{
position = self[keccak256(abi.encodePacked(owner, tickLower, tickUpper))];
// positionKey = keccak256(abi.encodePacked(owner, tickLower, tickUpper))
bytes32 positionKey;
/// @solidity memory-safe-assembly
assembly {
mstore(0x06, tickUpper) // [0x23, 0x26)
mstore(0x03, tickLower) // [0x20, 0x23)
mstore(0, owner) // [0x0c, 0x20)
positionKey := keccak256(0x0c, 0x1a)
}
position = self[positionKey];
}

/// @notice Credits accumulated fees to a user's position
Expand Down
24 changes: 24 additions & 0 deletions test/libraries/Position.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

import {Test} from "forge-std/Test.sol";
import {Position} from "src/libraries/Position.sol";

contract PositionTest is Test {
using Position for mapping(bytes32 => Position.Info);

mapping(bytes32 => Position.Info) internal positions;

function test_get_fuzz(address owner, int24 tickLower, int24 tickUpper) public {
bytes32 positionKey = keccak256(abi.encodePacked(owner, tickLower, tickUpper));
Position.Info storage expectedPosition = positions[positionKey];
Position.Info storage position = positions.get(owner, tickLower, tickUpper);
bytes32 expectedPositionSlot;
bytes32 positionSlot;
assembly ("memory-safe") {
expectedPositionSlot := expectedPosition.slot
positionSlot := position.slot
}
assertEq(positionSlot, expectedPositionSlot, "slots not equal");
}
}

0 comments on commit 126ec86

Please sign in to comment.