【问题标题】:How can I make the data provided by some ChainLink aggregator get updated every time I click on my "getLatestPrice" button?每次单击“getLatestPrice”按钮时,如何使某些 ChainLink 聚合器提供的数据得到更新?
【发布时间】:2021-09-25 18:29:56
【问题描述】:

这是我第一次在 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:返回参数类型 uint256 不能隐式转换为预期类型(第一个返回变量的类型)int256。返回时间戳; ^-------^

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

感谢阅读

【问题讨论】:

  • 通常情况下,您希望每个 stackoverflow 问题有 1 个问题。你能再为你的第二个问题再做一个吗?
  • 好的,@PatrickCollins 先生,我刚刚成功,在这里:stackoverflow.com/questions/68422733/…
  • 可爱,谢谢!

标签: javascript solidity smartcontracts chainlink


【解决方案1】:

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

当您调用 getLatestPrice 时,它实际上只是从该合约中读取数据。

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


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

【讨论】:

  • 我们能否触发这些节点比常规更新更快地更新价格,例如通过支付链接币?否则,我们是否应该运行一个节点来自己更新价格?
  • 如果您想自己触发更新,是的,您会想运行自己的价格馈送。这是一项艰巨的任务。
【解决方案2】:

Testnet 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();
        
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    相关资源
    最近更新 更多