Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contract event listener is not firing when running hardhat tests on ethers v6 #4044

Closed
6be709c0 opened this issue Jun 19, 2023 · 13 comments · Fixed by #4071
Closed

Contract event listener is not firing when running hardhat tests on ethers v6 #4044

6be709c0 opened this issue Jun 19, 2023 · 13 comments · Fixed by #4071
Labels
area:hardhat-ethers status:ready This issue is ready to be worked on type:bug Something isn't working

Comments

@6be709c0
Copy link

6be709c0 commented Jun 19, 2023

Version of Hardhat

2.15.0

What happened?

I'm running into the same issue as this one #1692 from 2 years ago with the correct modification in ethers v6 but the contract.on is not triggered.

 await new Promise(async (resolve) => {
      contractA.on("TokensMinted", (amount: BigNumber) => {
        // THIS LINE NEVER GETS HIT
        console.log("###########");
        resolve(true);
      });

      const contractTx: ContractTransaction = await contractA.mint(123);
      const contractReceipt: ContractReceipt = await contractTx.wait();

      for (const event of contractReceipt.logs!) {
        console.log(event.fragment.name, event.args[0].toString()); // TokensMinted 123
      }

      // Works
      //  contractA.emit("TokensMinted", 123);
    });

Contract:

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.18;

contract ContractA {
    event TokensMinted(uint amount);

    function mint(uint amount) public {
        emit TokensMinted(amount);
    }
}

I'm adding the Promise to ensure to wait until the event is emitted.
From the receipt, the event is properly emitted, then it wait indefinitely because the event is never emitted.

If I emit the event manually using contractA.emit("TokensMinted", 123);, then I get the console.log and the promise is resolved.

Minimal reproduction steps

Repo to reproduce the issue easily: https://github.com/lescaudr/ethers-event-issue
pnpm install
pnpm compile
pnpm test

Search terms

No response

@fvictorio
Copy link
Member

Thank you @lescaudr, I can confirm this. It's an important problem, I'll try to get it fixed asap.

@fvictorio fvictorio added status:ready This issue is ready to be worked on type:bug Something isn't working area:hardhat-ethers and removed status:triaging labels Jun 20, 2023
@tevdoradze
Copy link

tevdoradze commented Jun 20, 2023

The same problem, .on is not working with hardhat node v2.12.3. Is there a version where it works?

@fvictorio
Copy link
Member

@tevdoradze it should still work if you use ethers v5 (and the associated plugins: @nomicfoundation/hardhat-toolbox@2, @nomiclabs/hardhat-ethers@2 and @nomicfoundation/hardhat-chai-matchers@1)

@fvictorio
Copy link
Member

Released in hardhat-ethers v3.0.3 🎉

@shaggyrec
Copy link

Released in hardhat-ethers v3.0.3 🎉

It still doesn't work in tests

@fvictorio
Copy link
Member

@shaggyrec if you have reproduction steps, please open an issue about it.

@6be709c0
Copy link
Author

6be709c0 commented Jul 2, 2023

I confirm it works perfectly @shaggyrec, see the branch "fixed" on https://github.com/lescaudr/ethers-event-issue where it's now using "hardhat": "^2.16.1". instead of 2.15.0

Thanks @fvictorio

@shaggyrec
Copy link

Yeah. Seems like working for your case and the standard hardhat Lock.sol contract (I rewrote the test with events to test events handling).

But for Patric Collins' tutorial updated to the latest versions it doesn't work.
https://github.com/shaggyrec/hardhat-smartcontract-lottery-ts/blob/main/test/unit/Raffle.test.ts#L163

I have no idea why.

@6be709c0
Copy link
Author

6be709c0 commented Jul 2, 2023

You are right @shaggyrec, indexed events are not triggered, I created a new issue #4098. Also found that when triggering multiple events, only the first is triggered.

@tevdoradze
Copy link

tevdoradze commented Jul 5, 2023

if it doesn't work in your tests you are most likely not waiting long enough:

  • Change the pollingInterval from 5s to 1s: yourcontract.provider.pollingInterval = 1;
  • Resolve the test with done:
it("test", function (done) {
   ...
      yourcontract.on(
        ...,
        async (...) => {
          ...
          done();
        }
      );
      ...
  });

If you work with an upgradable contract you may need to add // @ts-ignore above the line where we change the pollingInterval

@6be709c0
Copy link
Author

6be709c0 commented Jul 5, 2023

@tevdoradze polling interval is not the resolution here in ether v6. The bug has been confirmed, and is in progress

@Devankit2022
Copy link

update?

@6be709c0
Copy link
Author

6be709c0 commented Aug 2, 2023

It has been fixed 3 weeks ago #4098 (comment)

david-zk added a commit to zama-ai/fhevm-hardhat-template that referenced this issue Aug 8, 2023
+fix tests ( NomicFoundation/hardhat#4044 )
by upgrading @nomicfoundation/hardhat-ethers to 3.0.4
david-zk added a commit to zama-ai/fhevm-hardhat-template that referenced this issue Aug 8, 2023
+ fix ( NomicFoundation/hardhat#4044 )
by upgrading @nomicfoundation/hardhat-ethers to 3.0.4
+ add some docs
david-zk added a commit to zama-ai/fhevm-hardhat-template that referenced this issue Aug 21, 2023
+ fix ( NomicFoundation/hardhat#4044 )
by upgrading @nomicfoundation/hardhat-ethers to 3.0.4
+ add some docs
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area:hardhat-ethers status:ready This issue is ready to be worked on type:bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants