1

Similar to this question How to get all the historical data from chainlink price feeds? I would like to retrieve the historical data of a particular price feed to be used off-chain for a large number of consecutive updates. However, according to the docs it seems like the round IDs are no longer incremental (i.e. you can't just call getRoundData(latest_round-_step) for steps in a certain range).

Is the solution to simply do a linear search backwards and try all entries smaller than the latest roundID, or is there a recursive way to achieve this right now (is the previous round ID saved somewhere)?

Patrick Collins
  • 11,186
  • 5
  • 44
  • 97
faihu
  • 11
  • 1

3 Answers3

0

Is the solution to simply do a linear search backwards and try all entries smaller than the latest roundID

Basically, the answer to this is yes.

A better way would be to use the Graph to index the data feeds and then pull from the graph.

Patrick Collins
  • 11,186
  • 5
  • 44
  • 97
0

The roundId is calculated from the phase and aggregatorRoundId:

function addPhase(uint16 _phase, uint64 _originalId) internal pure returns (uint80)
{
    return uint80(uint256(_phase) << PHASE_OFFSET | _originalId);
}

Since we know how the roundId is generated we can move through only the valid ones, no searching required.

I made a library for getting the next() and prev() roundId from a given roundId. You can check it out here https://github.com/JonahGroendal/chainlink-round-id-calc/blob/master/contracts/ChainlinkRoundIdCalc.sol

Jonah
  • 655
  • 1
  • 7
  • 17
0

Update September 2022

You can now easily view historical price data by using checkthechain.

Below is a minimal working example using the Python package:

from ctc.protocols import chainlink_utils

feed = '0x31e0a88fecb6ec0a411dbe0e9e76391498296ee9'

data = await chainlink_utils.async_get_feed_data(feed)

Alternatively, you can also use their API.

Markus Schick
  • 1,228
  • 3
  • 15