[英]Is there a simpler way to get decentralized data from Chainlink than doing separate multiple API calls through nodes?
[英]How to get the final value from Chainlink Data feeds in decimals?
正在编写一个 NFT 合约,其中铸造单个代币需要相当于 40000 美元的 ETH。 所以想出了使用 ChainLink 数据源进行转换。 显然他们返回不需要的整数值。
这是代码
// 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;
}
}
这是我在 Remix 上测试时未除以小数时收到的值。 结果1
这是我在 Remix 上测试时除以小数时收到的值。 结果2
我如何在这里获得 3165.27730535。
来自最新更新的文档:
Solidity 尚未完全支持定点数。 它们可以声明,但不能分配给或分配自。
说到这里,您可以将 uint 转换为字符串,并尝试像处理浮点变量一样处理这种类型。 您可以在此处查看有关此操作的更多信息。
我没有看到将其转换为十进制有任何优势。 如果您需要在前端以十进制显示它,只需在那里转换它。
否则你可以在谷歌上搜索一些图书馆(比如这个)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.