Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

LESSON 9 => TypeError: Cannot read properties of undefined (reading '0') #5779

Answered by alymurtazamemon
harry2855 asked this question in Q&A
Discussion options

const { network, ethers } = require("hardhat")
const {developmentChains, networkConfig} = require("../helper-hardhat-config")
const {verify} = require("../utils/verify")
const VRF_SUB_FUND_AMOUNT = ethers.parseEther("2")
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.getAddress
 const transactionResposne = await vrfCoordinatorV2Mock.createSubscription()
 const transactionReceipt = await transactionResposne.wait(1)
 
 subscriptionId = transactionReceipt.events[0].args.subId
 // we have to fund the subsciption
 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"]
 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) {
 console.log("Verifying")
 await verify(raffle.address, args)
 }
}
module.exports.tags = ["all","raffle"]

This is my deploy code for raffle. Probably the error is in this line.

 subscriptionId = transactionReceipt.events[0].args.subId
You must be logged in to vote

@harry2855 No this is not an ethers issue, instead the mock contract response was not emitting the event of subscript created. But we can by pass it because we know it is the first subscript so we can do this

- subscriptionId = transactionReceipt.events[0].args.subId
+ subscriptionId = 1

And also address is know renamed to target vrfCoordinatorV2Address = vrfCoordinatorV2Mock.target;

Replies: 2 comments 20 replies

Comment options

@harry2855 You can console log the transactionReceipt and see if there is any event or not, because it is saying undefined.

You must be logged in to vote
16 replies
Comment options

@harry2855 Please leave your repository link so I can test it.

Comment options

Comment options

Hey @alymurtazamemon , I rolled back to ether v5 but now I am getting following error

TypeError: (0 , ethers_1.getAddress) is not a function

I have commited changes in the repo.

Comment options

@harry2855 No this is not an ethers issue, instead the mock contract response was not emitting the event of subscript created. But we can by pass it because we know it is the first subscript so we can do this

- subscriptionId = transactionReceipt.events[0].args.subId
+ subscriptionId = 1

And also address is know renamed to target vrfCoordinatorV2Address = vrfCoordinatorV2Mock.target;

Answer selected by harry2855
Comment options

I have rolled back to the previous version now and now running into another error. You can check in the thread.

Comment options

Try this version of Ethers. I had almost a similar problem. This solved it.

yarn add ethers@5.7.2
Comment options

Rolling back to previous versions of ethers shouldn't be the marked answer guys.

Comment options

now in latest version of ethers can someone say how to log the events from transaction receipt ., because in the txn receipt object thre is no events section @alymurtazamemon

Comment options

Hey @alymurtazamemon , I rolled back to ether v5 but now I am getting following error

TypeError: (0 , ethers_1.getAddress) is not a function

I have commited changes in the repo.

Hi @harry2855 - did you ever find a solution to the error: TypeError: (0 , ethers_1.getAddress) is not a function?

You must be logged in to vote
4 replies
Comment options

Have you tried with contract.address?

Comment options

I was getting the same error. In my case @nomiclabs/hardhat-ethers was causing the problem. The following version solved it.

@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers@^0.3.0-beta.13
Comment options

hi Mohammed, thank you that seems to have worked.

I wonder why the previous project (i copied the package.json file from the older project) didn't through up the same error?

Anyway, thanks for your help!

I ultimately finished lesson 14 with ethers v6 without reading the SubId from the event triggered. Still struggling to read emitted events using ethers v6. If anyone can point me in the right direction that would be awesome. Thank you!

Comment options

Ahmed, have you (or anyone else) found out why the subscription ID is not being emitted in ehters v5?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /