Advanced Foundry, Section 3 DeFi-StableCoin, Lesson 19 testRevertsIfMintedDscBreaksHealthFactor fails with the wrong error #2750
-
Hi, I'm going through the course, at lesson 19 of stablecoin. I am getting this error when I run the test: $ forge test --mt testRevertsIfMintedDscBreaksHealthFactor
[⠒] Compiling...
[⠘] Compiling 3 files with Solc 0.8.26
[⠃] Solc 0.8.26 finished in 1.58s
Compiler run successful!
Ran 1 test for test/DSCEngineTest.t.sol:DSCEngineTest
[FAIL: Error != expected error: ERC20: transfer amount exceeds balance != DSCEngine__HealthFactorTooLow(0)] testRevertsIfMintedDscBreaksHealthFactor() (gas: 116694)
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 1.38ms (172.25µs CPU time) Here is my test: function testRevertsIfMintedDscBreaksHealthFactor() public {
(, int256 price,,,) = MockV3Aggregator(ethUsdPriceFeed).latestRoundData();
amountToMint =
(amountCollateral * (uint256(price) * dscengine.getAdditionalFeedPrecision())) / dscengine.getPrecision();
vm.startPrank(user);
ERC20Mock(weth).approve(address(dscengine), amountCollateral);
uint256 expectedHealthFactor =
dscengine.calculateHealthFactor(amountToMint, dscengine.getUsdValue(weth, amountCollateral));
console.log("User WETH balance:", ERC20Mock(weth).balanceOf(USER));
console.log("Amount collateral:", amountCollateral);
console.log("Amount to mint:", amountToMint);
console.log("Expected health factor:", expectedHealthFactor);
vm.expectRevert(abi.encodeWithSelector(DSCEngine.DSCEngine__HealthFactorTooLow.selector, expectedHealthFactor));
dscengine.depositCollateralAndMintDSC(weth, amountCollateral, amountToMint);
vm.stopPrank();
} You can see the rest of my code here: Verbose output with the trace: $ forge test --mt testRevertsIfMintedDscBreaksHealthFactor -vvvv
[⠒] Compiling...
[⠘] Compiling 3 files with Solc 0.8.26
[⠃] Solc 0.8.26 finished in 1.61s
Compiler run successful!
Ran 1 test for test/DSCEngineTest.t.sol:DSCEngineTest
[FAIL: Error != expected error: ERC20: transfer amount exceeds balance != DSCEngine__HealthFactorTooLow(500000000000000000 [5e17])] testRevertsIfMintedDscBreaksHealthFactor() (gas: 117511)
Logs:
User WETH balance: 10000000000000000000
Amount collateral: 10000000000000000000
Amount to mint: 24000000000000000000000
Expected health factor: 500000000000000000
Traces:
[10497440] DSCEngineTest::setUp()
├─ [4702604] → new DeployDSC@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
│ └─ ← [Return] 23375 bytes of code
├─ [5589325] DeployDSC::run()
│ ├─ [3731080] → new <unknown>@0x104fBc016F4bb334D775a19E8A6510109AC63E00
│ │ ├─ [0] VM::startBroadcast()
│ │ │ └─ ← [Return]
│ │ ├─ [372255] → new <unknown>@0x90193C961A926261B756D1E5bb255e67ff9498A1
│ │ │ └─ ← [Return] 1082 bytes of code
│ │ ├─ [372255] → new <unknown>@0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496
│ │ │ └─ ← [Return] 1082 bytes of code
│ │ ├─ [658858] → new <unknown>@0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3
│ │ │ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: DeployDSC: [0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f], value: 100000000000 [1e11])
│ │ │ └─ ← [Return] 2829 bytes of code
│ │ ├─ [658858] → new <unknown>@0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76
│ │ │ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: DeployDSC: [0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f], value: 100000000000 [1e11])
│ │ │ └─ ← [Return] 2829 bytes of code
│ │ ├─ [0] VM::stopBroadcast()
│ │ │ └─ ← [Return]
│ │ └─ ← [Return] 6894 bytes of code
│ ├─ [934] 0x104fBc016F4bb334D775a19E8A6510109AC63E00::activeNetworkConfig() [staticcall]
│ │ └─ ← [Return] 0x90193C961A926261B756D1E5bb255e67ff9498A1, 0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496, 0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3, 0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76, 77814517325470205911140941194401928579557062014761831930645393041380819009408 [7.781e76]
│ ├─ [0] VM::startBroadcast(<pk>)
│ │ └─ ← [Return]
│ ├─ [741806] → new DecentralizedStableCoin@0x5FbDB2315678afecb367f032d93F642f64180aa3
│ │ ├─ emit OwnershipTransferred(previousOwner: 0x0000000000000000000000000000000000000000, newOwner: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266)
│ │ └─ ← [Return] 3361 bytes of code
│ ├─ [875671] → new DSCEngine@0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
│ │ └─ ← [Return] 3696 bytes of code
│ ├─ [2446] DecentralizedStableCoin::transferOwnership(DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512])
│ │ ├─ emit OwnershipTransferred(previousOwner: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266, newOwner: DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512])
│ │ └─ ← [Stop]
│ ├─ [0] VM::stopBroadcast()
│ │ └─ ← [Return]
│ └─ ← [Return] DecentralizedStableCoin: [0x5FbDB2315678afecb367f032d93F642f64180aa3], DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], 0x104fBc016F4bb334D775a19E8A6510109AC63E00
├─ [934] 0x104fBc016F4bb334D775a19E8A6510109AC63E00::activeNetworkConfig() [staticcall]
│ └─ ← [Return] 0x90193C961A926261B756D1E5bb255e67ff9498A1, 0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496, 0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3, 0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76, 77814517325470205911140941194401928579557062014761831930645393041380819009408 [7.781e76]
├─ [24728] 0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3::mint(user: [0x6CA6d1e2D5347Bfab1d91e883F1915560e09129D], 10000000000000000000 [1e19])
│ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: user: [0x6CA6d1e2D5347Bfab1d91e883F1915560e09129D], value: 10000000000000000000 [1e19])
│ └─ ← [Stop]
└─ ← [Stop]
[117511] DSCEngineTest::testRevertsIfMintedDscBreaksHealthFactor()
├─ [8993] 0x90193C961A926261B756D1E5bb255e67ff9498A1::latestRoundData() [staticcall]
│ └─ ← [Return] 1, 240000000000 [2.4e11], 1, 1, 1
├─ [270] DSCEngine::getPrecision() [staticcall]
│ └─ ← [Return] 1000000000000000000 [1e18]
├─ [248] DSCEngine::getAdditionalFeedPrecision() [staticcall]
│ └─ ← [Return] 10000000000 [1e10]
├─ [0] VM::startPrank(ECRecover: [0x0000000000000000000000000000000000000001])
│ └─ ← [Return]
├─ [24651] 0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3::approve(DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], 10000000000000000000 [1e19])
│ ├─ emit Approval(owner: ECRecover: [0x0000000000000000000000000000000000000001], spender: DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], value: 10000000000000000000 [1e19])
│ └─ ← [Return] true
├─ [4596] DSCEngine::getUsdValue(0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3, 10000000000000000000 [1e19]) [staticcall]
│ ├─ [993] 0x90193C961A926261B756D1E5bb255e67ff9498A1::latestRoundData() [staticcall]
│ │ └─ ← [Return] 1, 240000000000 [2.4e11], 1, 1, 1
│ └─ ← [Return] 24000000000000000000000 [2.4e22]
├─ [719] DSCEngine::calculateHealthFactor(24000000000000000000000 [2.4e22], 24000000000000000000000 [2.4e22]) [staticcall]
│ └─ ← [Return] 500000000000000000 [5e17]
├─ [2607] 0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3::balanceOf(user: [0x6CA6d1e2D5347Bfab1d91e883F1915560e09129D]) [staticcall]
│ └─ ← [Return] 10000000000000000000 [1e19]
├─ [0] console::log("User WETH balance:", 10000000000000000000 [1e19]) [staticcall]
│ └─ ← [Stop]
├─ [0] console::log("Amount collateral:", 10000000000000000000 [1e19]) [staticcall]
│ └─ ← [Stop]
├─ [0] console::log("Amount to mint:", 24000000000000000000000 [2.4e22]) [staticcall]
│ └─ ← [Stop]
├─ [0] console::log("Expected health factor:", 500000000000000000 [5e17]) [staticcall]
│ └─ ← [Stop]
├─ [0] VM::expectRevert(custom error 0xf28dceb3: 00000000000000000000000000000000…00000000000000000000000000000000 (128 bytes))
│ └─ ← [Return]
├─ [35841] DSCEngine::depositCollateralAndMintDSC(0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3, 10000000000000000000 [1e19], 24000000000000000000000 [2.4e22])
│ ├─ emit CollateralDeposited(user: ECRecover: [0x0000000000000000000000000000000000000001], token: 0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3, amount: 10000000000000000000 [1e19])
│ ├─ [5528] 0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3::transferFrom(ECRecover: [0x0000000000000000000000000000000000000001], DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], 10000000000000000000 [1e19])
│ │ ├─ emit Approval(owner: ECRecover: [0x0000000000000000000000000000000000000001], spender: DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], value: 0)
│ │ └─ ← [Revert] revert: ERC20: transfer amount exceeds balance
│ └─ ← [Revert] revert: ERC20: transfer amount exceeds balance
└─ ← [Revert] Error != expected error: ERC20: transfer amount exceeds balance != DSCEngine__HealthFactorTooLow(500000000000000000 [5e17])
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 1.33ms (182.38µs CPU time)
Ran 1 test suite in 1.05s (1.33ms CPU time): 0 tests passed, 1 failed, 0 skipped (1 total tests)
Failing tests:
Encountered 1 failing test in test/DSCEngineTest.t.sol:DSCEngineTest
[FAIL: Error != expected error: ERC20: transfer amount exceeds balance != DSCEngine__HealthFactorTooLow(500000000000000000 [5e17])] testRevertsIfMintedDscBreaksHealthFactor() (gas: 117511)
Encountered a total of 1 failing tests, 0 tests succeeded AFAIU, the test user puts 10 eth collateral, worth $24,000 USD at $2400 price for ETH, then tries to mint all 24000 worth of DSC. It should revert because it should only be able to mint half of that, due to LIQUIDATION_TRESHOLD = 50, but it doesn't... I'm lost. Am I doing something wrong with the math or health factor calculations? Did I mess up the calculateHealthFactor refactoring? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
Hello @BowtiedHarpyEagle, Please run |
Beta Was this translation helpful? Give feedback.
Interesting. If your
user
is address(1), then why did you mint the token to a different address and not address(1) in your setup as shown in the stack trace below