0

I'm using a library for managing Events. Now when I'm running the test case script then I'm getting an error.

ERROR

  1. If I don't mention the library when I'm deploying the Users.sol contract.
AssertionError: Expected event "CreateUser" to be emitted, but it doesn't exist in the contract.
  1. When I deploy the Users.sol with the library(see Testcases.js) file.
NomicLabsHardhatPluginError: You tried to link the contract Users with Events, which is not one of its libraries.
This contract doesn't need linking any libraries.

Library: Myevents.sol

pragma solidity ^0.8.13;

library Myevents {
    
    event CreateUser(
        address indexed userAddress,
        uint256 duration
    );
}

SmartContract: Users.sol

pragma solidity ^0.8.13;

import {Myevents} from "./libraries/Myevents.sol";

contract Users {

  function createUser(address _user, uint256 _duration) {

      // some functionality
      emit Myevents.CreateUser(_user, _duration);

   }
}

Testcase.js

// deploying library
const Events = await ethers.getContractFactory("MyEvents");
const events = await Events.deploy(); 
console.log("Events library address:", events.address);

// using lib with contract
const user = await ethers.getContractFactory("Users", {
    libraries: { 
       Myevents: events.address,
}});

it("Test 1.01 Users | CreateUser ---> create user", async function () {
    await expect(
      user
    .connect(myAddress)
    .createUser(userAddress, duration)
    )
      .to.emit(events, "CreateUser")
      .withArgs(myAddress.address, duration);
  });

I have a couple of questions.

  1. Do I have to explicitly reference the library as I did in Testcase.js?
  2. How to use this library in testcases.js?

2 Answers 2

0
  1. You do not, this error explicitly tells you not to
NomicLabsHardhatPluginError: You tried to link the contract Users with Events, which is not one of its libraries.
This contract doesn't need linking any libraries.
  1. Just remove the second param. When your contract gets compiled it automatically glues the lib to it. so you will have
const user = await ethers.getContractFactory("Users");
0

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")

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.