Skip to content

Commit

Permalink
slippage params routing (#264)
Browse files Browse the repository at this point in the history
* slippage params routing

* tests

* PR comment

* exact out tests
  • Loading branch information
hensha256 authored Aug 4, 2024
1 parent e2d2508 commit eb0cf58
Show file tree
Hide file tree
Showing 36 changed files with 160 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134520
133519
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134658
135555
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127708
128029
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127846
128167
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_Bytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8165
8438
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120569
119534
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119764
118729
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128636
127601
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135466
134431
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186973
185972
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178908
177907
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238505
237494
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
230464
229453
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134520
133519
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119623
118622
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
118801
117800
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126227
125192
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120505
119470
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129377
128342
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134178
133143
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186288
185287
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
183138
182137
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238438
237427
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
235312
234301
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
229590
228579
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132941
131940
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124990
123989
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119281
118280
30 changes: 18 additions & 12 deletions src/V4Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {

constructor(IPoolManager _poolManager) BaseActionsRouter(_poolManager) {}

// TODO native support !!
function _handleAction(uint256 action, bytes calldata params) internal override {
// swap actions and payment actions in different blocks for gas efficiency
if (action < Actions.SETTLE_ALL) {
if (action < Actions.SETTLE) {
if (action == Actions.SWAP_EXACT_IN) {
IV4Router.ExactInputParams calldata swapParams = params.decodeSwapExactInParams();
_swapExactInput(swapParams);
Expand All @@ -50,13 +49,20 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
revert UnsupportedAction(action);
}
} else {
if (action == Actions.SETTLE_ALL) {
Currency currency = params.decodeCurrency();
// TODO should it have a maxAmountOut added slippage protection?
_settle(currency, msgSender(), _getFullDebt(currency));
if (action == Actions.SETTLE_TAKE_PAIR) {
(Currency settleCurrency, Currency takeCurrency) = params.decodeCurrencyPair();
_settle(settleCurrency, msgSender(), _getFullDebt(settleCurrency));
_take(takeCurrency, msgSender(), _getFullCredit(takeCurrency));
} else if (action == Actions.SETTLE_ALL) {
(Currency currency, uint256 maxAmount) = params.decodeCurrencyAndUint256();
uint256 amount = _getFullDebt(currency);
if (amount > maxAmount) revert V4TooMuchRequested();
_settle(currency, msgSender(), amount);
} else if (action == Actions.TAKE_ALL) {
(Currency currency, address recipient) = params.decodeCurrencyAndAddress();
_take(currency, _mapRecipient(recipient), _getFullCredit(currency));
(Currency currency, uint256 minAmount) = params.decodeCurrencyAndUint256();
uint256 amount = _getFullCredit(currency);
if (amount < minAmount) revert V4TooLittleReceived();
_take(currency, msgSender(), amount);
} else if (action == Actions.SETTLE) {
(Currency currency, uint256 amount, bool payerIsUser) = params.decodeCurrencyUint256AndBool();
_settle(currency, _mapPayer(payerIsUser), _mapSettleAmount(amount, currency));
Expand All @@ -78,7 +84,7 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
uint128 amountOut = _swap(
params.poolKey, params.zeroForOne, int256(-int128(amountIn)), params.sqrtPriceLimitX96, params.hookData
).toUint128();
if (amountOut < params.amountOutMinimum) revert TooLittleReceived();
if (amountOut < params.amountOutMinimum) revert V4TooLittleReceived();
}

function _swapExactInput(IV4Router.ExactInputParams calldata params) private {
Expand All @@ -100,7 +106,7 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
currencyIn = pathKey.intermediateCurrency;
}

if (amountOut < params.amountOutMinimum) revert TooLittleReceived();
if (amountOut < params.amountOutMinimum) revert V4TooLittleReceived();
}
}

Expand All @@ -114,7 +120,7 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
params.hookData
)
).toUint128();
if (amountIn > params.amountInMaximum) revert TooMuchRequested();
if (amountIn > params.amountInMaximum) revert V4TooMuchRequested();
}

function _swapExactOutput(IV4Router.ExactOutputParams calldata params) private {
Expand All @@ -135,7 +141,7 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
amountOut = amountIn;
currencyOut = pathKey.intermediateCurrency;
}
if (amountIn > params.amountInMaximum) revert TooMuchRequested();
if (amountIn > params.amountInMaximum) revert V4TooMuchRequested();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/IV4Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {PathKey} from "../libraries/PathKey.sol";
/// @notice Interface containing all the structs and errors for different v4 swap types
interface IV4Router {
/// @notice Emitted when an exactInput swap does not receive its minAmountOut
error TooLittleReceived();
error V4TooLittleReceived();
/// @notice Emitted when an exactOutput is asked for more than its maxAmountIn
error TooMuchRequested();
error V4TooMuchRequested();

/// @notice Parameters for a single-hop exact-input swap
struct ExactInputSingleParams {
Expand Down
15 changes: 8 additions & 7 deletions src/libraries/Actions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ library Actions {

// closing deltas on the pool manager
// settling
uint256 constant SETTLE_ALL = 0x09;
uint256 constant SETTLE = 0x10;
uint256 constant SETTLE = 0x09;
uint256 constant SETTLE_ALL = 0x10;
uint256 constant SETTLE_PAIR = 0x11;
// taking
uint256 constant TAKE = 0x12;
uint256 constant TAKE_ALL = 0x13;
uint256 constant TAKE_PORTION = 0x14;
uint256 constant TAKE_PAIR = 0x15;

uint256 constant CLOSE_CURRENCY = 0x16;
uint256 constant CLEAR_OR_TAKE = 0x17;
uint256 constant SWEEP = 0x18;
uint256 constant SETTLE_TAKE_PAIR = 0x16;
uint256 constant CLOSE_CURRENCY = 0x17;
uint256 constant CLEAR_OR_TAKE = 0x18;
uint256 constant SWEEP = 0x19;

// minting/burning 6909s to close deltas
uint256 constant MINT_6909 = 0x19;
uint256 constant BURN_6909 = 0x20;
uint256 constant MINT_6909 = 0x20;
uint256 constant BURN_6909 = 0x21;
}
12 changes: 6 additions & 6 deletions test/router/Payments.gas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ contract PaymentsTests is RoutingTestHelpers, GasSnapshot {
IV4Router.ExactInputSingleParams(key0, true, uint128(amountIn), 0, 0, bytes(""));

plan = plan.add(Actions.SWAP_EXACT_IN_SINGLE, abi.encode(params));
plan = plan.add(Actions.SETTLE_ALL, abi.encode(key0.currency0));
plan = plan.add(Actions.TAKE_ALL, abi.encode(key0.currency1, address(this)));
plan = plan.add(Actions.SETTLE_ALL, abi.encode(key0.currency0, MAX_SETTLE_AMOUNT));
plan = plan.add(Actions.TAKE, abi.encode(key0.currency1, address(this), ActionConstants.OPEN_DELTA));

bytes memory data = plan.encode();
router.executeActions(data);
Expand All @@ -39,8 +39,7 @@ contract PaymentsTests is RoutingTestHelpers, GasSnapshot {
IV4Router.ExactInputSingleParams(key0, true, uint128(amountIn), 0, 0, bytes(""));

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

bytes memory data = plan.encode();
router.executeActions(data);
Expand All @@ -57,7 +56,7 @@ contract PaymentsTests is RoutingTestHelpers, GasSnapshot {

plan = plan.add(Actions.SWAP_EXACT_IN_SINGLE, abi.encode(params));
plan = plan.add(Actions.SETTLE, abi.encode(key0.currency0, ActionConstants.CONTRACT_BALANCE, false));
plan = plan.add(Actions.TAKE_ALL, abi.encode(key0.currency1, address(this)));
plan = plan.add(Actions.TAKE, abi.encode(key0.currency1, address(this), ActionConstants.OPEN_DELTA));

bytes memory data = plan.encode();
router.executeActions(data);
Expand All @@ -74,7 +73,8 @@ contract PaymentsTests is RoutingTestHelpers, GasSnapshot {

plan = plan.add(Actions.SWAP_EXACT_IN_SINGLE, abi.encode(params));
plan = plan.add(Actions.SETTLE, abi.encode(currency0, ActionConstants.CONTRACT_BALANCE, false));
plan = plan.add(Actions.TAKE_ALL, abi.encode(key0.currency1, ActionConstants.MSG_SENDER));
plan =
plan.add(Actions.TAKE, abi.encode(key0.currency1, ActionConstants.MSG_SENDER, ActionConstants.OPEN_DELTA));

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

0 comments on commit eb0cf58

Please sign in to comment.