1.0.0 • Published 2 years ago

@ltonetwork/lto-chain-listener v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

github-banner

LTO Public Chain Listener

Listen to the public chain transactions of LTO Network

Quick Start

To listen to each transaction event, import the LTOChainListener and call start():

import LTOChainListener from 'lto-chain-listener';
// OR
const LTOChainListener = require('lto-chain-listener').default; // `.default` for CommonJS

const listener = new LTOChainListener(...options); // see available options below

// for now, the only event emitted is `new-transaction`
listener.on('new-transaction', (transaction) => {
  console.log('We have a new transaction!', transaction);
});

try {
  listener.start();
} catch (error) {
  // something bad happened, you can handle error here
}

- Options

The LTOChainListener accepts a few options for different behaviors. See the table below for reference:

propertydescriptionformatdefault value
processingHeightThe height the listener should start processingNumberThe value in local storage (localStorage.getItem('processingHeight')) or 1
publicNodeURLThe public node URL the listener should get data fromStringhttps://testnet.lto.network
processIntervalInMSThe interval between each request made to the public nodeNumber (in milisseconds)5000
shouldRetryStartWhether or not the listener should retry starting if it failsBooleanfalse
testingModeWhether or not the listener should run on testing mode (runs only once instead of listening for new blocks)Booleanfalse
logLevelThe level of logging on the listenerinfo, error, warn or debuginfo

You can define these options as the following:

new LTOChainListener({
  processingHeight: 100,
  publicNodeURL: 'some-node-url',
  processIntervalInMS: 2000,
  shouldRetryStart: false,
  testingMode: true,
  logLevel: 'debug',
});

- Events

The chain listener emits events while it's processing blocks. For now, the only event emitted is new-transaction. You can listen to this event and run whichever code you want when a new transaction is found.

listener.on('new-transaction', (transaction) => {
  console.log('We have a new transaction!', transaction);
});

listener.start();

Note: it's important to run listener.start() only AFTER you create your listeners, otherwise they won't work properly

listener.start();

listener.on('new-transaction', (transaction) => {
  // this will not work, as it is created after `listener.start()`
});