Skip to content

Commit

Permalink
feat: Remove addLiquidity and removeLiquidity in BinFungiblePositionM…
Browse files Browse the repository at this point in the history
…anager
  • Loading branch information
ChefSnoopy committed Aug 7, 2024
1 parent 9faef2d commit 1234db4
Show file tree
Hide file tree
Showing 30 changed files with 93 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
180493
180350
Original file line number Diff line number Diff line change
@@ -1 +1 @@
634649
634518
Original file line number Diff line number Diff line change
@@ -1 +1 @@
900919
900791
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1266119
1265997
Original file line number Diff line number Diff line change
@@ -1 +1 @@
913840
913724
Original file line number Diff line number Diff line change
@@ -1 +1 @@
900919
900791
Original file line number Diff line number Diff line change
@@ -1 +1 @@
222862
222788
Original file line number Diff line number Diff line change
@@ -1 +1 @@
212366
212283
Original file line number Diff line number Diff line change
@@ -1 +1 @@
146512
146429
Original file line number Diff line number Diff line change
@@ -1 +1 @@
212365
212282
Original file line number Diff line number Diff line change
@@ -1 +1 @@
237897
237793
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1017493
1017338
Original file line number Diff line number Diff line change
@@ -1 +1 @@
977654
977526
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1021803
1021675
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1096421
1096266
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1056634
1056506
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1094163
1094035
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1017505
1017350
Original file line number Diff line number Diff line change
@@ -1 +1 @@
977666
977538
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1021800
1021672
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1094403
1094248
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1054616
1054488
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1092141
1092013
30 changes: 0 additions & 30 deletions src/pool-bin/BinFungiblePositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,36 +96,6 @@ contract BinFungiblePositionManager is
return abi.decode(vault.lock(abi.encode(msg.sender, false, lockData)), (bytes[]));
}

