Search code examples
javascriptethers.jshardhat

Error deploying smart contract to goerli testnet using hardhat and alchemy : Cannot read properties of null (reading 'sendTransaction')


Getting the error while trying to deploy a smart contract from hardhat. I've already read: Error deploying smart contract using hardhat -- Cannot read property 'sendTransaction' of null and: Error deploying smart contract using Hardhat -- Error HH9: Error while loading Hardhat's configuration That still does not solve my problems and the topics looks like closed with solutions. Error details:

TypeError: Cannot read properties of null (reading 'sendTransaction')
    at ContractFactory.<anonymous> (E:\CryptoDev\hardhat-simple-storage\node_modules\@ethersproject\contracts\src.ts\index.ts:1247:38)
    at step (E:\CryptoDev\hardhat-simple-storage\node_modules\@ethersproject\contracts\lib\index.js:48:23)
    at Object.next (E:\CryptoDev\hardhat-simple-storage\node_modules\@ethersproject\contracts\lib\index.js:29:53)
    at fulfilled (E:\CryptoDev\hardhat-simple-storage\node_modules\@ethersproject\contracts\lib\index.js:20:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

My hardhat.config.js file:

require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();

const GOERLI_RPC_URL = process.env.GOERLI_RPC_URL;
const PRIVATE_KEY = process.env.PRIVATE_KEY;

module.exports = {
  defaultNetwork: "hardhat",
  networks: {
    goerli: {
      url: "https://eth-goerli.g.alchemy.com/v2/K9IsbfM7Z0jHrR5VTyg0rOsu0ghafL9D",
      accounts: PRIVATE_KEY,
      chainId: 5,
    },
  },
  solidity: "0.8.9",
};

My deploy.js:

const { ethers } = require("hardhat");

async function main() {
  const SimpleStorageFactory = await ethers.getContractFactory("SimpleStorage");
  console.log("Depoying contract. Please wait...");
  const simpleStorage = await SimpleStorageFactory.deploy();
  await simpleStorage.deployed();
  console.log(`Deployed contract address: ${simpleStorage.address}`);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exitCode = 1;
  });

In addition when I use:

goerli: {
      url: GOERLI_RPC_URL,
      accounts: PRIVATE_KEY,
      chainId: 5,
    },

it throws another error:

 * Invalid value undefined for HardhatConfig.networks.goerli.url - Expected a value of type string.

Any idea how to fix at least : TypeError: Cannot read properties of null (reading 'sendTransaction') error since it doesn't let me to continue.


Solution

  • keep the value of the accounts in an array

     networks: {
        goerli: {
          url: "https://eth-goerli.g.alchemy.com/v2/K9IsbfM7Z0jHrR5VTyg0rOsu0ghafL9D",
          **accounts: [PRIVATE_KEY]**,
          chainId: 5,
        },
      },