如何从小数链链接数据提要中获取最终值?
正在写一份NFT合同,在其中造成单个令牌需要ETH相当于40000美元。因此,发现使用链链链接数据提要进行转换。显然,他们以不需要的整数返回值。
这是这里的代码
// 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;
}
}
,当我在混音中测试时收到的价值不将其除以小数。 result1
这是当我用小数分配remix时,我在remix上进行测试时收到的值。 result2
我如何在这里获得3165.27730535。
Was writing a NFT contract where to mint a single token requires ETH equivalent to 40000 USD. So figured out to use ChainLink data feeds for conversion. 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.
Result1
Here is the value that I received when tested on Remix when I divide it by the decimals.
Result2
How do I get 3165.27730535 here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从最新的更新文档中:
这样说,您可以将UINT转换为字符串,并尝试处理此类类型,例如浮点变量。
您可以看到更多此操作在这里。
From latest updated documentation:
Said this, you can convert the uint to string and try to handle this types like floating point variables.
You can see more abou this operation here.
我认为将其转换为十进制方面没有任何优势。如果您需要在前端以十进制显示它,则只需将其转换在那里。
否则,您可以在Google上搜索一些库(例如这个)
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)