Hardhat部署合约报错:TypeError: simpleStorage.deployed is not a function
解决Hardhat部署合约时的
deployed is not a function错误 问题现象
执行部署命令:
yarn hardhat run scripts/deploy.js
出现以下错误:
Deploying contract... TypeError: simpleStorage.deployed is not a function at main (/home/himdipsinh/folder/hardhat-simple-storage-mt/scripts/deploy.js:33:25) at processTicksAndRejections (node:internal/process/task_queues:95:5) error Command failed with exit code 1.
预期输出:
Deploying contract... Deployed contract to: "some address here"
原因
你的项目使用的是ethers v6(package.json中ethers: "^6.4.0")和适配v6的Hardhat插件(如@nomicfoundation/hardhat-ethers: "^3.0.0"),而deployed()是ethers v5中的方法,v6已经移除了这个API。
在ethers v6中,SimpleStorageFactory.deploy()本身就是一个异步操作,执行完成后直接返回已部署完成的合约实例,无需额外调用deployed()等待。
解决方案
修改部署脚本,删除await simpleStorage.deployed()这一行即可。如果需要等待交易被链上确认,可以替换为await simpleStorage.deploymentTransaction().wait()(可传入数字指定确认区块数,如wait(1))。
修改后的部署脚本:
const { ethers } = require("hardhat") async function main() { const SimpleStorageFactory = await ethers.getContractFactory("SimpleStorage") console.log("Deploying contract...") const simpleStorage = await SimpleStorageFactory.deploy() // 可选:等待1个区块确认,不需要可删除这行 await simpleStorage.deploymentTransaction().wait(1) console.log(`Deployed contract to: ${simpleStorage.address}`) } main() .then(() => process.exit(0)) .catch((error) => { console.error(error) process.exit(1) })
验证
重新执行部署命令:
yarn hardhat run scripts/deploy.js
即可得到预期的部署成功输出。
内容的提问来源于stack exchange,提问作者Himdipsinh Chudasama
相关产品推荐
相关产品推荐

