Lesson 12 - Can't write test for DSCMint function #1747
-
I want to write a test for a situation in which user wants to mint DSCs more than collateral. function mintDSC(uint256 amountDscToMint) public moreThanZero(amountDscToMint) {
s_DSCMinted[msg.sender] += amountDscToMint;
_revertIfHealthFactorIsBroken(msg.sender);
bool minted = i_dsc.mint(msg.sender, amountDscToMint);
if (!minted) {
revert DSCEngine__MintFailed();
}
}
function _revertIfHealthFactorIsBroken(address user) internal view {
uint256 userHealthFactor = _healthFactor(user);
if (userHealthFactor < MIN_HEALTH_FACTOR) {
revert DSCEngine__BreaksHealthFactor(userHealthFactor);
}
} the problem lies in function testMintRevertsIfHealthFactorIsBroken() public depositedCollateral {
vm.startPrank(USER);
vm.expectRevert(abi.encodeWithSelector(DSCEngine.DSCEngine__BreaksHealthFactor, ??));
engine.mintDSC(100000);
vm.stopPrank();
} what value should i give to the custom error in or mkae me aware if i am doing it the wrong way. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 13 replies
-
Hello @smahdi-dev, You can just use |
Beta Was this translation helpful? Give feedback.
-
Hello @smahdi-dev, Below is your return (collateralAdjustedForThreshold * PRECISION) / totalDscMinted; function _healthFactor(address user) internal view returns (uint256) {
// total DSC minted
// total collateral value
(uint256 totalDscMinted, uint256 collateralValueInUsd) = _getAccountInformation(user);
if (collateralValueInUsd == 0) {
revert DSCEngine__NoCollateralDeposited();
}
if (totalDscMinted == 0) {
revert DSCEngine__NoDscMinted();
}
uint256 collateralAdjustedForThreshold = (collateralValueInUsd * LIQUIDATION_THRESHOLD) / LIQUIDATION_PRECISION;
return (collateralAdjustedForThreshold * PRECISION) / totalDscMinted;
} Below is mine function _health_factor(address _user) internal view returns (uint256 health_factor) {
(uint256 total_DSC_minted, uint256 collateral_value_in_USD) = _get_account_information(_user);
console2.log("the total collateral value in usd is: ", collateral_value_in_USD);
console2.log("the total DSC minted is: ", total_DSC_minted);
if (total_DSC_minted == 0) return type(uint256).max;
uint256 collateral_amount_for_threshold =
(collateral_value_in_USD * LIQUIDATION_THRESHOLD) / LIQUIDATION_THRESHOLD_DIVIDER;
console2.log("the collateral amount for threshold is: ", collateral_amount_for_threshold);
health_factor = collateral_amount_for_threshold / total_DSC_minted;
console2.log("the calculated health factor is: ", health_factor);
return health_factor;
} Maybe Patrick multiplied it in his codebase, and it worked for him, but when I ran into an issue and what the tutor did in the tutorial didn't work, I came back to thinking and figuring out how it would work for me. What a programmer does is figure out stuff. |
Beta Was this translation helpful? Give feedback.
Hello @smahdi-dev, Below is your
_healthFactor
function. Why did you multiplycollateralAdjustedForThreshold
byPRECISION
in the line below