1

Here Is my 01-Deploy-raffle.js Code

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

const FUND_AMOUNT = ethers.parseEther("1"); // 1 Ether, or 1e18 (10^18) Wei

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

  if (chainId == 31337) {
    // create VRFV2 Subscription
    vrfCoordinatorV2Mock = await ethers.getContract("VRFCoordinatorV2Mock");
    vrfCoordinatorV2Address = vrfCoordinatorV2Mock.address;
    const transactionResponse = await vrfCoordinatorV2Mock.createSubscription();
    const transactionReceipt = await transactionResponse.wait();
    subscriptionId = transactionReceipt.events[0].args.subId;
    // Fund the subscription
    // Our mock makes it so we don't actually have to worry about sending fund
    await vrfCoordinatorV2Mock.fundSubscription(subscriptionId, FUND_AMOUNT);
  } else {
    vrfCoordinatorV2Address = networkConfig[chainId]["vrfCoordinatorV2"];
    subscriptionId = networkConfig[chainId]["subscriptionId"];
  }
  const waitBlockConfirmations = developmentChains.includes(network.name)
    ? 1
    : VERIFICATION_BLOCK_CONFIRMATIONS;

  log("----------------------------------------------------");
  const arguments = [
    vrfCoordinatorV2Address,
    subscriptionId,
    networkConfig[chainId]["gasLane"],
    networkConfig[chainId]["keepersUpdateInterval"],
    networkConfig[chainId]["raffleEntranceFee"],
    networkConfig[chainId]["callbackGasLimit"],
  ];
  const raffle = await deploy("Raffle", {
    from: deployer,
    args: arguments,
    log: true,
    waitConfirmations: waitBlockConfirmations,
  });

  // Ensure the Raffle contract is a valid consumer of the VRFCoordinatorV2Mock contract.
  if (developmentChains.includes(network.name)) {
    const vrfCoordinatorV2Mock = await ethers.getContract(
      "VRFCoordinatorV2Mock"
    );
    await vrfCoordinatorV2Mock.addConsumer(subscriptionId, raffle.address);
  }

  // Verify the deployment
  if (
    !developmentChains.includes(network.name) &&
    process.env.ETHERSCAN_API_KEY
  ) {
    log("Verifying...");
    await verify(raffle.address, arguments);
  }

  log("Enter lottery with command:");
  const networkName = network.name == "hardhat" ? "localhost" : network.name;
  log(`yarn hardhat run scripts/enterRaffle.js --network ${networkName}`);
  log("----------------------------------------------------");
};

module.exports.tags = ["all", "raffle"];

And Here is my helper-hardhat-config.js

const { ethers } = require("hardhat");
const networkConfig = {
  default: {
    name: "hardhat",
    keepersUpdateInterval: "30",
  },
  31337: {
    name: "localhost",
    subscriptionId: "588",
    gasLane:
      "0x474e34a077df58807dbe9c96d3c009b23b3c6d0cce433e59bbf5b34f823bc56c", // 30 gwei
    keepersUpdateInterval: "30",
    raffleEntranceFee: ethers.parseEther("0.01"), // 0.01 ETH
    callbackGasLimit: "500000", // 500,000 gas
  },
  11155111: {
    name: "sepolia",
    subscriptionId: "6926",
    gasLane:
      "0x474e34a077df58807dbe9c96d3c009b23b3c6d0cce433e59bbf5b34f823bc56c", // 30 gwei
    keepersUpdateInterval: "30",
    raffleEntranceFee: ethers.parseEther("0.01"), // 0.01 ETH
    callbackGasLimit: "500000", // 500,000 gas
    vrfCoordinatorV2: "0x8103B0A8A00be2DDC778e6e7eaa21791Cd364625",
  },
};

const developmentChains = ["hardhat", "localhost"];
const VERIFICATION_BLOCK_CONFIRMATIONS = 6;
const frontEndContractsFile =
  "../nextjs-smartcontract-lottery-fcc/constants/contractAddresses.json";
