0

I want to check the price at a previous timestamp, and I see I can use the getHistoricalPrice function .

     * 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) {
        (
            uint80 id, 
            int price,
            uint startedAt,
            uint timeStamp,
            uint80 answeredInRound
        ) = priceFeed.getRoundData(roundId);
        require(timeStamp > 0, "Round not complete");
        return price;
    }

How do I determine the roundId of the timestamp I want to query? (sources https://docs.chain.link/docs/historical-price-data/)

1 Answer 1

1

Deploying an onchain iterator would be the fastest solution here, but is this for a smart contract or py/js script? Truth be told, there's no real way to do what you're looking for in a timely or cheap manner. Whatever the case, you would need to iterate the rounds backwards until you get your timestamp or close enough.

10
  • This is for a smart contract. Will try to use the manual implementation, that iterates backwards at least for now before I find something else, hopefully.
    – HallonDev
    Mar 10, 2022 at 17:16
  • 1
    Don't just blindly try to check the chainlink contract itself. Check the phaseAggregators(phaseId) public mapping to get a collection of addresses (where the data is stored). the highest you can go is phaseId(). These aggregators also have their own roundId mappings... so depending on how many phases there are, you are in for a DYNAMIC amount of memory consumption... This bytecode will be large and expensive. Good luck.
    – CAT6
    Mar 11, 2022 at 19:14
  • Hey, came across a useful project whose implementation I have borrowed. I was having a hard time wrapping my head around phases. I understand the implementation up to function roundIdsToSearch; then things start getting complicated. Am trying to think about how to adapt function roundIdsToSearch, since the data am collecting is not likely to extend beyond a single day. How to treat daysToFetch and dataPointsToFetchPerDay parameters.
    – HallonDev
    Mar 21, 2022 at 17:32
  • I am doing something a bit different. I need to get historical prices: microwindow(5m) and macroWindow(1h), from current block.timestamp. Here is my project, and my implementation is under function _fetch github.com/VictorONN/OverlayChainlinkfeed/blob/master/Chainlink/… If you get some time to have a look at this I would appreciate thanks.
    – HallonDev
    Mar 21, 2022 at 17:32
  • 1
    I haven't actually peeked at it, but I'm pretty sure Yearn has what you are after.
    – CAT6
    Mar 24, 2022 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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