Can I get this timestamp through RPC getblock?
4 Answers
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.
-
Why do I use HTTP to request the RPC method getblock. Don't you see the return message (timestamp. Now)?– AndyMay 10, 2022 at 8:30
-
1As 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.– JacoMay 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?– AndyMay 11, 2022 at 6:42
-
1Decode 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.– JacoMay 11, 2022 at 8:53 -
You only told me the way, but didn't say the specific way? What do you use to analyze?– AndyMay 11, 2022 at 10:03
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}`)
-
Thank you, but if you use HTTP to request the RPC method. (api.query.timestamp.now.at(hash);) How to write this?– AndyMay 9, 2022 at 9:41
-
You mean wirh Curl? You probably would need to calculate the storage hash. There is an explanation here: shawntabrizi.com/substrate/querying-substrate-storage-via-rpc May 9, 2022 at 19:26
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
-
Why do I use HTTP to request the RPC method getblock. Don't you see the return message (timestamp. Now)?– AndyMay 10, 2022 at 8:30
Why do I use HTTP to request the RPC method getblock. Don't you see the return message (timestamp. Now)?
-
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 BotMay 11, 2022 at 3:44