Usage of Chainlink getHistoricalData() in Solidity. What is the roundID?

Viewed 304

I am currently trying to use the function getRoundData() from Chainlink within a contract of mine. The function expects the parameter roundID (uint80) when called. I am trying to implement a getHistoricalData() function, described in this tutorial:

https://docs.chain.link/docs/historical-price-data/

My question is: What is the roundID? Where do I find those IDs?

Here is some test code I wrote:

 // SPDX-License-Identifier: MIT 

pragma solidity ^0.8.0;

import '@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol';

contract TestContract {

    AggregatorV3Interface internal priceFeed;

    constructor() {
        
        // PriceFeed on Kovan Test Net
        priceFeed = AggregatorV3Interface(0x22B58f1EbEDfCA50feF632bD73368b2FdA96D541);
        
    }


    /**
     * Returns historical price for a round id.
     * roundId is NOT incremental. Not all roundIds are valid.
     * You must know a valid roundId before consuming historical data.
     *
     * ROUNDID VALUES:
     *    InValid:      18446744073709562300
     *    Valid:        18446744073709562301
     *    
     * @dev A timestamp with zero value means the round is not complete and should not be used.
     */
    function getHistoricalPrice(uint80 roundId) public view returns (int256) {
        (
            , 
            int price,
            ,
            uint timeStamp,
        ) = priceFeed.getRoundData(roundId);
        require(timeStamp > 0, "Round not complete");
        return price;
    }
}

When I call the function with the roundID = 18446744073709562301, the following error-message pops up:

call to TestContract.getHistoricalPrice errored: VM execution error.
Reverted 0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000f4e6f20646174612070726573656e740000000000000000000000000000000000
1 Answers

As of right now, you need to must know the roundId yourself. You could do this by observing the rounds as they are submitted on-chain and recording the roundId's (this could be recorded off-chain or on-chain, although on-chain may get expensive). This process may be made easier in the future.

The error message is due to the wrong feed address being pointed to in the constructor. Try this line instead:

priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);

Reference: Historical Price Data Chainlink Docs

Related