Cyfrin Updraft.

帖子

分享您的知识。

Cyfrin Moderator Pst.
Mar 29, 2024
专家问答

What's the Role of MockV3Aggregator in FundMe Contrct?

In our project, we have utilized the code from the Foundry fund me project on GitHub, specifically involving the MockV3Aggregator. When testing the PriceFeedVersionIsAccurate() function in the test script, it seems to be calling the getVersion() function in FundMe.sol, which in turn references s_priceFeed. s_priceFeed is defined as AggregatorV3Interface private s_priceFeed; and is imported from AggregatorV3Interface.sol using import {AggregatorV3Interface} from '@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol';.

My question pertains to how HelperConfig.s.sol resolves the issue and whether s_priceFeed is actually MockV3Aggregator instead of AggregatorV3Interface when working with Anvil. Any insights or clarifications on this matter would be greatly appreciated. Thank you for your attention to this query.

When we test with the PriceFeedVersionIsAccurate() function in the test script.

function testPriceFeedVersionIsAccurate() public {
    // This is a unit test
    uint256 version = fundMe.getVersion();
    assertEq(version, 4);
}

As of my understanding, it is actually call the following function in FundMe.sol

function getVersion() public view returns (uint256) {
    return s_priceFeed.version();
}
  • Foundry
  • Solidity
0
1
分享
评论
.

答案

1
Cyfrin Moderator Ans.
Mar 29 2024, 09:16

The s_price_feed storage variable is initialized within the constructor function at line 48 in the FundMe.sol contract.

constructor(address priceFeed) {
    s_priceFeed = AggregatorV3Interface(priceFeed);
    i_owner = msg.sender;
}

The constructor function expects the priceFeed address, which, in the case of Anvil (deploying locally), corresponds to the address of the MockV3Aggregator.

Subsequently, the HelperConfig.s.sol file plays a crucial role by both deploying and providing the address for the MockV3Aggregator when deploying the FundMe contract using the DeployScript.s.sol.

0
官方答案
评论
.

你知道答案吗?

请登录并分享。

Cyfrin Updraft is an education platform specializing on teaching the next generation of smart contract developers

200帖子201答案
Sui.X.Peera.

赚取你的 1000 Sui 份额

获取声誉积分,并因帮助 Sui 社区成长而获得奖励。