Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

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

Open
JawadLegend opened this issue Nov 6, 2023 · 4 comments
Open

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

JawadLegend opened this issue Nov 6, 2023 · 4 comments

Comments

@JawadLegend
Copy link

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
Copy link
Author

@alymurtazamemon please help

@ErinOFarrell
Copy link

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
Copy link

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
Copy link

You can also use

vrfCoordinatorV2Address = await vrfCoordinatorV2Mock.getAddress()

with Hardhat version 2.22.2

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants