0

enter image description here

Can I get this timestamp through RPC getblock?

1
  • It sounds like you want to use a tool like Substrate Sidecar for what you are doing here. These properties are not easily accessible via RPC.
    – Shawn Tabrizi
    May 9, 2022 at 13:38

4 Answers 4

4

If you just use chain_getBlock, one of the extrinsics would be timestamp.set as applied by the block author.

This contains the Compact<u64> value of the current timestamp, i.e. milliseconds since epoch. It will match with the timestamp.now in the state.

Since it is an extrinsic, it is available in all nodes, even those where the state has been pruned - assuming obviously that the chain runtime logic adds these timestamp.set extrinsics, which most do.

As explained in a comment from mine below, this approach (like another retrieving the storage) assumes that you have access to a SCALE-decoder for your environment which is able to decode Substrate-supplied data based on the on-chain metadata. If you do not have one, your best only-via-RPC option is to use Sidecar which performs the decoding on your behalf.

16
  • Why do I use HTTP to request the RPC method getblock. Don't you see the return message (timestamp. Now)?
    – Andy
    May 10, 2022 at 8:30
  • 1
    As explained above, it is an extrinsic. So the extrinsic for the timestamp.set call will have the information. (On relay-chains this is generally the first , but obviously could change in order). The decoded extrinsic will have the information, i.e. the decoded part of the extrinsics key in the block.
    – Jaco
    May 10, 2022 at 9:36
  • I don't understand. Can you be more specific? I also know that it needs to be parsed, but what exactly? How to analyze? Through what analysis?
    – Andy
    May 11, 2022 at 6:42
  • 1
    Decode the extrinsics in the block. Those retrieved as part of the extrinsics array in your screenshot. One of those decoded extrinsics would be timestamp.set - it would most probably be the first extrinsic if from a relay chain, i.e. 0x280402000b919bbc3b8001. From this decoded extrinsic, get the first argument to this function - it is the Compact<u64> that denotes the miliseconds.
    – Jaco
    May 11, 2022 at 8:53
  • You only told me the way, but didn't say the specific way? What do you use to analyze?
    – Andy
    May 11, 2022 at 10:03
2

You can get the block time of a specific block like this.
Keep in mind that this only works on an archive node since a pruning node does not have the state of ancient blocks. You can use an indexer for more performance here.

import { ApiPromise, WsProvider } from '@polkadot/api';

const wsProvider = new WsProvider('wss://rpc.polkadot.io');
const api = await ApiPromise.create({ provider: wsProvider });

const blockNum = 10227995;
const hash = await api.rpc.chain.getBlockHash(blockNum);
const header = await api.rpc.chain.getHeader(hash);
const timestamp = await api.query.timestamp.now.at(hash);
const date = new Date(timestamp.toNumber());

console.log(`Block time for block ${blockNum}: ${date}`)
2
1

Substrate does not directly provide an RPC for these kinds of queries. The Substrate RPC is kept minimal and generic.

Instead, we provide a tool called Substrate Sidecar which you can run alongside your node, and that provides a REST api for many of the common queries you would expect for a Substrate blockchain.

With this, you can access the same storage item that Oliver mentioned above:

/pallets/timestamp/storage/now
1
  • Why do I use HTTP to request the RPC method getblock. Don't you see the return message (timestamp. Now)?
    – Andy
    May 10, 2022 at 8:30
-1

enter image description here

Why do I use HTTP to request the RPC method getblock. Don't you see the return message (timestamp. Now)?

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 11, 2022 at 3:44

Your Answer

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

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