nxt-monitor

0.1.2 • Public • Published

nxt-monitor

A node.js module for monitoring and interacting with the Nxt blockchain. It executes callback functions when a certain event on the blockchain occurs, e.g. a new block or specific type of transaction.

Installation

npm install nxt-monitor

Usage

See also: https://github.com/toenu23/nxt-monitor-examples

Require the module

var nxt = require('nxt-monitor');

Configure

// Connection options
var options = {
  nxt: {
    protocol: 'http',
    hostname: '127.0.0.1',
    port: '7876',
  },
};

When not provided, the connection will default to http://127.0.0.1:7876

Initialize

The initialize function is needed to set the config and start monitoring (see below).

// Initialize
nxt.init(options);

Making an API call

Making a call to the Nxt API is very simple. Use the "call" function and provide it with an object containing the request parameters. The API response will be returned as an object.

See Nxt API documentation for possible requests and responses.

// Example API Call
var query = { requestType: 'getBlockchainStatus' };
nxt.call(query, function(err, resp) {
  console.log(resp);
});

Filters and callbacks

You can add filters to the options object, which define under which circumstances your callback function is executed. All filters need at least the "callback" attribute, which is the function to be run.

The filters set in the parameter filters will be applied to transactions, while filters in blockfilters will be applied to blocks.

You can filter for any parameter returned in the block- or transaction-objects by the Nxt API. See examples/examples.js for some more examples.

The filter conditions are defined in the conditions parameter and the callback function in the callback parameter.

See Nxt API documentation for transaction types and subtypes.

// Callback to be executed when a new transaction/block matches the filter
var myCallback = function(data) {
  // data contains the transaction or block object
  console.log(data);
}

options.filters = [
  // Payments recieved by an account
  {
    conditions : [
      { key : 'type', value : 0 },
      { key : 'recipientRS', value : 'NXT-W6CT-NPDH-AAQW-HWCHA'}
    ],
    callback : myCallback
  }
];

options.blockfilters = [
  // Blocks generated by a specific account
  {
    conditions : [
      { key : 'generatorRS', value : 'NXT-TGNZ-E8VK-69EX-3L9LX' }
    ],
    callback : myCallback
  }
];

The parameters key and value must be defined. key corresponds to the key in the API response and value it its value, against which it will be compared.

value can be an array. The callback function will be triggered if any of the values matches.

If attachment is set to true, it means that the value to be checked is an attachment of a transaction. (e.g. needed for the asset ID of an asset transaction, product ID of a marketplace transaction etc.)

Comparison of strings is case-sensitive by default. Set casesensitive to false for case-insensitive comparison.

The parameter operator defines how the values are going to be compared. Currently the following options are possible:

  • equals (Default)
  • notequals
  • greater
  • greatereq
  • less
  • lesseq
  • contains
  • notcontains
  • beginswith
  • notbeginswith
  • endswith
  • notendswith
  • regex
// Any payments greater or equal to 100 NXT (10000000000 NQT)
{
  conditions : [
    { key : 'type', value : 0 },
    { key : 'amountNQT', operator : 'greatereq', value : 10000000000 }
  ],
  callback : myCallback
},

// Transfers of a specific asset
{
  conditions : [
    { key : 'type', value : 2 },
    { key : 'subtype', value : 1 },
    { key : 'asset', attachment : true, value : 12071612744977229797 }
  ],
  callback : myCallback
},

Package Sidebar

Install

npm i nxt-monitor

Weekly Downloads

0

Version

0.1.2

License

Public Domain

Last publish

Collaborators

  • toenu