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

chore: Update with latest v4-core changes #32

Merged
merged 6 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186974
185311
Original file line number Diff line number Diff line change
@@ -1 +1 @@
656948
638186
Original file line number Diff line number Diff line change
@@ -1 +1 @@
945285
906704
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1312637
1274539
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153583
148991
Original file line number Diff line number Diff line change
@@ -1 +1 @@
224877
216053
Original file line number Diff line number Diff line change
@@ -1 +1 @@
252792
241767
Original file line number Diff line number Diff line change
@@ -1 +1 @@
54387
54579
Original file line number Diff line number Diff line change
@@ -1 +1 @@
52359
52095
2 changes: 1 addition & 1 deletion .forge-snapshots/BinFungibleTokenTest#testBurn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
27300
26887
2 changes: 1 addition & 1 deletion .forge-snapshots/BinFungibleTokenTest#testMint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
67877
67685
Original file line number Diff line number Diff line change
@@ -1 +1 @@
162255
154616
Original file line number Diff line number Diff line change
@@ -1 +1 @@
156725
145793
Original file line number Diff line number Diff line change
@@ -1 +1 @@
159530
151891
Original file line number Diff line number Diff line change
@@ -1 +1 @@
160249
152610
Original file line number Diff line number Diff line change
@@ -1 +1 @@
160312
152651
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186338
179342
Original file line number Diff line number Diff line change
@@ -1 +1 @@
172624
164893
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170630
162899
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170705
162936
Original file line number Diff line number Diff line change
@@ -1 +1 @@
200509
193297
Original file line number Diff line number Diff line change
@@ -1 +1 @@
162954
155207
2 changes: 1 addition & 1 deletion .forge-snapshots/CLSwapRouterTest#ExactInput.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
266645
255442
2 changes: 1 addition & 1 deletion .forge-snapshots/CLSwapRouterTest#ExactInputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
201852
192524
2 changes: 1 addition & 1 deletion .forge-snapshots/CLSwapRouterTest#ExactOutput.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
272728
260676
2 changes: 1 addition & 1 deletion .forge-snapshots/CLSwapRouterTest#ExactOutputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
201502
191719
2 changes: 1 addition & 1 deletion .forge-snapshots/NonfungiblePositionManager#collect.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
276679
265629
Original file line number Diff line number Diff line change
@@ -1 +1 @@
190144
176568
Original file line number Diff line number Diff line change
@@ -1 +1 @@
218325
210284
2 changes: 1 addition & 1 deletion .forge-snapshots/NonfungiblePositionManager#mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
653033
609324
2 changes: 1 addition & 1 deletion lib/pancake-v4-core
Submodule pancake-v4-core updated 238 files
9 changes: 7 additions & 2 deletions src/SwapRouterBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ abstract contract SwapRouterBase is ISwapRouterBase {
}

function _payAndSettle(Currency currency, address msgSender, int128 settleAmount) internal virtual {
_pay(currency, msgSender, address(vault), uint256(uint128(settleAmount)));
vault.settleAndMintRefund(currency, msgSender);
if (currency.isNative()) {
vault.settle{value: uint256(uint128(settleAmount))}(currency);
} else {
vault.sync(currency);
_pay(currency, msgSender, address(vault), uint256(uint128(settleAmount)));
vault.settle(currency);
}
}

function _pay(Currency currency, address payer, address recipient, uint256 amount) internal virtual;
Expand Down
44 changes: 28 additions & 16 deletions src/pool-bin/BinFungiblePositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ contract BinFungiblePositionManager is
bytes32 amountIn = params.amount0.encode(params.amount1);
(BalanceDelta delta, BinPool.MintArrays memory mintArray) = poolManager.mint(
params.poolKey,
IBinPoolManager.MintParams({liquidityConfigs: liquidityConfigs, amountIn: amountIn}),
IBinPoolManager.MintParams({liquidityConfigs: liquidityConfigs, amountIn: amountIn, salt: bytes32(0)}),
ZERO_BYTES
);

// delta amt0/amt1 will always be positive in mint case
if (delta.amount0() < 0 || delta.amount1() < 0) revert IncorrectOutputAmount();
if (uint128(delta.amount0()) < params.amount0Min || uint128(delta.amount1()) < params.amount1Min) {
// delta amt0/amt1 will always be negative in mint case
if (delta.amount0() > 0 || delta.amount1() > 0) revert IncorrectOutputAmount();
if (uint128(-delta.amount0()) < params.amount0Min || uint128(-delta.amount1()) < params.amount1Min) {
revert OutputAmountSlippage();
}

Expand All @@ -194,17 +194,19 @@ contract BinFungiblePositionManager is
}
}

