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

Position tests #659

Merged
merged 13 commits into from
May 21, 2024
37 changes: 37 additions & 0 deletions test/libraries/Position.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.20;

import {Test} from "forge-std/Test.sol";
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);
Expand All @@ -21,4 +22,40 @@ contract PositionTest is Test {
}
assertEq(positionSlot, expectedPositionSlot, "slots not equal");
}

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

uint128 oldLiquidity = position.liquidity;

if (position.liquidity == 0 && liquidityDelta == 0) {
vm.expectRevert(Position.CannotUpdateEmptyPosition.selector);
}

// new liquidity cannot overflow uint128
uint256 newLiquidity = uint256(int256(uint256(position.liquidity)) + int256(liquidityDelta));
if (newLiquidity > type(uint128).max) {
vm.expectRevert(SafeCast.SafeCastOverflow.selector);
}

Position.update(position, liquidityDelta, newFeeGrowthInside0X128, newFeeGrowthInside1X128);
if (liquidityDelta == 0) {
assertEq(position.liquidity, oldLiquidity);
} else if (liquidityDelta > 0) {
assertGt(position.liquidity, oldLiquidity);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this not be assertEq(position.liq, oldLiquidity + liquidityDelta) and same with the else brach too

} else {
assertLt(position.liquidity, oldLiquidity);
}

assertEq(position.feeGrowthInside0LastX128, newFeeGrowthInside0X128);
assertEq(position.feeGrowthInside1LastX128, newFeeGrowthInside1X128);
}
}
Loading