Skip to content

Commit

Permalink
CVF 54 plus test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hensha256 committed Sep 1, 2024
1 parent df63baa commit 2de7daf
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_Bytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8389
8591
12 changes: 6 additions & 6 deletions src/V4Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ abstract contract V4Router is IV4Router, BaseActionsRouter, DeltaResolver {
} else if (action == Actions.SETTLE_ALL) {
(Currency currency, uint256 maxAmount) = params.decodeCurrencyAndUint256();
uint256 amount = _getFullDebt(currency);
if (amount > maxAmount) revert V4TooMuchRequested();
if (amount > maxAmount) revert V4TooMuchRequested(maxAmount, amount);
_settle(currency, msgSender(), amount);
} else if (action == Actions.TAKE_ALL) {
(Currency currency, uint256 minAmount) = params.decodeCurrencyAndUint256();
uint256 amount = _getFullCredit(currency);
if (amount < minAmount) revert V4TooLittleReceived();
if (amount < minAmount) revert V4TooLittleReceived(minAmount, amount);
_take(currency, msgSender(), amount);
} else if (action == Actions.SETTLE) {
(Currency currency, uint256 amount, bool payerIsUser) = params.decodeCurrencyUint256AndBool();
Expand All @@ -88,7 +88,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 V4TooLittleReceived();
if (amountOut < params.amountOutMinimum) revert V4TooLittleReceived(params.amountOutMinimum, amountOut);
}

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

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

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

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

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 V4TooLittleReceived();
error V4TooLittleReceived(uint256 minAmountOutReceived, uint256 amountReceived);
/// @notice Emitted when an exactOutput is asked for more than its maxAmountIn
error V4TooMuchRequested();
error V4TooMuchRequested(uint256 maxAmountInRequested, uint256 amountRequested);

