Replies: 2 comments 5 replies
-
|
Hello @s3bc40, considering that the revert is coming from your |
Beta Was this translation helpful? Give feedback.
-
|
let me correct your code, getHealthFactor(uint256 totalDscMinted, uint256 collateralValueInUsd)
This is the result of fixing the function code: function redeemCollateral(uint256 collateralSeed, uint256 amountCollateral) public {
ERC20Mock collateral = _getCollateralFromSeed(collateralSeed);
uint256 maxCollatealToRedeem = engine.getCollateralBalanceOfUser(msg.sender, address(collateral));
amountCollateral = bound(amountCollateral, 0, maxCollatealToRedeem);
if (amountCollateral == 0) {
return;
}
// avoid breaks health factor
console.log("checking health factor");
(uint256 totalDscMinted, uint256 totalCollateralValue) = engine.getAccountInformation(msg.sender);
uint256 collateralValueInUsd = engine.getUsdValue(address(collateral), amountCollateral);
uint256 healthFactor = engine.calculateHealthFactor(totalDscMinted, totalCollateralValue - collateralValueInUsd);
if (healthFactor < engine.getMinHealthFactor()) {
return;
}
console.log("health factor is good");
vm.prank(msg.sender);
engine.redeemCollateral(address(collateral), amountCollateral);
}note: i named my |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey 👋
I have been struggling a lot on fuzz testing to understand why my code was breaking when in the course it was not. 99% it was on my side, but after fixing the
mintDscfor invariant test in theHandler.t.solI was getting the same error over and over again.Here is the
mintDschandler function, just to give some context at what point I did have this error:The error: [Revert] DSCEngine__BreaksHealthFactor(702670004378033987 [7.026e17])
[103634] Handler::redeemCollateral(51540240699854426746100967214 [5.154e28], 1035285307183729588843981678733247299005494427896378025 [1.035e54]) ├─ [2808] DSCEngine::getCollateralBalanceOfUser(0x00000000000000000000000000000000000375d9, ERC20Mock: [0x90193C961A926261B756D1E5bb255e67ff9498A1]) [staticcall] │ └─ ← [Return] 1921311765 [1.921e9] ├─ [0] console::log("Bound result", 1889356931 [1.889e9]) [staticcall] │ └─ ← [Stop] ├─ [0] VM::startPrank(0x00000000000000000000000000000000000375d9) │ └─ ← [Return] ├─ [85785] DSCEngine::redeemCollateral(ERC20Mock: [0x90193C961A926261B756D1E5bb255e67ff9498A1], 1889356931 [1.889e9]) │ ├─ emit CollateralRedeemed(redeemedFrom: 0x00000000000000000000000000000000000375d9, redeemedTo: 0x00000000000000000000000000000000000375d9, token: ERC20Mock: [0x90193C961A926261B756D1E5bb255e67ff9498A1], amount: 1889356931 [1.889e9]) │ ├─ [29805] ERC20Mock::transfer(0x00000000000000000000000000000000000375d9, 1889356931 [1.889e9]) │ │ ├─ emit Transfer(from: DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], to: 0x00000000000000000000000000000000000375d9, value: 1889356931 [1.889e9]) │ │ └─ ← [Return] true │ ├─ [8993] MockV3Aggregator::latestRoundData() [staticcall] │ │ └─ ← [Return] 1, 200000000000 [2e11], 1, 1, 1 │ ├─ [8993] MockV3Aggregator::latestRoundData() [staticcall] │ │ └─ ← [Return] 1, 100000000000 [1e11], 1, 1, 1 │ └─ ← [Revert] DSCEngine__BreaksHealthFactor(702670004378033987 [7.026e17]) └─ ← [Revert] DSCEngine__BreaksHealthFactor(702670004378033987 [7.026e17])I have checked all the discussion to see if it was something already fixed or something I have done wrong:
Everything seemed fine, even my previous tests (unit or integration) were passing.
A lot of time my
BreakHealthFactorwas triggered in my fuzz testing, which showed some test reverted(runs: 128, calls: 16384, reverts: 12)but not on the course : so I am curious to understand.So here is my suggestion, I do not know if it is relevant or not while testing, but I have added a check on the
HealthFactorbefore doing adsce.redeemCollateralon the boundedamountCollateral:Now I am reaching the result on the course:
(runs: 128, calls: 16384, reverts: 0)Is my solution is considered as cheating since I am checking the health factor before, or does it seem logical ? I am open to understand any suggestion 👍
Thanks for reading!
Beta Was this translation helpful? Give feedback.
All reactions