This module is intended to listen for events that takes place in Ethereum blockchain and act accordingly.
Basically, it polls events from the provided contracts every X milliseconds and executes the corresponding callback for that event passing the event information to it.
const EthereumEventProcessor = require('ethereum-event-processor');
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
const options = { pollingInterval: 500, startBlock: 200, blocksToWait: 20, blocksToRead: 20 };
const eventProcessor = new EthereumEventProcessor(web3, contract.address, contract.abi, options);
eventProcessor.on('EventName', (event) => {
console.log(fromBlock, lastBlock);
});
eventProcessor.on('blocks_processed', (fromBlock, lastBlock) => {
console.log(fromBlock, lastBlock);
});
eventProcessor.listen();
eventProcessor.stop();
Arguments:
A Web3 connection object to the blokchain.
The address of the contract to listen for events.
The ABI in JSON format of the contract to listen for events.
A JavaScript object containing the configuration for the event processor behaviour.
startBlock Block number to start listening from.
pollingInterval Interval in milliseconds between every poll to the blockchain.
blocksToWait Number of blocks to wait before start reading events from new blocks. This will make the interval to be skipped if (latestBlockNumber - lastReadBlock + blocksToRead) < blocksToWait
.
blocksToRead Number of blocks to read in each interval.
Starts or resumes the events processor consuming process. It can receive the same arguments as the constructor options to override default behaviour.
Stop the current event processor consuming process.
Receives a function to be executed every time a new set of blocks is processed. The function receives the start and end numbers of the blocks that have been processed in that iteration.
Receives a function to be executed every time the event specified as a string with eventName
has been received. The function receives the event object as an argument.