Lesson 9: Trouble in getting my contract verified on Etherscan #1629
-
I am at the end of lesson 9 and after writing the staging test, I used the below deploy command. yarn hardhat deploy --network rinkeby My contract was successfully compiled and deployed but for some reason it skipped the verify logic in my code. Nothing to compile
reusing "Raffle" at 0x8032523AEE8Ff14CFe022bf8EEeFaAc044028D17
<------------------------------------->
Done in 6.09s. Things I tried:
Added to this I tried some tinkering with my code and found out that most probable issue might be with process.env.ETHERCAN_API_KEY not returning 'true' in below piece of code. if (!developmentChains.includes(network.name) && process.env.ETHERCAN_API_KEY) {
log("Verifying...")
await verify(raffle.address, args)
}
log("<------------------------------------->") Result if I remove process.env.ETHERCAN_API_KEY check from above code. Nothing to compile
reusing "Raffle" at 0x8032523AEE8Ff14CFe022bf8EEeFaAc044028D17
Verifying...
Verifying contract...
NomicLabsHardhatPluginError: You are trying to verify a contract in 'rinkeby', but no API token was found for this network. Please provide one in your hardhat config. For example:
{
...
etherscan: {
apiKey: {
rinkeby: 'your API key'
}
}
}
I have attached my files below. Please take a look. const { network, ethers } = require("hardhat")
const { developmentChains, networkConfig } = require("../helper-hardhat-config")
const { verify } = require("../Utils/verify")
const VRF_SUB_FUND_AMOUNT = ethers.utils.parseEther("2")
module.exports = async function ({ getNamedAccounts, deployments }) {
const { deploy, log } = deployments
const { deployer } = await getNamedAccounts()
//These variables are coming from HRE
const chainId = network.config.chainId
let vrfCoordinatorV2Address
let subscriptionId
if (developmentChains.includes(network.name)) {
const vrfCoordinatorV2Mock = await ethers.getContract("VRFCoordinatorV2Mock")
vrfCoordinatorV2Address = vrfCoordinatorV2Mock.address
const transactionResponse = await vrfCoordinatorV2Mock.createSubscription()
const transactionReceipt = await transactionResponse.wait(1)
subscriptionId = transactionReceipt.events[0].args.subId
await vrfCoordinatorV2Mock.fundSubscription(subscriptionId, VRF_SUB_FUND_AMOUNT)
} else {
vrfCoordinatorV2Address = networkConfig[chainId]["vrfCoordinatorV2"]
subscriptionId = networkConfig[chainId]["subscriptionId"]
}
const entranceFee = networkConfig[chainId]["entranceFee"]
const gasLane = networkConfig[chainId]["gasLane"]
const callbackGasLimit = networkConfig[chainId]["callbackGasLimit"]
const interval = networkConfig[chainId]["interval"]
args = [
vrfCoordinatorV2Address,
entranceFee,
gasLane,
subscriptionId,
callbackGasLimit,
interval,
]
const raffle = await deploy("Raffle", {
from: deployer,
args: args,
log: true,
waitConfirmations: network.config.blockConfirmations || 1,
})
if (!developmentChains.includes(network.name) && process.env.ETHERCAN_API_KEY) {
log("Verifying...")
await verify(raffle.address, args)
}
log("<------------------------------------->")
}
module.exports.tags = ["all", "raffle"] helper-hardhat-config.js const { ethers } = require("hardhat")
const networkConfig = {
4: {
name: "rinkeby",
vrfCoordinatorV2: "0x6168499c0cFfCaCD319c818142124B7A15E857ab",
entranceFee: ethers.utils.parseEther("0.01"),
gasLane: "0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc",
subscriptionId: "10139",
callbackGasLimit: "500000",
interval: "30",
},
31337: {
name: "hardhat",
entranceFee: ethers.utils.parseEther("0.01"),
gasLane: "0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc",
callbackGasLimit: "500000",
interval: "30",
},
}
const developmentChains = ["hardhat", "localhost"]
module.exports = { networkConfig, developmentChains } verify.js const { run } = require("hardhat")
async function verify(contractAddress, args) {
console.log("Verifying contract...")
try {
await run("verify:verify", {
address: contractAddress,
constructorArguments: args,
})
} catch (e) {
if (e.message.toLowerCase().includes("already verified")) {
console.log("Already verified!")
} else {
console.log(e)
}
}
}
module.exports = { verify } hardhat.config.js 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()
const RINKEBY_RPC_URL = process.env.RINKEBY_RPC_URL || "RINKEBY"
const PRIVATE_KEY = process.env.PRIVATE_KEY || "KEY"
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY || "KEY"
module.exports = {
solidity: "0.8.7",
defaultNetwork: "hardhat",
networks: {
hardhat: {
chainId: 31337,
blockConfirmations: 1,
},
rinkeby: {
chainId: 4,
blockConfirmations: 6,
url: RINKEBY_RPC_URL,
accounts: [PRIVATE_KEY],
},
},
etherscan: {
apikey: ETHERSCAN_API_KEY,
},
gasReporter: {
enabled: false,
currency: "USD",
outputFile: "gas-report.txt",
noColors: true,
// coinmarketcap: process.env.COINMARKETCAP_API_KEY,
},
namedAccounts: {
deployer: {
default: 0,
},
player: {
default: 1,
},
},
mocha: {
timeout: 300000,
},
} Appreciate anyone who takes a look. Also, this is the first time I posted a question on the forum so apologies for any mistakes. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
In 01-deploy-raffle.js, process.env.ETHERCAN_API_KEY You need to add require("dotenv").config() at top. In hardhat.config.js {
...
etherscan: {
apiKey: {
rinkeby: 'your API key'
}
}
} As suggested in error |
Beta Was this translation helpful? Give feedback.
-
@Avinash104 Correct the spelling - apikey
+ apiKey |
Beta Was this translation helpful? Give feedback.
@Avinash104 Correct the spelling