const frontEndAbiFile =
  "../nextjs-smartcontract-lottery-fcc/constants/abi.json";

module.exports = {
  networkConfig,
  developmentChains,
  VERIFICATION_BLOCK_CONFIRMATIONS,
  frontEndContractsFile,
  frontEndAbiFile,
};

And Am getting this error

Error: ERROR processing /home/apex/Web3/Lottery Web3/deploy/01-deploy-raffle.js:
TypeError: Cannot read properties of undefined (reading 'vrfCoordinatorV2')
    at Object.module.exports [as func] (/home/apex/Web3/Lottery Web3/deploy/01-deploy-raffle.js:29:53)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at DeploymentsManager.executeDeployScripts (/home/apex/Web3/Lottery Web3/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1211:22)
    at DeploymentsManager.runDeploy (/home/apex/Web3/Lottery Web3/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1060:5)
    at SimpleTaskDefinition.action (/home/apex/Web3/Lottery Web3/node_modules/hardhat-deploy/src/index.ts:450:5)
    at Environment._runTaskDefinition (/home/apex/Web3/Lottery Web3/node_modules/hardhat/src/internal/core/runtime-environment.ts:358:14)
    at Environment.run (/home/apex/Web3/Lottery Web3/node_modules/hardhat/src/internal/core/runtime-environment.ts:191:14)
    at SimpleTaskDefinition.action (/home/apex/Web3/Lottery Web3/node_modules/hardhat-deploy/src/index.ts:601:32)
    at Environment._runTaskDefinition (/home/apex/Web3/Lottery Web3/node_modules/hardhat/src/internal/core/runtime-environment.ts:358:14)
    at Environment.run (/home/apex/Web3/Lottery Web3/node_modules/hardhat/src/internal/core/runtime-environment.ts:191:14)
    at DeploymentsManager.executeDeployScripts (/home/apex/Web3/Lottery Web3/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1214:19)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at DeploymentsManager.runDeploy (/home/apex/Web3/Lottery Web3/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1060:5)
    at SimpleTaskDefinition.action (/home/apex/Web3/Lottery Web3/node_modules/hardhat-deploy/src/index.ts:450:5)
    at Environment._runTaskDefinition (/home/apex/Web3/Lottery Web3/node_modules/hardhat/src/internal/core/runtime-environment.ts:358:14)
    at Environment.run (/home/apex/Web3/Lottery Web3/node_modules/hardhat/src/internal/core/runtime-environment.ts:191:14)
    at SimpleTaskDefinition.action (/home/apex/Web3/Lottery Web3/node_modules/hardhat-deploy/src/index.ts:601:32)
    at Environment._runTaskDefinition (/home/apex/Web3/Lottery Web3/node_modules/hardhat/src/internal/core/runtime-environment.ts:358:14)
    at Environment.run (/home/apex/Web3/Lottery Web3/node_modules/hardhat/src/internal/core/runtime-environment.ts:191:14)
    at SimpleTaskDefinition.action (/home/apex/Web3/Lottery Web3/node_modules/hardhat-deploy/src/index.ts:690:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Idk A lot about web3 But if someone can solve this , your welcome

1
  • Its was a silly error I just corrected my Hardhat.config.js file the chainId was missing
    – Apex Aryan
    Jan 1 at 14:32

1 Answer 1

0

Your code seems fine. The error appears from this line of code

vrfCoordinatorV2Address = networkConfig[chainId]["vrfCoordinatorV2"];

I think you may set the wrong chain_id in network.config (const chainId = network.config.chainId). Thus, networkConfig[chainId] will be undefined => networkConfig[chainId]["vrfCoordinatorV2"] will return error as shown in your console.

Hope it helps you and it would be better to upload your networkConfig also.

1
  • So there was a problem with my haredhat.config.js File the chain ID where missing, thanks :)
    – Apex Aryan
    Jan 1 at 14:31

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.