Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting date to block number for different chains #8

Open
0237h opened this issue Jan 24, 2023 · 2 comments
Open

Converting date to block number for different chains #8

0237h opened this issue Jan 24, 2023 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@0237h
Copy link
Collaborator

0237h commented Jan 24, 2023

Currently, the conversion from a date string into a block number only work for the Antelope chain using the (deprecated) GraphQL endpoint.

It would be best to have a general way of converting a date string (or timestamp) to the appropriate block number according to the targeted blockchain.

@0237h 0237h added the enhancement New feature or request label Jan 24, 2023
@0237h 0237h self-assigned this Jan 24, 2023
@0237h
Copy link
Collaborator Author

0237h commented Feb 4, 2023

For now, those are the best options.

Antelope chains

Can be served using DFuse GraphQL endpoints using the blockIDByTime method.

Example

Query

query ($date: Time!) {
  block: blockIDByTime(time: $date) {
    num
  }
}

Data

{
  "date": "2023-01-17T07:59:17Z"
}

Response

{
  "data": {
    "block": {
      "num": 289523536
    }
  }
}

Other chains

Defillama provides an API endpoint at https://coins.llama.fi/block/{chain}/{timestamp}.

Example

curl -X 'GET' 'https://coins.llama.fi/block/ethereum/1675336377' -H 'accept: application/json'

{
  "height": 16540743,
  "timestamp": 1675336379
}

@0237h 0237h mentioned this issue Feb 6, 2023
3 tasks
@0237h
Copy link
Collaborator Author

0237h commented Feb 7, 2023

Presenting another possible solution to this problem.

Calculating block number from timestamp via estimates

The idea is to use the negative start_block_num feature of Substreams/Firehose to query the latest block available from the endpoint. By continuously timing the response for a few blocks, we can estimate the speed of the block production for that chain.

After having refined this metric enough time, we can calculate how far the targeted block is from now:

target_block = latest_block - (now - timestamp)*speed
                               ^^^^^^^^^^^^^^^  ^^^^^
                               diff in seconds  in block/s

We could use the timestamp contained in the block to make the estimates and compare the final target_block timestamp to the actual requested timestamp to adjust for miscalculation. This would allow for precise targeting by moving forward or backward from the block estimate, until we hit the targeted timestamp.

The main drawback to this method is that we need to assume some things about the format of the returned data, in order to extract the block number and timestamp information from it. We need to know where to locate this data inside the response and it's guaranteed to change from chain to chain as they don't all share the same .proto definition (e.g. block.number for Antelope and block.height for Arweave).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant