Hardhat部署NFT合约报错:TypeError: AstroMint.deploy is not a function
问题:Hardhat部署NFT合约时出现
TypeError: AstroMint.deploy is not a function 执行部署命令:
npx hardhat run scripts/deploy.js --network dexitTestnet
出现如下错误:
TypeError: AstroMint.deploy is not a function at main (/home/astrodude/dxt/astro-mint/scripts/deploy.js:16:39) at Object.<anonymous> (/home/astrodude/dxt/astro-mint/scripts/deploy.js:25:1) at Module._compile (node:internal/modules/cjs/loader:1101:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) at node:internal/main/run_main_module:17:47
我的deploy.js代码
const hardhat = require("hardhat"); const { MerkleTree } = require('merkletreejs'); const keccak256 = require('keccak256'); const whiteList = require('../utils/whitelist'); const BASE_URI = 'ipfs://Qmb5A1fFECM2iFHgUioii2khT814nCi6VU9aHXHHqNxHCK/'; const proxyRegistryAddress = '0xf57b2c51ded3a29e6891aba85459d600256cf317'; async function main() { const leadNodes = whiteList.map(address => keccak256(address)); const merkleTree = new MerkleTree(leadNodes, keccak256, { sortPairs : true }); const root = merkleTree.getRoot(); const AstroMint = hardhat.ethers.getContractFactory('AstroMint'); const astroMint = await AstroMint.deploy(BASE_URI, root, proxyRegistryAddress); await astroMint.deployed(); console.log('Astro mint is deployed to ', astroMint.address); } // We recommend this pattern to be able to use async/await everywhere // and properly handle errors. main().catch((error) => { console.error(error); process.exitCode = 1; });
我的package.json代码
{ "name": "astro-mint", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "@next/font": "13.1.2", "@nomicfoundation/hardhat-chai-matchers": "^1.0.0", "@nomicfoundation/hardhat-network-helpers": "^1.0.0", "@nomicfoundation/hardhat-toolbox": "^2.0.1", "@nomiclabs/hardhat-ethers": "^2.0.0", "@nomiclabs/hardhat-etherscan": "^3.0.0", "@nomiclabs/hardhat-waffle": "^2.0.3", "@openzeppelin/contracts": "^4.8.1", "@typechain/ethers-v5": "^10.1.0", "@typechain/hardhat": "^6.1.2", "@types/chai": "^4.2.0", "@types/mocha": ">=9.1.0", "bnc-onboard": "^1.39.1", "chai": "^4.2.0", "dotenv": "^16.0.3", "ethers": "^5.7.2", "hardhat" : "2.12.6", "hardhat-gas-reporter": "^1.0.8", "keccak256": "^1.0.6", "merkletreejs": "^0.3.9", "next": "13.1.2", "react": "18.2.0", "react-dom": "18.2.0", "solidity-coverage": "^0.8.1", "ts-node": ">=8.0.0", "typechain": "^8.1.0", "typescript": ">=4.5.0" }, "devDependencies": { "autoprefixer": "^10.4.13", "postcss": "^8.4.21", "tailwindcss": "^3.2.4" } }
我已经在网上搜索相关解决方案但未找到有效答案,多次重装Hardhat也无法解决该问题。
解决方案
问题核心是getContractFactory是异步方法,你没有添加await,导致AstroMint拿到的是Promise对象而非合约工厂实例,因此没有deploy方法。
修改deploy.js中的对应代码:
// 原代码 const AstroMint = hardhat.ethers.getContractFactory('AstroMint'); // 修改为 const AstroMint = await hardhat.ethers.getContractFactory('AstroMint');
同时建议检查以下几点确保部署流程正常:
- 确认合约文件
AstroMint.sol存在于contracts/目录下,合约名称与调用时的'AstroMint'完全一致(大小写敏感) - 执行
npx hardhat compile重新编译合约,确保生成了正确的合约artifact - 确保hardhat.config.js中已正确配置dexitTestnet网络的参数
内容的提问来源于stack exchange,提问作者bela jonas
相关产品推荐
相关产品推荐

