Skip to content

Commit

Permalink
Remove SETTLE_TAKE_PAIR command (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
hensha256 authored Nov 21, 2024
1 parent d767807 commit 8acce8b
Show file tree
Hide file tree
Showing 34 changed files with 39 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129438
129542
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131502
131456
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123707
123661
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123849
123803
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_Bytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7117
7007
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115672
117265
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115929
117522
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124744
126337
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130408
132001
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
185177
186735
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170441
171999
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
239949
241519
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
225213
226783
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129438
131009
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114702
116273
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114927
116498
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121930
123523
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
117002
118595
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125817
127410
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129689
131282
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
183515
185073
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175756
177314
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
237372
238942
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
229613
231183
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
216949
218519
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128716
130287
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120957
122528
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116103
117674
2 changes: 1 addition & 1 deletion lib/v4-core
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 8acce8b

Please sign in to comment.