Giter VIP home page Giter VIP 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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

truffle-events's Issues

Location of ABI needs to be variablized

The output location for ABIs can be changed from the default in Truffle.js:

module.exports = {
    contracts_build_directory: path.join(__dirname, "src/abi"),
}

This often has to be done when building a React application using Drizzle, since React only has access to files under its src folder at compile time. Consequently, this leads to a problem with unit tests that use the truffle-events library, which hardcodes the path to the contract ABI:

  getAbi: function(contractName) {
    let c = JSON.parse(fs.readFileSync(`./build/contracts/${contractName}.json`, `utf8`));
    return c.abi;
  }

Resulting in the following error:

  Error: ENOENT: no such file or directory, open './build/contracts/MyContract.json'
      at Object.openSync (fs.js:448:3)
      at Object.readFileSync (fs.js:348:35)
      at Object.getAbi (node_modules/truffle-events/index.js:6:27)

Truffle 5.0.1 (or possibly before) introduces breaking changes.

I just updated truffle, zeppelin and others now that they are supporting solc 0.5.0. I found that the test cases using this library were failing. I found that the transaction result.receipt.logs was empty and seem to have been moved to rawLogs. I suspect the logs array is reserved for logs decoded by truffle for the specific contract.

I thought I would bring it up in case you plan on maintaining this library.

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.