Replies: 2 comments 4 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.
-
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
mintDsc
for invariant test in theHandler.t.sol
I was getting the same error over and over again.Here is the
mintDsc
handler function, just to give some context at what point I did have this error:The error: [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
BreakHealthFactor
was 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
HealthFactor
before doing adsce.redeemCollateral
on 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