Whether researching user trends over time, extracting insights related to network events, or concocting a prediction model, knowing how to extract and derive meaning from block data is a valuable skill for any software engineer or data scientist. This tutorial will give you the basics.
Depending on the method call, you may need a block hash or block height as an input variable. The
'getStatus' method can retrieve block hashes, among other values. Additionally, users can access a block explorer to retrieve blocks to parse.
In your terminal, use
bcl client-keys create to create a new client id/secret key pair.
By calling the
'getStatus' command, we can gather miscellaneous information pertaining to the current status of the network:
In your terminal, use
node file_name.js to execute the program. Your code should return values corresponding to the node’s connections, protocol version, difficulty, and more. It also returns a
'blocks' value, which corresponds to the height of the most recent block.
However, the code doesn’t yet provide a block hash, which is required for most of the parse methods. To get the block hash, accompany the
'status' method with an input string of
This code returns the
'syncTipHash', which is the hash of the last block validated as part of the main chain, and
'lastBlockHash', which returns the hash of the last block propagated on the network. In most cases, these values will be identical.
The return values will only be “out of sync” in the case of an active orphan chain, which represents an accidental fork where miners append blocks to an out-of-date chain. Alternatively, you can instead use the
'getBestBlockHash' input to protect against active orphans.
The block hash is found at
'block' method is the most comprehensive of its bunch. With
'block', we can see the time, hash, transaction list, difficulty, time, and many other pieces of information. By plugging the retrieved block hash into
'block', we also get all the information at that hash.
'block' within a function that takes a hash, and call that function within the body of our
This output can be filtered to only report whichever data is relevant to the user’s needs. For example, we can get just the difficulty and reward if we indicate as such:
Lastly, let’s package the entire thing into a chain-agnostic function so this code can be deployed to parse BTC or BCH blocks. Update the ‘coin’ field with a variable that corresponds to the input of the larger function. You may recall this strategy from previous installments:
At this point, we’ve produced “starter code” that enables users to retrieve the most recent BTC or BCH block and parse for pertinent information. This can be incorporated into a larger project that compares BTC and BCH block stats, or uncover trends over time by analyzing recent blocks against their predecessors.
This also represents the foundation for a block explorer. One can listen in for new blocks with Websockets and execute the code with each new block. You can also record all block information and display it in a user-friendly format on a block explorer website or as part of an application.