Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
hensha256 committed Sep 5, 2024
2 parents 6cffcf2 + 6213761 commit c18f1b5
Show file tree
Hide file tree
Showing 49 changed files with 70 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129817
129823
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131809
131815
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124044
124020
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124186
124162
Original file line number Diff line number Diff line change
@@ -1 +1 @@
155298
155273
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154282
154257
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136749
136724
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132457
132432
Original file line number Diff line number Diff line change
@@ -1 +1 @@
173309
173284
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143991
143966
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
340450
340425
Original file line number Diff line number Diff line change
@@ -1 +1 @@
348904
348879
Original file line number Diff line number Diff line change
@@ -1 +1 @@
348196
348171
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickLower.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
318768
318743
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickUpper.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
319438
319413
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
245007
244982
Original file line number Diff line number Diff line change
@@ -1 +1 @@
374841
374816
Original file line number Diff line number Diff line change
@@ -1 +1 @@
324799
324774
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withClose.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
376099
376074
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withSettlePair.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
375225
375200
Original file line number Diff line number Diff line change
@@ -1 +1 @@
420246
420221
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_Bytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7322
7278
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115513
115543
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115990
116020
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124805
124835
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130523
130553
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179531
179561
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170231
170261
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
228518
228548
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
219242
219272
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129817
129823
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114807
114813
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115253
115259
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121745
121771
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
117046
117072
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125861
125887
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129778
129804
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179610
179632
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175494
175516
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
229449
229467
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
225357
225375
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
220658
220676
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129056
129074
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121023
121041
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116399
116417
2 changes: 2 additions & 0 deletions src/PositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ contract PositionManager is
// the locker is the payer or receiver
address caller = msgSender();
if (currencyDelta < 0) {
// Casting is safe due to limits on the total supply of a pool
_settle(currency, caller, uint256(-currencyDelta));
} else if (currencyDelta > 0) {
_take(currency, caller, uint256(currencyDelta));
Expand Down Expand Up @@ -417,6 +418,7 @@ contract PositionManager is
if (payer == address(this)) {
currency.transfer(address(poolManager), amount);
} else {
// Casting from uint256 to uint160 is safe due to limits on the total supply of a pool
permit2.transferFrom(payer, address(poolManager), uint160(amount), Currency.unwrap(currency));
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/V4Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
_getFullCredit(params.zeroForOne ? params.poolKey.currency0 : params.poolKey.currency1).toUint128();
}
uint128 amountOut = _swap(
params.poolKey, params.zeroForOne, int256(-int128(amountIn)), params.sqrtPriceLimitX96, params.hookData
params.poolKey, params.zeroForOne, -int256(uint256(amountIn)), params.sqrtPriceLimitX96, params.hookData
).toUint128();
if (amountOut < params.amountOutMinimum) revert V4TooLittleReceived(params.amountOutMinimum, amountOut);
}
Expand Down Expand Up @@ -127,8 +127,16 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
_getFullDebt(params.zeroForOne ? params.poolKey.currency1 : params.poolKey.currency0).toUint128();
}
uint128 amountIn = (
-_swap(
params.poolKey, params.zeroForOne, int256(int128(amountOut)), params.sqrtPriceLimitX96, params.hookData
uint256(
-int256(
_swap(
params.poolKey,
params.zeroForOne,
int256(uint256(amountOut)),
params.sqrtPriceLimitX96,
params.hookData
)
)
)
).toUint128();
if (amountIn > params.amountInMaximum) revert V4TooMuchRequested(params.amountInMaximum, amountIn);
Expand All @@ -151,7 +159,9 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
pathKey = params.path[i - 1];
(PoolKey memory poolKey, bool oneForZero) = pathKey.getPoolAndSwapDirection(currencyOut);
// The output delta will always be negative, except for when interacting with certain hook pools
amountIn = (-_swap(poolKey, !oneForZero, int256(uint256(amountOut)), 0, pathKey.hookData)).toUint128();
amountIn = (
uint256(-int256(_swap(poolKey, !oneForZero, int256(uint256(amountOut)), 0, pathKey.hookData)))
).toUint128();

amountOut = amountIn;
currencyOut = pathKey.intermediateCurrency;
Expand Down
1 change: 1 addition & 0 deletions src/base/DeltaResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ abstract contract DeltaResolver is ImmutableState {
int256 _amount = poolManager.currencyDelta(address(this), currency);
// If the amount is positive, it should be taken not settled.
if (_amount > 0) revert DeltaNotNegative(currency);
// Casting is safe due to limits on the total supply of a pool
amount = uint256(-_amount);
}

Expand Down
12 changes: 8 additions & 4 deletions src/libraries/SlippageCheck.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ library SlippageCheck {
// Thus, we only cast the delta if it is guaranteed to be negative.
// And we do NOT revert in the positive delta case. Since a positive delta means the hook is crediting tokens to the user for minting/increasing liquidity, we do not check slippage.
// This means this contract will NOT support _positive_ slippage checks (minAmountOut checks) on pools where the hook returns a positive delta on mint/increase.
int128 amount0 = delta.amount0();
int128 amount1 = delta.amount1();
if (amount0 < 0 && amount0Max < uint128(-amount0)) revert MaximumAmountExceeded(amount0Max, uint128(-amount0));
if (amount1 < 0 && amount1Max < uint128(-amount1)) revert MaximumAmountExceeded(amount1Max, uint128(-amount1));
int256 amount0 = delta.amount0();
int256 amount1 = delta.amount1();
if (amount0 < 0 && amount0Max < uint128(uint256(-amount0))) {
revert MaximumAmountExceeded(amount0Max, uint128(uint256(-amount0)));
}
if (amount1 < 0 && amount1Max < uint128(uint256(-amount1))) {
revert MaximumAmountExceeded(amount1Max, uint128(uint256(-amount1)));
}
}
}

0 comments on commit c18f1b5

Please sign in to comment.