/// @notice Parameters for a single-hop exact-input swap
struct ExactInputSingleParams {
Expand Down
19 changes: 11 additions & 8 deletions test/router/Payments.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ contract PaymentsTests is RoutingTestHelpers, GasSnapshot {
plan = plan.add(Actions.TAKE_ALL, abi.encode(key0.currency0, MIN_TAKE_AMOUNT));

bytes memory data = plan.encode();
vm.expectRevert(IV4Router.V4TooMuchRequested.selector);
vm.expectRevert(abi.encodeWithSelector(IV4Router.V4TooMuchRequested.selector, amountIn - 1, amountIn));
router.executeActions(data);
}

Expand All @@ -73,10 +73,12 @@ contract PaymentsTests is RoutingTestHelpers, GasSnapshot {

plan = plan.add(Actions.SWAP_EXACT_IN_SINGLE, abi.encode(params));
plan = plan.add(Actions.SETTLE_ALL, abi.encode(key0.currency0, MAX_SETTLE_AMOUNT));
plan = plan.add(Actions.TAKE_ALL, abi.encode(key0.currency0, expectedAmountOut + 1));
plan = plan.add(Actions.TAKE_ALL, abi.encode(key0.currency1, expectedAmountOut + 1));

bytes memory data = plan.encode();
vm.expectRevert(IV4Router.V4TooLittleReceived.selector);
vm.expectRevert(
abi.encodeWithSelector(IV4Router.V4TooLittleReceived.selector, expectedAmountOut + 1, expectedAmountOut)
);
router.executeActions(data);
}

Expand All @@ -92,7 +94,9 @@ contract PaymentsTests is RoutingTestHelpers, GasSnapshot {
plan = plan.add(Actions.TAKE_ALL, abi.encode(key0.currency0, MIN_TAKE_AMOUNT));

bytes memory data = plan.encode();
vm.expectRevert(IV4Router.V4TooMuchRequested.selector);
vm.expectRevert(
abi.encodeWithSelector(IV4Router.V4TooMuchRequested.selector, expectedAmountIn - 1, expectedAmountIn)
);
router.executeActions(data);
}

Expand All @@ -105,10 +109,10 @@ contract PaymentsTests is RoutingTestHelpers, GasSnapshot {

plan = plan.add(Actions.SWAP_EXACT_OUT_SINGLE, abi.encode(params));
plan = plan.add(Actions.SETTLE_ALL, abi.encode(key0.currency0, MAX_SETTLE_AMOUNT));
plan = plan.add(Actions.TAKE_ALL, abi.encode(key0.currency0, amountOut + 1));
plan = plan.add(Actions.TAKE_ALL, abi.encode(key0.currency1, amountOut + 1));

bytes memory data = plan.encode();
vm.expectRevert(IV4Router.V4TooLittleReceived.selector);
vm.expectRevert(abi.encodeWithSelector(IV4Router.V4TooLittleReceived.selector, amountOut + 1, amountOut));
router.executeActions(data);
}

Expand All @@ -121,10 +125,9 @@ contract PaymentsTests is RoutingTestHelpers, GasSnapshot {

plan = plan.add(Actions.SWAP_EXACT_OUT_SINGLE, abi.encode(params));
plan = plan.add(Actions.SETTLE_ALL, abi.encode(key0.currency0, expectedAmountIn));
plan = plan.add(Actions.TAKE_ALL, abi.encode(key0.currency0, amountOut));
plan = plan.add(Actions.TAKE_ALL, abi.encode(key0.currency1, amountOut));

bytes memory data = plan.encode();
vm.expectRevert(IV4Router.V4TooLittleReceived.selector);
router.executeActions(data);
}

Expand Down
16 changes: 12 additions & 4 deletions test/router/V4Router.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ contract V4RouterTest is RoutingTestHelpers {
plan = plan.add(Actions.SWAP_EXACT_IN_SINGLE, abi.encode(params));
bytes memory data = plan.finalizeSwap(key0.currency0, key0.currency1, ActionConstants.MSG_SENDER);

vm.expectRevert(IV4Router.V4TooLittleReceived.selector);
vm.expectRevert(
abi.encodeWithSelector(IV4Router.V4TooLittleReceived.selector, expectedAmountOut + 1, expectedAmountOut)
);
router.executeActions(data);
}

Expand Down Expand Up @@ -176,7 +178,9 @@ contract V4RouterTest is RoutingTestHelpers {
plan = plan.add(Actions.SWAP_EXACT_IN, abi.encode(params));
bytes memory data = plan.finalizeSwap(key0.currency0, key0.currency1, ActionConstants.MSG_SENDER);

vm.expectRevert(IV4Router.V4TooLittleReceived.selector);
vm.expectRevert(
abi.encodeWithSelector(IV4Router.V4TooLittleReceived.selector, 992054607780215625 + 1, 992054607780215625)
);
router.executeActions(data);
}

Expand Down Expand Up @@ -484,7 +488,9 @@ contract V4RouterTest is RoutingTestHelpers {
plan = plan.add(Actions.SWAP_EXACT_OUT_SINGLE, abi.encode(params));
bytes memory data = plan.finalizeSwap(key0.currency0, key0.currency1, ActionConstants.MSG_SENDER);

vm.expectRevert(IV4Router.V4TooMuchRequested.selector);
vm.expectRevert(
abi.encodeWithSelector(IV4Router.V4TooMuchRequested.selector, expectedAmountIn - 1, expectedAmountIn)
);
router.executeActions(data);
}

Expand Down Expand Up @@ -540,7 +546,9 @@ contract V4RouterTest is RoutingTestHelpers {
plan = plan.add(Actions.SWAP_EXACT_OUT, abi.encode(params));
bytes memory data = plan.finalizeSwap(key0.currency0, key0.currency1, ActionConstants.MSG_SENDER);

vm.expectRevert(IV4Router.V4TooMuchRequested.selector);
vm.expectRevert(
abi.encodeWithSelector(IV4Router.V4TooMuchRequested.selector, expectedAmountIn - 1, expectedAmountIn)
);
router.executeActions(data);
}

Expand Down

0 comments on commit 2de7daf

Please sign in to comment.