Please i need to debug my script
see the top line. it says type error id must be of type string, recieved undefined. this means that you have a runtime bug where your id isnt getting defined, hence typescript is throwing this type error. revie your code and see where your defining the Id var and use console.logs to see why its not getting defined. paste your code in here i can help you
// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through node .
//
// You can also run a script with npx hardhat run . If you do that, Hardhat
// will compile your contracts, add the Hardhat Runtime Environment’s members to the
// global scope, and execute the script.
import { ethers, run, network } from “hardhat”
// async main
async function main() {
const SimpleStorageFactory = await ethers.getContractFactory(
“SimpleStorage”
)
console.log(“Deploying contract …”)
const simpleStorage = await SimpleStorageFactory.deploy()
await simpleStorage.deployed()
console.log(Deployed contract to:${simpleStorage.address})
//What happens when we deploy to our hardhat network?
if (network.config.chainId === 4 && process.env.ETHERSCAN_API_KEY) {
console.log(“Waiting for block confirmations …”)
await simpleStorage.deployTransaction.wait(6)
await verify(simpleStorage.address, [])
}
const currentValue = await simpleStorage.retrieve()
console.log(Current Value is : ${currentValue})
//Update the current value
const transactionResponse = await simpleStorage.store(7)
await transactionResponse.wait(1)
const updatedValue = await simpleStorage.retrieve()
console.log(Updated Value is: ${updatedValue})
}
//async function verify(contractAddress, args) {
const verify = async (contractAddress: string, args: any[]) => {
console.log(“Verifying contract …”)
try {
await run(“verify: verify”, {
address: contractAddress,
constructorArguments: args,
})
} catch (e: any) {
if (e.message.toLowerCase().includes(“already verified”)) {
console.log(“Already Verified!”)
} else {
console.log(e)
}
}
}
// main
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error)
process.exit(1)
})
Please share your code in the following way so it can be easy for us to review it
Carlos Z
// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through node <script>.
//
// You can also run a script with npx hardhat run <script>. If you do that, Hardhat
// will compile your contracts, add the Hardhat Runtime Environment's members to the
// global scope, and execute the script.
import { ethers, run, network } from "hardhat"
// async main
async function main() {
const SimpleStorageFactory = await ethers.getContractFactory(
"SimpleStorage"
)
console.log("Deploying contract ...")
const simpleStorage = await SimpleStorageFactory.deploy()
await simpleStorage.deployed()
console.log(Deployed contract to:${simpleStorage.address})
//What happens when we deploy to our hardhat network?
if (network.config.chainId === 4 && process.env.ETHERSCAN_API_KEY) {
console.log("Waiting for block confirmations ...")
await simpleStorage.deployTransaction.wait(6)
await verify(simpleStorage.address, [])
}
const currentValue = await simpleStorage.retrieve()
console.log(Current Value is : ${currentValue})
//Update the current value
const transactionResponse = await simpleStorage.store(7)
await transactionResponse.wait(1)
const updatedValue = await simpleStorage.retrieve()
console.log(Updated Value is: ${updatedValue})
}
//async function verify(contractAddress, args) {
const verify = async (contractAddress: string, args: any[]) => {
console.log("Verifying contract ...")
try {
await run("verify: verify", {
address: contractAddress,
constructorArguments: args,
})
} catch (e: any) {
if (e.message.toLowerCase().includes("already verified")) {
console.log("Already Verified!")
} else {
console.log(e)
}
}
}
// main
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error)
process.exit(1)
})
ok doesnt seem to be any issues with your code. except can i ask what command your running the script with. I see in this block of code
//What happens when we deploy to our hardhat network?
if (network.config.chainId === 4 && process.env.ETHERSCAN_API_KEY) {
console.log("Waiting for block confirmations ...")
await simpleStorage.deployTransaction.wait(6)
await verify(simpleStorage.address, [])
}
that your trying to deploy to the network which has chainID of 4. whiat networ is this. I dont know any network that has its chainId as 4. unless you have specifically defined hardhat to assign to chainId 4 in your hardhat.Config.js file. if you are trying to runt script on hardhat your comman should be something similar to
npx hardhat run scripts/yourScript.ts --network hardhat
if this isnt the issue and the above block isnt being entered i would imagine its failing because you cant evrify a contract on hardhat as its a local network. you should do it on a testnet like goerli or something
I deploy this script in hardhat network
I use this in powershell
Yarn hardhat run scripts/deploy.ts --network hardhat