[Ethereum] To get event args when I using ethers.js

ethers.jsevents

I'm a three months old programmer.
So if what I'm saying is ambiguous, please let me know and help me being a better programmer.

Now I'm coding a test code as using hardhat and ether.js.

My solidity contract is about orderbook token exchange.
When people make an order, the contract emit specific event named "Make".


event Make(
    bytes32 indexed orderId, 
    address indexed maker,
    address makerToken, 
    address takerToken,
    uint256 amountSell,
    uint256 amountBuy
);

orderId is made by hashing some data.

And I want to check if event is emitted correctly.
While coding test codes, I've spent quite a long time to know how to get emitted events.

finally I can get events using code below.
but what I really want is

  1. get 1st arg of event and put it a var. (in case, bytes32 hash)
    looks like, const hash = await events.args[0];

  2. compare 2nd arg of event and compare it with value. (in case, address maker)
    looks like, expect(await events.args[1]).to.equal(add1.address);

actually it's not a two question. just one.

here's my test code.

const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("EX contract", function () {
    let EX, ex, token, Token, add0, add1, add2, add3;

    before(async function () {
      EX = await ethers.getContractFactory("EX");
      Token = await ethers.getContractFactory("Token");
      ex = await EX.deploy(1000);
      token = await Token.deploy();
      [add0, add1, add2, add3] = await ethers.getSigners();
    });
  
    beforeEach(async function () {
      await token.mint(add1.address, 200000);
    });

    describe("Make / Take order", function () {
        await ex.connect(add1).makeOrder(token.address, 100, {value : 100});
        await ex.connect(add1).makeOrder(token.address, 100, {value : 200});
        
        let eventFilter = ex.filters.Make();
        let events = await ex.queryFilter(eventFilter, "latest");

        console.log(events);            //"events" contains too many information.
    });
});

Help me please, mates!

Best Answer

I suggest to try OpenZeppelin test helpers. They have a check expectEvent that reads events from the transaction's receipt.

In your case it should look like

it("Make / Take order", async function () {
  const receipt = await ex.connect(add1).makeOrder(token.address, 100, {value : 100});

  expectEvent(receipt, 'Make', {
    orderId: '<EXPECTED_ORDER_ID>',
    maker: '<EXPECTED_MAKER>',
  });
});
Related Topic