每次单击 "getLatestPrice" 按钮时,如何更新某些 ChainLink 聚合器提供的数据?

How can I make the data provided by some ChainLink aggregator get updated every time I click on my "getLatestPrice" button?

这是我第一次在 Remix 上部署合约以及学习如何在 Solidity 上编码。

我已经阅读了这篇文章guide并成功部署了提供的智能合约模板:

pragma solidity ^0.6.7;

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

contract PriceConsumerV3 {

AggregatorV3Interface internal priceFeed;

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

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

然而,我认为在部署上面的模板后,每当我点击 getLatestPrice button 这样的货币对的价格会立即更新,我大错特错,价格实际上被“冻结”了第一次点击后。

所以,我想知道在上面的模板中必须输入什么才能实现该目标

此外,我尝试通过在 return price; 下方键入 return timeStamp; 来打印 timeStamp,但是在编译时,Remix 编译器回复:

TypeError: Return argument type uint256 is not implicitly convertible to expected type (type of first return variable) int256. return timeStamp; ^-------^

所以,出于好奇,我如何将 uint256 变量转换为 int256 变量以获得每个更新价格的时间戳(每次我点击 getLatestPrice button)?

感谢阅读

测试网价格源仅偶尔更新(因此它们不会每秒甚至每分钟更新价格或时间)

如果您想查看正在调用的函数,请看这里的示例

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.6;
pragma experimental ABIEncoderV2;

//To run on remix use Injected Web3 with Metamask on Rinkeby network activated

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

contract ChainlinkPriceFeed {
    
    int public countButtonClicks;
    int public kovanEthPrice;
    uint public kovanTimestamp;
    uint80 public kovanRoundID;
    
    /**
     * Network: Kovan
     * Aggregator: ETH/USD
     */
    constructor() public {
        countButtonClicks = 0;
        kovanEthPrice = -1;
        kovanTimestamp = 0;
        kovanRoundID = 0;
        // Examples -> Rinkeby network 
        // See https://docs.chain.link/docs/reference-contracts/ for available feeds and blockchains
        // priceData["ETHUSD"] = 0x8A753747A1Fa494EC906cE90E9f37563A8AF630e;
        // priceData["BTCUSD"] = 0xECe365B379E1dD183B20fc5f022230C044d51404;
        // priceData["LINKUSD"] = 0xd8bD0a1cB028a31AA859A21A3758685a95dE4623;
        // priceData["AUDUSD"]= 0x21c095d2aDa464A294956eA058077F14F66535af;
        //Examples -> Kovan Netowrk see https://docs.chain.link/docs/ethereum-addresses/
    }

    /**
     * Returns the latest price information from the asset address
     */
    function getLatestPrice(address assetAddress) public view returns 
    (int price, uint80 roundID, int decimals, string memory description, uint timestamp) 
    {
        AggregatorV3Interface priceFeed = AggregatorV3Interface(assetAddress);
        (            
            roundID, 
            price,
            uint startedAt,
            timeStamp,
            uint80 answeredInRound
        ) = priceFeed.latestRoundData();
        description = priceFeed.description();
        decimals = priceFeed.decimals();

        return (price, roundID, decimals, description, timestamp);
    }
    
    function getKovanEthPrice() public view returns (int price, uint timeStamp, uint80 roundID) {
        AggregatorV3Interface priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);
        (            
            uint80 roundID, 
            int price,
            uint startedAt,
            uint timeStamp,
            uint80 answeredInRound
        ) = priceFeed.latestRoundData();
        
        return (price, timeStamp, roundID);
    }
    
    function counterKovanEthPrice() public {
        countButtonClicks = countButtonClicks+1;
        (kovanEthPrice, kovanTimestamp, kovanRoundID) = getKovanEthPrice();
        
    }
}

喂价合约实际上是由一组chainlink节点独立更新的,您的合约只是从该合约中读取。

当您调用 getLatestPrice 时,它实际上只是在读取该合同。

此外,喂价合约会根据某些阈值和偏差进行更新。特别是在 testnet 上,它们的更新非常零星。


如果你能为你的 TypeError 提出一个单独的问题,那将是最理想的,谢谢!