Lesson7: TypeError: Cannot read properties of undefined (reading '11155111') #5399
-
I'm having a problem as I'm trying to deploy to the sepolia testnet. Please can someone help me
Hardhat Config require("@nomicfoundation/hardhat-toolbox")
require("dotenv").config()
require("@nomiclabs/hardhat-etherscan")
require("hardhat-gas-reporter")
require("solidity-coverage")
require("hardhat-deploy")
require("@nomiclabs/hardhat-ethers")
/** @type import('hardhat/config').HardhatUserConfig */
const SEPOLIA_RPC_URL = process.env.SEPOLIA_RPC_URL
const PRIVATE_KEY = process.env.PRIVATE_KEY
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY || ""
const COINMARKETCAP_API_KEY = process.env.COINMARKETCAP_API_KEY
module.exports = {
solidity: {
compilers: [{ version: "0.8.18" }, { version: "0.6.6" }],
},
defaultNetwork: "hardhat",
networks: {
sepolia: {
url: SEPOLIA_RPC_URL,
accounts: [PRIVATE_KEY],
chainId: 11155111,
blockConfirmations: 6,
},
localhost: {
url: "http://127.0.0.1:8545/",
chainId: 31337,
},
},
etherscan: {
apiKey: {
sepolia: ETHERSCAN_API_KEY,
},
},
gasReporter: {
enabled: false,
outputFile: "gas-report.txt",
noColors: true,
currency: "USD",
coinmarketcap: COINMARKETCAP_API_KEY,
token: "MATIC",
},
namedAccounts: {
deployer: {
default: 0, // here this will by default take the first account as deployer
1: 0, // similarly on mainnet it will take the first account as deployer. Note though that depending on how hardhat network are configured, the account 0 on one network can be different than on another
},
},
} Verify const { run } = require("hardhat")
async function verify(contractAddress, args) {
console.log("Verifying contract...")
try {
await run("verify:verify", {
address: contractAddress,
constructorArgument: args,
})
} catch (e) {
if (e.message.toLowerCase().includes("already verified")) {
console.log("Already verified contract")
} else {
console.log(e)
}
}
}
module.exports = { verify } 01-deploy-fundme //imports
const { network } = require("hardhat")
const { networkConfig } = require("../helper-hardhat-config")
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
//When should use a mock when going for localhost network or hardhat
if (chainId === 31337) {
const ethUsdAggregator = await deployments.get("MockV3Aggregator")
ethUsdPriceFeedAddress = ethUsdAggregator.address
} else {
ethUsdPriceFeedAddress = networkConfig[chainId]["ethUsdPriceFeed"]
}
const args = [ethUsdPriceFeedAddress]
log("Deploying Fundme contract and waiting for confirmations...")
const fundMe = await deploy("FundMe", {
from: deployer,
args: args, //The Price Feed Address to enter the constructor in the contract.
log: true,
waitConfirmations: network.config.blockConfirmations || 1,
})
log(`Fudme contracts deployed at: ${fundMe.address}`)
log("___________________________________________")
// await verify(fundMe.address, args)
if (!chainId == 31337 && process.env.ETHERSCAN_API_KEY) {
await verify(fundMe.address, args)
}
}
module.exports.tabs = ["all", "fundme"] Helper Hardhat const networkConfig = {
31337: {
name: "localhost"
},
11155111: {
name: "sepolia",
ethUsdPriceFeed: "0x694AA1769357215DE4FAC081bf1f309aDC325306",
},
// 137: {
// name: "mumbai", //Polygon Testnet
// ethUsdPriceFeed: "0x0715A7794a1dc8e42615F059dD6e406A6594651A",
// },
}
const developmentChains = ["hardhat", "localhost"]
module.export = {
networkConfig,
developmentChains,
} Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Your code should work, I've tested it and it's working. Just check if you've installed the packages, especially dotenv. Then check your environment (make sure private key and RPC URL are alright), and then change your deploy-fund-me.js to this: const { network } = require('hardhat')
const { networkConfig, developmentChains } = require('../helper-hardhat-config')
const { verify } = require('../utils/verify')
module.exports = async ({ getNamedAccounts, deployments }) => {
const { deploy, log, get } = deployments
const { deployer } = await getNamedAccounts()
const chainId = network.config.chainId
let ethUsdPriceFeedAddress
if (developmentChains.includes(network.name)) {
const ethUsdAggregator = await get('MockV3Aggregator')
ethUsdPriceFeedAddress = ethUsdAggregator.address
} else {
ethUsdPriceFeedAddress = networkConfig[chainId]['ethUsdPriceFeed']
}
const args = [ethUsdPriceFeedAddress]
const fundMe = await deploy('FundMe', {
from: deployer,
args: args,
log: true,
waitConfirmations: network.config.blockConfirmations || 1,
})
if (
!developmentChains.includes(network.name) &&
process.env.ETHERSCAN_API_KEY
) {
await verify(fundMe.address, args)
}
log('---------------------------------------------')
}
module.exports.tags = ['all', 'fundme'] i hope this will fix your issue |
Beta Was this translation helpful? Give feedback.
-
Thank you. I found out that I don't have dotenv installed but as I tried to install It I got this error
And when I previously used this line of code
When I changed it to |
Beta Was this translation helpful? Give feedback.
-
@AKIN-THOMAS The error pops up because in the 01-deploy-fundme.js file, you are accessing The reason is, in you helper file you are not exporting the variables correctly; module.export = {
networkConfig,
developmentChains,
} it should be module.exports = {
networkConfig,
developmentChains,
} |
Beta Was this translation helpful? Give feedback.
@AKIN-THOMAS The error pops up because in the 01-deploy-fundme.js file, you are accessing
networkConfig
from helper file which tried to read the network setting for it11155111
but thenetworkConfig
variable is undefined in the deploy file.The reason is, in you helper file you are not exporting the variables correctly;
it should be
module.exports
notmodule.export
;