Skip to content

Commit 12d3d45

Browse files
committed
add tests for failure case
1 parent 0a34880 commit 12d3d45

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

SwapRouter/test/SkipGoSwapRouter.t.sol

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,55 @@ contract SkipGoSwapRouterTest is Test {
8686
assertEq(amountOut, expectedAmountOut);
8787
}
8888

89+
function test_revertSwapExactIn_WhenAmountOutIsLessThanAmountIn() public {
90+
address weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
91+
address usdc = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
92+
address wbtc = 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599;
93+
94+
SkipGoSwapRouter.Hop[] memory hops = new SkipGoSwapRouter.Hop[](2);
95+
96+
{
97+
UniswapV2Adapter.UniswapV2Data memory hopOneData = UniswapV2Adapter.UniswapV2Data({
98+
pool: 0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc,
99+
zeroToOne: false,
100+
fee: 300
101+
});
102+
103+
bytes memory encodedHopOneData = abi.encode(hopOneData);
104+
105+
hops[0] = SkipGoSwapRouter.Hop({exchangeType: 1, data: encodedHopOneData});
106+
107+
UniswapV3Adapter.UniswapV3Data memory hopTwoData = UniswapV3Adapter.UniswapV3Data({
108+
tokenIn: usdc,
109+
tokenOut: wbtc,
110+
fee: 3000,
111+
quoter: 0x5e55C9e631FAE526cd4B0526C4818D6e0a9eF0e3,
112+
swapRouter: 0xE592427A0AEce92De3Edee1F18E0157C05861564
113+
});
114+
115+
bytes memory encodedHopTwoData = abi.encode(hopTwoData);
116+
117+
hops[1] = SkipGoSwapRouter.Hop({exchangeType: 2, data: encodedHopTwoData});
118+
}
119+
120+
uint256 amountIn = 1 ether;
121+
122+
address alice = makeAddr("alice");
123+
124+
deal(weth, alice, amountIn);
125+
126+
uint256 expectedAmountOut = router.getAmountOut(amountIn, hops);
127+
128+
vm.startPrank(alice);
129+
130+
IERC20(weth).approve(address(router), amountIn);
131+
132+
vm.expectRevert("amount out is less than amount out min");
133+
router.swapExactIn(amountIn, expectedAmountOut + 1, weth, wbtc, hops);
134+
135+
vm.stopPrank();
136+
}
137+
89138
function test_swapExactOut() public {
90139
address weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
91140
address usdc = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
@@ -144,4 +193,53 @@ contract SkipGoSwapRouterTest is Test {
144193

145194
assertEq(amountIn, expectedAmountIn);
146195
}
196+
197+
function test_revertSwapExactOut_WhenAmountInIsGreaterThanAmountInMax() public {
198+
address weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
199+
address usdc = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
200+
address wbtc = 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599;
201+
202+
SkipGoSwapRouter.Hop[] memory hops = new SkipGoSwapRouter.Hop[](2);
203+
204+
{
205+
UniswapV2Adapter.UniswapV2Data memory hopOneData = UniswapV2Adapter.UniswapV2Data({
206+
pool: 0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc,
207+
zeroToOne: false,
208+
fee: 300
209+
});
210+
211+
bytes memory encodedHopOneData = abi.encode(hopOneData);
212+
213+
hops[0] = SkipGoSwapRouter.Hop({exchangeType: 1, data: encodedHopOneData});
214+
215+
UniswapV3Adapter.UniswapV3Data memory hopTwoData = UniswapV3Adapter.UniswapV3Data({
216+
tokenIn: usdc,
217+
tokenOut: wbtc,
218+
fee: 3000,
219+
quoter: 0x5e55C9e631FAE526cd4B0526C4818D6e0a9eF0e3,
220+
swapRouter: 0xE592427A0AEce92De3Edee1F18E0157C05861564
221+
});
222+
223+
bytes memory encodedHopTwoData = abi.encode(hopTwoData);
224+
225+
hops[1] = SkipGoSwapRouter.Hop({exchangeType: 2, data: encodedHopTwoData});
226+
}
227+
228+
uint256 amountOut = 100000000;
229+
230+
address alice = makeAddr("alice");
231+
232+
uint256 expectedAmountIn = router.getAmountIn(amountOut, hops);
233+
234+
deal(weth, alice, expectedAmountIn);
235+
236+
vm.startPrank(alice);
237+
238+
IERC20(weth).approve(address(router), expectedAmountIn);
239+
240+
vm.expectRevert("amount in is greater than amount in max");
241+
router.swapExactOut(amountOut, expectedAmountIn - 1, weth, wbtc, hops);
242+
243+
vm.stopPrank();
244+
}
147245
}

0 commit comments

Comments
 (0)