Skip to content

Commit

Permalink
change return type
Browse files Browse the repository at this point in the history
  • Loading branch information
xyek committed May 31, 2024
1 parent be6f9bf commit 27aa510
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
23 changes: 13 additions & 10 deletions src/libraries/StateLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,28 @@ library StateLibrary {
// index of Position.Info mapping in Pool.State: mapping(bytes32 => Position.Info) positions;
uint256 public constant POSITIONS_OFFSET = 6;

struct PoolStateView {
Slot0 slot0;
uint256 feeGrowthGlobal0X128;
uint256 feeGrowthGlobal1X128;
uint128 liquidity;
}

/**
* @notice Get the global state of a pool.
* @dev Corresponds to pools[poolId]
* @param manager The pool manager contract.
* @param poolId The ID of the pool.
* @return state The state of the pool.
* @return slot0 The slot0 of the pool.
* @return feeGrowthGlobal0X128 The global fee growth for token0.
* @return feeGrowthGlobal1X128 The global fee growth for token1.
* @return liquidity The liquidity of the pool.
*/
function getPoolState(IPoolManager manager, PoolId poolId) internal view returns (PoolStateView memory state) {
function getPoolState(IPoolManager manager, PoolId poolId)
internal
view
returns (Slot0 slot0, uint256 feeGrowthGlobal0X128, uint256 feeGrowthGlobal1X128, uint128 liquidity)
{
bytes32 stateSlot = _getPoolStateSlot(poolId);
bytes memory data = manager.extsload(stateSlot, 4);
assembly {
state := add(data, 32)
slot0 := mload(add(data, 32))
feeGrowthGlobal0X128 := mload(add(data, 64))
feeGrowthGlobal1X128 := mload(add(data, 96))
liquidity := mload(add(data, 128))
}
}

Expand Down
23 changes: 13 additions & 10 deletions test/libraries/StateLibrary.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {Hooks} from "../../src/libraries/Hooks.sol";
import {TickMath} from "../../src/libraries/TickMath.sol";
import {IPoolManager} from "../../src/interfaces/IPoolManager.sol";
import {PoolKey} from "../../src/types/PoolKey.sol";
import {Slot0} from "../../src/types/Slot0.sol";
import {BalanceDelta} from "../../src/types/BalanceDelta.sol";
import {PoolId, PoolIdLibrary} from "../../src/types/PoolId.sol";
import {CurrencyLibrary, Currency} from "../../src/types/Currency.sol";
Expand Down Expand Up @@ -51,20 +52,22 @@ contract StateLibraryTest is Test, Deployers, Fuzzers, GasSnapshot {
uint256 swapAmount = 100 ether;
swap(key, true, -int256(swapAmount), ZERO_BYTES);

StateLibrary.PoolStateView memory state = StateLibrary.getPoolState(manager, poolId);
(Slot0 slot0, uint256 feeGrowthGlobal0X128, uint256 feeGrowthGlobal1X128, uint128 liquidity) =
StateLibrary.getPoolState(manager, poolId);
snapLastCall("extsload getPoolState");

assertEq(state.slot0.sqrtPriceX96(), 78680104762184586858280382455);
assertEq(state.slot0.tick(), -139);
assertEq(state.slot0.protocolFee(), 0);
assertEq(state.slot0.lpFee(), 3000);
assertEq(slot0.sqrtPriceX96(), 78680104762184586858280382455);
assertEq(slot0.tick(), -139);
assertEq(slot0.protocolFee(), 0);
assertEq(slot0.lpFee(), 3000);

uint256 liquidity = StateLibrary.getLiquidity(manager, poolId);
assertEq(state.liquidity, liquidity);
uint256 liquidityExpected = StateLibrary.getLiquidity(manager, poolId);
assertEq(liquidity, liquidityExpected);

(uint256 feeGrowthGlobal0, uint256 feeGrowthGlobal1) = StateLibrary.getFeeGrowthGlobals(manager, poolId);
assertEq(state.feeGrowthGlobal0X128, feeGrowthGlobal0);
assertEq(state.feeGrowthGlobal1X128, feeGrowthGlobal1);
(uint256 feeGrowthGlobal0Expected, uint256 feeGrowthGlobal1Expected) =
StateLibrary.getFeeGrowthGlobals(manager, poolId);
assertEq(feeGrowthGlobal0X128, feeGrowthGlobal0Expected);
assertEq(feeGrowthGlobal1X128, feeGrowthGlobal1Expected);
}

function test_getSlot0() public {
Expand Down

0 comments on commit 27aa510

Please sign in to comment.