Skip to main content

Blocks

To install the SDK, please refer to the Getting Started page. Once this is done and you have installed the uledger-sdk, you may proceed with the following steps.

The code snippets below offer examples for searching for blocks using the ULedger SDK. When initiating a search for blocks, it’s critical to define the scope of your search, which can be influenced by the needs of your application or the specific details you have concerning the block. You can retrieve blocks and the transactions contained within them by conducting searches using the blockchain ID and block ID. In this tutorial, we will guide you through both of these search strategies.

info

This tutorial assumes that you have an existing blockchain set up with ULedger. To execute this successfully, you will need to have a BMS Session created. For additional information, please visit Getting Started and Import ULedger SDK.

Search Blocks by Blockchain ID

In the code snippet provided, you'll see how to search for blocks by using the blockchain ID. This method is user-friendly since the search parameter—the blockchain ID—is static and unchanging. In addition to the blockchain ID, there are other parameters incorporated into the function call. Let's explain each one in detail.

  • Limit: Indicates the maximum number of transactions that should be returned.
  • Offset: Indicates if you would like to offset the index. This allows us to move the starting point of the response array.
  • Sort: A boolean value that indicates if you would like the response array to be sorted by time. When set to true, the array will be ordered from newest to oldest.
  • Trim: A boolean parameter that indicates if you would like the transactions to omit the payload data.
// Pagination customizations
const limit = 10;
const offset = 0;
const sort = true;
// "trim" = exclude the inner payload of the block
const trim = true;
// List all the blocks in our test blockchain
const blocks = await session.listBlocks("{{blockchain id}}", limit, offset, sort, trim);
console.log("Retrieved blocks in chain:\n", blocks);

Search a Block with the Block ID

In the code snippet below, the process for searching for an individual block using the block ID is illustrated. This is a practical method when you need to retrieve details about a specific block. The trim parameter is a boolean that dictates whether you want the returned block information to exclude the payload data — which consists of the transactions within that block. If trim is set to true, the search will return only the metadata of the block, not the transaction details.

// "trim" = exclude the inner payload of the block
const trim = true;
// Search for block by id
const block = await session.searchBlockById("{{block id}}", trim);
console.log("Retrieved block by ID:\n", block);

Troubleshooting/FAQ

Below are a few examples of known issues or just things to look for if you're having issues creating transactions.

  1. I Just Searched for My Block ID and Do Not See It in the BMS:

    • One possibility is that the block has not been minted yet. In this case, just wait a few minutes and check again. It usually takes about 5 minutes for a block to be minted.
  2. I Searched for Blocks Using My Blockchain ID and the Result Is an Empty Array:

    • In this case, it's possible that no blocks have been minted on your blockchain yet. To get some blocks minted, you need to upload transactions first. For additional information, please visit the Create Transaction tutorial.
© 2023 ULedger Inc. All rights reserved