0

mockOffChain.js

const { ethers, network } = require("hardhat")

async function mockKeepers() {
    // const raffle = await ethers.getContractAt("Raffle")
    const contractAddress = (await deployments.get("Raffle")).address
    const raffle_contract = await ethers.getContractAt("Raffle", contractAddress)
    const raffle = raffle_contract.address
    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)
    })

Error

yarn hardhat run scripts/mockOffChain.js --network localhost
yarn run v1.22.19
warning package.json: No license field
$ '/home/rajvir_singh/FCC Full stack Web3 course/Hardhat Lottery L9/node_modules/.bin/hardhat' run scripts/mockOffChain.js --network localhost
TypeError: Cannot read properties of undefined (reading 'checkUpkeep')
    at mockKeepers (/home/rajvir_singh/FCC Full stack Web3 course/Hardhat Lottery L9/scripts/mockOffChain.js:49:54)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I don't understand how to resolve this error

1 Answer 1

0

You set the variable raffle in this line of code:

const raffle = raffle_contract.address

This will only store the address of the contract in raffle, not the contracts functions and things like that. As a result, the error tells you that checkUpkeep is undefined - it's a function in the Raffle contract (I assume), but the variable is only the address.

otoh, you have stored the contract in a different variable - raffle_contract. As a result, I would think you should be able to change this line:

const { upkeepNeeded } = await raffle.callStatic.checkUpkeep(checkData)

to:

const { upkeepNeeded } = await raffle_contract.callStatic.checkUpkeep(checkData)

and as long as there is a function called checkUpkeep in the contract, it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.