1

file Library

library Events {
    error Mint();
}

file Contract calling the event

emit Event.Mint()

Can't get Chai detect the event that is in the library

Throw this error in both cases: AssertionError: Event "Events.Mint" doesn't exist in the contract

try:

await expect(contract.connect(minter).mint(data)).to.emit(contract, 'Events.Mint')

or

await expect(contract.connect(minter).mint(data)).to.emit(contract, 'Mint')

is this possible?

2 Answers 2

2

When you have a contract that emits an event in an underlying library instead of the contract itself, since the contract doesn't contain the ABI of the event you probably face this error:

AssertionError: Expected event "MyEvent" to be emitted, but it doesn't exist in the contract. Please make sure you've compiled its latest version before running the test.

to fix it you can create a contract instance with the library ABI (The one that contains the event) and the contract address.

it's possible in two ways:

contractInstance is the instance with the contract ABI

MyEvent is the event emits in the library

EventLibrary is the library that emits the event

EventLibraryInstance is the library instance that has been used in deploying the contract

contractInstanceWithLibraryABI is the instance with the library ABI and the contract address

first:

let contractInstanceWithLibraryABI = await ethers.getContractAt("EventLibrary", contractInstance.address, signer)

await expect(
     await contractInstance.myFunction()
).to.emit(contractInstanceWithLibraryABI, "MyEvent")

second:

contractInstanceWithLibraryABI = await EventLibraryInstance.attach(
    contractInstance.address
)

await expect(
     await contractInstance.myFunction()
).to.emit(contractInstanceWithLibraryABI, "MyEvent")

0

According to this post, it is possible to by defining the event in your main contract as well. Here is an example:

library EventEmitterLib {
    function emit(string s) {
        Emit(s);
    }
    
    event Emit(string s);
}

contract EventEmitterContract {
    using EventEmitterLib for string;
    
    function emit(string s) {
        s.emit();
    }
    
    event Emit(string s);
}

However, I'm confused because your event is actually an error in a library named Event.

2
  • Thanks for your reply. I was trying to group all the events in a separate file. So I made the library. Finally, I decided to continue incluiding the events in the contract.
    – Tomis
    Commented Sep 6, 2022 at 13:37
  • I was doing the same approach with errors (including all in a separate file in a library) and chai is working ok. I thought that I was doing a mistake.
    – Tomis
    Commented Sep 6, 2022 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.