Lesson 10 mockOffchain.js #1110
-
I copied and pasted the mockOffchain.js code from Lesson 10, see below: const { ethers, network } = require("hardhat")
async function mockKeepers() {
const raffle = await ethers.getContract("Raffle")
const checkData = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(""))
const { upkeepNeeded } = await raffle.callStatic.checkUpkeep(checkData)
if (upkeepNeeded) {
const tx = await raffle.performUpkeep(checkData)
const txReceipt = await tx.wait(1)
const requestId = txReceipt.events[1].args.requestId
console.log(`Performed upkeep with RequestId: ${requestId}`)
if (network.config.chainId == 31337) {
await mockVrf(requestId, raffle)
}
} else {
console.log("No upkeep needed!")
}
}
async function mockVrf(requestId, raffle) {
console.log("We on a local network? Ok let's pretend...")
const vrfCoordinatorV2Mock = await ethers.getContract("VRFCoordinatorV2Mock")
await vrfCoordinatorV2Mock.fulfillRandomWords(requestId, raffle.address)
console.log("Responded!")
const recentWinner = await raffle.getRecentWinner()
console.log(`The winner is: ${recentWinner}`)
}
mockKeepers()
.then(() => process.exit(0))
.catch((error) => {
console.error(error)
process.exit(1)
})
when I run
I get
When I console.log(upkeepNeeded) it is false, and when I console.log(network.config.chainId) it is undefined. When I run scripts/mockOffchain.js, I now get this error in my browser when a user tries to enter the raffle:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
@Ed-Marcavage Make to run a local node in a separate terminal |
Beta Was this translation helpful? Give feedback.
-
"when I console.log(network.config.chainId) it is undefined." If you do not have it, add these in hardhat.config.js and then run again: hardhat: {
// // If you want to do some forking, uncomment this
// forking: {
// url: MAINNET_RPC_URL
// }
chainId: 31337,
},
localhost: {
chainId: 31337,
}, |
Beta Was this translation helpful? Give feedback.
-
@oojoseph67 If cahinId was not your issue, and your upkeepNeeded is returning false, then do the following It might resolve. |
Beta Was this translation helpful? Give feedback.
"when I console.log(network.config.chainId) it is undefined." If you do not have it, add these in hardhat.config.js and then run again: