Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use State for consistency, add natspec on SwapResults #860

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
using SafeCast for *;
using Pool for *;
using Hooks for IHooks;
using Position for mapping(bytes32 => Position.Info);
using Position for mapping(bytes32 => Position.State);
using CurrencyDelta for Currency;
using LPFeeLibrary for uint24;
using CurrencyReserves for Currency;
Expand Down
12 changes: 6 additions & 6 deletions src/libraries/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {CustomRevert} from "./CustomRevert.sol";
library Pool {
using SafeCast for *;
using TickBitmap for mapping(int16 => uint256);
using Position for mapping(bytes32 => Position.Info);
using Position for Position.Info;
using Position for mapping(bytes32 => Position.State);
using Position for Position.State;
using Pool for State;
using ProtocolFeeLibrary for *;
using LPFeeLibrary for uint24;
Expand Down Expand Up @@ -84,7 +84,7 @@ library Pool {
uint128 liquidity;
mapping(int24 => TickInfo) ticks;
mapping(int16 => uint256) tickBitmap;
mapping(bytes32 => Position.Info) positions;
mapping(bytes32 => Position.State) positions;
}

/// @dev Common checks for valid tick inputs.
Expand Down Expand Up @@ -184,7 +184,7 @@ library Pool {
(uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) =
getFeeGrowthInside(self, tickLower, tickUpper);

Position.Info storage position = self.positions.get(params.owner, tickLower, tickUpper, params.salt);
Position.State storage position = self.positions.get(params.owner, tickLower, tickUpper, params.salt);
(uint256 feesOwed0, uint256 feesOwed1) =
position.update(liquidityDelta, feeGrowthInside0X128, feeGrowthInside1X128);

Expand Down Expand Up @@ -237,9 +237,9 @@ library Pool {
}
}

// the results after a swap, written to storage and returned
// Tracks the state of a pool throughout a swap, and bubbles these values up at the end of the swap
snreynolds marked this conversation as resolved.
Show resolved Hide resolved
struct SwapResult {
// the final current sqrt(price)
// the final sqrt(price)
snreynolds marked this conversation as resolved.
Show resolved Hide resolved
uint160 sqrtPriceX96;
// the tick associated with the current price
int24 tick;
Expand Down
10 changes: 5 additions & 5 deletions src/libraries/Position.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ library Position {
error CannotUpdateEmptyPosition();

// info stored for each user's position
struct Info {
struct State {
// the amount of liquidity owned by this position
uint128 liquidity;
// fee growth per unit of liquidity as of the last update to liquidity or fees owed
uint256 feeGrowthInside0LastX128;
uint256 feeGrowthInside1LastX128;
}

/// @notice Returns the Info struct of a position, given an owner and position boundaries
/// @notice Returns the State struct of a position, given an owner and position boundaries
/// @param self The mapping containing all user positions
/// @param owner The address of the position owner
/// @param tickLower The lower tick boundary of the position
/// @param tickUpper The upper tick boundary of the position
/// @param salt A unique value to differentiate between multiple positions in the same range
/// @return position The position info struct of the given owners' position
function get(mapping(bytes32 => Info) storage self, address owner, int24 tickLower, int24 tickUpper, bytes32 salt)
function get(mapping(bytes32 => State) storage self, address owner, int24 tickLower, int24 tickUpper, bytes32 salt)
internal
view
returns (Info storage position)
returns (State storage position)
{
bytes32 positionKey = calculatePositionKey(owner, tickLower, tickUpper, salt);
position = self[positionKey];
Expand Down Expand Up @@ -74,7 +74,7 @@ library Position {
/// @return feesOwed0 The amount of currency0 owed to the position owner
/// @return feesOwed1 The amount of currency1 owed to the position owner
function update(
Info storage self,
State storage self,
int128 liquidityDelta,
uint256 feeGrowthInside0X128,
uint256 feeGrowthInside1X128
Expand Down
8 changes: 4 additions & 4 deletions src/libraries/StateLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ library StateLibrary {
/// @notice index of tickBitmap mapping in Pool.State
uint256 public constant TICK_BITMAP_OFFSET = 5;

/// @notice index of Position.Info mapping in Pool.State: mapping(bytes32 => Position.Info) positions;
/// @notice index of Position.State mapping in Pool.State: mapping(bytes32 => Position.State) positions;
uint256 public constant POSITIONS_OFFSET = 6;

/**
Expand Down Expand Up @@ -255,7 +255,7 @@ library StateLibrary {
{
bytes32 slot = _getPositionInfoSlot(poolId, positionId);

// read all 3 words of the Position.Info struct
// read all 3 words of the Position.State struct
bytes32[] memory data = manager.extsload(slot, 3);

assembly ("memory-safe") {
Expand Down Expand Up @@ -284,7 +284,7 @@ library StateLibrary {

/**
* @notice Calculate the fee growth inside a tick range of a pool
* @dev pools[poolId].feeGrowthInside0LastX128 in Position.Info is cached and can become stale. This function will calculate the up to date feeGrowthInside
* @dev pools[poolId].feeGrowthInside0LastX128 in Position.State is cached and can become stale. This function will calculate the up to date feeGrowthInside
* @param manager The pool manager contract.
* @param poolId The ID of the pool.
* @param tickLower The lower tick of the range.
Expand Down Expand Up @@ -337,7 +337,7 @@ library StateLibrary {
// slot key of Pool.State value: `pools[poolId]`
bytes32 stateSlot = _getPoolStateSlot(poolId);

// Pool.State: `mapping(bytes32 => Position.Info) positions;`
// Pool.State: `mapping(bytes32 => Position.State) positions;`
bytes32 positionMapping = bytes32(uint256(stateSlot) + POSITIONS_OFFSET);

// slot of the mapping key: `pools[poolId].positions[positionId]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ProxyPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ contract ProxyPoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909
using SafeCast for *;
using Pool for *;
using Hooks for IHooks;
using Position for mapping(bytes32 => Position.Info);
using Position for mapping(bytes32 => Position.State);
using CurrencyDelta for Currency;
using LPFeeLibrary for uint24;
using CurrencyReserves for Currency;
Expand Down
12 changes: 6 additions & 6 deletions test/libraries/Position.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {Position} from "../../src/libraries/Position.sol";
import {SafeCast} from "../../src/libraries/SafeCast.sol";

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

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

function test_fuzz_get(address owner, int24 tickLower, int24 tickUpper, bytes32 salt) public view {
bytes32 positionKey = keccak256(abi.encodePacked(owner, tickLower, tickUpper, salt));
Position.Info storage expectedPosition = positions[positionKey];
Position.Info storage position = positions.get(owner, tickLower, tickUpper, salt);
Position.State storage expectedPosition = positions[positionKey];
Position.State storage position = positions.get(owner, tickLower, tickUpper, salt);
bytes32 expectedPositionSlot;
bytes32 positionSlot;
assembly ("memory-safe") {
Expand All @@ -25,11 +25,11 @@ contract PositionTest is Test {

function test_fuzz_update(
int128 liquidityDelta,
Position.Info memory pos,
Position.State memory pos,
uint256 newFeeGrowthInside0X128,
uint256 newFeeGrowthInside1X128
) public {
Position.Info storage position = positions[0];
Position.State storage position = positions[0];
position.liquidity = pos.liquidity;
position.feeGrowthInside0LastX128 = pos.feeGrowthInside0LastX128;
position.feeGrowthInside1LastX128 = pos.feeGrowthInside1LastX128;
Expand Down
Loading