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

truffle-events

v0.0.7

Published

<p align="center"> <img src="./images/img.jpeg" /> </p>

Downloads

19

Readme

Truffle Events

A simple utility library to form a transaction for deep event testing i.e. approveAndCall function. Works best with 'truffle-assertions' library.

Why I Created This Library?

If you are doing something like approveAndCall function, you might have events inside 2 or more contracts. I called them as 'deep events'.

At first, I thought it was a Ganache issue. But, after spending some time experimenting with it, I can confirm that this issue was not only happened in Ganache. It happened in Geth too.

After some research, I found this answer. That explains a lot. Only events that were emitted from the contract test scope will be returned.

For example:

pragma solidity ^0.4.24;

contract Foo {
    event LogNumber(uint256 number);

    function doSomething() public {
        emit LogNumber(100);
    }

    function doSomethingExtra(address bar) public {
        emit LogNumber(100);
        Bar b = Bar(bar);
        b.doSomething();
    }
}

contract Bar {
    event LogAlphabet(string word);

    function doSomething() public {
        emit LogAlphabet("Hello!");
    }
}

In above example, if you are testing doSomethingExtra(), you will only get LogNumber event inside the transaction object:

// code omitted for brevity

var tx = await foo.doSomethingExtra(); // has no `LogAlphabet` event inside

// code omitted for brevity

But, if you check the transaction receipt, you will actually see 2 logs were emitted (well, that's what we expect and should be).

But, the result is not in decoded form. You need some knowledge to decode the events from that transaction receipt result. Tip: you can use this online tool to generate your event's hash.

Lucky enough, I found a library by Consensys to help us with events decoding. This Truffle Events is using that abi-decoder library as dependency.

How It Works?

Since the truffle-assertions library require a transaction object with decoded events, what I did here is basically decode the emitted event that we have in another contract/receiver contract and form a minimal transaction object that is compatible with truffle-assertions library.

Usage

Install the package:

$ yarn add truffle-events
$ npm i truffle-events

The magic:

// all 3 arguments are required
truffleEvent.formTxObject('ContractName', eventIndex, txScope);

// example based on 2 contracts above
var barScope = truffleEvent.formTxObject('Bar', 1, fooScope);

Test example:

var truffleAssert = require('truffle-assertions');
var truffleEvent  = require('truffle-events');

// code omitted for brevity

it("Foo#doSomethingExtra", async function(){
  var fooScope = await f.doSomethingExtra(b.address);
  var barScope = truffleEvent.formTxObject('Bar', 1, fooScope);

  truffleAssert.eventEmitted(fooScope, 'LogNumber', (ev) => {
    return ev.number == 100;
  });

  truffleAssert.eventEmitted(barScope, 'LogAlphabet', (ev) => {
    return ev.word == "Hello!";
  });
});

Contributing

Feel free to fork and submit your PR. I'm happy to review and merge it. As for issues, I can't promise any support at this moment.

License

This package is released under MIT license.