1

The assertion for the following test fails:

    it('should access MAX_COUNT', async () => {
        const maxCount = await myContract.functions.MAX_COUNT();
        expect(maxCount).to.equal(64);
    });

With the following error:

AssertionError: expected [ BigNumber { value: "64" } ] to equal 64

This is the solidity code for (the concise version of) the smart contract being tested:

// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.17;

contract MyContract
{
    uint256 public constant MAX_COUNT = 64;
}

Why is the return value [ BigNumber { value: "64" } ] instead of just BigNumber { value: "64" }?


For context, this question was originally created while trying to figure out this one: How to correctly import @nomicfoundation/hardhat-chai-matchers into hardhat project? ... However turns out to be completely unrelated.

bguiz
  • 27,371
  • 47
  • 154
  • 243

1 Answers1

2

This test should fail:

    it('should access MAX_COUNT', async () => {
        const maxCount = await multiSend.functions.MAX_COUNT();
        expect(maxCount).to.equal(64);
    });

However, this should pass:

    it('should access MAX_COUNT', async () => {
        const maxCount = await multiSend.MAX_COUNT();
        expect(maxCount).to.equal(64);
    });

And this test should pass too:

    it('should access MAX_COUNT', async () => {
        const [maxCount] = await multiSend.functions.MAX_COUNT();
        expect(maxCount).to.equal(64);
    });

The clue is in the documentation, specifically the difference between the return types when called via Contract.functions.XYZ() vs Contract.XYZ():

docs for Contract.functions call

In summary Contract.XYZ() returns either Promise< any > or Promise< Result >; whereas Contract.functions.XYZ() always returns Promise< Result >

docs for Result return type

The Result object is designed for functions that can return multiple values etc, so that's why it it needs to be array-destructured.

bguiz
  • 27,371
  • 47
  • 154
  • 243