Suppose that in my solidity contract, I need to see the price of ETH in terms of USD at a specific time, say on January 1, 2021. I would like to use the Historical Price Data Feed from chainlink (URL below). I imagine that to achieve this, I would have to pass the roundId as a parameter when calling the outermost function, so that the contract can verify that this roundId indeed gives the price corresponding to the timestamp of January 1, 2021. I am happy with this construction. However, I do not entirely understand the data given back. How do I verify that the roundId indeed corresponds to the timestamp of interest?

URL to the documentation: https://docs.chain.link/docs/historical-price-data/


function latestRoundData() external view 
    returns (
        uint80 roundId, 
        int256 answer, 
        uint256 startedAt, 
        uint256 updatedAt, 
        uint80 answeredInRound

Chainlink explanation with these return arguments:

  • roundId: The round ID.
  • answer: The price.
  • startedAt: Timestamp of when the round started.
  • updatedAt: Timestamp of when the round was updated.
  • answeredInRound: The round ID of the round in which the answer was computed.

The startedAt and updatedAt values returned will give the time (unix epoch) that the round was started and submitted. You can compare this to your 'Jan 1 2021' value

  • Ok, so how can I verify that a roundId corresponds to the given timestamp? Also, how do I find the correct roundId off-chain? Jul 8 at 11:13
  • Obviously it is very unlikely that a roundId timestamp corresponds exactly to the date of interest... Jul 8 at 11:14
  • it's hard to find the roundId that corresponds to a given timestamp unless you do a lot of iterating off chain. But unfortunately the way the data is stored on-chain, it's not easy to just plug in a date/time and get a price back. There should be a blog post coming out in the next few weeks that addresses how to solve this exact issue. Jul 29 at 2:30
  • Thanks a lot for taking the time to respond. In fact, we would be happy with an off-chain solution such as the one you describe. How would this work exactly? How can we find the correct roundId corresponding to a timestamp, and how can we verify correctness on-chain? Jul 30 at 11:45
  • I'll be working on exactly this later this week, I have a couple of rough ideas in my head, but don't want to respond unless I know they work. Will response back here once I've got a basic example working Aug 2 at 1:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.