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

ethereum-event-logs

v1.3.1

Published

Ethereum event log parser

Downloads

58

Readme

ethereum-event-logs

Build Status Coverage Status

Parse Ethereum event logs with ease! 🎡

Features:

  • Can parse logs for multiple event ABIs at the same time
  • Accurately decodes event parameter values (both indexed and non-indexed)
  • Automatically decodes non-indexable values as bytes32 (read more)
  • Not tied to on any particular web3 library, can be used independently

Install

  • NPM/Yarn: ethereum-event-logs

Example usage

Imagine the following contract:

pragma solidity ^0.4.24;

contract Events {
    event Event1(string indexed stringVar1, string stringVar2);
    event Event2(bytes32 indexed bytes32Var, bool indexed boolVar, string stringVar);
    event Event3(address indexed addressVar, uint uintVar, uint64[] indexed uint64Var1, uint64[] uint64Var2);
    event Event4(bytes bytesVar) anonymous;

    constructor () public {}

    function emit() public {
        emit Event1('test1', 'test2');
        emit Event2(keccak256('test1'), false, 'test2');
        uint64[] memory numbers = new uint64[](3);
        numbers[0] = 100;
        numbers[1] = 101;
        numbers[2] = 102;
        emit Event3(address(this), 2342, numbers, numbers);
        emit Event4('0x123');
    }
}

Let's say we deploy it and call emit() via an on-chain transaction. The receipt for this transaction will contain the event logs.

Here is how we can parse the logs using this library:

// import the parser
const { parseLog } = require('ethereum-event-logs')
const { abi } = require('./ExampleContract.json')

const receipt = /* execute tx on chain and wait for receipt */

// we can parse all events in the contract by passing through the ABI:
const events = parseLog(receipt.logs, abi)

console.log(events)
/*
[
  {
    name: 'Event1',
    address: '0x...',
    blockNumber: 123...,
    blockHash: '0x...',
    transactionHash: '0x...',
    args: {
      stringVar1: '0x...', // === web3.utils.sha3('test1')
      stringVar2: 'test2'
    },
    log: {
      ... // the log object which represents this event
    }
  },
  {
    name: 'Event2',
    address: '0x...',
    blockNumber: 123...,
    blockHash: '0x...',
    transactionHash: '0x...',
    args: {
      bytes32Var: '0x...', // === web3.utils.sha3('test1')
      boolVar: false,
      stringVar: 'test2'
    },
    log: {
      ... // the log object which represents this event
    }
  },
  {
    name: 'Event3',
    address: '0x...',
    blockNumber: 123...,
    blockHash: '0x...',
    transactionHash: '0x...',
    args: {
      addressVar: '0x...', // address of contract
      uintVar: '2342',
      uint64Var1: '0x...',
      uint64Var2: [ '100', '101', '102' ],
    },
    log: {
      ... // the log object which represents this event
    }
  }
  // Event4 is defined as anonymous, hence it doesn't get parsed
]
 */

Notice above that Event1.stringVar1 is returned as a SHA3 hash instead of the actual string. This is because only finite-sized scalar types (i.e. that fit within a 64 byte hex code) can be indexed as an event argument. All other types have their values hashed prior to indexing.

To only search for an individual event just pass that event's ABI on its own:

const eventAbi = abi.find(({ name }) => name === 'Event1')

const events = parseLog(receipt.log, [ eventAbi ])

You can filter by contract address:

const events = parseLog(receipt.log, [ eventAbi ], {
  address: '0x....'
})

You can also filter by block number:

const events = parseLog(receipt.log, [ eventAbi ], {
  blockNumber: 1234...
})

If there is an argument decoding failure then the corresponding event instance won't be included in the final results.

Dev guide

  • Install deps: yarn
  • Tests: yarn test
  • Tests with coverage: yarn test:coverage
  • Build final lib: yarn build
  • Lint: yarn lint

License

MIT