npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eth-event-listener

v1.0.22

Published

### by Builderssquad Bloackchain Developers

Downloads

5

Readme

Eth Event Listener

by Builderssquad Bloackchain Developers

Motivation

Make a simple smart contract event listener to get events from the smart contract in NodeJS app and be configure the finality

Do not care about internal integrity and be notified about new finalized event inside the nodejs app

Implementation

  • NodeJS module
  • build on top of getPastEvents (https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#getpastevents)
  • Used key-value storage DB to cash the data. if you delete the storage it will replay all transactions

Dependencies

  • levelup db - to keep processing index
  • configuration file - to provide info about rpc, confirmation config, contract address, abi and startBlock

Install

npm i eth-event-listener --save

Example

See in test folder

// db dependencies - read more about levelup (https://github.com/Level/awesome) to understand how to configure it
const levelup = require('levelup');
const memdown = require('memdown'); 

// sync service
const { startSyncService } = require('eth-event-listener');

// init a key-value storage db (can be any), 
// here is in example the data is stored in memory, but you can chose different storage
const db = levelup(memdown(), { keyEncoding: 'json' });

const options = {
    // @required: list of all supported chains and chain specifics
    chains: require("./chains.json"),
    // @required: chosen chain from chains list
    chain: 1,
    // @required: contract information of USDT: https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7
    contract: { abi: require('./erc20.abi.json'),
                address : "0xdAC17F958D2ee523a2206206994597C13D831ec7",
                event: "Transfer"
    },// @optional: this callback is triggered between event processing circles, processing starts right after this script is finished
    // advanced functionality: skip using the contract address and event, use topics instead
    trialCallback : (cb)=> {
        console.log("start of procssing")
        cb()
    },
    // @required: init an event's handler. 
    // if you callback error, it will try to send it again till success - this is mostly all what you need for your app :)
    // this method will never send duplicated transactions, 
    // possibly it can send the outdated transaction after newer transaction. it depends on blockchain node congested state. please follow the event sourcing pattern to replay such transactions
    eventsCallback : (events, cb)=> {
        console.log('incoming unique events', events)
        cb()
    },
    // @optional: the block where smart contract was deployed
    startBlock: 4634748
    
}


startSyncService(db, options, (err)=> {  
    console.log("process is terminated, err: " + err ); 
});

Event fields

You can find the information here https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#contract-events-return

The structure of the returned event Object looks as follows:

  • event - String: The event name.
  • signature - String|Null: The event signature, null if it’s an anonymous event.
  • address - String: Address this event originated from.
  • returnValues - Object: The return values coming from the event, e.g. {myVar: 1, myVar2: '0x234...'}.
  • logIndex - Number: Integer of the event index position in the block.
  • transactionIndex - Number: Integer of the transaction’s index position the event was created in.
  • transactionHash 32 Bytes - String: Hash of the transaction this event was created in.
  • blockHash 32 Bytes - String: Hash of the block this event was created in. null when it’s still pending.
  • blockNumber - Number: The block number this log was created in. null when still pending.
  • raw.data - String: The data containing non-indexed log parameter.
  • raw.topics - Array: An array with max 4 32 Byte topics, topic 1-3 contains indexed parameters of the event.