Skip to content

  Spin up a managed Avalanche node cluster in minutes! Learn More.

The Sync Blog

Updates and insights from the Bloq team

« Blog Home

How-to: Retrieve and Parse Blocks

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.

The Bloq Connect Javascript SDK

Bloq Connect’s Javascript SDK is a powerful tool that provides users a streamlined mechanism to retrieve and parse blocks. Here, we’ll be doing this for Bitcoin and Bitcoin Cash:

The following Javascript methods are called:

  • block 
  • blockHash
  • blocks
  • blockTransactions
  • rawBlock

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.

Code Setup

If this is your first time using Bloq, reference the Getting Started tutorial to set up your Bloq account and install the Javascript SDK.

In your terminal, use bcl client-keys create to create a new client id/secret key pair.

Create a new Javascript (.js) file, and in your text editor of choice, paste in the following code as the header for your file. Plug in your newly generated client keys where prompted:

Retrieve Block

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 'getLastBlockHash':

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 'status.lastblockhash':

Parse Block

Now that we’ve retrieved a block hash, we can plug that into an additional method to grab block information at the associated block hash. There are several methods in the Javascript SDK we can use.

The '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.

Because Javascript is synchronous, meaning that all commands execute simultaneously, we need to create an “order” so the hash is retrieved before it’s parsed. A simple solution is to package 'block' within a function that takes a hash, and call that function within the body of our 'status' method:

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:

Next Steps

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.

Join the Bloq newsletter for the latest updates

This field is for validation purposes and should be left unchanged.