2

I'm using hardhat as a framework to develop my smartcontracts. So far it's been great, but im clueless as to why I can't call the mint function from my tests. Here's the error message:

Token.connect(...).mint is not a function

Here's a skinned down version of my TESTing code:

describe("Token contract", function () {
    let TokenContract;
    let Token;
    beforeEach(async () => {
        TokenContract = await ethers.getContractFactory("Token");
        [owner, alice, bob, carol, fakeRouter] = await ethers.getSigners();
        Token = await Contract.deploy(); //sender is by default owner/first address
     });
    it('onlyOwner modifier', async () => {
        await expect(Token.connect(alice).mint(bob.address, 
        10)).to.be.revertedWith('operator: caller is not the operator')
    });

Here is the mint function residing in my smartcontract code:

/// @notice Creates `_amount` token to `_to`. Must only be called by the owner(MasterChef).
function mint(address _to, uint256 _amount) public onlyOwner {
    _mint(_to, _amount);
    _moveDelegates(address(0), _delegates[_to], _amount);
}

Anybody has an idea of what's going on?

1
  • What's Contract.deploy()? There's no definition of Contract in your snippet.
    – Ismael
    May 30, 2021 at 2:44

1 Answer 1

3

This was already answered in Hardhat's discord, but in case anyone ends up here...

The problem is that the mint function was overloaded. That is, the contract had two implementations of that function with different signatures:

contract Foo {
  function mint(uint amount) public { ... }
  function mint(uint amount, address to) public { ... }
}

When this happens, you need to use the full signature in the ethers.js contract as the name of the function:

await foo['mint(uint)'](123)

// or
await foo['mint(uint,address)'](123, "0x123...")

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.