问题描述
从文档中,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)..用小数格式化并去掉不需要的小数位?
解决方法
想通了。 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);
}