/// @inheritdoc IBinFungiblePositionManager
function addLiquidity(AddLiquidityParams calldata params)
external
payable
override
checkDeadline(params.deadline)
returns (uint128 amount0, uint128 amount1, uint256[] memory tokenIds, uint256[] memory liquidityMinted)
{
bytes memory addLiquidityData = abi.encode(CallbackData(CallbackDataType.AddLiquidity, abi.encode(params)));

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

/// @inheritdoc IBinFungiblePositionManager
function removeLiquidity(RemoveLiquidityParams calldata params)
external
payable
override
checkDeadline(params.deadline)
returns (uint128 amount0, uint128 amount1, uint256[] memory tokenIds)
{
bytes memory removeLiquidityData =
abi.encode(CallbackData(CallbackDataType.RemoveLiquidity, abi.encode(params)));

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

function lockAcquired(bytes calldata rawData) external override returns (bytes memory returnData) {
if (msg.sender != address(vault)) revert OnlyVaultCaller();

Expand Down
20 changes: 0 additions & 20 deletions src/pool-bin/interfaces/IBinFungiblePositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,4 @@ interface IBinFungiblePositionManager is IBinFungibleToken, IPeripheryPayments,
/// @param deadline is the deadline for the batched actions to be executed
/// @return returnData is the endocing of each actions return information
function modifyLiquidities(bytes calldata payload, uint256 deadline) external payable returns (bytes[] memory);

/// @notice Add liquidity, user will receive ERC1155 tokens as receipt of bin share ownership.
/// @dev The ID of the ERC11155 token is keccak256(abi.encode(poolkey.toId, binId))
/// @return amount0 Amount of token0 added
/// @return amount1 Amount of token1 added
/// @return tokenIds Ids of token minted
/// @return liquidityMinted Amount of liquidity added
function addLiquidity(AddLiquidityParams calldata)
external
payable
returns (uint128 amount0, uint128 amount1, uint256[] memory tokenIds, uint256[] memory liquidityMinted);

/// @notice Remove liquidity, burn NFT and retrieve back the ERC20 tokens for the liquidity
/// @return amount0 Amount of token0 removed
/// @return amount1 Amount of token1 removed
/// @return tokenIds Ids of token burnt
function removeLiquidity(RemoveLiquidityParams calldata)
external
payable
returns (uint128 amount0, uint128 amount1, uint256[] memory tokenIds);
}
6 changes: 3 additions & 3 deletions test/pool-bin/BinFungiblePositionManager_AddLiquidity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,9 @@ contract BinFungiblePositionManager_AddLiquidityTest is Test, GasSnapshot, Liqui
addParams.to = user;

if (user == alice) {
(,, aliceTokenIds, aliceLiquidityMinted) = binFungiblePositionManager.addLiquidity(addParams);
(,, aliceTokenIds, aliceLiquidityMinted) = addLiquidity(addParams);
} else {
(,, bobTokenIds, bobLiquidityMinted) = binFungiblePositionManager.addLiquidity(addParams);
(,, bobTokenIds, bobLiquidityMinted) = addLiquidity(addParams);
}

for (uint256 j; j < binIds.length; j++) {
Expand Down Expand Up @@ -676,7 +676,7 @@ contract BinFungiblePositionManager_AddLiquidityTest is Test, GasSnapshot, Liqui
function _getModifyLiquiditiesAddPayload(
IBinFungiblePositionManager.AddLiquidityParams memory params,
bool shouldCloseCurrency
) internal view returns (bytes memory) {
) internal pure returns (bytes memory) {
// generate modifyLiquidities data
bytes memory addLiquidityData = abi.encode(
IBinFungiblePositionManager.CallbackData(
Expand Down
14 changes: 7 additions & 7 deletions test/pool-bin/BinFungiblePositionManager_RemoveLiquidity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ contract BinFungiblePositionManager_RemoveLiquidityTest is Test, GasSnapshot, Li
token0.mint(alice, 1 ether);
token1.mint(alice, 1 ether);
params = _getAddParams(key1, binIds, 1 ether, 1 ether, activeId, alice);
(,,, uint256[] memory liquidityMinted) = binFungiblePositionManager.addLiquidity(params);
(,,, uint256[] memory liquidityMinted) = addLiquidity(params);

// remove more than minted, arithemtic as bal goes into negative
liquidityMinted[0] = liquidityMinted[0] + 1;
Expand All @@ -469,7 +469,7 @@ contract BinFungiblePositionManager_RemoveLiquidityTest is Test, GasSnapshot, Li
uint24[] memory binIds = getBinIds(activeId, 3);
IBinFungiblePositionManager.AddLiquidityParams memory params;
params = _getAddParams(key1, binIds, 1 ether, 1 ether, activeId, alice);
(,,, uint256[] memory liquidityMinted) = binFungiblePositionManager.addLiquidity(params);
(,,, uint256[] memory liquidityMinted) = addLiquidity(params);
vm.stopPrank();

assertEq(token0.balanceOf(bob), 0 ether);
Expand Down Expand Up @@ -500,7 +500,7 @@ contract BinFungiblePositionManager_RemoveLiquidityTest is Test, GasSnapshot, Li
uint24[] memory binIds = getBinIds(activeId, 3);
IBinFungiblePositionManager.AddLiquidityParams memory params;
params = _getAddParams(key1, binIds, 1 ether, 1 ether, activeId, alice);
(,,, uint256[] memory liquidityMinted) = binFungiblePositionManager.addLiquidity(params);
(,,, uint256[] memory liquidityMinted) = addLiquidity(params);
vm.stopPrank();

assertEq(token0.balanceOf(bob), 0 ether);
Expand Down Expand Up @@ -529,9 +529,9 @@ contract BinFungiblePositionManager_RemoveLiquidityTest is Test, GasSnapshot, Li
token0.mint(alice, 3 ether);
token1.mint(alice, 3 ether);
params = _getAddParams(key1, binIds, 1 ether, 1 ether, activeId, alice);
(,,, uint256[] memory liquidityMinted1) = binFungiblePositionManager.addLiquidity(params);
(,,, uint256[] memory liquidityMinted1) = addLiquidity(params);
params = _getAddParams(key2, binIds, 2 ether, 2 ether, activeId, alice);
(,,, uint256[] memory liquidityMinted2) = binFungiblePositionManager.addLiquidity(params);
(,,, uint256[] memory liquidityMinted2) = addLiquidity(params);

assertEq(token0.balanceOf(alice), 0);
assertEq(token1.balanceOf(alice), 0);
Expand Down Expand Up @@ -626,7 +626,7 @@ contract BinFungiblePositionManager_RemoveLiquidityTest is Test, GasSnapshot, Li
function _getModifyLiquiditiesRemovePayload(
IBinFungiblePositionManager.RemoveLiquidityParams memory params,
bool shouldCloseCurrency
) internal view returns (bytes memory) {
) internal pure returns (bytes memory) {
// generate modifyLiquidities data
bytes memory removeLiquidityData = abi.encode(
IBinFungiblePositionManager.CallbackData(
Expand Down Expand Up @@ -656,7 +656,7 @@ contract BinFungiblePositionManager_RemoveLiquidityTest is Test, GasSnapshot, Li
function _getModifyLiquiditiesAddPayload(
IBinFungiblePositionManager.AddLiquidityParams memory params,
bool shouldCloseCurrency
) internal view returns (bytes memory) {
) internal pure returns (bytes memory) {
// generate modifyLiquidities data
bytes memory addLiquidityData = abi.encode(
IBinFungiblePositionManager.CallbackData(
Expand Down
28 changes: 25 additions & 3 deletions test/pool-bin/BinQuoter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,37 @@ contract BinQuoterTest is Test, GasSnapshot, LiquidityParamsHelper {
uint24[] memory binIds = getBinIds(activeId, 3);
IBinFungiblePositionManager.AddLiquidityParams memory addParams;
addParams = _getAddParams(key, binIds, 10 ether, 10 ether, activeId, alice);
binFungiblePositionManager.addLiquidity(addParams);

bytes memory addLiquidityData = abi.encode(
IBinFungiblePositionManager.CallbackData(
IBinFungiblePositionManager.CallbackDataType.AddLiquidity, abi.encode(addParams)
)
);
bytes[] memory lockDataArray = new bytes[](1);
lockDataArray[0] = addLiquidityData;
binFungiblePositionManager.modifyLiquidities(abi.encode(lockDataArray), block.timestamp + 1);
addParams = _getAddParams(key2, binIds, 10 ether, 10 ether, activeId, alice);
binFungiblePositionManager.addLiquidity(addParams);

addLiquidityData = abi.encode(
IBinFungiblePositionManager.CallbackData(
IBinFungiblePositionManager.CallbackDataType.AddLiquidity, abi.encode(addParams)
)
);
lockDataArray[0] = addLiquidityData;
binFungiblePositionManager.modifyLiquidities(abi.encode(lockDataArray), block.timestamp + 1);

// add liquidity for ETH-token0 native pool (10 eth each)
token0.mint(alice, 10 ether);
vm.deal(alice, 10 ether);
addParams = _getAddParams(key3, binIds, 10 ether, 10 ether, activeId, alice);
binFungiblePositionManager.addLiquidity{value: 10 ether}(addParams);
// binFungiblePositionManager.addLiquidity{value: 10 ether}(addParams);
addLiquidityData = abi.encode(
IBinFungiblePositionManager.CallbackData(
IBinFungiblePositionManager.CallbackDataType.AddLiquidity, abi.encode(addParams)
)
);
lockDataArray[0] = addLiquidityData;
binFungiblePositionManager.modifyLiquidities{value: 10 ether}(abi.encode(lockDataArray), block.timestamp + 1);
}

function testQuoter_quoteExactInputSingle_zeroForOne() public {
Expand Down
28 changes: 25 additions & 3 deletions test/pool-bin/BinSwapRouter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,37 @@ contract BinSwapRouterTest is Test, GasSnapshot, LiquidityParamsHelper {
uint24[] memory binIds = getBinIds(activeId, 3);
IBinFungiblePositionManager.AddLiquidityParams memory addParams;
addParams = _getAddParams(key, binIds, 10 ether, 10 ether, activeId, alice);
binFungiblePositionManager.addLiquidity(addParams);
// binFungiblePositionManager.addLiquidity(addParams);
bytes memory addLiquidityData = abi.encode(
IBinFungiblePositionManager.CallbackData(
IBinFungiblePositionManager.CallbackDataType.AddLiquidity, abi.encode(addParams)
)
);
bytes[] memory lockDataArray = new bytes[](1);
lockDataArray[0] = addLiquidityData;
binFungiblePositionManager.modifyLiquidities(abi.encode(lockDataArray), block.timestamp + 1);
addParams = _getAddParams(key2, binIds, 10 ether, 10 ether, activeId, alice);
binFungiblePositionManager.addLiquidity(addParams);

addLiquidityData = abi.encode(
IBinFungiblePositionManager.CallbackData(
IBinFungiblePositionManager.CallbackDataType.AddLiquidity, abi.encode(addParams)
)
);
lockDataArray[0] = addLiquidityData;
binFungiblePositionManager.modifyLiquidities(abi.encode(lockDataArray), block.timestamp + 1);

// add liquidity for ETH-token0 native pool (10 eth each)
token0.mint(alice, 10 ether);
vm.deal(alice, 10 ether);
addParams = _getAddParams(key3, binIds, 10 ether, 10 ether, activeId, alice);
binFungiblePositionManager.addLiquidity{value: 10 ether}(addParams);

addLiquidityData = abi.encode(
IBinFungiblePositionManager.CallbackData(
IBinFungiblePositionManager.CallbackDataType.AddLiquidity, abi.encode(addParams)
)
);
lockDataArray[0] = addLiquidityData;
binFungiblePositionManager.modifyLiquidities{value: 10 ether}(abi.encode(lockDataArray), block.timestamp + 1);
}

function testLockAcquired_VaultOnly() public {
Expand Down
15 changes: 10 additions & 5 deletions test/pool-bin/BinSwapRouterInvariant.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,16 @@ contract BinSwapRouterHandler is Test, LiquidityParamsHelper {
uint24[] memory binIds = getBinIds(activeId, 3);
IBinFungiblePositionManager.AddLiquidityParams memory addParams;
addParams = _getAddParams(pk, binIds, amt, amt, activeId, alice);
if (isNativePool) {
binFungiblePositionManager.addLiquidity{value: amt}(addParams);
} else {
binFungiblePositionManager.addLiquidity(addParams);
}
bytes memory addLiquidityData = abi.encode(
IBinFungiblePositionManager.CallbackData(
IBinFungiblePositionManager.CallbackDataType.AddLiquidity, abi.encode(addParams)
)
);
bytes[] memory lockDataArray = new bytes[](1);
lockDataArray[0] = addLiquidityData;
binFungiblePositionManager.modifyLiquidities{value: isNativePool ? amt : 0}(
abi.encode(lockDataArray), block.timestamp + 1
);
vm.stopPrank();
}
}
Expand Down

0 comments on commit 1234db4

Please sign in to comment.