如何从latestrounddata()转换返回值?

发布于 2025-01-28 18:23:06 字数 1019 浏览 4 评论 0原文

我一直在遵循有关链链接文档的教程,以获取使用Matic/USD Mumbai TestNet数据feed的Matic的当前价格,函数getLatestPrice()返回65990700。我读到latestrounddata()返回WEI中的值。然后,当我使用此网站对其进行转换时值得matic。我得到了0.0000000065485509 Matic。

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract PriceConsumerV3 {

    AggregatorV3Interface internal priceFeed;

    /**
     * Network: Mumbai
     * Aggregator: MATIC/USD
     * Address: 0xd0D5e3DB44DE05E9F294BB0a3bEEaF030DE24Ada
     */
    constructor() {
        priceFeed = AggregatorV3Interface(0xd0D5e3DB44DE05E9F294BB0a3bEEaF030DE24Ada);
    }

    /**
     * Returns the latest price
     */
    function getLatestPrice() public view returns (int) {
        (
            /*uint80 roundID*/,
            int price,
            /*uint startedAt*/,
            /*uint timeStamp*/,
            /*uint80 answeredInRound*/
        ) = priceFeed.latestRoundData();
        return price;
    }
}

我想念什么吗?

I've been following a tutorial on chainlink documentation, to get the current price of Matic using MATIC/USD Mumbai Testnet Data feed, the function getLatestPrice() returns 65990700. I read that latestRoundData() returns the value in Wei. Then when I converted it using this website https://polygonscan.com/unitconverter to see how much this value is worth of Matic. I got 0.000000000065485509 Matic.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract PriceConsumerV3 {

    AggregatorV3Interface internal priceFeed;

    /**
     * Network: Mumbai
     * Aggregator: MATIC/USD
     * Address: 0xd0D5e3DB44DE05E9F294BB0a3bEEaF030DE24Ada
     */
    constructor() {
        priceFeed = AggregatorV3Interface(0xd0D5e3DB44DE05E9F294BB0a3bEEaF030DE24Ada);
    }

    /**
     * Returns the latest price
     */
    function getLatestPrice() public view returns (int) {
        (
            /*uint80 roundID*/,
            int price,
            /*uint startedAt*/,
            /*uint timeStamp*/,
            /*uint80 answeredInRound*/
        ) = priceFeed.latestRoundData();
        return price;
    }
}

am I missing something ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2025-02-04 18:23:06

ChainLink USD DataFeeds以8个小数的精度返回价格数据,而不是18。

如果要将值转换为18个小数,则可以将10个零添加到结果:

price * 1e10

请参阅指定的 Decimals 函数在指定的< a href =“ https://mumbai.polygonscan.com/address/0xd0d5e3db44de05e9f294bb0a3beeaf030de24ada#readcontract”

Chainlink USD datafeeds return price data with 8 decimals precision, not 18.

If you want to convert the value to 18 decimals, you can add 10 zeros to the result:

price * 1e10

See the decimals function output on the specified feed contract.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文