admin管理员组

文章数量:1353145

This is my hardhat.config.js -


module.exports = {
  solidity: "0.8.4",
  networks: {
    hardhat: {
      chainId: 1337
    },
    mumbai: {
      url: ";,
      accounts: [process.env.pk]
    },
    // polygon: {
    //   url: "/",
    //   accounts: [process.env.pk]
    // }
  }
};

When running npx hardhat test the following error appears:

**Error HH8: There's one or more errors in your config file:
   Invalid account: #0 for network: mumbai - Expected string, received undefined**`

Seems I have a couple of errors with my hardhat.config.js file but can't locate. I am using Nader Dabit's full-stack-web3 tutorial for full stack web3 development.

This is my hardhat.config.js -


module.exports = {
  solidity: "0.8.4",
  networks: {
    hardhat: {
      chainId: 1337
    },
    mumbai: {
      url: "https://rpc-mumbai.matic.today",
      accounts: [process.env.pk]
    },
    // polygon: {
    //   url: "https://polygon-rpc./",
    //   accounts: [process.env.pk]
    // }
  }
};

When running npx hardhat test the following error appears:

**Error HH8: There's one or more errors in your config file:
   Invalid account: #0 for network: mumbai - Expected string, received undefined**`

Seems I have a couple of errors with my hardhat.config.js file but can't locate. I am using Nader Dabit's full-stack-web3 tutorial for full stack web3 development.

Share Improve this question edited Jan 2, 2023 at 13:08 Sunil Sharma 1,3153 gold badges17 silver badges31 bronze badges asked Mar 20, 2022 at 1:47 ptm12ptm12 112 silver badges3 bronze badges 1
  • 2 Your process.env.pk is undefined. Just make sure its set properly. – skara9 Commented Mar 20, 2022 at 2:24
Add a ment  | 

3 Answers 3

Reset to default 11

Correct:

 mumbai: {
  url: "https://rpc-mumbai.matic.today",
  accounts: process.env.pk

Incorrect:

 mumbai: {
  url: "https://rpc-mumbai.matic.today",
  accounts: [process.env.pk] <-- remove the array

The issue here is with how the accounts datatype is defined.

accounts is of type HardhatNetworkAccountsUserConfig. It contains an array of account objects, which is of type HardhatNetworkAccountUserConfig.

(Notice how accounts has 'Accounts' in this type name while account has 'Account')

Now, this account is an object that contains two values: private key and balance . You can read more about it here.

This is enough background knowledge. Let's jump into the code.

First, define accounts as such:

import {HardhatNetworkAccountsUserConfig} from "hardhat/types/config";
import { ethers } from "ethers";

const accounts: HardhatNetworkAccountsUserConfig = [
  {
    privateKey: process.env.pk!,
    balance: ethers.utils.parseEther('10000').toString()
  }
]

Notice the '!' after process.env.pk. The reason for its addition is well explained in this StackOverflow thread.

Now, define the config using the code below:

module.exports = {
  solidity: "0.8.4",
  networks: {
    hardhat: {
      chainId: 1337
    },
    mumbai: {
      url: "https://rpc-mumbai.matic.today",
      accounts
    // polygon: {
    //   url: "https://polygon-rpc./",
    //   accounts
    // }
  }
};

npx hardhat node should work fine now.

I had the same issue and solved it a different way because it was still saying invalid account (although I was on the goerli testnet). I figured out you need to put a 0x in front of the private key so I added an OR statement to the PRIVATE_KEY variable as follows:

const PRIVATE_KEY =
    process.env.PRIVATE_KEY ||
    "0xfake1private2key3fake4private5key6fake7private8key"

and then used that variable as follows:

networks: {
        goerli: {
            url: GOERLI_RPC_URL,
            accounts: [PRIVATE_KEY],
        },

本文标签: