The building blocks for each blockchain. As such there are a number of examples for working with blocks and headers, that could be useful.
#How do I retrieve the header/extrinsic hash from blocks?
A block hash refers to the hash over the header, the extrinsic hash refers to the hash of the encoded extrinsic. Since all objects returned by the API implements the
.hash => Hash getter, we can simply use this to view the actual hash.
#How do I extract the block author?
The block author is encoded inside the consensus logs for the block. To extract, you need to decode the log (which the API does do) and then map the index of the validator to the list of session validators. This extraction is however available on the api derive for new head subscriptions, which returns an extended header with the author populated (assuming that the digest logs are known).
For a single header only, the derives also contain a
getHeader, which once again returns a header extended with the author -
#How do I view extrinsic information?
The transactions are included in a signed block as part of the extrinsics - some of these will be unsigned and generated by the block author and some of these may be submitted from external sources and be signed. (Some palettes do use unsigned transactions, so signed/unsigned is not an indication of origin). To retrieve the block and display the transaction information, we can do the following -
In the above
.toHuman() is used to format into a human-readable representation. You can inspect/extract specific fields from the decoded extrinsic as required, for instance
ex.method.section would return the pallete that executed this transaction.
#How do I map extrinsics to their events?
While the blocks contain the extrinsics, the system event storage will contain the events and the details needed to allow for a mapping between. For events the
phase is an enum that would be
isApplyExtrinsic with the index in the cases where it refers to an extrinsic in a block. This index maps through the order of the extrinsics as found.
To perform a mapping between the two, we need information from both sources.
#How do I determine if an extrinsic succeeded/failed?
This is an extension of the above example where extrinsics are mapped to their blocks. However in this example, we will look for specific extrinsic events, in this case the
system.ExtrinsicFailed events. The same logic can be applied to inspect any other type of expected event.