Skip to content

Commit

Permalink
feat: Add batch test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefSnoopy committed Aug 6, 2024
1 parent 65da894 commit e5acfba
Show file tree
Hide file tree
Showing 26 changed files with 290 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
180746
180763
Original file line number Diff line number Diff line change
@@ -1 +1 @@
634934
634951
Original file line number Diff line number Diff line change
@@ -1 +1 @@
901854
901871
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1268280
1268297
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
913834
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
900913
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
222821
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
212335
Original file line number Diff line number Diff line change
@@ -1 +1 @@
146632
146615
Original file line number Diff line number Diff line change
@@ -1 +1 @@
212950
212997
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238013
238072
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1014995
1015012
Original file line number Diff line number Diff line change
@@ -1 +1 @@
975172
975189
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1019314
1019331
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1093912
1093929
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1054141
1054158
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1091663
1091680
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1015007
1015024
Original file line number Diff line number Diff line change
@@ -1 +1 @@
975184
975201
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1019311
1019328
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1091894
1091911
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1052123
1052140
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1089641
1089658
7 changes: 3 additions & 4 deletions src/pool-bin/BinFungiblePositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ contract BinFungiblePositionManager is
(amount0, amount1, tokenIds, liquidityMinted) = abi.decode(
vault.lock(abi.encode(msg.sender, true, addLiquidityData)), (uint128, uint128, uint256[], uint256[])
);

emit TransferBatch(msg.sender, address(0), params.to, tokenIds, liquidityMinted);
}

/// @inheritdoc IBinFungiblePositionManager
Expand All @@ -131,8 +129,6 @@ contract BinFungiblePositionManager is

(amount0, amount1, tokenIds) =
abi.decode(vault.lock(abi.encode(msg.sender, true, removeLiquidityData)), (uint128, uint128, uint256[]));

emit TransferBatch(msg.sender, params.from, address(0), tokenIds, params.amounts);
}

function lockAcquired(bytes calldata rawData) external override returns (bytes memory returnData) {
Expand Down Expand Up @@ -249,6 +245,8 @@ contract BinFungiblePositionManager is
}
}

emit TransferBatch(sender, address(0), params.to, tokenIds, mintArray.liquidityMinted);

return abi.encode(uint128(-delta.amount0()), uint128(-delta.amount1()), tokenIds, mintArray.liquidityMinted);
}

Expand Down Expand Up @@ -286,6 +284,7 @@ contract BinFungiblePositionManager is
++i;
}
}
emit TransferBatch(sender, params.from, address(0), tokenIds, params.amounts);

return abi.encode(delta.amount0(), delta.amount1(), tokenIds);
}
Expand Down
131 changes: 131 additions & 0 deletions test/pool-bin/BinFungiblePositionManager_AddLiquidity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,137 @@ contract BinFungiblePositionManager_AddLiquidityTest is Test, GasSnapshot, Liqui
}
}

function testBatch_AddLiquidityWithActiveId_WithoutCloseCurrency() public {
// pre-test, verify alice has 1e18 token0 and token1
token0.mint(alice, 1 ether);
token1.mint(alice, 1 ether);
assertEq(token0.balanceOf(alice), 1 ether);
assertEq(token1.balanceOf(alice), 1 ether);

vm.startPrank(alice);
uint24[] memory binIds = getBinIds(activeId, 3);
IBinFungiblePositionManager.AddLiquidityParams memory params;
params = _getAddParams(key1, binIds, 1 ether, 1 ether, activeId, alice);

// Expect emitted events
uint256[] memory liquidityMinted = new uint256[](binIds.length);
bytes32 binReserves = PackedUint128Math.encode(0, 0); // binReserve=0 for new pool
liquidityMinted[0] = calculateLiquidityMinted(binReserves, 0 ether, 0.5 ether, binIds[0], 10, 0);
liquidityMinted[1] = calculateLiquidityMinted(binReserves, 0.5 ether, 0.5 ether, binIds[1], 10, 0);
liquidityMinted[2] = calculateLiquidityMinted(binReserves, 0.5 ether, 0 ether, binIds[2], 10, 0);
uint256[] memory tokenIds = new uint256[](binIds.length);
for (uint256 i; i < binIds.length; i++) {
tokenIds[i] = key1.toId().toTokenId(binIds[i]);
}
vm.expectEmit();
emit TransferBatch(alice, address(0), alice, tokenIds, liquidityMinted);

// generate modifyLiquidities payload
bytes[] memory payloadArray = new bytes[](1);
bytes memory addliquidityData = abi.encode(
IBinFungiblePositionManager.CallbackData(
IBinFungiblePositionManager.CallbackDataType.AddLiquidity, abi.encode(params)
)
);
payloadArray[0] = addliquidityData;
bytes memory payload = abi.encode(payloadArray);

// amt0, amt1 = total amt0/amt1 from addLiquidity -- 660207 -
snapStart("BinFungiblePositionManager_AddLiquidityTest#testBatch_AddLiquidityWithActiveId_WithoutCloseCurrency");
bytes[] memory returnDataArrayBytes = binFungiblePositionManager.modifyLiquidities(payload, block.timestamp + 1);
snapEnd();
(uint128 amt0, uint128 amt1, uint256[] memory _tokenIds, uint256[] memory _liquidityMinted) =
abi.decode(returnDataArrayBytes[0], (uint128, uint128, uint256[], uint256[]));

// verify token taken from alice
assertEq(amt0, 1 ether);
assertEq(amt1, 1 ether);
assertEq(token0.balanceOf(alice), 0);
assertEq(token1.balanceOf(alice), 0);

for (uint256 i; i < binIds.length; i++) {
// verify nft minted to user
uint256 balance = binFungiblePositionManager.balanceOf(alice, tokenIds[i]);
assertEq(balance, _liquidityMinted[i]);
assertGt(balance, 0);

// verify return value from addLiquidity
assertEq(tokenIds[i], _tokenIds[i]);
}
}

