Hardhat

“HardhatUserConfig”類型中不存在“namedAccounts”

  • September 14, 2022

無法將“namedAccounts”添加到 hardhat.config.ts 中的“HardhatUserConfig”,因為它會引發以下編譯錯誤。

'namedAccounts' does not exist in type 'HardhatUserConfig'

下面是我的配置文件:

import "@nomiclabs/hardhat-etherscan";
import "hardhat-gas-reporter";
import * as dotenv from 'dotenv';
import { HardhatUserConfig } from "hardhat/config"
dotenv.config();

const RINKEBY_RPC_URL = process.env.RINKEBY_RPC_URL || "";
const PRIVATE_KEY = process.env.PRIVATE_KEY || "";
const ETHERSCAN_APIKEY = process.env.ETHERSCAN_APIKEY;
const GOERLI_RPC_URL = process.env.GOERLI_RPC_URL || "";

const config: HardhatUserConfig = {
 solidity: "0.8.7",
 defaultNetwork: "hardhat",
 networks: {
   rinkeby: {      
     url: RINKEBY_RPC_URL,
     accounts: [PRIVATE_KEY],
     chainId: 4
   },     
   goerli: {
     url: GOERLI_RPC_URL,
     accounts: [PRIVATE_KEY],
     chainId: 5
   },
   localhost: {
     url: 'http://127.0.0.1:8545/',
     chainId: 31337      
   },   
 },
 etherscan: {
   apiKey: ETHERSCAN_APIKEY
 },
 gasReporter: {
   enabled: true,
   outputFile: "gas-report.txt",
   noColors: true    
 },
 //This throws error
 namedAccounts: {
   deployer: {
     default: 0
   },
   player: {
     default: 1
   }
 },
}

export default config;

這些是我正在使用的軟體包版本。

{
 "name": "sample-project",
 "devDependencies": {
   "@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers@^0.3.0-beta.13",
   "@nomiclabs/hardhat-etherscan": "^3.1.0",
   "@typechain/ethers-v5": "^10.1.0",
   "@typechain/hardhat": "^6.1.2",
   "@types/chai": "^4.3.3",
   "@types/node": "^18.7.15",
   "chai": "^4.3.6",
   "dotenv": "^16.0.2",
   "ethereum-waffle": "^3.4.4",
   "ethers": "^5.7.0",
   "hardhat": "^2.11.0",
   "hardhat-contract-sizer": "^2.6.1",
   "hardhat-deploy": "^0.11.13",
   "hardhat-gas-reporter": "^1.0.9",
   "prettier": "^2.7.1",
   "prettier-plugin-solidity": "^1.0.0-beta.24",
   "solhint": "^3.3.7",
   "solidity-coverage": "^0.8.0",
   "ts-node": "^10.9.1",
   "typechain": "^8.1.0",
   "typescript": "^4.8.2"
 },
 "dependencies": {
   "@chainlink/contracts": "^0.4.1"
 }
}

您需要導入hardhat-deploy才能擴展配置類型。

範例(es6):

import { HardhatUserConfig } from "hardhat/config";
import "hardhat-deploy"; // add this line

const config: HardhatUserConfig = {
 solidity: "0.8.9",
 namedAccounts: {
   deployer: { default: 0 },
 },
};

export default config;

在此處查看具有復雜配置的範例項目。

引用自:https://ethereum.stackexchange.com/questions/135618