OpenZeppelin / openzeppelin-test-helpers

Assertion library for Ethereum smart contract testing
https://docs.openzeppelin.com/test-helpers
MIT License
416 stars 132 forks source link

expectEvent() is not parsing receipt correctly when used with hardhat/ether.js #174

Closed Kirol54 closed 2 years ago

Kirol54 commented 2 years ago

I'm running a simple hardhat project with OZ's ERC20 contract and using ether.js to test it and I've encountered a similar issue as #155

Test code:

let txResponse = await erc20Contract.increaseAllowance(addr1.address, 10);
expectEvent(await txResponse.wait(), "Approval");

fails with:

 No 'Approval' events found
      + expected - actual

      -false
      +true

The event can be found when looking at tx receipt return by txResponse.wait();

The workaround I found requires replacing the following line: https://github.com/OpenZeppelin/openzeppelin-test-helpers/blob/9a63072cfe4f72526b76cacde50a0528d75b634a/src/expectEvent.js#L22-L24 which normally returns: [ { event: '0', args: undefined } ]

with: return ({ event: receipt.events[name].event, args: receipt.events[name].args }); result: [ {event: 'Approval', args: [......] }]

Obviously not sure what knock-off effect that may have on different projects but thought that may be useful to address the issue quicker

I had to use ethereum-waffle package with it's expect().to.emit instead.

frangio commented 2 years ago

This library does not work with Ethers.js, only with Web3.js. In order to use with Hardhat, you need to install and use the plugin hardhat-truffle5.

nventuro commented 2 years ago

If working with Hardhat and Ethers is non-negotiable, you might want to look at this simple port of this function for this setup + TypeScript, which powers most of Balancer's tests: https://github.com/balancer-labs/balancer-v2-monorepo/blob/master/pvt/helpers/src/test/expectEvent.ts