Debugging Transactions

Create an application to monitor announced transactions.

Background

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

The new Catapult REST 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 the transactions you send using NEM2-CLI.

Prerequisites

Let’s get into some code

  1. 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');
  1. Open the connection. You only need to open the connection once. Then listen to all desired channels.
listener.open().then(() => {
        listener.open().get();
listener.open().then(() => {
  1. 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(() => {

    listener
        .confirmed(Address.createFromRawAddress('SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54'))
        .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(() => {

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

Troubleshooting: Monitoring transactions on the client side

The nem2-sdk for typescript base listener was designed to work on Node.js backend environments.

To make the code work in the client side (e.g., Angular, React, Vue.), pass the browser implementation of the WebSocket to the Listener.

const listener = new Listener('ws://localhost:3000', WebSocket);
listener.open().then(() => ...

What’s next?

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