首页 > 解决方案 > 格式化 Chainlink AggregatorV3Interface 对美元的“答案”?

问题描述

从文档中, AggregatorV3Interface 返回..

Return Values
roundId: The round ID.
answer: The price.
startedAt: Timestamp of when the round started.
updatedAt: Timestamp of when the round was updated.
answeredInRound: The round ID of the round in which the answer
was computed.

也有人说这answer是一个int256。

我正在恢复价值120160203575

这是以美元为单位的8。

你如何将这个 unit8 格式化成美元格式的数字?(例如1201.00 美元).. 用小数格式化并去掉不需要的小数位?

标签: cryptographyblockchainethereumchainlink

解决方案


弄清楚了。Chainlink 可以decimals连同答案一起返回。

 function getLatestPrice() public view returns (int, uint8) {
    (
      uint80 roundID,
      int price,
      uint startedAt,
      uint timeStamp,
      uint80 answeredInRound
  ) = priceFeed.latestRoundData();

    uint8 decimals = priceFeed.decimals();
    return (price, decimals);
  }

推荐阅读