首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >maticPriceInUSD返回零,为什么,我试图使用链接来实时计算价格。

maticPriceInUSD返回零,为什么,我试图使用链接来实时计算价格。
EN

Ethereum用户
提问于 2023-04-07 22:28:31
回答 1查看 19关注 0票数 1
代码语言:javascript
复制
function getMaticAmountAndPrice(uint256 tokensToBuy, address priceFeed) public view returns (uint256, uint256) {
        // get the latest MATIC price in USD
        AggregatorV3Interface aggregator = AggregatorV3Interface(priceFeed);
        (, int256 price, , , ) = aggregator.latestRoundData();
        require(block.timestamp >= 10, "Price feed not ready");
        uint256 maticPriceInUSD = uint256(price);

        // get the total cost in MATIC for the desired amount of tokens
        uint256 costInWei = tokenPrice * tokensToBuy;
        uint256 costInMatic = costInWei / 1 ether;

        // calculate the equivalent cost in USD based on the current MATIC price
        uint256 costInUSD = costInMatic * maticPriceInUSD / 1e18;
        return (costInMatic, costInUSD);
    }
EN

回答 1

Ethereum用户

发布于 2023-04-08 03:14:36

根据所提供的信息,为了解决问题,您应该检查两件事:

  1. 确保您正在使用正确的地址,马季奇的价格饲料。
  2. 确保您的合同部署在正确的网络上。也就是说,如果您使用多边形测试网的价格提要地址,则需要在该网络上部署契约以获得正确的数据。我想提到的是,如果您想要这样做的话,在本地Remix网络上直接测试价格提要是不可能的。

如果您能够共享整个合同,并提供您正在测试的环境的一些细节,这样我就可以提供更精确的建议,这将是有帮助的。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/148668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档