PatrickAlphaC / hardhat-smartcontract-lottery-fcc

MIT License
117 stars 183 forks source link

TypeError: Cannot read properties of undefined (reading '0') #195

Open JawadLegend opened 8 months ago

JawadLegend commented 8 months ago

i got this error error when i run yarn hardhat test i deleted the artifacts and cache folders but the error still persist.


yarn run v1.22.19
warning package.json: No license field
$ "C:\Users\Hidden network\myPortfolio\hardhat-smartcontract-lottery\node_modules\.bin\hardhat" test

  Raffle Unit Test
    constructor
      1) "before each" hook for "initializes the raffle correctly"

  0 passing (1s)
  1 failing

  1) Raffle Unit Test
       "before each" hook for "initializes the raffle correctly":
     ERROR processing C:\Users\Hidden network\myPortfolio\hardhat-smartcontract-lottery\deploy\01-deploy-raffle.js:
TypeError: Cannot read properties of undefined (reading '0')````

i got the same ```TypeError: Cannot read properties of undefined (reading '0')```  when i run  ```yarn hardhat deploy```  as well

The full code is below here

```const { network, ethers } = require("hardhat")
const {developmentChains, networkConfig} = require("../helper-hardhat-config")
const VRF_SUB_FUND_AMOUNT = ethers.parseEther("0.25")
const { verify } = require("../helper-hardhat-config")

module.exports = async function ({ getNamedAccounts, deployments }) {
    const {deploy, log} = deployments
    const {deployer} = await getNamedAccounts()
    const chainId = network.config.chainId

    let vrfCoordinatorV2Address, 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

        // Fund the subscription
        //usually, you'd need the link token on a real network
        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"]
    const 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.ETHERSCAN_API_KEY){
        log("Verifying...")
        await verify(raffle.address, args)
    }
    log("----------------------------------------------------")
}

module.exports.tags = ["all","raffle"]```
JawadLegend commented 8 months ago

@alymurtazamemon please help

ErinOFarrell commented 8 months ago

Hey, I also ran into this problem a few days ago, and I found out that the events[0] array is not called "events" in the transactionReceipt object anymore. If you do subscriptionId = transactionReceipt.logs[0].args.subId instead it might work. I did run into a problem with the mock address right after solving this problem, so if you get an error that looks like this

An unexpected error occurred:
Error: ERROR processing /home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/deploy/01-deploy-raffle.js:
Error: invalid address (argument="address", value=undefined, code=INVALID_ARGUMENT, version=address/5.7.0) (argument="vrfCoordinatorV2", value=undefined, code=INVALID_ARGUMENT, version=abi/5.7.0)
at Logger.makeError (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/logger/src.ts/index.ts:269:28)
at Logger.throwError (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/logger/src.ts/index.ts:281:20)
at Logger.throwArgumentError (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/logger/src.ts/index.ts:285:21)
at AddressCoder.Coder._throwError (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/abi/src.ts/coders/abstract-coder.ts:68:16)
at AddressCoder.encode (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/abi/src.ts/coders/address.ts:22:18)
at /home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/abi/src.ts/coders/array.ts:71:19
at Array.forEach ()
at pack (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/abi/src.ts/coders/array.ts:54:12)
at TupleCoder.encode (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/abi/src.ts/coders/tuple.ts:54:20)
at AbiCoder.encode (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/abi/src.ts/abi-coder.ts:111:15)
at DeploymentsManager.executeDeployScripts (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1214:19)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at runNextTicks (node:internal/process/task_queues:65:3)
at listOnTimeout (node:internal/timers:528:9)
at processTimers (node:internal/timers:502:7)
at DeploymentsManager.runDeploy (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1060:5)
at SimpleTaskDefinition.action (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/hardhat-deploy/src/index.ts:447:5)
at Environment._runTaskDefinition (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/hardhat/src/internal/core/runtime-environment.ts:358:14)
at Environment.run (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/hardhat/src/internal/core/runtime-environment.ts:191:14)
at SimpleTaskDefinition.action (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/hardhat-deploy/src/index.ts:598:32)

then try vrfCoordinatorV2Address = vrfCoordinatorV2Mock.target

UnknownBlackman commented 7 months ago

Hey, I also ran into this problem a few days ago, and I found out that the events[0] array is not called "events" in the transactionReceipt object anymore. If you do subscriptionId = transactionReceipt.logs[0].args.subId instead it might work. I did run into a problem with the mock address right after solving this problem, so if you get an error that looks like this

An unexpected error occurred:
Error: ERROR processing /home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/deploy/01-deploy-raffle.js:
Error: invalid address (argument="address", value=undefined, code=INVALID_ARGUMENT, version=address/5.7.0) (argument="vrfCoordinatorV2", value=undefined, code=INVALID_ARGUMENT, version=abi/5.7.0)
at Logger.makeError (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/logger/src.ts/index.ts:269:28)
at Logger.throwError (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/logger/src.ts/index.ts:281:20)
at Logger.throwArgumentError (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/logger/src.ts/index.ts:285:21)
at AddressCoder.Coder._throwError (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/abi/src.ts/coders/abstract-coder.ts:68:16)
at AddressCoder.encode (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/abi/src.ts/coders/address.ts:22:18)
at /home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/abi/src.ts/coders/array.ts:71:19
at Array.forEach ()
at pack (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/abi/src.ts/coders/array.ts:54:12)
at TupleCoder.encode (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/abi/src.ts/coders/tuple.ts:54:20)
at AbiCoder.encode (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/@ethersproject/abi/src.ts/abi-coder.ts:111:15)
at DeploymentsManager.executeDeployScripts (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1214:19)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at runNextTicks (node:internal/process/task_queues:65:3)
at listOnTimeout (node:internal/timers:528:9)
at processTimers (node:internal/timers:502:7)
at DeploymentsManager.runDeploy (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1060:5)
at SimpleTaskDefinition.action (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/hardhat-deploy/src/index.ts:447:5)
at Environment._runTaskDefinition (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/hardhat/src/internal/core/runtime-environment.ts:358:14)
at Environment.run (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/hardhat/src/internal/core/runtime-environment.ts:191:14)
at SimpleTaskDefinition.action (/home/erinofarrell/hh-fcc/hardhat-smartcontract-lottery-fcc/node_modules/hardhat-deploy/src/index.ts:598:32)

then try vrfCoordinatorV2Address = vrfCoordinatorV2Mock.target

Thanks I was having this same error and this solved it. If you don't mind please do a pull to the main code base to correct it.

ilkinm03 commented 3 months ago

You can also use

vrfCoordinatorV2Address = await vrfCoordinatorV2Mock.getAddress()

with Hardhat version 2.22.2