lesson 9 (typescript) TypeError: Cannot read properties of undefined (reading 'entranceFee') #5632
-
I struck around 15:20:00 when I try to run However in my
import { BigNumber } from "ethers"
import { ethers } from "hardhat"
export interface networkConfigItem {
name?: string
subscriptionId?: string
gasLane?: string
keepersUpdateInterval?: string
entranceFee?: string | BigNumber
callbackGasLimit?: string
vrfCoordinatorV2?: string
interval?: string
}
export interface networkConfigInfo {
[key: number]: networkConfigItem
}
export const networkConfig: networkConfigInfo = {
331337: {
name: "localhost",
subscriptionId: "588",
gasLane:
"0x474e34a077df58807dbe9c96d3c009b23b3c6d0cce433e59bbf5b34f823bc56c",
keepersUpdateInterval: "30",
entranceFee: ethers.utils.parseEther("0.01"), // 0.01 ETH
callbackGasLimit: "500000", // 500,000 gas
interval: "30",
},
11155111: {
name: "sepolia",
subscriptionId: "588",
gasLane:
"0x474e34a077df58807dbe9c96d3c009b23b3c6d0cce433e59bbf5b34f823bc56c",
keepersUpdateInterval: "30",
entranceFee: ethers.utils.parseEther("0.01"), // 0.01 ETH,
callbackGasLimit: "500000", // 500,000 gas,
vrfCoordinatorV2: "0x8103B0A8A00be2DDC778e6e7eaa21791Cd364625",
interval: "30",
},
1: {
name: "mainnet",
keepersUpdateInterval: "30",
// entranceFee: ethers.utils.parseEther("0.01"), // 0.01 ETH,
},
}
export const developmentChains = ["hardhat", "localhost"]
export const VERIFICATION_BLOCK_CONFIRMATIONS = 6
export const frontEndContractsFile = ""
export const frontEndAbitFile = ""
import { DeployFunction } from "hardhat-deploy/types"
import { HardhatRuntimeEnvironment } from "hardhat/types"
import { ethers, network } from "hardhat"
import {
networkConfig,
developmentChains,
VERIFICATION_BLOCK_CONFIRMATIONS,
} from "../helper-hardhat-config"
import { verify } from "../utils/verify"
import { ModuleKind } from "typescript"
const VRF_FUND_AMOUNT = ethers.utils.parseEther("2")
const waitBlockConfirmations = developmentChains.includes(network.name)
? 1
: VERIFICATION_BLOCK_CONFIRMATIONS
const deployLottery: DeployFunction = async (
hre: HardhatRuntimeEnvironment
) => {
const { deployments, getNamedAccounts, network, ethers } = hre
const { deploy, log } = deployments
const { deployer } = await getNamedAccounts()
const chainId = network.config.chainId!
let vrfCoordinatorV2Address: string | undefined,
subscriptionId: string | undefined
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 (made by mock)
await vrfCoordinatorV2Mock.fundSubscription(
subscriptionId,
VRF_FUND_AMOUNT
)
} else {
vrfCoordinatorV2Address = networkConfig[chainId]["vrfCoordinatorV2"]
subscriptionId = networkConfig[chainId]["subscriptionId"]
}
// const interval =
const args: any[] = [
vrfCoordinatorV2Address,
subscriptionId,
networkConfig[chainId]["entranceFee"],
networkConfig[chainId]["gasLane"],
networkConfig[chainId]["callbackGasLimit"],
networkConfig[chainId]["interval"],
]
const lottery = await deploy("Lottery", {
from: deployer,
args: args,
log: true,
waitConfirmations: waitBlockConfirmations,
})
if (
!developmentChains.includes(network.name) &&
process.env.ETHERSCAN_API_KEY
) {
log("verifying . . .")
await verify(lottery.address, args)
}
log("----------------------------------")
}
export default deployLottery
deployLottery.tags = ["all", "lottery"]
// Raffle //
// Enter the lottery (paying some amount)
// Pick a random winner (verifiably random)
// Winner to be selectd every X minutes -> completly automate
// Chainlink Oracle -> randomness, Automated Execution (Chainlink Keepers)
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
import "@chainlink/contracts/src/v0.8/interfaces/AutomationCompatibleInterface.sol";
error Lottery__NotEnoughtETHEntered();
error Lottery__TranferFail();
error Lottery__NotOpen();
error Lottery__UpkeepNotNeeded(
uint256 currentBalance,
uint256 numPlayers,
uint256 lotteryState
);
/**
* @title A sample Raffle Contract
* @author sw
* @notice This contracts is for creating an untamperable decentralized smart contract
* @dev This implements Chainlink VRF v2 and Chainlink Keeper
*/
contract Lottery is VRFConsumerBaseV2, AutomationCompatibleInterface {
/* State Variables */
enum LotteryState {
OPEN,
CALCULATING
} // enum = uint256 0 = OPEN, 1 = CALCULATING
/* State Variables */
uint256 private immutable i_entranceFee;
address payable[] private s_players;
VRFCoordinatorV2Interface private immutable i_vrfCoordinator;
bytes32 immutable i_gasLane;
uint64 private immutable i_subscriptionId;
uint32 private immutable i_callbackGasLimit;
uint16 private constant REQUEST_CONFIRMATIONS = 3;
uint32 private constant NUM_WORDS = 1;
// Lottery Variables
address private s_recentWinner;
LotteryState private s_lotteryState;
uint256 private s_lastTimeStamp;
uint256 private immutable i_interval;
/* Events */
event RaffleEnter(address indexed player);
event RequestedRaffleWinner(uint256 indexed requestId);
event WinnerPicked(address indexed winner);
constructor(
address vrfCoordinatorV2, // contract
uint64 subscriptionId,
uint256 entranceFee,
bytes32 gasLane, // keyHash
uint32 callbackGasLimit,
uint256 interval
) VRFConsumerBaseV2(vrfCoordinatorV2) {
i_entranceFee = entranceFee;
i_vrfCoordinator = VRFCoordinatorV2Interface(vrfCoordinatorV2);
i_gasLane = gasLane;
i_subscriptionId = subscriptionId;
i_callbackGasLimit = callbackGasLimit;
s_lotteryState = LotteryState.OPEN;
s_lastTimeStamp = block.timestamp;
i_interval = interval;
}
function enterLottery() public payable {
// require(msg.value > i_lotteryentranceFee, "Not enough ETH!");
if (msg.value < i_entranceFee) {
revert Lottery__NotEnoughtETHEntered();
}
if (s_lotteryState != LotteryState.OPEN) {
revert Lottery__NotOpen();
}
s_players.push(payable(msg.sender));
emit RaffleEnter(msg.sender);
}
/**
* @dev This is the function that the Chainlink Keeper nodes call
* they look for the `upkeepNeeded` to return true.
* The following should be true in order to return true:
* 1. Our time interval should have passed
* 2. The lottery should have at least 1 player, and have some ETH
* 3. Our subscription is funded with LINK
* 4, The lottery should be in an "open" state.
*/
function checkUpkeep(
bytes memory /*chcekData*/
)
public
view
override
returns (bool upkeepNeeded, bytes memory /* performData */)
{
bool isOpen = LotteryState.OPEN == s_lotteryState;
bool timePassed = ((block.timestamp - s_lastTimeStamp) > i_interval);
bool hasPlayers = (s_players.length > 0);
bool hasBalance = address(this).balance > 0;
upkeepNeeded = (isOpen && timePassed && hasPlayers && hasBalance);
return (upkeepNeeded, "0x0");
}
function performUpkeep(bytes calldata /* perfromData */) external override {
(bool upkeepNeeded, ) = checkUpkeep("");
if (!upkeepNeeded) {
revert Lottery__UpkeepNotNeeded(
address(this).balance,
s_players.length,
uint256(s_lotteryState)
);
}
s_lotteryState = LotteryState.CALCULATING;
uint256 requestId = i_vrfCoordinator.requestRandomWords(
i_gasLane,
i_subscriptionId,
REQUEST_CONFIRMATIONS,
i_callbackGasLimit,
NUM_WORDS
);
emit RequestedRaffleWinner(requestId);
}
function fulfillRandomWords(
uint256 /* requestId */,
uint256[] memory randomWords
) internal override {
uint256 indexOfWinner = randomWords[0] % s_players.length;
address payable recentWinner = s_players[indexOfWinner];
s_recentWinner = recentWinner;
s_lotteryState = LotteryState.OPEN;
s_players = new address payable[](0);
s_lastTimeStamp = block.timestamp;
(bool success, ) = recentWinner.call{value: address(this).balance}("");
//require(success)
if (!success) {
revert Lottery__TranferFail();
}
emit WinnerPicked(recentWinner);
}
/* View / Pure funcitons */
function getentranceFee() public view returns (uint256) {
return i_entranceFee;
}
function getPlayer(uint256 index) public view returns (address) {
return s_players[index];
}
function getRecentWinner() public view returns (address) {
return s_recentWinner;
}
function getLottery() public view returns (LotteryState) {
return s_lotteryState;
}
function getNumWords() public pure returns (uint256) {
return NUM_WORDS;
}
function getNumberOfPlayer() public view returns (uint256) {
return s_players.length;
}
function getLatestTimeStamp() public view returns (uint256) {
return s_lastTimeStamp;
}
function getRequestConfirmations() public pure returns (uint256) {
return REQUEST_CONFIRMATIONS;
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
@toppyc4 I think chainId is wrong in the helper file |
Beta Was this translation helpful? Give feedback.
@toppyc4 I think chainId is wrong in the helper file
331337
due to that this linenetworkConfig[chainId]["entranceFee"], does not able to read
entranceFeefrom object.