function testBatch_AddLiquidityWithActiveId() public {
// pre-test, verify alice has 1e18 token0 and token1
token0.mint(alice, 1 ether);
token1.mint(alice, 1 ether);
assertEq(token0.balanceOf(alice), 1 ether);
assertEq(token1.balanceOf(alice), 1 ether);

vm.startPrank(alice);
uint24[] memory binIds = getBinIds(activeId, 3);
IBinFungiblePositionManager.AddLiquidityParams memory params;
params = _getAddParams(key1, binIds, 1 ether, 1 ether, activeId, alice);

// Expect emitted events
uint256[] memory liquidityMinted = new uint256[](binIds.length);
bytes32 binReserves = PackedUint128Math.encode(0, 0); // binReserve=0 for new pool
liquidityMinted[0] = calculateLiquidityMinted(binReserves, 0 ether, 0.5 ether, binIds[0], 10, 0);
liquidityMinted[1] = calculateLiquidityMinted(binReserves, 0.5 ether, 0.5 ether, binIds[1], 10, 0);
liquidityMinted[2] = calculateLiquidityMinted(binReserves, 0.5 ether, 0 ether, binIds[2], 10, 0);
uint256[] memory tokenIds = new uint256[](binIds.length);
for (uint256 i; i < binIds.length; i++) {
tokenIds[i] = key1.toId().toTokenId(binIds[i]);
}
vm.expectEmit();
emit TransferBatch(alice, address(0), alice, tokenIds, liquidityMinted);

// generate modifyLiquidities payload
bytes[] memory payloadArray = new bytes[](3);
bytes memory addliquidityData = abi.encode(
IBinFungiblePositionManager.CallbackData(
IBinFungiblePositionManager.CallbackDataType.AddLiquidity, abi.encode(params)
)
);
payloadArray[0] = addliquidityData;

// set current close data
payloadArray[1] = abi.encode(
IBinFungiblePositionManager.CallbackData(
IBinFungiblePositionManager.CallbackDataType.CloseCurrency, abi.encode(key1.currency0)
)
);
payloadArray[2] = abi.encode(
IBinFungiblePositionManager.CallbackData(
IBinFungiblePositionManager.CallbackDataType.CloseCurrency, abi.encode(currency1)
)
);

bytes memory payload = abi.encode(payloadArray);

// amt0, amt1 = total amt0/amt1 from addLiquidity -- 660207 -
snapStart("BinFungiblePositionManager_AddLiquidityTest#testBatch_AddLiquidityWithActiveId");
bytes[] memory returnDataArrayBytes = binFungiblePositionManager.modifyLiquidities(payload, block.timestamp + 1);
snapEnd();
(uint128 amt0, uint128 amt1, uint256[] memory _tokenIds, uint256[] memory _liquidityMinted) =
abi.decode(returnDataArrayBytes[0], (uint128, uint128, uint256[], uint256[]));

// verify token taken from alice
assertEq(amt0, 1 ether);
assertEq(amt1, 1 ether);
assertEq(token0.balanceOf(alice), 0);
assertEq(token1.balanceOf(alice), 0);

for (uint256 i; i < binIds.length; i++) {
// verify nft minted to user
uint256 balance = binFungiblePositionManager.balanceOf(alice, tokenIds[i]);
assertEq(balance, _liquidityMinted[i]);
assertGt(balance, 0);

// verify return value from addLiquidity
assertEq(tokenIds[i], _tokenIds[i]);
}
}

function testAddLiquidityOutsideActiveId() public {
// add at the left side, so all tokenY
token1.mint(alice, 2 ether);
Expand Down
Loading

0 comments on commit e5acfba

Please sign in to comment.