Giter Site home page Giter Site logo

truffle-events's Introduction

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.

truffle-events's People

Contributors

zulhfreelancer avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.