Transaction Tracker

Step 1: Install Henesis CLI

Installation

npm install -g @haechi-labs/henesis-cli

Login with your Henesis account

henesis login

Step 2: Clone the Sample Code

Clone the sample repository

git clone https://github.com/HAECHI-LABS/sample-tx-tracker

Move to sample directory

cd sample-tx-tracker

Install dependencies

npm install

Step 3: Configure subscription for transaction status change

Configuration for ClientId, Private Key and Node Endpoint

To subscribe the changes of the transaction status through Henesis, you need the following information:

  • CLIENT_ID : Your client id. You can check it through the henesis account:describe command.

  • PRIVATE_KEY : A private key of your EOA account for test. The EOA account should have enough ETHs to send transactions. (how to check your private key in metamask)

  • NODE_ENDPOINT : The URL of the blockchain node you want to connect. (how to check INFURA key)

The ClientId can be checked using the following CLI command.

henesis account:describe
Email: haechi@haechi.io
Name: haechi
Organization: haechi-labs
ClientId: a481485a958f1b82ac210ec4eea27943

Enter the ClientId, Private Key and node endpoint in the .env configuration file.

CLIENT_ID=<your-client-id>
PRIVATE_KEY=<your private key>
NODE_ENDPOINT='https://ropsten.infura.io/v3/<your-key>'

Create the TransactionTracker instance

You can use CLIENT_ID to create a TransactionTracker instance for subscribing the transaction status change.

const { TransactionTracker } = require('@haechi-labs/henesis-sdk-js');
const { CLIENT_ID, PRIVATE_KEY, NODE_ENDPOINT } = process.env;
const tracker = new TransactionTracker(CLIENT_ID, {
platform: 'ethereum',
network: 'ropsten',
});

See here for a list of supported blockchains.

Subscribe the Transaction Status Change

You can subscribe to the status of transactions through subscription

  • You can check the status of a transaction through the message.data.type .

  • The message.ack() should be called when data processing is done

const subscription = await tracker.subscribe(
"transaction",
{
subscriptionId: "your-subscription-id",
ackTimeout: 30 * 1000 // default is 10 * 1000 (ms)
}
);
subscription.on("message", async (message) => {
console.log(`now transaction status is: ${message.data.type}`)
switch(message.data.type) {
case 'pending' :
transactions[message.data.result.transactionHash] = { status: 'pending'}
break;
case 'receipt' :
console.log('message.data.result',message.data.result)
transactions[message.data.result.transactionHash] = { ...message.data.result, status: 'receipt' }
break;
case 'confirmation' :
console.log('message.data.result',message.data.result)
transactions[message.data.result.transactionHash] = {...message.data.result, status: 'confirmation' }
break;
}
message.ack();
});
subscription.on("error", async (error) => {
console.log('err',error);
});

Tracking the Transaction Status Change

Track a transaction using the trackTransaction function.

  • Set the txHash of transaction whose state change you want to track.

  • Initiate to track transactions by putting txHash into henesis#trackTransaction

  • Set timeout 30 seconds, and confirmation to 6.

// Start tracking transaction
// txHash which is generated by web3 or could be copied by etherscan
tracker.trackTransaction(txHash, {
timeout: 30*1000,
confirmation: 6
});

Step 4: Check for the Transaction Status Change

Run the API server and check for the transaction status change.

$ node index

When the status of a transaction changes and the message is delivered, you can see the following information in your console:

now transaction status is: receipt
message.data.result { blockHash:
'0x1328a899d25d873b12836f6faa6e28df45aeafcd2cf2c4984e2a157cfeb3b69a',
blockNumber: 6706349,
contractAddress: null,
cumulativeGasUsed: 21004,
from: '0x453011a4f948b22762290c1f4de3e2210c311c06',
gasUsed: 21004,
logs: [],
logsBloom:
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: true,
to: '0x453011a4f948b22762290c1f4de3e2210c311c06',
transactionHash:
'0xac1ad6d9f04161c2a4bbc0327a7a3a6a9bced4c5da9ff8752a3774fd98c51e7b',
transactionIndex: 0 }