简体   繁体   中英

hardhat test: <functionName> is not a function

i'm testing my contracts on hardhat network with fork of BSC.

i'm deploying my token contract that have mint function:

// @dev 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);
}

then i'm deploying it on test using > npx hardhat test , it will run tests of this code:

...
it("Should deploy", async () => {
        token = await Token.deploy();
        await token.deployed();
        console.debug(`\t\t\tToken Contract Address: ${cyan}`, token.address);
        const supply = await token.totalSupply()
        console.debug(`\t\t\tToken totalSupply: ${yellow}`, supply);
        await token.mint(owner.address, web3.utils.toWei("1000", 'ether'))
        console.debug(`\t\t\tToken owner balance: ${cyan}`, token.balanceOf(owner.address));
 });
 ...

test print the first 2 console debug correctly :

 Token Contract Address: 0x5FbDB2315678afecb367f032d93F642f64180aa3
 Token totalSupply: 0

also token.totalSupply() works, so the token is deployed correctly, but when it have to call token.mint() it give this error:

 TypeError: token.mint is not a function
  at Context.<anonymous> (test/general.js:102:21)
  at runMicrotasks (<anonymous>)
  at processTicksAndRejections (internal/process/task_queues.js:95:5)

i tried to clean all the artifacts running > npx hardhat clean and delated all the cache, but i still have the error

if you declared two mint functions then you have to explicitly use the fully qualified signature. Example:

token["mint(address,uint256)"](owner.address, web3.utils.toWei("1000", 'ether'))

I found the solution here https://github.com/ethers-io/ethers.js/issues/407

functionName is not a function this error occurs if the function is not available in your smart contract. in your case the function name is not mint() it's _mint().

in beforeEach() block of code get your contract object by -

const token = await ethers.getContractFactory("Token")

If you haven't defined beforeEach() block define it before it() block

  beforeEach(async function () {
    Token = await ethers.getContractFactory("Token")
    token = await token.deploy()
  })

define Token and token using let keyword before, also somewhere in your code you may be missing await somewhere so that could be also the reason. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM