2

I'm looking to get my hands on a historical block -> timestamp mapping. Although it's possible to get this data using web3.eth.getBlock it's rather costly to do it for all individual blocks.

Since this was likely done thousands of times before, there must (hopium) likely be some open source csv, 3rd party api, or something that supplies this data in an easier way.

Anyone?

2 Answers 2

1

1. Batch requests

You can send multiple eth_getBlockByNumber queries in one Batch request - which means you could include e.g. 1000 eth_getBlockByNumber in one request.

Some node providers on a free plan limit the number of requests in a single batch or the number of returned results.

Hier the web3js simplified implementation with dequanto library: Web3Client.ts#L256 and the usage would be:

const from = 10_000;
const end = 50_000;
const nrs = Array.from(new Array(end - start)).map((_, i) => start + i);

const client = new EthWeb3Client();
const blocks = await client.getBlocks(nrs);
const times = blocks.map(b => [ b.number, b.timestamp ]);

2. Approximation

If you don't need a seconds-perfect precision, you could use an average block time, so you could pick a portion of block numbers from the range, make the query, and later calculate the avg block time and the expected timestamps for other block numbers. Sample implementation: ContractService.ts#L361

2
0

You could use Infura's API. Specifically, you should look into the "getBlockByNumber" section. You will need to register a new free account and get an API key to be able to use the endpoint.

Example query from the docs:

curl https://mainnet.infura.io/v3/YOUR-PROJECT-ID \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params": ["0x5BAD55",false],"id":1}'

Note: block number param is given in hex format in example above.

Then, you could batch the API requests in JavaScript by following the steps detailed in the top answer here.

3
  • 1
    Does 'params' take more than 1 blockNumber? It's not clear from the docs
    – Geert-Jan
    Commented Jun 14, 2021 at 7:08
  • I don't think it does. You'll need to multiple queries (each with a block number) and batch them all in parallel for minimal latency. Commented Jun 14, 2021 at 8:35
  • 1
    Yeah ok, but that's still 1 req / block unfortunately. I'm running an archive blockchain, so could do the same locally, but was looking for something like getBlocks(from, to) or something.
    – Geert-Jan
    Commented Jun 14, 2021 at 16:09

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.