smartcontractkit / full-blockchain-solidity-course-js

Learn Blockchain, Solidity, and Full Stack Web3 Development with Javascript
12.25k stars 2.94k forks source link

Cannot read properties of undefined (reading 'JsonRpcProvider') #5005

Open kushuchiha opened 1 year ago

kushuchiha commented 1 year ago

Lesson

Lesson 9

Could you please leave a link to the timestamp in the video where this error occurs? (You can right click a video and "copy video URL at current time")

No response

Operating System

Windows

Describe the bug

I am getting following error for few days now, I have tried and surfed for possible solutions but couldn't lend on any,Please guide me to the issue here

Error: ERROR processing skip func of /home/kush_uchiha/hardhat_projects/hardhat-smartContract-lottery/deploy/00-deploy-mocks.js:
TypeError: Cannot read properties of undefined (reading 'JsonRpcProvider')
    at Object.<anonymous> (/home/kush_uchiha/hardhat_projects/hardhat-smartContract-lottery/node_modules/@nomiclabs/hardhat-ethers/src/internal/ethers-provider-wrapper.ts:4:61)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/home/kush_uchiha/hardhat_projects/hardhat-smartContract-lottery/node_modules/@nomiclabs/hardhat-ethers/src/internal/provider-proxy.ts:7:1)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at DeploymentsManager.executeDeployScripts (/home/kush_uchiha/hardhat_projects/hardhat-smartContract-lottery/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1101:15)
    at DeploymentsManager.runDeploy (/home/kush_uchiha/hardhat_projects/hardhat-smartContract-lottery/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1053:16)
    at SimpleTaskDefinition.action (/home/kush_uchiha/hardhat_projects/hardhat-smartContract-lottery/node_modules/hardhat-deploy/src/index.ts:409:5)
    at Environment._runTaskDefinition (/home/kush_uchiha/hardhat_projects/hardhat-smartContract-lottery/node_modules/hardhat/src/internal/core/runtime-environment.ts:330:14)
    at Environment.run (/home/kush_uchiha/hardhat_projects/hardhat-smartContract-lottery/node_modules/hardhat/src/internal/core/runtime-environment.ts:163:14)
    at SimpleTaskDefinition.action (/home/kush_uchiha/hardhat_projects/hardhat-smartContract-lottery/node_modules/hardhat-deploy/src/index.ts:555:32)
    at Environment._runTaskDefinition (/home/kush_uchiha/hardhat_projects/hardhat-smartContract-lottery/node_modules/hardhat/src/internal/core/runtime-environment.ts:330:14)
    at Environment.run (/home/kush_uchiha/hardhat_projects/hardhat-smartContract-lottery/node_modules/hardhat/src/internal/core/runtime-environment.ts:163:14)
    at SimpleTaskDefinition.action (/home/kush_uchiha/hardhat_projects/hardhat-smartContract-lottery/node_modules/hardhat-deploy/src/index.ts:640:5)
    at Environment._runTaskDefinition (/home/kush_uchiha/hardhat_projects/hardhat-smartContract-lottery/node_modules/hardhat/src/internal/core/runtime-environment.ts:330:14)

My deploy-Raffle.js is as follows

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

const provider = new ethers.JsonRpcProvider("http://127.0.0.1:8545")
// we make this as per arguments in constructor of raffle.sol and modify helper hardhat config accordingly
// here are the list for reference
/*      address vrfCoordinatorV2,
        uint256 entranceFee,
        bytes32 gasLane,
        uint64 subscriptionId,
        uint32 callbackGasLimit,
        uint256 interval */
const VRF_SUB_FUND_AMOUNT = ethers.utils.parseEther("2")

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

    if (developmentChains.includes(network.name)) {
        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
        await vrfCoordinatorV2Mock.fundSubscription(subscriptionId, VRF_SUB_FUND_AMOUNT)
    } else {
        vrfCoordinatorV2Address = networkConfig[chainId]["vrfCoordinatorV2"]
        subscriptionId = networkConfig[chainId]["subscriptionId"]
    }

    const entranceFee = networkConfig[chainId]["entranceFee"]

    const callbackGasLimit = networkConfig[chainId]["callbackGasLimit"]
    const interval = networkConfig[chainId]["interval"]
    const gasLane = ethers.utils.hexZeroPad(
        "0x79d3d8832d904592c0bf9818b621522c988bb8b0c05cdc3b15aea1b6e8db0c15",
        32
    )

    const args = [
        vrfCoordinatorV2Address,
        entranceFee,
        gasLane,
        subscriptionId,
        callbackGasLimit,
        interval,
    ]

    const raffle = await deploy(
        "Raffle",
        {
            from: deployer,
            args: args,
            log: true,
            waitconfirmations: network.config.blockConfirmations || 1,
        },
        { provider }
    )

    if (!developmentChains.includes(network.name) && process.env.ETHERSCAN_API_KEY) {
        log("Verifying......")
        await verify(raffle.address, args)
    }

    log("OVER..........")
}

module.exports.tags = ["all", "raffle"]
alymurtazamemon commented 1 year ago

@kushuchiha You do not need this line here const provider = new ethers.JsonRpcProvider("http://127.0.0.1:8545"), but if you want to use it then it should be like this;

-const provider = new ethers.JsonRpcProvider("http://127.0.0.1:8545")
+const provider = new ethers.providers.JsonRpcProvider("http://127.0.0.1:8545")
MemoBaca commented 1 year ago

What ethers version are you using? Hardhat doesn't support yet the latest ethers version. If you are using ethers 6 downgrading will fix the problem. You can do so running this command:

yarn upgrade ethers@^5.7.2