3

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

我已经阅读了本指南并成功部署了提供的智能合约模板:

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 按钮时,该货币对的价格就会立即更新,我错了,第一次点击后价格竟然“冻结”了。

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

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

TypeError:返回参数类型 uint256 不能隐式转换为预期类型(第一个返回变量的类型)int256。返回时间戳;^--------^

因此,出于好奇,如何将 uint256 变量转换为 int256 变量,以便获取每个更新价格的时间戳(每次单击getLatestPrice button)?

谢谢阅读

4

2 回答 2

3

价格馈送合约实际上是由一组链节点独立更新的,而您的合约只是从该合约中读取。

当你打电话时getLatestPrice,它实际上只是从该合同中读取。

此外,价格馈送合同会根据某些阈值和偏差进行更新。尤其是在测试网上,它们的更新非常零星。


如果您可以为您提出一个单独的问题,TypeError那将是理想的,谢谢!

于 2021-07-17T13:45:38.370 回答
2

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

这是一个示例,如果您想查看该函数是否被调用

// 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();
        
    }
}
于 2021-07-17T09:47:47.113 回答