Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
hensha256 committed Nov 21, 2024
2 parents d4d4092 + 8acce8b commit a639f6f
Show file tree
Hide file tree
Showing 33 changed files with 38 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129430
129534
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131494
131448
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123699
123653
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123841
123795
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_Bytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7039
6929
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116945
118538
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115921
117514
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124736
126329
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130400
131993
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
185169
186727
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
171714
173272
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
239941
241511
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
226486
228056
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129430
131001
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115975
117546
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114919
116490
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123203
124796
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116994
118587
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125809
127402
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129681
131274
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
183507
185065
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
177029
178587
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
237364
238934
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
230886
232456
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
216941
218511
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128708
130279
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122230
123801
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116095
117666
7 changes: 1 addition & 6 deletions src/V4Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
return;
}
} else {
if (action == Actions.SETTLE_TAKE_PAIR) {
(Currency settleCurrency, Currency takeCurrency) = params.decodeCurrencyPair();
_settle(settleCurrency, msgSender(), _getFullDebt(settleCurrency));
_take(takeCurrency, msgSender(), _getFullCredit(takeCurrency));
return;
} else if (action == Actions.SETTLE_ALL) {
if (action == Actions.SETTLE_ALL) {
(Currency currency, uint256 maxAmount) = params.decodeCurrencyAndUint256();
uint256 amount = _getFullDebt(currency);
if (amount > maxAmount) revert V4TooMuchRequested(maxAmount, amount);
Expand Down
1 change: 0 additions & 1 deletion src/libraries/Actions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ library Actions {
uint256 constant TAKE_PORTION = 0x14;
uint256 constant TAKE_PAIR = 0x15;

uint256 constant SETTLE_TAKE_PAIR = 0x16;
uint256 constant CLOSE_CURRENCY = 0x17;
uint256 constant CLEAR_OR_TAKE = 0x18;
uint256 constant SWEEP = 0x19;
Expand Down
3 changes: 2 additions & 1 deletion test/router/Payments.gas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ contract PaymentsTests is RoutingTestHelpers, GasSnapshot {
IV4Router.ExactInputSingleParams(key0, true, uint128(amountIn), 0, bytes(""));

plan = plan.add(Actions.SWAP_EXACT_IN_SINGLE, abi.encode(params));
plan = plan.add(Actions.SETTLE_TAKE_PAIR, abi.encode(key0.currency0, key0.currency1));
plan = plan.add(Actions.SETTLE, abi.encode(key0.currency0, amountIn, true));
plan = plan.add(Actions.TAKE_ALL, abi.encode(key0.currency1, 0));

bytes memory data = plan.encode();
router.executeActions(data);
Expand Down
35 changes: 4 additions & 31 deletions test/router/Payments.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,6 @@ contract PaymentsTests is RoutingTestHelpers, GasSnapshot {
plan = Planner.init();
}

function test_settleTakePair() public {
uint256 amountIn = 1 ether;
uint256 expectedAmountOut = 992054607780215625;
IV4Router.ExactInputSingleParams memory params =
IV4Router.ExactInputSingleParams(key0, true, uint128(amountIn), 0, bytes(""));

plan = plan.add(Actions.SWAP_EXACT_IN_SINGLE, abi.encode(params));
plan = plan.add(Actions.SETTLE_TAKE_PAIR, abi.encode(key0.currency0, key0.currency1));

uint256 inputBalanceBefore = key0.currency0.balanceOfSelf();
uint256 outputBalanceBefore = key0.currency1.balanceOfSelf();
// router is empty before
assertEq(currency0.balanceOf(address(router)), 0);
assertEq(currency1.balanceOf(address(router)), 0);

bytes memory data = plan.encode();
router.executeActions(data);

uint256 inputBalanceAfter = key0.currency0.balanceOfSelf();
uint256 outputBalanceAfter = key0.currency1.balanceOfSelf();

// router is empty
assertEq(currency0.balanceOf(address(router)), 0);
assertEq(currency1.balanceOf(address(router)), 0);
// caller's balance changed by input and output amounts
assertEq(inputBalanceBefore - inputBalanceAfter, amountIn);
assertEq(outputBalanceAfter - outputBalanceBefore, expectedAmountOut);
}

function test_exactIn_settleAll_revertsSlippage() public {
uint256 amountIn = 1 ether;
IV4Router.ExactInputSingleParams memory params =
Expand Down Expand Up @@ -172,9 +143,10 @@ contract PaymentsTests is RoutingTestHelpers, GasSnapshot {
IV4Router.ExactInputSingleParams(key0, true, uint128(amountIn), 0, bytes(""));

plan = plan.add(Actions.SWAP_EXACT_IN_SINGLE, abi.encode(params));
plan = plan.add(Actions.SETTLE, abi.encode(key0.currency0, amountIn, true));
// take 15 bips to Bob
plan = plan.add(Actions.TAKE_PORTION, abi.encode(key0.currency1, bob, 15));
plan = plan.add(Actions.SETTLE_TAKE_PAIR, abi.encode(key0.currency0, key0.currency1));
plan = plan.add(Actions.TAKE_ALL, abi.encode(key0.currency1, 0));

uint256 inputBalanceBefore = key0.currency0.balanceOfSelf();
uint256 outputBalanceBefore = key0.currency1.balanceOfSelf();
Expand Down Expand Up @@ -208,9 +180,10 @@ contract PaymentsTests is RoutingTestHelpers, GasSnapshot {
IV4Router.ExactInputSingleParams(key0, true, uint128(amountIn), 0, bytes(""));

plan = plan.add(Actions.SWAP_EXACT_IN_SINGLE, abi.encode(params));
plan = plan.add(Actions.SETTLE, abi.encode(key0.currency0, amountIn, true));
// bips is larger than maximum bips
plan = plan.add(Actions.TAKE_PORTION, abi.encode(key0.currency1, bob, BipsLibrary.BPS_DENOMINATOR + 1));
plan = plan.add(Actions.SETTLE_TAKE_PAIR, abi.encode(key0.currency0, key0.currency1));
plan = plan.add(Actions.TAKE_ALL, abi.encode(key0.currency1, 0));

bytes memory data = plan.encode();

Expand Down
4 changes: 3 additions & 1 deletion test/shared/Planner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ library Planner {
returns (bytes memory)
{
if (takeRecipient == ActionConstants.MSG_SENDER) {
plan = plan.add(Actions.SETTLE_TAKE_PAIR, abi.encode(inputCurrency, outputCurrency));
// blindly settling and taking all, without slippage checks, isnt recommended in prod
plan = plan.add(Actions.SETTLE_ALL, abi.encode(inputCurrency, type(uint256).max));
plan = plan.add(Actions.TAKE_ALL, abi.encode(outputCurrency, 0));
} else {
plan = plan.add(Actions.SETTLE, abi.encode(inputCurrency, ActionConstants.OPEN_DELTA, true));
plan = plan.add(Actions.TAKE, abi.encode(outputCurrency, takeRecipient, ActionConstants.OPEN_DELTA));
Expand Down

0 comments on commit a639f6f

Please sign in to comment.