smartcontractkit / full-blockchain-solidity-course-js

Learn Blockchain, Solidity, and Full Stack Web3 Development with Javascript
11.94k stars 2.89k forks source link

Lesson07: Failed to get chainId, falling back to net_version #6450

Open caducus7 opened 5 months ago

caducus7 commented 5 months ago

Hello all, I'm having this issue on Lesson 07, when trying to deploy on Sepolia for the first time. I've been searching around but can't find a reason for this or tinker my way through to get the chainId to be read. I'm fairly sure it has something to do with the RPC_URL from alchemy but nothing has worked for me so far. Might it be a hardhat toolbox issue?

//hardhat-config.js

require("@nomicfoundation/hardhat-toolbox")
require("dotenv").config
require("@nomicfoundation/hardhat-verify")
require("hardhat-gas-reporter")
require("solidity-coverage")
require("hardhat-deploy")

/** @type import('hardhat/config').HardhatUserConfig */
const SEPOLIA_RPC_URL =
    process.env.SEPOLIA_RPC_URL ||
    "https://eth-sepolia.g.alchemy.com/v2/myappkey"
const PRIVATE_KEY =
    process.env.PRIVATE_KEY ||
    "myprivatekey"
const ETHERSCAN_API_KEY =
    process.env.ETHERSCAN_API_KEY || "etherscanapikey"
const COINMARKETCAP_API_KEY =
    process.env.COINMARKETCAP_API_KEY || "coimarketcapkey"

module.exports = {
    solidity: {
        compilers: [{ version: "0.8.8" }, { version: "0.6.6" }],
    },
    defaultNetwork: "hardhat",
    networks: {
        sepolia: {
            url: SEPOLIA_RPC_URL,
            chainId: 11155111,
            accounts: [PRIVATE_KEY],
            blockConfirmations: 6,
        },
        hardhat: {
            chainId: 31337,
        },
    },

//helper-hardhat-config if relevant

const networkConfig = {
    31337: {
        name: "localhost",
    },
    // Price Feed Address, values can be obtained at https://docs.chain.link/data-feeds/price-feeds/addresses
    11155111: {
        name: "sepolia",
        ethUsdPriceFeed: "0x694AA1769357215DE4FAC081bf1f309aDC325306",
    },
}

const developmentChains = ["hardhat", "localhost"]

module.exports = { networkConfig, developmentChains }

//01-deploy-fund-me relevant chainId code

const { verifyMessage } = require("ethers")
const { networkConfig, developmentChains } = require("../helper-hardhat-config")
const { network } = require("hardhat")
const { verify } = require("../utils/verify")
require("dotenv").config()

module.exports = async ({ getNamedAccounts, deployments }) => {
    const { deploy, log } = deployments
    const { deployer } = await getNamedAccounts()
    const chainId = network.config.chainId

    let ethUsdPriceFeedAddress
    if (chainId == 31337) {
        const ethUsdAggregator = await deployments.get("MockV3Aggregator")
        ethUsdPriceFeedAddress = ethUsdAggregator.address
    } else {
        ethUsdPriceFeedAddress = networkConfig[chainId]["ethUsdPriceFeed"]
    } 

Originally posted by @caducus7 in https://github.com/smartcontractkit/full-blockchain-solidity-course-js/issues/6296#issuecomment-1932691465

Screenshot_1

rocco1226 commented 5 months ago

I'm also a rookie for web3 development and I can't resolve the problem. But I think if you are fairly convinced that the issue was related to the RPC_URL from alchemy, you can choose another web3 development platform, such as infura, to try another RPC_URL.

caducus7 commented 5 months ago

But I think if you are fairly convinced that the issue was related to the RPC_URL from alchemy, you can choose another web3 development platform, such as infura, to try another RPC_URL.

You are correct and I tried that, but I still get the same problem. What I meant was the way the RPC_URL is being handled by the code has to be the issue, since the deployment isn't happening at all. Could it be a hardhat issue? Can't seem to figure this out..

rocco1226 commented 4 months ago

But I think if you are fairly convinced that the issue was related to the RPC_URL from alchemy, you can choose another web3 development platform, such as infura, to try another RPC_URL.

You are correct and I tried that, but I still get the same problem. What I meant was the way the RPC_URL is being handled by the code has to be the issue, since the deployment isn't happening at all. Could it be a hardhat issue? Can't seem to figure this out..

I had pushed my code in repo,you can check my code. My contract was deployed well.

tusharr1411 commented 4 months ago

It should be require("dotenv").config() instead of require("dotenv").config in your hardhat.config.js (parentheses after config)