Monitoring blockchain

Debugging transactions

This tutorial will guide you in the creation of an application to monitor announced transactions.

Background

If you are used to NIS1 API calls, you know that after announcing a transaction you received a success message or some error indicating the reason why the transaction has not been included in the block.

The new NIS2-API works differently, being asynchronous. When a user announces a transaction through the API, it always returns an OK.

To know the status of the transaction, which can be OK or Failure, you have to:

  1. Check the status via API endpoint.
  2. Listen to the different WebSocket channels.

Note

While developing, it is encouraged to open terminals with different listeners to monitor transactions you send using NEM2-CLI.

Prerequisites

Let’s get into some code

Create a new listener to get notified every time a transaction related to your account reaches the network and gets confirmed.

const listener = new Listener('http://localhost:3000');
        Listener listener = new Listener("http://localhost:3000");
const listener = new Listener('http://localhost:3000');

Open the connection. You only need to open the connection once and then connect to all desired channels.

listener.open().then(() => {
        listener.open().get();
listener.open().then(() => {

In this example, we will reach only transactions that get confirmed. You can also create other listeners to check unconfirmed, aggregate bounded or transactions that fail.

listener.open().then(() => {

    const address =  Address.createFromRawAddress('SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54');

    listener.confirmed(address).subscribe(
        transaction => console.log(transaction),
        err => console.error(err)
    );
});
        Address address = Address.createFromRawAddress("SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54");

        listener.open().get();

        Transaction transaction = listener.confirmed(address).take(1).toFuture().get();

        System.out.println(transaction);
listener.open().then(() => {

    const address =  Address.createFromRawAddress('SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54');

    listener.confirmed(address).subscribe(
        transaction => console.log(transaction),
        err => console.error(err)
    );
});
nem2-cli monitor confirmed --address SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54

What’s next?

Run your application and try to send a transfer transaction to the selected account. If all goes well, you will see transaction information in your terminal.

Listening new blocks

Following this guide you will build a simple application to get notified when a new block is included in the network.

Prerequisites

Let’s get into some code

const listener = new Listener('http://localhost:3000');

listener.open().then(() => {

    listener.newBlock().subscribe(
        block => console.log(block),
        err => console.error(err)
    );

});
        Listener listener = new Listener("http://localhost:3000");

        listener.open().get();

        BlockInfo blockInfo = listener.newBlock().take(1).toFuture().get();

        System.out.println(blockInfo);
const listener = new Listener('http://localhost:3000');

listener.open().then(() => {

    listener.newBlock().subscribe(
        block => console.log(block),
        err => console.error(err)
    );

});
nem2-cli monitor block

Getting block by height

A simple program to get block information given a height.

Prerequisites

Let’s get into some code

Do you have curiosity to see what happened in genesis block? Run the following code to find out what happened.

const blockchainHttp = new BlockchainHttp('http://localhost:3000');

// Replace with block height
const height = 1;

blockchainHttp.getBlockByHeight(height).subscribe(
    block => console.log(block),
    err => console.error(err)
);
        final BlockchainHttp blockchainHttp = new BlockchainHttp("http://localhost:3000");

        // Replace with block height
        final BigInteger blockHeight = BigInteger.valueOf(1);

        final BlockInfo blockInfo = blockchainHttp.getBlockByHeight(blockHeight).toFuture().get();

        System.out.print(blockInfo);
const blockchainHttp = new BlockchainHttp('http://localhost:3000');

// Replace with block height
const height = 1;

blockchainHttp.getBlockByHeight(height).subscribe(
    block => console.log(block),
    err => console.error(err)
);

Here you have a snippet to check the number last block number.

const blockchainHttp = new BlockchainHttp('http://localhost:3000');

blockchainHttp.getBlockchainHeight().subscribe(
    height => console.log(height.compact()),
    err => console.error(err)
);
        final BlockchainHttp blockchainHttp = new BlockchainHttp("http://localhost:3000");

        final BigInteger blockchainHeight = blockchainHttp.getBlockchainHeight().toFuture().get();

        System.out.print(blockchainHeight);
const blockchainHttp = new BlockchainHttp('http://localhost:3000');

blockchainHttp.getBlockchainHeight().subscribe(
    height => console.log(height.compact()),
    err => console.error(err)
);
nem2-cli blockchain height