简体   繁体   English

如何以小数形式从 Chainlink 数据馈送中获取最终值?

[英]How to get the final value from Chainlink Data feeds in decimals?

Was writing a NFT contract where to mint a single token requires ETH equivalent to 40000 USD.正在编写一个 NFT 合约,其中铸造单个代币需要相当于 40000 美元的 ETH。 So figured out to use ChainLink data feeds for conversion.所以想出了使用 ChainLink 数据源进行转换。 Apparently they return the value in whole numbers which is not required.显然他们返回不需要的整数值。

Here's the code这是代码

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

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

contract PriceConsumerV3 {

    AggregatorV3Interface internal priceFeed;

    /**
     * Network: Rinkeby
     * Aggregator: ETH/USD
     * Address: 0x8A753747A1Fa494EC906cE90E9f37563A8AF630e
     */
    constructor() {
        priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
    }

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

}

Here is the value that I received when tested on Remix when I did not divide it by the decimals.这是我在 Remix 上测试时未除以小数时收到的值。 Result1结果1

Here is the value that I received when tested on Remix when I divide it by the decimals.这是我在 Remix 上测试时除以小数时收到的值。 Result2结果2

How do I get 3165.27730535 here.我如何在这里获得 3165.27730535。

From latest updated documentation:来自最新更新的文档:

Fixed point numbers are not fully supported by Solidity yet. Solidity 尚未完全支持定点数。 They can be declared, but cannot be assigned to or from.它们可以声明,但不能分配给或分配自。

Said this, you can convert the uint to string and try to handle this types like floating point variables.说到这里,您可以将 uint 转换为字符串,并尝试像处理浮点变量一样处理这种类型。 You can see more abou this operation here .您可以在此处查看有关此操作的更多信息。

I don't see any advantage in converting it to decimal.我没有看到将其转换为十进制有任何优势。 If you need to display it in decimal on the front-end just convert it there.如果您需要在前端以十进制显示它,只需在那里转换它。

Otherwise you can search some library on google (like this one )否则你可以在谷歌上搜索一些图书馆(比如这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM