Failing when setting fail on revert = true. #1863
Replies: 5 comments 44 replies
-
function invariant_protocolMustHaveMoreValueThanTotalSupply() public view {
// get the value of the allthe ollateral in the protocol
// compare it to all the debt(dsc)
uint256 totalSupply = dsc.totalSupply();
uint256 totalWethDeposited = IERC20(weth).balanceOf(address(dsce));
uint256 totalWBtcDeposited = IERC20(wbtc).balanceOf(address(dsce));
uint256 wethValue = dsce.getUsdValue(weth, totalWethDeposited);
uint256 wbtcValue = dsce.getUsdValue(wbtc, totalWBtcDeposited);
console.log("weth value: ", wethValue);
console.log("wbtc value: ", wbtcValue);
console.log("Total Supply: ", totalSupply);
assert(wethValue + wbtcValue >= totalSupply);
} pragma solidity ^0.8.18;
import {Test} from "forge-std/Test.sol";
import {DSCEngine} from "../../src/DSCEngine.sol";
import {DecentralizedStableCoin} from "../../src/DecentralizedStableCoin.sol";
import {ERC20Mock} from "@openzeppelin/contracts/mocks/token/ERC20Mock.sol";
contract Handler is Test {
DSCEngine dsce;
DecentralizedStableCoin dsc;
ERC20Mock weth;
ERC20Mock wbtc;
uint256 MAX_DEPOSIT_SIZE = type(uint96).max; // this'll allow to get the max uint96 value
constructor(DSCEngine _dscEngine, DecentralizedStableCoin _dsc) {
dsce = _dscEngine;
dsc = _dsc;
address[] memory collateralTokens = dsce.getCollateralTokens();
weth = ERC20Mock(collateralTokens[0]);
wbtc = ERC20Mock(collateralTokens[1]);
}
function depositCollateral(
uint256 collateralSeed,
uint256 amountCollateral
) public {
ERC20Mock collateral = _getCollateralFromSeed(collateralSeed);
amountCollateral = bound(amountCollateral, 1, MAX_DEPOSIT_SIZE);
vm.startPrank(msg.sender);
collateral.mint(msg.sender, amountCollateral);
collateral.approve(address(dsce), amountCollateral);
dsce.depositCollateral(address(collateral), amountCollateral);
vm.stopPrank();
}
// Helper Functions
function _getCollateralFromSeed(
uint256 collateralSeed
) private view returns (ERC20Mock) {
if (collateralSeed % 2 == 0) {
return weth;
}
return wbtc;
}
} |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Hello @Juice-07, So I got the time to debug your code, and I think i found something. I wouldn't directly point you to what i found since you mentioned that you want to enhance your debugging skills. Compare my output for My output for [105643] Handler::mint_and_deposit_collateral(false, 659)
├─ [0] console::log("Bound Result", 659) [staticcall]
│ └─ ← [Stop]
├─ [0] VM::startPrank(0x0000000000000000dd9cFc42Bf690486e25D366d)
│ └─ ← [Return]
├─ [29528] ERC20Mock::mint(0x0000000000000000dd9cFc42Bf690486e25D366d, 659)
│ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: 0x0000000000000000dd9cFc42Bf690486e25D366d, value: 659)
│ └─ ← [Stop]
├─ [24651] ERC20Mock::approve(DSCEngine: [0x5FbDB2315678afecb367f032d93F642f64180aa3], 659)
│ ├─ emit Approval(owner: 0x0000000000000000dd9cFc42Bf690486e25D366d, spender: DSCEngine: [0x5FbDB2315678afecb367f032d93F642f64180aa3], value: 659)
│ └─ ← [Return] true
├─ [60216] DSCEngine::depositCollateral(ERC20Mock: [0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3], 659)
│ ├─ emit collateral_deposited(_user: 0x0000000000000000dd9cFc42Bf690486e25D366d, address_of_collateral_token: ERC20Mock: [0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3], _amount_deposited: 659)
│ ├─ [27689] ERC20Mock::transferFrom(0x0000000000000000dd9cFc42Bf690486e25D366d, DSCEngine: [0x5FbDB2315678afecb367f032d93F642f64180aa3], 659)
│ │ ├─ emit Approval(owner: 0x0000000000000000dd9cFc42Bf690486e25D366d, spender: DSCEngine: [0x5FbDB2315678afecb367f032d93F642f64180aa3], value: 0)
│ │ ├─ emit Transfer(from: 0x0000000000000000dd9cFc42Bf690486e25D366d, to: DSCEngine: [0x5FbDB2315678afecb367f032d93F642f64180aa3], value: 659)
│ │ └─ ← [Return] true
│ └─ ← [Stop]
├─ [0] VM::stopPrank()
│ └─ ← [Return]
└─ ← [Stop] Your output for [124802] Handler::mintAndDepositCollateral(15253010732783067804194855451661709012757140991998531914879 [1.525e58], 109287051600633930442037615707777945433927314713 [1.092e47])
├─ [0] console::log("Bound result", 31176204170279910451729223793 [3.117e28]) [staticcall]
│ └─ ← [Stop]
├─ [46789] ERC20Mock::mint(0x0000000000000000000000000000000000001007, 31176204170279910451729223793 [3.117e28])
│ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: 0x0000000000000000000000000000000000001007, value: 31176204170279910451729223793 [3.117e28])
│ └─ ← [Stop]
├─ [24739] ERC20Mock::approve(DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], 31176204170279910451729223793 [3.117e28])
│ ├─ emit Approval(owner: Handler: [0x2e234DAe75C793f67A35089C9d99245E1C58470b], spender: DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], value: 31176204170279910451729223793 [3.117e28])
│ └─ ← [Return] true
├─ [36085] DSCEngine::depositCollateral(ERC20Mock: [0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76], 31176204170279910451729223793 [3.117e28])
│ ├─ emit CollateralDeposited(user: Handler: [0x2e234DAe75C793f67A35089C9d99245E1C58470b], token: ERC20Mock: [0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76], amount: 31176204170279910451729223793 [3.117e28])
│ ├─ [3773] ERC20Mock::transferFrom(Handler: [0x2e234DAe75C793f67A35089C9d99245E1C58470b], DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], 31176204170279910451729223793 [3.117e28])
│ │ └─ ← [Revert] ERC20InsufficientBalance(0x2e234DAe75C793f67A35089C9d99245E1C58470b, 0, 31176204170279910451729223793 [3.117e28])
│ └─ ← [Revert] ERC20InsufficientBalance(0x2e234DAe75C793f67A35089C9d99245E1C58470b, 0, 31176204170279910451729223793 [3.117e28])
└─ ← [Revert] ERC20InsufficientBalance(0x2e234DAe75C793f67A35089C9d99245E1C58470b, 0, 31176204170279910451729223793 [3.117e28]) Looking forward to hearing from you |
Beta Was this translation helpful? Give feedback.
-
I am really confused, lol. Where is the below coming from? If you don't have [235] Handler::depositCollateral(0xDDeaa6F253cBb8855a7a7f17eddD32FE40b61b41, 115792089237316195423570985008687907853269984665640564039457584007913129639933 [1.157e77])
└─ ← [Revert] EvmError: Revert |
Beta Was this translation helpful? Give feedback.
-
@EngrPips , I completely understand how confusing this can be. The reason the transaction is failing is likely because the function is trying to interact with an address that either doesn’t exist or doesn’t have the function implemented. I’ve encountered this issue a few times myself. I know your question is likely related to the tokenAddress being passed into the constructor ?
like his case function _getCollateralFromSeed(
uint256 collateralSeed
) private view returns (ERC20Mock) {
if (collateralSeed % 2 == 0) {
return weth;
}
return wbtc;
}
ERC20Mock erc20MocK = _gerCollateralFreomSeed(seed);
console.log(address(erc20Mock));// A potential fix would be to clear the cache using
in his case would be return address instead of ERC20Mock in _getCollateralFromSeed function _getCollateralFromSeed(
uint256 collateralSeed
) private view returns (address) {
if (collateralSeed % 2 == 0) {
return collateralTokens[0];
}
return collateralTokens[1];
}
address erc20MocKAddress = _gerCollateralFreomSeed(seed);
console.log(address(erc20Mock)); |
Beta Was this translation helpful? Give feedback.
-
When I run my handler test it passes with 0 reverts, but when I set fail on revert to true, it throws an error : Ran 1 test for test/fuzz/Invariants.t.sol:Invariants
[FAIL. Reason: invariant_protocolMustHaveMoreValueThanTotalSupply persisted failure revert]
[Sequence]
sender=0x0000000000000000000000000000000000000dd9 addr=[test/fuzz/Handler.t.sol:Handler]0x2e234DAe75C793f67A35089C9d99245E1C58470b calldata=depositCollateral(address,uint256) args=[0xDDeaa6F253cBb8855a7a7f17eddD32FE40b61b41, 115792089237316195423570985008687907853269984665640564039457584007913129639933 [1.157e77]]
invariant_protocolMustHaveMoreValueThanTotalSupply() (runs: 1, calls: 1, reverts: 1)
Traces:
[10898465] Invariants::setUp()
├─ [4420818] → new DeployDSC@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
│ └─ ← [Return] 21968 bytes of code
├─ [5156465] DeployDSC::run()
│ ├─ [3336984] → new HelperConfig@0x104fBc016F4bb334D775a19E8A6510109AC63E00
│ │ ├─ [0] VM::startBroadcast()
│ │ │ └─ ← [Return]
│ │ ├─ [372455] → new MockV3Aggregator@0x90193C961A926261B756D1E5bb255e67ff9498A1
│ │ │ └─ ← [Return] 1083 bytes of code
│ │ ├─ [516543] → new ERC20Mock@0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496
│ │ │ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: DeployDSC: [0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f], value: 100000000000 [1e11])
│ │ │ └─ ← [Return] 2117 bytes of code
│ │ ├─ [372455] → new MockV3Aggregator@0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3
│ │ │ └─ ← [Return] 1083 bytes of code
│ │ ├─ [516543] → new ERC20Mock@0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76
│ │ │ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: DeployDSC: [0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f], value: 100000000000 [1e11])
│ │ │ └─ ← [Return] 2117 bytes of code
│ │ ├─ [0] VM::stopBroadcast()
│ │ │ └─ ← [Return]
│ │ └─ ← [Return] 6345 bytes of code
│ ├─ [912] HelperConfig::activeNetworkConfig() [staticcall]
│ │ └─ ← [Return] MockV3Aggregator: [0x90193C961A926261B756D1E5bb255e67ff9498A1], MockV3Aggregator: [0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3], ERC20Mock: [0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496], ERC20Mock: [0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76], 77814517325470205911140941194401928579557062014761831930645393041380819009408 [7.781e76]
│ ├─ [0] VM::startBroadcast()
│ │ └─ ← [Return]
│ ├─ [591465] → new DecentralizedStableCoin@0x5FbDB2315678afecb367f032d93F642f64180aa3
│ │ ├─ emit OwnershipTransferred(previousOwner: 0x0000000000000000000000000000000000000000, newOwner: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266)
│ │ └─ ← [Return] 2610 bytes of code
│ ├─ [987600] → new DSCEngine@0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
│ │ └─ ← [Return] 4255 bytes of code
│ ├─ [2424] DecentralizedStableCoin::transferOwnership(DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512])
│ │ ├─ emit OwnershipTransferred(previousOwner: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266, newOwner: DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512])
│ │ └─ ← [Stop]
│ ├─ [0] VM::stopBroadcast()
│ │ └─ ← [Return]
│ └─ ← [Return] DecentralizedStableCoin: [0x5FbDB2315678afecb367f032d93F642f64180aa3], DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], HelperConfig: [0x104fBc016F4bb334D775a19E8A6510109AC63E00]
├─ [912] HelperConfig::activeNetworkConfig() [staticcall]
│ └─ ← [Return] MockV3Aggregator: [0x90193C961A926261B756D1E5bb255e67ff9498A1], MockV3Aggregator: [0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3], ERC20Mock: [0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496], ERC20Mock: [0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76], 77814517325470205911140941194401928579557062014761831930645393041380819009408 [7.781e76]
├─ [1066148] → new Handler@0x2e234DAe75C793f67A35089C9d99245E1C58470b
│ ├─ [1291] DSCEngine::getCollateralTokens() [staticcall]
│ │ └─ ← [Return] [0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496, 0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76]
│ └─ ← [Return] 4646 bytes of code
└─ ← [Stop]
[235] Handler::depositCollateral(0xDDeaa6F253cBb8855a7a7f17eddD32FE40b61b41, 115792089237316195423570985008687907853269984665640564039457584007913129639933 [1.157e77])
└─ ← [Revert] EvmError: Revert
[63377] Invariants::invariant_protocolMustHaveMoreValueThanTotalSupply()
├─ [2349] DecentralizedStableCoin::totalSupply() [staticcall]
│ └─ ← [Return] 0
├─ [2607] ERC20Mock::balanceOf(DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512]) [staticcall]
│ └─ ← [Return] 0
├─ [2607] ERC20Mock::balanceOf(DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512]) [staticcall]
│ └─ ← [Return] 0
├─ [15045] DSCEngine::getUsdValue(ERC20Mock: [0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496], 0) [staticcall]
│ ├─ [8993] MockV3Aggregator::latestRoundData() [staticcall]
│ │ └─ ← [Return] 1, 200000000000 [2e11], 1, 1, 1
│ └─ ← [Return] 0
├─ [15045] DSCEngine::getUsdValue(ERC20Mock: [0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76], 0) [staticcall]
│ ├─ [8993] MockV3Aggregator::latestRoundData() [staticcall]
│ │ └─ ← [Return] 1, 100000000000 [1e11], 1, 1, 1
│ └─ ← [Return] 0
├─ [0] console::log("weth value: ", 0) [staticcall]
│ └─ ← [Stop]
├─ [0] console::log("wbtc value: ", 0) [staticcall]
│ └─ ← [Stop]
├─ [0] console::log("Total Supply: ", 0) [staticcall]
│ └─ ← [Stop]
└─ ← [Stop]
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 6.05ms (1.44ms CPU time)
The test:
`function invariant_protocolMustHaveMoreValueThanTotalSupply() public view {
// get the value of the allthe ollateral in the protocol
// compare it to all the debt(dsc)
uint256 totalSupply = dsc.totalSupply();
uint256 totalWethDeposited = IERC20(weth).balanceOf(address(dsce));
uint256 totalWBtcDeposited = IERC20(wbtc).balanceOf(address(dsce));
import {Test} from "forge-std/Test.sol";
import {DSCEngine} from "../../src/DSCEngine.sol";
import {DecentralizedStableCoin} from "../../src/DecentralizedStableCoin.sol";
import {ERC20Mock} from "@openzeppelin/contracts/mocks/token/ERC20Mock.sol";
contract Handler is Test {
DSCEngine dsce;
DecentralizedStableCoin dsc;
}`
Beta Was this translation helpful? Give feedback.
All reactions