Skip to content

Commit

Permalink
move helpers to the same file
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepoint committed Jul 1, 2024
1 parent 9f472a2 commit b92abcc
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 215 deletions.
21 changes: 3 additions & 18 deletions test/position-managers/Execute.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ import {LiquidityRange, LiquidityRangeId, LiquidityRangeIdLibrary} from "../../c

import {LiquidityFuzzers} from "../shared/fuzz/LiquidityFuzzers.sol";

contract ExecuteTest is Test, Deployers, GasSnapshot, LiquidityFuzzers {
import {LiquidityOperations} from "../shared/LiquidityOperations.sol";

contract ExecuteTest is Test, Deployers, GasSnapshot, LiquidityFuzzers, LiquidityOperations {
using FixedPointMathLib for uint256;
using CurrencyLibrary for Currency;
using LiquidityRangeIdLibrary for LiquidityRange;
using PoolIdLibrary for PoolKey;
using SafeCast for uint256;

NonfungiblePositionManager lpm;

PoolId poolId;
address alice = makeAddr("ALICE");
address bob = makeAddr("BOB");
Expand Down Expand Up @@ -177,19 +177,4 @@ contract ExecuteTest is Test, Deployers, GasSnapshot, LiquidityFuzzers {
function test_execute_crossShard() public {}
// cross-feed: collect and increase on different keys
function test_execute_crossFeed() public {}

function _mint(
LiquidityRange memory _range,
uint256 liquidity,
uint256 deadline,
address recipient,
bytes memory hookData
) internal {
bytes[] memory calls = new bytes[](1);
calls[0] = abi.encodeWithSelector(lpm.mint.selector, _range, liquidity, deadline, recipient, hookData);
Currency[] memory currencies = new Currency[](2);
currencies[0] = currency0;
currencies[1] = currency1;
lpm.unlockAndExecute(calls, currencies);
}
}
69 changes: 7 additions & 62 deletions test/position-managers/FeeCollection.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,19 @@ import {LiquidityRange, LiquidityRangeId, LiquidityRangeIdLibrary} from "../../c

import {LiquidityFuzzers} from "../shared/fuzz/LiquidityFuzzers.sol";

contract FeeCollectionTest is Test, Deployers, GasSnapshot, LiquidityFuzzers {
import {LiquidityOperations} from "../shared/LiquidityOperations.sol";

contract FeeCollectionTest is Test, Deployers, GasSnapshot, LiquidityFuzzers, LiquidityOperations {
using FixedPointMathLib for uint256;
using CurrencyLibrary for Currency;
using LiquidityRangeIdLibrary for LiquidityRange;

NonfungiblePositionManager lpm;

PoolId poolId;
address alice = makeAddr("ALICE");
address bob = makeAddr("BOB");

uint256 constant STARTING_USER_BALANCE = 10_000_000 ether;

// unused value for the fuzz helper functions
uint128 constant DEAD_VALUE = 6969.6969 ether;

// expresses the fee as a wad (i.e. 3000 = 0.003e18)
uint256 FEE_WAD;

Expand Down Expand Up @@ -189,8 +186,9 @@ contract FeeCollectionTest is Test, Deployers, GasSnapshot, LiquidityFuzzers {
// alice collects only her fees
uint256 balance0AliceBefore = currency0.balanceOf(alice);
uint256 balance1AliceBefore = currency1.balanceOf(alice);
vm.prank(alice);
vm.startPrank(alice);
BalanceDelta delta = _collect(tokenIdAlice, alice, ZERO_BYTES, false);
vm.stopPrank();
uint256 balance0AliceAfter = currency0.balanceOf(alice);
uint256 balance1AliceAfter = currency1.balanceOf(alice);

Expand All @@ -201,8 +199,9 @@ contract FeeCollectionTest is Test, Deployers, GasSnapshot, LiquidityFuzzers {
// bob collects only his fees
uint256 balance0BobBefore = currency0.balanceOf(bob);
uint256 balance1BobBefore = currency1.balanceOf(bob);
vm.prank(bob);
vm.startPrank(bob);
delta = _collect(tokenIdBob, bob, ZERO_BYTES, false);
vm.stopPrank();
uint256 balance0BobAfter = currency0.balanceOf(bob);
uint256 balance1BobAfter = currency1.balanceOf(bob);

Expand Down Expand Up @@ -276,58 +275,4 @@ contract FeeCollectionTest is Test, Deployers, GasSnapshot, LiquidityFuzzers {
tolerance
);
}

function _mint(
LiquidityRange memory _range,
uint256 liquidity,
uint256 deadline,
address recipient,
bytes memory hookData
) internal returns (BalanceDelta) {
bytes[] memory calls = new bytes[](1);
calls[0] = abi.encodeWithSelector(lpm.mint.selector, _range, liquidity, deadline, recipient, hookData);
Currency[] memory currencies = new Currency[](2);
currencies[0] = currency0;
currencies[1] = currency1;
int128[] memory result = lpm.unlockAndExecute(calls, currencies);
return toBalanceDelta(result[0], result[1]);
}

function _increaseLiquidity(uint256 tokenId, uint256 liquidityToAdd, bytes memory hookData, bool claims) internal {
bytes[] memory calls = new bytes[](1);
calls[0] = abi.encodeWithSelector(lpm.increaseLiquidity.selector, tokenId, liquidityToAdd, hookData, claims);

Currency[] memory currencies = new Currency[](2);
currencies[0] = currency0;
currencies[1] = currency1;
lpm.unlockAndExecute(calls, currencies);
}

function _decreaseLiquidity(uint256 tokenId, uint256 liquidityToRemove, bytes memory hookData, bool claims)
internal
returns (BalanceDelta)
{
bytes[] memory calls = new bytes[](1);
calls[0] = abi.encodeWithSelector(lpm.decreaseLiquidity.selector, tokenId, liquidityToRemove, hookData, claims);

Currency[] memory currencies = new Currency[](2);
currencies[0] = currency0;
currencies[1] = currency1;
int128[] memory result = lpm.unlockAndExecute(calls, currencies);
return toBalanceDelta(result[0], result[1]);
}

function _collect(uint256 tokenId, address recipient, bytes memory hookData, bool claims)
internal
returns (BalanceDelta)
{
bytes[] memory calls = new bytes[](1);
calls[0] = abi.encodeWithSelector(lpm.collect.selector, tokenId, recipient, hookData, claims);

Currency[] memory currencies = new Currency[](2);
currencies[0] = currency0;
currencies[1] = currency1;
int128[] memory result = lpm.unlockAndExecute(calls, currencies);
return toBalanceDelta(result[0], result[1]);
}
}
21 changes: 3 additions & 18 deletions test/position-managers/Gas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {NonfungiblePositionManager} from "../../contracts/NonfungiblePositionManager.sol";
import {LiquidityRange, LiquidityRangeId, LiquidityRangeIdLibrary} from "../../contracts/types/LiquidityRange.sol";

contract GasTest is Test, Deployers, GasSnapshot {
import {LiquidityOperations} from "../shared/LiquidityOperations.sol";

contract GasTest is Test, Deployers, GasSnapshot, LiquidityOperations {
using FixedPointMathLib for uint256;
using CurrencyLibrary for Currency;
using LiquidityRangeIdLibrary for LiquidityRange;
using PoolIdLibrary for PoolKey;

NonfungiblePositionManager lpm;

PoolId poolId;
address alice = makeAddr("ALICE");
address bob = makeAddr("BOB");
Expand Down Expand Up @@ -310,19 +310,4 @@ contract GasTest is Test, Deployers, GasSnapshot {
function test_gas_burn() public {}
function test_gas_burnEmpty() public {}
function test_gas_collect() public {}

function _mint(
LiquidityRange memory _range,
uint256 liquidity,
uint256 deadline,
address recipient,
bytes memory hookData
) internal {
bytes[] memory calls = new bytes[](1);
calls[0] = abi.encodeWithSelector(lpm.mint.selector, _range, liquidity, deadline, recipient, hookData);
Currency[] memory currencies = new Currency[](2);
currencies[0] = currency0;
currencies[1] = currency1;
lpm.unlockAndExecute(calls, currencies);
}
}
84 changes: 27 additions & 57 deletions test/position-managers/IncreaseLiquidity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,20 @@ import {LiquidityRange, LiquidityRangeId, LiquidityRangeIdLibrary} from "../../c

import {Fuzzers} from "@uniswap/v4-core/src/test/Fuzzers.sol";

contract IncreaseLiquidityTest is Test, Deployers, GasSnapshot, Fuzzers {
import {LiquidityOperations} from "../shared/LiquidityOperations.sol";

contract IncreaseLiquidityTest is Test, Deployers, GasSnapshot, Fuzzers, LiquidityOperations {
using FixedPointMathLib for uint256;
using CurrencyLibrary for Currency;
using LiquidityRangeIdLibrary for LiquidityRange;
using PoolIdLibrary for PoolKey;

NonfungiblePositionManager lpm;

PoolId poolId;
address alice = makeAddr("ALICE");
address bob = makeAddr("BOB");

uint256 constant STARTING_USER_BALANCE = 10_000_000 ether;

// unused value for the fuzz helper functions
uint128 constant DEAD_VALUE = 6969.6969 ether;

// expresses the fee as a wad (i.e. 3000 = 0.003e18 = 0.30%)
uint256 FEE_WAD;

Expand Down Expand Up @@ -112,8 +109,9 @@ contract IncreaseLiquidityTest is Test, Deployers, GasSnapshot, Fuzzers {
uint256 balance0BeforeAlice = currency0.balanceOf(alice);
uint256 balance1BeforeAlice = currency1.balanceOf(alice);

vm.prank(alice);
vm.startPrank(alice);
_increaseLiquidity(tokenIdAlice, liquidityDelta, ZERO_BYTES, false);
vm.stopPrank();

// alice did not spend any tokens
assertEq(balance0BeforeAlice, currency0.balanceOf(alice));
Expand Down Expand Up @@ -160,8 +158,9 @@ contract IncreaseLiquidityTest is Test, Deployers, GasSnapshot, Fuzzers {
uint256 balance0BeforeAlice = currency0.balanceOf(alice);
uint256 balance1BeforeAlice = currency1.balanceOf(alice);

vm.prank(alice);
vm.startPrank(alice);
_increaseLiquidity(tokenIdAlice, liquidityDelta, ZERO_BYTES, false);
vm.stopPrank();

// alice did not spend any tokens
assertEq(balance0BeforeAlice, currency0.balanceOf(alice));
Expand Down Expand Up @@ -207,16 +206,18 @@ contract IncreaseLiquidityTest is Test, Deployers, GasSnapshot, Fuzzers {
token1Owed / 2
);

vm.prank(alice);
vm.startPrank(alice);
_increaseLiquidity(tokenIdAlice, liquidityDelta, ZERO_BYTES, false);
vm.stopPrank();
}

{
// bob collects his fees
uint256 balance0BeforeBob = currency0.balanceOf(bob);
uint256 balance1BeforeBob = currency1.balanceOf(bob);
vm.prank(bob);
vm.startPrank(bob);
_collect(tokenIdBob, bob, ZERO_BYTES, false);
vm.stopPrank();
uint256 balance0AfterBob = currency0.balanceOf(bob);
uint256 balance1AfterBob = currency1.balanceOf(bob);
assertApproxEqAbs(
Expand All @@ -235,8 +236,9 @@ contract IncreaseLiquidityTest is Test, Deployers, GasSnapshot, Fuzzers {
// alice collects her fees, which should be about half of the fees
uint256 balance0BeforeAlice = currency0.balanceOf(alice);
uint256 balance1BeforeAlice = currency1.balanceOf(alice);
vm.prank(alice);
vm.startPrank(alice);
_collect(tokenIdAlice, alice, ZERO_BYTES, false);
vm.stopPrank();
uint256 balance0AfterAlice = currency0.balanceOf(alice);
uint256 balance1AfterAlice = currency1.balanceOf(alice);
assertApproxEqAbs(
Expand Down Expand Up @@ -288,8 +290,9 @@ contract IncreaseLiquidityTest is Test, Deployers, GasSnapshot, Fuzzers {

uint256 balance0BeforeAlice = currency0.balanceOf(alice);
uint256 balance1BeforeAlice = currency1.balanceOf(alice);
vm.prank(alice);
vm.startPrank(alice);
_increaseLiquidity(tokenIdAlice, liquidityDelta, ZERO_BYTES, false);
vm.stopPrank();
uint256 balance0AfterAlice = currency0.balanceOf(alice);
uint256 balance1AfterAlice = currency1.balanceOf(alice);

Expand All @@ -301,8 +304,9 @@ contract IncreaseLiquidityTest is Test, Deployers, GasSnapshot, Fuzzers {
// bob collects his fees
uint256 balance0BeforeBob = currency0.balanceOf(bob);
uint256 balance1BeforeBob = currency1.balanceOf(bob);
vm.prank(bob);
vm.startPrank(bob);
_collect(tokenIdBob, bob, ZERO_BYTES, false);
vm.stopPrank();
uint256 balance0AfterBob = currency0.balanceOf(bob);
uint256 balance1AfterBob = currency1.balanceOf(bob);
assertApproxEqAbs(
Expand Down Expand Up @@ -343,8 +347,9 @@ contract IncreaseLiquidityTest is Test, Deployers, GasSnapshot, Fuzzers {
(uint256 token0Owed, uint256 token1Owed) = lpm.feesOwed(tokenIdAlice);

// bob collects fees so some of alice's fees are now cached
vm.prank(bob);
vm.startPrank(bob);
_collect(tokenIdBob, bob, ZERO_BYTES, false);
vm.stopPrank();

// swap to create more fees
swap(key, true, -int256(swapAmount), ZERO_BYTES);
Expand All @@ -369,8 +374,9 @@ contract IncreaseLiquidityTest is Test, Deployers, GasSnapshot, Fuzzers {
newToken1Owed
);

vm.prank(alice);
vm.startPrank(alice);
_increaseLiquidity(tokenIdAlice, liquidityDelta, ZERO_BYTES, false);
vm.stopPrank();
}

// alice did not spend any tokens
Expand Down Expand Up @@ -407,8 +413,9 @@ contract IncreaseLiquidityTest is Test, Deployers, GasSnapshot, Fuzzers {
(uint256 token0Owed, uint256 token1Owed) = lpm.feesOwed(tokenIdAlice);

// bob collects fees so some of alice's fees are now cached
vm.prank(bob);
vm.startPrank(bob);
_collect(tokenIdBob, bob, ZERO_BYTES, false);
vm.stopPrank();

// donate to create more fees
donateRouter.donate(key, 20e18, 20e18, ZERO_BYTES);
Expand All @@ -432,8 +439,9 @@ contract IncreaseLiquidityTest is Test, Deployers, GasSnapshot, Fuzzers {
newToken1Owed
);

vm.prank(alice);
vm.startPrank(alice);
_increaseLiquidity(tokenIdAlice, liquidityDelta, ZERO_BYTES, false);
vm.stopPrank();
}

// alice did not spend any tokens
Expand All @@ -449,48 +457,10 @@ contract IncreaseLiquidityTest is Test, Deployers, GasSnapshot, Fuzzers {
assertApproxEqAbs(token0Owed, 5e18, 1 wei);
assertApproxEqAbs(token1Owed, 5e18, 1 wei);

vm.prank(bob);
vm.startPrank(bob);
BalanceDelta result = _collect(tokenIdBob, bob, ZERO_BYTES, false);
vm.stopPrank();
assertApproxEqAbs(result.amount0(), 5e18, 1 wei);
assertApproxEqAbs(result.amount1(), 5e18, 1 wei);
}

function _mint(
LiquidityRange memory _range,
uint256 liquidity,
uint256 deadline,
address recipient,
bytes memory hookData
) internal {
bytes[] memory calls = new bytes[](1);
calls[0] = abi.encodeWithSelector(lpm.mint.selector, _range, liquidity, deadline, recipient, hookData);
Currency[] memory currencies = new Currency[](2);
currencies[0] = currency0;
currencies[1] = currency1;
lpm.unlockAndExecute(calls, currencies);
}

function _increaseLiquidity(uint256 tokenId, uint256 liquidityToAdd, bytes memory hookData, bool claims) internal {
bytes[] memory calls = new bytes[](1);
calls[0] = abi.encodeWithSelector(lpm.increaseLiquidity.selector, tokenId, liquidityToAdd, hookData, claims);

Currency[] memory currencies = new Currency[](2);
currencies[0] = currency0;
currencies[1] = currency1;
lpm.unlockAndExecute(calls, currencies);
}

function _collect(uint256 tokenId, address recipient, bytes memory hookData, bool claims)
internal
returns (BalanceDelta)
{
bytes[] memory calls = new bytes[](1);
calls[0] = abi.encodeWithSelector(lpm.collect.selector, tokenId, recipient, hookData, claims);

Currency[] memory currencies = new Currency[](2);
currencies[0] = currency0;
currencies[1] = currency1;
int128[] memory result = lpm.unlockAndExecute(calls, currencies);
return toBalanceDelta(result[0], result[1]);
}
}
Loading

0 comments on commit b92abcc

Please sign in to comment.