Cyfrin / foundry-defi-stablecoin-cu

250 stars 117 forks source link

DSCEngine Revert #47

Closed justin-moss-swd closed 9 months ago

justin-moss-swd commented 1 year ago

I am having an issue with a function call causing a revert. I have copied exactly down what was in the tutorials and the repo:

  import {DecentralizedStableCoin} from "./DecentralizedStableCoin.sol";
  import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
  import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
  import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

  contract DSCEngine is ReentrancyGuard{
      function getUsdValue(address token, uint256 amount) public view returns (uint256) {
          AggregatorV3Interface priceFeed = AggregatorV3Interface(s_priceFeeds[token]);
          (, int256 price,,,) = priceFeed.latestRoundData();
          //return ((uint256(price) * ADDITIONAL_FEED_PRECISION) * amount) / PRECISION;
      }
  }
  import {Test, console} from "forge-std/Test.sol";
  import {DeployDSC} from "../../script/DeployDSC.s.sol";
  import {DecentralizedStableCoin} from "../../src/DecentralizedStableCoin.sol";
  import {DSCEngine} from "../../src/DSCEngine.sol";
  import {HelperConfig} from "../../script/HelperConfig.s.sol";

  contract DSCEngineTest is Test{
      DeployDSC deployer;
      DecentralizedStableCoin dsc;
      DSCEngine engine;
      HelperConfig config;
      address ethUsdPriceFeed;
      address weth;

      function setUp() public {
          deployer = new DeployDSC();
          (dsc, engine, config) = deployer.run();
          (ethUsdPriceFeed, , weth, , ) = config.activeNetworkConfig();
      }

      function testGetUsdValue() public {
          uint256 ethAmount = 15e18;
          // 15e18 ETH * $2000/ETH = $30,000e18
          uint256 expectedUsd = 15000e18;
          uint256 usdValue = engine.getUsdValue(weth, ethAmount);
          //assertEq(usdValue, expectedUsd);
      }
  }

When the engine test "testGetUsdValue()" and it calls engine.getUsdValue it reverts but gives no reason as to why even when I put in a million -vvvvvvv during compile. I just get this:

Running 1 test for test/unit/DSCEngineTest.t.sol:DSCEngineTest [FAIL. Reason: EvmError: Revert] testGetUsdValue() (gas: 12655) Traces: [9804687] DSCEngineTest::setUp() ├─ [4418018] → new DeployDSC@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f │ └─ ← 21954 bytes of code ├─ [5214147] DeployDSC::run() │ ├─ [3744733] → new HelperConfig@0x104fBc016F4bb334D775a19E8A6510109AC63E00 │ │ ├─ [0] VM::startBroadcast() │ │ │ └─ ← () │ │ ├─ [372255] → new MockV3Aggregator@0x90193C961A926261B756D1E5bb255e67ff9498A1 │ │ │ └─ ← 1082 bytes of code │ │ ├─ [658696] → new ERC20Mock@0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496 │ │ │ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: DeployDSC: [0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f], value: 200000000000 [2e11]) │ │ │ └─ ← 2828 bytes of code │ │ ├─ [372255] → new MockV3Aggregator@0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3 │ │ │ └─ ← 1082 bytes of code │ │ ├─ [658696] → new ERC20Mock@0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76 │ │ │ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: DeployDSC: [0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f], value: 100000000000 [1e11]) │ │ │ └─ ← 2828 bytes of code │ │ ├─ [0] VM::stopBroadcast() │ │ │ └─ ← () │ │ └─ ← 6963 bytes of code │ ├─ [912] HelperConfig::activeNetworkConfig() [staticcall] │ │ └─ ← MockV3Aggregator: [0x90193C961A926261B756D1E5bb255e67ff9498A1], MockV3Aggregator: [0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3], ERC20Mock: [0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496], ERC20Mock: [0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76], 77814517325470205911140941194401928579557062014761831930645393041380819009408 [7.781e76] │ ├─ [0] VM::startBroadcast() │ │ └─ ← () │ ├─ [749823] → new DecentralizedStableCoin@0x5FbDB2315678afecb367f032d93F642f64180aa3 │ │ ├─ emit OwnershipTransferred(previousOwner: 0x0000000000000000000000000000000000000000, newOwner: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266) │ │ └─ ← 3401 bytes of code │ ├─ [478954] → new DSCEngine@0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 │ │ └─ ← 1936 bytes of code │ ├─ [2446] DecentralizedStableCoin::transferOwnership(DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512]) │ │ ├─ emit OwnershipTransferred(previousOwner: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266, newOwner: DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512]) │ │ └─ ← () │ ├─ [0] VM::stopBroadcast() │ │ └─ ← () │ └─ ← DecentralizedStableCoin: [0x5FbDB2315678afecb367f032d93F642f64180aa3], DSCEngine: [0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512], HelperConfig: [0x104fBc016F4bb334D775a19E8A6510109AC63E00] ├─ [912] HelperConfig::activeNetworkConfig() [staticcall] │ └─ ← MockV3Aggregator: [0x90193C961A926261B756D1E5bb255e67ff9498A1], MockV3Aggregator: [0xBb2180ebd78ce97360503434eD37fcf4a1Df61c3], ERC20Mock: [0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496], ERC20Mock: [0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76], 77814517325470205911140941194401928579557062014761831930645393041380819009408 [7.781e76] └─ ← ()

[12655] DSCEngineTest::testGetUsdValue() ├─ [5457] DSCEngine::getUsdValue(ERC20Mock: [0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496], 15000000000000000000 [1.5e19]) [staticcall] │ ├─ [0] 0x0000000000000000000000000000000000000000::latestRoundData() [staticcall] │ │ └─ ← () │ └─ ← "EvmError: Revert" └─ ← "EvmError: Revert"

Test result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 3.18ms Ran 1 test suites: 0 tests passed, 1 failed, 0 skipped (1 total tests)

Failing tests: Encountered 1 failing test in test/unit/DSCEngineTest.t.sol:DSCEngineTest [FAIL. Reason: EvmError: Revert] testGetUsdValue() (gas: 12655)

I am really confused because I have another project with the same code and it works. What could cause the revert?

Blngogo commented 1 year ago

i think the expected usd should be 30000e18, as you calculated. But you declared it as 15000e18.

usmanfarooq91 commented 1 year ago
[12655] DSCEngineTest::testGetUsdValue()
├─ [5457] DSCEngine::getUsdValue(ERC20Mock: [0xA8452Ec99ce0C64f20701dB7dD3abDb607c00496], 15000000000000000000 [1.5e19]) [staticcall]
│ ├─ [0] 0x0000000000000000000000000000000000000000::latestRoundData() [staticcall]
│ │ └─ ← ()
│ └─ ← "EvmError: Revert"
└─ ← "EvmError: Revert"

Here you can see latestRoundData() function is being called by a zero address which is why you are getting "EvmError: Revert" error.

Make sure you are initializing the s_priceFeeds mapping correctly with the right token to price feed address pair.

If you need further help please post your issue on the main discussion forum, we will be happy to help.

PatrickAlphaC commented 9 months ago

Closing due to stale, thanks all