【问题标题】:hardhat test: <functionName> is not a function安全帽测试:<functionName> 不是函数
【发布时间】:2021-12-15 13:43:08
【问题描述】:

我正在使用 BSC 的分支在安全帽网络上测试我的合约。

我正在部署具有铸币功能的代币合约:

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

然后我使用&gt; npx hardhat test 将其部署在测试中,它将运行此代码的测试:

...
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));
 });
 ...

测试打印前 2 个控制台调试正确:

 Token Contract Address: 0x5FbDB2315678afecb367f032d93F642f64180aa3
 Token totalSupply: 0

token.totalSupply() 也可以工作,因此令牌部署正确,但是当它必须调用 token.mint() 时会出现此错误:

 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)

我试图清理所有运行 &gt; npx hardhat clean 的工件并延迟所有缓存,但我仍然有错误

【问题讨论】:

  • mint() 函数是您的合约集中contract Token 或另一个合约(例如导入的库)的一部分吗?
  • @PetrHejda mint() 是contract Token 的一部分,如您所见,在mint() 内部有_mint() 是BEP20 导入合约的一部分,没有任何其他@ 987654335@函数

标签: blockchain solidity hardhat


【解决方案1】:

如果你声明了两个 mint 函数,那么你必须显式地使用完全限定的签名。 示例:

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

我在这里找到了解决方案 https://github.com/ethers-io/ethers.js/issues/407

【讨论】:

  • 这应该是公认的答案
【解决方案2】:

functionName is not a function如果该函数在您的智能合约中不可用,则会出现此错误。在您的情况下,函数名称不是 mint() 它是 _mint()。

【讨论】:

  • 正如您在问题中看到的,在合同中我有 mint() 函数(问题中的第一个代码)
猜你喜欢
  • 1970-01-01
  • 2022-07-21
  • 1970-01-01
  • 2022-12-23
  • 1970-01-01
  • 2023-04-09
  • 2022-08-09
  • 1970-01-01
  • 2021-10-01
相关资源
最近更新 更多