return abi.encode(delta.amount0(), delta.amount1(), tokenIds, mintArray.liquidityMinted);
return abi.encode(uint128(-delta.amount0()), uint128(-delta.amount1()), tokenIds, mintArray.liquidityMinted);
} else if (data.callbackDataType == CallbackDataType.RemoveLiquidity) {
RemoveLiquidityParams memory params = abi.decode(data.params, (RemoveLiquidityParams));

BalanceDelta delta = poolManager.burn(
params.poolKey, IBinPoolManager.BurnParams({ids: params.ids, amountsToBurn: params.amounts}), ZERO_BYTES
params.poolKey,
IBinPoolManager.BurnParams({ids: params.ids, amountsToBurn: params.amounts, salt: bytes32(0)}),
ZERO_BYTES
);

// delta amt0/amt1 will either be 0 or negative in removing liquidity
if (delta.amount0() > 0 || delta.amount1() > 0) revert IncorrectOutputAmount();
if (uint128(-delta.amount0()) < params.amount0Min || uint128(-delta.amount1()) < params.amount1Min) {
// delta amt0/amt1 will either be 0 or positive in removing liquidity
if (delta.amount0() < 0 || delta.amount1() < 0) revert IncorrectOutputAmount();
if (uint128(delta.amount0()) < params.amount0Min || uint128(delta.amount1()) < params.amount1Min) {
revert OutputAmountSlippage();
}

Expand All @@ -223,25 +225,35 @@ contract BinFungiblePositionManager is
}
}

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

/// @notice Transfer token from user to vault. If the currency is native, assume ETH is on contract
/// @param user If delta.amt > 0, take amt from user. else if delta.amt < 0, transfer amt to user
function _settleDeltas(address user, PoolKey memory poolKey, BalanceDelta delta) internal {
if (delta.amount0() > 0) {
pay(poolKey.currency0, user, address(vault), uint256(int256(delta.amount0())));
vault.settleAndMintRefund(poolKey.currency0, user);
vault.take(poolKey.currency0, user, uint128(delta.amount0()));
} else if (delta.amount0() < 0) {
vault.take(poolKey.currency0, user, uint128(-delta.amount0()));
if (poolKey.currency0.isNative()) {
vault.settle{value: uint256(int256(-delta.amount0()))}(poolKey.currency0);
} else {
vault.sync(poolKey.currency0);
pay(poolKey.currency0, user, address(vault), uint256(int256(-delta.amount0())));
vault.settle(poolKey.currency0);
}
}

if (delta.amount1() > 0) {
pay(poolKey.currency1, user, address(vault), uint256(int256(delta.amount1())));
vault.settleAndMintRefund(poolKey.currency1, user);
vault.take(poolKey.currency1, user, uint128(delta.amount1()));
} else if (delta.amount1() < 0) {
vault.take(poolKey.currency1, user, uint128(-delta.amount1()));
if (poolKey.currency1.isNative()) {
vault.settle{value: uint256(int256(-delta.amount1()))}(poolKey.currency1);
} else {
vault.sync(poolKey.currency1);
pay(poolKey.currency1, user, address(vault), uint256(int256(-delta.amount1())));
vault.settle(poolKey.currency1);
}
}
}
}
10 changes: 5 additions & 5 deletions src/pool-bin/BinSwapRouterBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ abstract contract BinSwapRouterBase is SwapRouterBase, IBinSwapRouterBase {
BalanceDelta delta = binPoolManager.swap(poolKey, swapForY, amountIn, hookData);

if (swapForY) {
if (settle) _payAndSettle(poolKey.currency0, payer, delta.amount0());
if (take) vault.take(poolKey.currency1, recipient, uint128(-delta.amount1()));
if (settle) _payAndSettle(poolKey.currency0, payer, -delta.amount0());
if (take) vault.take(poolKey.currency1, recipient, uint128(delta.amount1()));
} else {
if (settle) _payAndSettle(poolKey.currency1, payer, delta.amount1());
if (take) vault.take(poolKey.currency0, recipient, uint128(-delta.amount0()));
if (settle) _payAndSettle(poolKey.currency1, payer, -delta.amount1());
if (take) vault.take(poolKey.currency0, recipient, uint128(delta.amount0()));
}

amountOut = swapForY ? uint128(-delta.amount1()) : uint128(-delta.amount0());
amountOut = swapForY ? uint128(delta.amount1()) : uint128(delta.amount0());
}
}
Loading
Loading