[英]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.