You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署ETH ERC20智能合约遇TypeError:testToken.deployed不是函数

部署ERC20Capped合约时遇到TypeError: testToken.deployed is not a function

我正在基于OpenZeppelin的ERC20Capped开发首个以太坊智能合约TestToken,使用Hardhat(版本2.17.2)部署时触发错误:TypeError: testToken.deployed is not a function。已自行排查3天,尝试安装hardhat-ethers后仍未解决,相关代码及错误信息如下:

合约代码(contracts/TestToken.sol)

//contracts/TestToken.sol
//SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";


contract TestToken is ERC20Capped {

    address payable public owner;
    constructor(uint256 cap) ERC20("TestToken", "TST") ERC20Capped(cap * (10 ** decimals()))
    {  
        owner = payable(msg.sender);
        _mint(owner, 333666999000 * (10 ** decimals()));
    }

    function _mint(address account, uint256 amount) internal virtual override {
        require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
        super._mint(account, amount);
    }

    function destroy() public onlyOwner {
        selfdestruct(owner);
    }

    modifier onlyOwner {
        require(msg.sender == owner, "Only the owner can call this function");
        _;
    }
}

部署脚本(scripts/deploy.js)

const hre = require("hardhat");

async function main() {
  const TestToken = await hre.ethers.getContractFactory("TestToken");
  const testToken = await TestToken.deploy(333666999000);

  await testToken.deployed();

  console.log("Test Token deployed: ", testToken.address);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Hardhat配置文件(hardhat.config.js)

require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.17",
  networks: {
    goerli: {
      url: process.env.INFURA_GOERLI_ENDPOINT,
      accounts: [process.env.PRIVATE_KEY]
    }
  }
};

终端错误信息

TypeError: testToken.deployed is not a function
at main (C:\Users\XXXX\OneDrive\Desktop\ERC 20\test-token\scripts\deploy.js:7:19)
at processTicksAndRejections (node:internal/process/task_queues:95:5)

问题原因及解决方案

这个错误源于Hardhat集成的Ethers.js版本升级到v6+后,移除了deployed()方法。在Ethers v6中,deploy()方法会自动等待合约部署完成并返回已就绪的合约实例,无需额外调用deployed()。同时,获取合约地址的方式也从直接访问.address属性改为调用getAddress()方法。

修改后的部署脚本如下:

const hre = require("hardhat");

async function main() {
  const TestToken = await hre.ethers.getContractFactory("TestToken");
  // deploy()方法已自动处理部署确认,无需deployed()
  const testToken = await TestToken.deploy(333666999000);

  // Ethers v6中使用getAddress()获取合约地址
  console.log("Test Token deployed: ", await testToken.getAddress());
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

额外注意事项:

  1. 检查package.json中的@nomicfoundation/hardhat-toolbox版本,v3及以上版本默认集成Ethers v6,若要继续使用旧版v5写法,需安装@nomicfoundation/hardhat-toolbox@2.x.x版本。
  2. 合约中selfdestruct函数在Solidity 0.8.17中已被标记为过时,建议后续替换为自定义的资产清理逻辑,但这不影响当前部署问题的解决。
  3. 确认.env文件中的Infura节点地址和钱包私钥配置正确,避免部署时出现网络连接或权限问题。

内容的提问来源于stack exchange,提问作者Slasher Yoshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 20:55:31