I'm using a library
for managing Events
. Now when I'm running the test case script then I'm getting an error.
ERROR
- 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.
- 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.
- Do I have to explicitly reference the library as I did in
Testcase.js
? - How to use this library in testcases.js?