Hardhat Lottery : deployer undefined when deploy on Rinkeby #2051
-
In lesson 9 (hardhat Lottery), after the test, when I try to deploy on Rinkeby, I've got the following error :
After digging (putting some console.log to know which variable is undefined), I've found that it's the variable deployer that is undefined. 01-deploy-raffle.js : const { network, ethers } = require("hardhat")
const { developmentChains, networkConfig } = require("../helper-hardhat-config")
const { verify } = require("../utils/verify")
module.exports = async ({ getNamedAccounts, deployments }) => {
const { deploy, log } = deployments // same than declaring deployments.deploy and deployments.log
const { deployer } = await getNamedAccounts()
const chainId = network.config.chainId
let vrfCoordinatorV2Address, subscriptionId
const VRF_SUB_FUND_AMOUNT = ethers.utils.parseEther("2")
if (developmentChains.includes(network.name)) {
const vrfcoordinatorV2Mock = await ethers.getContract("VRFCoordinatorV2Mock") // establish the connexion to the mock contract here
vrfCoordinatorV2Address = vrfcoordinatorV2Mock.address
//below we programmatically create a subscription on chainlinkCRF by using some function
const transactionResponse = await vrfcoordinatorV2Mock.createSubscription()
const transactionReceipt = await transactionResponse.wait(1) // wait one block
subscriptionId = transactionReceipt.events[0].args.subId // here we grab the subscription id from an event who was generated by the contract vrf
await vrfcoordinatorV2Mock.fundSubscription(subscriptionId, VRF_SUB_FUND_AMOUNT) // here we suscribe VRF, with amount define above
} /*meaning we are not on a devchain but on testnet or mainnet*/ else {
vrfCoordinatorV2Address = networkConfig[chainId]["vrfCoordinatorV2"] // syntaxe to call the chainlink contract address present in helper-hardhat-config
console.log("vrf coordinator address : " + vrfCoordinatorV2Address)
subscriptionId = networkConfig[chainId]["subscriptionId"] //calling the data in the helper
console.log("subscription id : " + subscriptionId)
}
const entranceFee = networkConfig[chainId]["entranceFee"]
console.log("Entrance Fee : " + entranceFee)
const arg = []
console.log("Arg: " + arg)
const gasLane = networkConfig[chainId]["gasLane"]
console.log("Gas Lane : " + gasLane)
const callbackGasLimit = networkConfig[chainId]["callbackGasLimit"]
console.log("callback gas limit : " + callbackGasLimit)
const interval = networkConfig[chainId]["interval"]
console.log("interval : " + interval)
console.log("deployer: " + deployer)
const raffle = await deploy("Raffle", {
from: deployer,
args: [
vrfCoordinatorV2Address,
entranceFee,
gasLane,
subscriptionId,
callbackGasLimit,
interval,
],
log: true,
waitConfirmations: network.config.blockConfirmations || 1,
})
console.log("Raffles : " + raffle)
// if we are not on dev chain (hardhat) we verify with the code below
if (!developmentChains.includes(network.name) && process.env.ETHERSCAN_API_KEY) {
log("Verifying the contract on Etherscan....")
await verify(raffle.address, arg)
}
log("------------------------------------")
}
module.exports.tags = ["all", "raffle"] hardhat.config.js file : require("@nomiclabs/hardhat-waffle")
require("@nomiclabs/hardhat-etherscan")
require("hardhat-deploy")
require("solidity-coverage")
require("hardhat-gas-reporter")
require("hardhat-contract-sizer")
require("dotenv").config()
/** @type import('hardhat/config').HardhatUserConfig */
const RINKEBY_RPC_URL = process.env.RINKEBY_RPC_URL
const PRIVATE_KEY = process.env.PRIVATE_KEY
const COINMARKETCAP_API_KEY = process.env.COINMARKETCAP_API_KEY
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY
module.exports = {
defaultNetwork: "hardhat",
networks: {
hardhat: {
chainId: 31337,
blockConfirmations: 1,
},
rinkeby: {
chainId: 4,
blockConfirmations: 6,
url: RINKEBY_RPC_URL,
account: PRIVATE_KEY,
},
},
gasReporter: {
// this will generate a gaz report .txt file
enabled: false,
outputFile: "gas-report.txt",
noColors: true,
currency: "USD",
//coinmarketcap: COINMARKETCAP_API_KEY,
//token: "ETH",
},
solidity: "0.8.8",
namedAccounts: {
deployer: {
default: 0,
1: 0,
},
player: {
default: 1,
},
},
mocha: {
timeout: 300000, // 200 Seconds max
},
} helper-hardhat-config.js file : const { ethers } = require("hardhat")
const networkConfig = {
4: {
name: "rinkeby",
vrfCoordinatorV2: "0x6168499c0cFfCaCD319c818142124B7A15E857ab", // this is the coordinator chainlink adress
// for rinkeby, can be found here : https://docs.chain.link/docs/vrf/v2/supported-networks/
entranceFee: ethers.utils.parseEther("0.01"), // definition of the entrance fee on rinkeby
gasLane: "0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc", // this is the 30 Gwey hash (only one) in the rinkkeby docs
// can be found here : https://docs.chain.link/docs/vrf/v2/supported-networks/
subscriptionId: "19476", // this number is the subcription id from chainlik VRF
callbackGasLimit: "500000",
interval: "30",
},
31337: {
name: "hardhat",
// no vrfcoordinator because we use the mock
entranceFee: ethers.utils.parseEther("0.01"),
gasLane: "0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc",
callbackGasLimit: "500000",
interval: "30",
},
}
const developmentChains = ["hardhat", "localhost"]
module.exports = {
networkConfig,
developmentChains,
}
The .env file is filled with the good RPC link from alchemy, and the private key is the one from my metamask, connected to rinkeby. Can somebody please explain to me where is the mistake. And I don't really understand how the named account work (what 0: and 1: means exactly) Thanks very much for your answer. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
There is an error in your hardhat.config file in the rinkeby section. |
Beta Was this translation helpful? Give feedback.
There is an error in your hardhat.config file in the rinkeby section.
account: PRIVATE_KEY
should beaccounts: [PRIVATE_KEY]