首页 > 解决方案 > 任何人都知道为什么 Chainlink 的 PriceFeed 以“int”类型返回价格值,而价格应该始终 >= 0?

问题描述

在 PriceFeed 中获取最新价格的代码是:


pragma solidity ^0.6.7;

import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";

contract PriceConsumerV3 {

    AggregatorV3Interface internal priceFeed;

    /**
     * Network: Kovan
     * Aggregator: ETH/USD
     * Address: 0x9326BFA02ADD2366b30bacB125260Af641031331
     */
    constructor() public {
        priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);
    }

    /**
     * Returns the latest price
     */
    function getThePrice() public view returns (int) {
        (
            uint80 roundID, 
            int price,
            uint startedAt,
            uint timeStamp,
            uint80 answeredInRound
        ) = priceFeed.latestRoundData();
        return price;
    }
}

请注意,在函数getThePrice、行int price中,为什么 Chainlinkint在价格上使用类型?为什么不直接uint打字?是否有可能从 Chainlink PriceFeed 获得负价格?

标签: chainlink

解决方案


Chainlink 数据馈送使用int而不是uint因为某些价格可能是负数,例如当石油期货跌至 0 以下时。


推荐阅读