Hardhat

錯誤 HH8:您的配置文件中有一個或多個錯誤,應為 HttpNetworkConfig 類型的值

  • February 20, 2022

為什麼 Hardhat 會拋出錯誤?我從https://hardhat.org/config/複製並粘貼了預設配置文件?

*Error HH8: There's one or more errors in your config file:
Invalid value {"url":"https://eth-rinkeby.alchemyapi.io/v2/xxxxxxxxxx","accounts":[null]} for HardhatConfig.networks.rinkeby - Expected a value of type HttpNetworkConfig.*

To learn more about Hardhat's configuration, please go to https://hardhat.org/config/
/**
* @type import('hardhat/config').HardhatUserConfig
*/
require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-ethers");
require("@nomiclabs/hardhat-truffle5");
require("@nomiclabs/hardhat-etherscan");
require("hardhat-deploy");

require("dotenv").config();

const privateKey1 = process.env.PRIVATE_KEY;
const etherscanKey = process.env.ETHERSCAN_API_KEY;

module.exports = {
 defaultNetwork: "rinkeby",
 networks: {
   hardhat: {},
   rinkeby: {
     url: "https://eth-rinkeby.alchemyapi.io/v2/oL89FHmvkiWEfUgdjWON4NZBnB-497Is",
     accounts: [privateKey1],
   },
 },
 solidity: {
   version: "0.8.0",
   settings: {
     optimizer: {
       enabled: true,
       runs: 200,
     },
   },
 },
 paths: {
   sources: "./contracts",
   tests: "./test",
   cache: "./cache",
   artifacts: "./artifacts",
 },
 mocha: {
   timeout: 20000,
 },
};

必須執行source .env才能在 Ubuntu 20.04 LTS 中啟用 .env

您收到此錯誤是因為安全帽需要能夠驗證您用於網路的 URL 和帳戶對該網路有效。在您的特定情況下,您的私鑰不起作用。

兩種修復方法:

1.刪除你不想要的網路

在您的hardhat.config.js中,您可以註釋掉或刪除出錯的網路。你的錯誤是說:

Invalid value {"url":"https://eth-rinkeby.alchemyapi.io/v2/xxxxxxxxxx","accounts":[null]} for HardhatConfig.networks.rinkeby - Expected a value of type HttpNetworkConfig.*

所以它正在查看rinkebyURL 並沒有找到一個“好”的。將您的網路配置更改為:

       // rinkeby: {
       //     url: "https://eth-rinkeby.alchemyapi.io/v2/oL89FHmvkiWEfUgdjWON4NZBnB-497Is",
       //     accounts: [privateKey1],
       //   },

2.修復你的變數

您需要確保您的環境變數實際上被正確提取。做一個console.log(process.env.PRIVATE_KEY)console.log(process.env.RINKEBY_RPC_URL)是檢查的方法。或者,如果您在 linux 中,則執行echo $PRIVATE_KEY.

一旦你的所有變數都正常工作,配置就會工作

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