Issue in exercise 2, #15
-
I am facing this issue of exercise 2 Repo Link: https://github.com/farman1094/UniswapV2Practice codepragma solidity 0.8.24;
import {Test} from "forge-std/Test.sol";
import {console} from "forge-std/console.sol";
import {IERC20} from "lib/forge-std/src/interfaces/IERC20.sol";
import {IUniswapV2Router02} from "v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import {WETH, DAI, MKR, UNISWAP_V2_ROUTER_02 } from "src/constants.sol";
import {IWETH} from "src/interface/IWETH.sol";
contract IssueInTest is Test {
IUniswapV2Router02 public router = IUniswapV2Router02(UNISWAP_V2_ROUTER_02);
IWETH public weth = IWETH(WETH);
IERC20 public dai = IERC20(DAI);
IERC20 public mkr = IERC20(MKR);
address private constant user = address(100);
function setUp() public {
console.log(block.chainid);
deal(user, 100e18);
assert(user.balance == 100e18);
vm.startPrank(user);
weth.deposit{value: 100 ether}();
weth.approve(address(router), type(uint256).max);
vm.stopPrank();
}
function testSwapExactTokensForIssues() public {
address[] memory path = new address[](3);
path[0] = WETH;
path[1] = DAI;
path[2] = MKR;
uint deadline = block.timestamp + 1000;
uint amountIn = 1e18;
uint amountOutMin = 1;
vm.prank(user);
uint[] memory amounts = router.swapExactTokensForTokens(
amountIn,
amountOutMin,
path,
user,
deadline
);
uint amountOut = 1e17;
uint amountInMax = 50e18;
vm.prank(user);
uint[] memory amountsOut = router.swapTokensForExactTokens(
amountOut,
amountInMax,
path,
user,
deadline
);
console.log("amounts: WETH", amounts[0]);
console.log("amounts: DAI", amounts[1]);
console.log("amounts: MKR", amounts[2]);
}
} ├─ [3534] 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D::swapTokensForExactTokens(100000000000000000 [1e17], 50000000000000000000 [5e19], [0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 0x6B175474E89094C44Da98b954EedeAC495271d0F, 0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2], 0x0000000000000000000000000000000000000064, 1732807287 [1.732e9])
│ ├─ [504] 0x517F9dD285e75b599234F7221227339478d0FcC8::getReserves() [staticcall]
│ │ └─ ← [Return] 0x0000000000000000000000000000000000000000000000d511fd926ed23d45b300000000000000000000000000000000000000000000000000481274e527b948000000000000000000000000000000000000000000000000000000006748868f
│ └─ ← [Revert] revert: ds-math-sub-underflow
└─ ← [Revert] revert: ds-math-sub-underflow
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 13.77s (7.95s CPU time)
Ran 1 test suite in 15.36s (13.77s CPU time): 0 tests passed, 1 failed, 0 skipped (1 total tests)
Failing tests:
Encountered 1 failing test in test/IssueInTest.t.sol:IssueInTest
[FAIL: revert: ds-math-sub-underflow] testSwapExactTokensForIssues() (gas: 170738)``` |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 14 replies
-
This works fine for me. Ran 1 test for test/UniswapV2SwapTest.t.sol:UniswapV2SwapTest
[PASS] test_swapTokensForExactTokens() (gas: 164757)
Logs:
WETH 94124367944473348
DAI 337407440607281150345
MKR 100000000000000000
Traces:
[62118] UniswapV2SwapTest::setUp()
├─ [0] VM::deal(0x0000000000000000000000000000000000000064, 100000000000000000000 [1e20])
│ └─ ← [Return]
├─ [0] VM::startPrank(0x0000000000000000000000000000000000000064)
│ └─ ← [Return]
├─ [23974] 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2::deposit{value: 100000000000000000000}()
│ ├─ emit Deposit(param0: 0x0000000000000000000000000000000000000064, param1: 100000000000000000000 [1e20])
│ └─ ← [Stop]
├─ [24420] 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2::approve(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D, 115792089237316195423570985008687907853269984665640564039457584007913129639935 [1.157e77])
│ ├─ emit Approval(owner: 0x0000000000000000000000000000000000000064, spender: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D, value: 115792089237316195423570985008687907853269984665640564039457584007913129639935 [1.157e77])
│ └─ ← [Return] true
├─ [0] VM::stopPrank()
│ └─ ← [Return]
└─ ← [Stop]
[170357] UniswapV2SwapTest::test_swapTokensForExactTokens()
├─ [0] VM::prank(0x0000000000000000000000000000000000000064)
│ └─ ← [Return]
├─ [156224] 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D::swapTokensForExactTokens(100000000000000000 [1e17], 1000000000000000000 [1e18], [0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, 0x6B175474E89094C44Da98b954EedeAC495271d0F, 0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2], 0x0000000000000000000000000000000000000064, 1732802447 [1.732e9])
│ ├─ [2504] 0x517F9dD285e75b599234F7221227339478d0FcC8::getReserves() [staticcall]
│ │ └─ ← [Return] 375666095063796494027 [3.756e20], 211674029428388778 [2.116e17], 1732785371 [1.732e9]
│ ├─ [2504] 0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11::getReserves() [staticcall]
│ │ └─ ← [Return] 6552852397611689605386392 [6.552e24], 1822428920933467739475 [1.822e21], 1732802243 [1.732e9]
│ ├─ [15025] 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2::transferFrom(0x0000000000000000000000000000000000000064, 0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11, 94124367944473348 [9.412e16])
│ │ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000064, to: 0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11, value: 94124367944473348 [9.412e16])
│ │ └─ ← [Return] true
│ ├─ [48033] 0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11::swap(337407440607281150345 [3.374e20], 0, 0x517F9dD285e75b599234F7221227339478d0FcC8, 0x)
│ │ ├─ [13074] 0x6B175474E89094C44Da98b954EedeAC495271d0F::transfer(0x517F9dD285e75b599234F7221227339478d0FcC8, 337407440607281150345 [3.374e20])
│ │ │ ├─ emit Transfer(from: 0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11, to: 0x517F9dD285e75b599234F7221227339478d0FcC8, value: 337407440607281150345 [3.374e20])
│ │ │ └─ ← [Return] true
│ │ ├─ [602] 0x6B175474E89094C44Da98b954EedeAC495271d0F::balanceOf(0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11) [staticcall]
│ │ │ └─ ← [Return] 6552514990171082324236047 [6.552e24]
│ │ ├─ [534] 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2::balanceOf(0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11) [staticcall]
│ │ │ └─ ← [Return] 1822523045301412212823 [1.822e21]
│ │ ├─ emit Sync(reserve0: 6552514990171082324236047 [6.552e24], reserve1: 1822523045301412212823 [1.822e21])
│ │ ├─ emit Swap(sender: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D, amount0In: 0, amount1In: 94124367944473348 [9.412e16], amount0Out: 337407440607281150345 [3.374e20], amount1Out: 0, to: 0x517F9dD285e75b599234F7221227339478d0FcC8)
│ │ └─ ← [Stop]
│ ├─ [67846] 0x517F9dD285e75b599234F7221227339478d0FcC8::swap(0, 100000000000000000 [1e17], 0x0000000000000000000000000000000000000064, 0x)
│ │ ├─ [32706] 0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2::transfer(0x0000000000000000000000000000000000000064, 100000000000000000 [1e17])
│ │ │ ├─ emit Transfer(from: 0x517F9dD285e75b599234F7221227339478d0FcC8, to: 0x0000000000000000000000000000000000000064, value: 100000000000000000 [1e17])
│ │ │ └─ ← [Return] true
│ │ ├─ [602] 0x6B175474E89094C44Da98b954EedeAC495271d0F::balanceOf(0x517F9dD285e75b599234F7221227339478d0FcC8) [staticcall]
│ │ │ └─ ← [Return] 713073535671077644372 [7.13e20]
│ │ ├─ [715] 0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2::balanceOf(0x517F9dD285e75b599234F7221227339478d0FcC8) [staticcall]
│ │ │ └─ ← [Return] 111674029428388778 [1.116e17]
│ │ ├─ emit Sync(reserve0: 713073535671077644372 [7.13e20], reserve1: 111674029428388778 [1.116e17])
│ │ ├─ emit Swap(sender: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D, amount0In: 337407440607281150345 [3.374e20], amount1In: 0, amount0Out: 0, amount1Out: 100000000000000000 [1e17], to: 0x0000000000000000000000000000000000000064)
│ │ └─ ← [Stop]
│ └─ ← [Return] [94124367944473348 [9.412e16], 337407440607281150345 [3.374e20], 100000000000000000 [1e17]]
├─ [0] console::log("WETH", 94124367944473348 [9.412e16]) [staticcall]
│ └─ ← [Stop]
├─ [0] console::log("DAI", 337407440607281150345 [3.374e20]) [staticcall]
│ └─ ← [Stop]
├─ [0] console::log("MKR", 100000000000000000 [1e17]) [staticcall]
│ └─ ← [Stop]
├─ [715] 0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2::balanceOf(0x0000000000000000000000000000000000000064) [staticcall]
│ └─ ← [Return] 100000000000000000 [1e17]
├─ [0] VM::assertEq(100000000000000000 [1e17], 100000000000000000 [1e17], "MKR balance of user") [staticcall]
│ └─ ← [Return]
└─ ← [Stop]
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 12.52s (8.42s CPU time) |
Beta Was this translation helpful? Give feedback.
-
@farman1094 |
Beta Was this translation helpful? Give feedback.
-
Thank you, I found the answer! |
Beta Was this translation helpful? Give feedback.
@farman1094
Issue was low liquidity on Uni v2 DAI/MKR
It should now be fixed