0

When I use the hardhat tests, I got mintToken.owner is not a function , how to solve it ? Not just it ,all function i can not use

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


describe("测试模块", async function () {

    async function loadContract() {
        let MintToken = await ethers.getContractFactory("MintToken");
        let mintToken = MintToken.deploy("Victory");
        return mintToken;
    }


    it("查询owner是否正确", async function () {
        const mintToken = loadContract();
        let [signerAddress] = await ethers.provider.listAccounts();
        assert.equal(await mintToken.owner(), signerAddress);
    });
    it("查看部署的交易", async function () {
        const mintToken = loadContract();
        //console.log((await mintToken).deployTransaction);
    });
    it("查看合约的token余额", async function () {
        const mintToken = loadContract();

    });
});

enter image description here

and my contract code following:

pragma solidity >=0.8.0;

// SPDX-License-Identifier: UNLICENSED
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./Test.sol";

contract MintToken is ERC20, Test {
    uint256 private _initialSupply = 1000 * 10**4 * 10**18;

    constructor(string memory tokenname) ERC20(tokenname, "VCY") {
        _mint(msg.sender, _initialSupply);
    }

    //该代币总量1000万枚,具有白名单机制,只有白名单用户才可以mint。
    //这个_addr 和 具体的_amount在后端服务器上处理
    function whitelistMint(
        address _addr,
        bytes32[] memory proof,
        uint256 _amount
    ) external {
        require(verify(proof) == true);
        _mint(_addr, _amount);
    }

    function withdrawRemain() public onlyOwner {
        uint256 amount = address(this).balance;
        _transfer(address(this), msg.sender, amount);
    }

    function getName() public pure returns (string memory) {
        return "Victory";
    }
}
4
  • I'm guessing your mint token uses the Ownable lib from OpenZeppelin? Dec 11, 2022 at 12:44
  • yes From openzeppline ERC20
    – Hakeen
    Dec 11, 2022 at 12:52
  • @Hakeen Can you share the contract code? My guess is that you aren't inheriting from Ownable.
    – Ismael
    Dec 15, 2022 at 1:13
  • ok i have shard it plz
    – Hakeen
    Dec 15, 2022 at 8:43

1 Answer 1

0

Yes the error is correct since openzeppelin erc20 contract which you are inheriting didn't have owner state variable as globally declared, and your's MintToken contract also don't have that too.

2
  • should i inherit the ownable contract ?
    – Hakeen
    Dec 15, 2022 at 10:59
  • Yeah u can do that, or simply declare a new state variable 'address public owner', and set it in constructor in the MinToken contract. And I suggest you to change the contract name since MintToken sounds like some function but it's actually a token since inheriting erc20. Dec 15, 2022 at 12:31

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.