@diviproject/watchtower v1.0.3
🗼 Watchtower
Real-Time monitoring of the DIVI blockchain.
✨ Features
- Real-time events via. websockets
- Supports both
mainnet
andtestnet
- Automatically detects
masternode
andstaking
rewards - Generates address-specific events from transaction outputs
🔧 Installation & Setup
1. Install by running: npm i @diviproject/watchtower
2. Setup client
// Dependencies
import Watchtower from '@diviproject/watchtower'
// Setup client
const client = new Watchtower({
token: '48a6f518-a710-4253-bafd-6ab7204755e7'
});
// Listen for raw transactions
client.on('raw', tx => {
// Log to console
console.log({ tx });
});
// Listen for new blocks
client.on('block', block => {
// Log to console
console.log({ block });
});
3. Try it out! 🎉
📖 Documentation
Initialization
// Setup client
const client = new Watchtower({
token: '48a6f518-a710-4253-bafd-6ab7204755e7',
testnet: false
});
token
(String)
This needs to be a valid authentication token.
testnet
(Boolean)
Wether or not to connect to DIVI testnet instead of mainnet. Defaults to false
.
Listening to events
Watchtower exposes an EventEmitter. Subscribing to events is done with .on(event, callback)
. The following example will log raw transactions to the console.
// Listen for raw transactions
client.on('raw', tx => {
// Log to console
console.log(rx);
});
List of events
Event: open
An event for when the client has connected to the server.
client.on('open', () => {
console.log('Connection has been established.');
});
Event: pong
An event for when the client receives a Websocket pong
frame (in response to ping
) from the server. This can be used to confirm an active connection.
client.on('pong', () => {
console.log('Received pong frame from server.')
});
Event: block
This event will emit new blocks.
client.on('block', block => {
console.log(block);
// {
// height: 869865,
// blockhash: 'a3b05e170c46b9079cf7e88ef6b615ff2021b08115f0f50fc8329315d500ff3f'
// }
});
Event: raw
This event will emit raw transactions - including mempool transactions.
client.on('raw', tx => {
console.log(tx);
// {
// txid: '59290438e1cf7feb5394b6bbfca4b771d4a66ab117754884de664d26604a6dfe',
// version: 1,
// locktime: 0,
// vin: [ [Object] ],
// vout: [ [Object], [Object], [Object] ],
// blockhash: '22ac9bf136425e285a6334ffc341261a67dfaa18902e2ec3cfb9dfe1fa85d1b9',
// height: 869864,
// confirmations: 1,
// time: 1590791133,
// blocktime: 1590791133,
// size: 374
// }
});
Event: payment
This event will emit transaction payments (outputs) to specific addresses.
client.on('payment', payment => {
console.log(payment);
// {
// address: 'DCSwZjxhgGTW8KYW6sAhxeBxyyFZwAyn6j',
// value: 41834,
// confirmed: true,
// source: {
// txid: 'd5444b3c3478f32e3232eef2199734c65d4c7d6d7d29fd87174ff0eb19b056aa',
// version: 1,
// locktime: 0,
// vin: [ [Object] ],
// vout: [ [Object], [Object], [Object] ],
// blockhash: '00acb08200e7c1e400073d00b3eb81143098538a8129f824a29b1e2355aac172',
// height: 869872,
// confirmations: 1,
// time: 1590791651,
// blocktime: 1590791651,
// size: 210
// }
// }
});
Event: reward
This event will emit rewards (staking and masternode) to specific addresses.
client.on('reward', reward => {
console.log(reward);
// MASTERNODE REWARD
//
// {
// type: 'masternode',
// address: 'DCrFypze6SppdMBMdnwfNV21VupoHu6mtH',
// value: 540,
// source: {
// txid: '8854db105c0496b80b6226ab6463d71897deaa9a645bc7570b06ad3edbb1d780',
// version: 1,
// locktime: 0,
// vin: [ [Object] ],
// vout: [ [Object], [Object], [Object] ],
// blockhash: '5467e015cf4c042af66212e9a10af1317b6a7034d758ebd3f5f55d008955618f',
// height: 869873,
// confirmations: 1,
// time: 1590791687,
// blocktime: 1590791687,
// size: 234
// }
// }
//
// STAKING REWARD
//
// {
// type: 'staking',
// address: 'D5DE5fWpx43NirMBA458hjkx6mvpb215Uo',
// value: 456,
// source: {
// txid: '8854db105c0496b80b6226ab6463d71897deaa9a645bc7570b06ad3edbb1d780',
// version: 1,
// locktime: 0,
// vin: [ [Object] ],
// vout: [ [Object], [Object], [Object] ],
// blockhash: '5467e015cf4c042af66212e9a10af1317b6a7034d758ebd3f5f55d008955618f',
// height: 869873,
// confirmations: 1,
// time: 1590791687,
// blocktime: 1590791687,
// size: 234
// }
// }
});