Skip to content

Commit 9162838

Browse files
committed
SLippage check tests
1 parent 902b23d commit 9162838

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

test/router/V4Router.t.sol

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ contract V4RouterTest is RoutingTestHelpers {
2020
ERC20 -> ERC20 EXACT INPUT
2121
//////////////////////////////////////////////////////////////*/
2222

23+
function test_swapExactInputSingle_revertsForAmountOut() public {
24+
uint256 amountIn = 1 ether;
25+
uint256 expectedAmountOut = 992054607780215625;
26+
27+
// min amount out of 1 higher than the actual amount out
28+
IV4Router.ExactInputSingleParams memory params = IV4Router.ExactInputSingleParams(
29+
key0, true, uint128(amountIn), uint128(expectedAmountOut + 1), 0, bytes("")
30+
);
31+
32+
plan = plan.add(Actions.SWAP_EXACT_IN_SINGLE, abi.encode(params));
33+
bytes memory data = plan.finalizeSwap(key0.currency0, key0.currency1, address(this));
34+
35+
vm.expectRevert(IV4Router.TooLittleReceived.selector);
36+
router.executeActions(data);
37+
}
38+
2339
function test_swapExactInputSingle_zeroForOne() public {
2440
uint256 amountIn = 1 ether;
2541
uint256 expectedAmountOut = 992054607780215625;
@@ -56,6 +72,22 @@ contract V4RouterTest is RoutingTestHelpers {
5672
assertEq(outputBalanceAfter - outputBalanceBefore, expectedAmountOut);
5773
}
5874

75+
function test_swapExactInput_revertsForAmountOut() public {
76+
uint256 amountIn = 1 ether;
77+
uint256 expectedAmountOut = 992054607780215625;
78+
79+
tokenPath.push(currency0);
80+
tokenPath.push(currency1);
81+
IV4Router.ExactInputParams memory params = _getExactInputParams(tokenPath, amountIn);
82+
params.amountOutMinimum = uint128(expectedAmountOut + 1);
83+
84+
plan = plan.add(Actions.SWAP_EXACT_IN, abi.encode(params));
85+
bytes memory data = plan.finalizeSwap(key0.currency0, key0.currency1, address(this));
86+
87+
vm.expectRevert(IV4Router.TooLittleReceived.selector);
88+
router.executeActions(data);
89+
}
90+
5991
function test_swapExactIn_1Hop_zeroForOne() public {
6092
uint256 amountIn = 1 ether;
6193
uint256 expectedAmountOut = 992054607780215625;
@@ -287,6 +319,21 @@ contract V4RouterTest is RoutingTestHelpers {
287319
ERC20 -> ERC20 EXACT OUTPUT
288320
//////////////////////////////////////////////////////////////*/
289321

322+
function test_swapExactOutputSingle_revertsForAmountIn() public {
323+
uint256 amountOut = 1 ether;
324+
uint256 expectedAmountIn = 1008049273448486163;
325+
326+
IV4Router.ExactOutputSingleParams memory params = IV4Router.ExactOutputSingleParams(
327+
key0, true, uint128(amountOut), uint128(expectedAmountIn - 1), 0, bytes("")
328+
);
329+
330+
plan = plan.add(Actions.SWAP_EXACT_OUT_SINGLE, abi.encode(params));
331+
bytes memory data = plan.finalizeSwap(key0.currency0, key0.currency1, address(this));
332+
333+
vm.expectRevert(IV4Router.TooMuchRequested.selector);
334+
router.executeActions(data);
335+
}
336+
290337
function test_swapExactOutputSingle_zeroForOne() public {
291338
uint256 amountOut = 1 ether;
292339
uint256 expectedAmountIn = 1008049273448486163;
@@ -327,6 +374,22 @@ contract V4RouterTest is RoutingTestHelpers {
327374
assertEq(outputBalanceAfter - outputBalanceBefore, amountOut);
328375
}
329376

377+
function test_swapExactOut_revertsForAmountIn() public {
378+
uint256 amountOut = 1 ether;
379+
uint256 expectedAmountIn = 1008049273448486163;
380+
381+
tokenPath.push(currency0);
382+
tokenPath.push(currency1);
383+
IV4Router.ExactOutputParams memory params = _getExactOutputParams(tokenPath, amountOut);
384+
params.amountInMaximum = uint128(expectedAmountIn - 1);
385+
386+
plan = plan.add(Actions.SWAP_EXACT_OUT, abi.encode(params));
387+
bytes memory data = plan.finalizeSwap(key0.currency0, key0.currency1, address(this));
388+
389+
vm.expectRevert(IV4Router.TooMuchRequested.selector);
390+
router.executeActions(data);
391+
}
392+
330393
function test_swapExactOut_1Hop_zeroForOne() public {
331394
uint256 amountOut = 1 ether;
332395
uint256 expectedAmountIn = 1008049273448486163;

0 commit comments

Comments
 (0)