Lesson 10: getEntranceFee() returns "undefined" #2218
-
So I followed along with the other similar answers to this, yet, as much as I can tell, my hardhat local host chainId is consistently 31337 throughout my files, yet I still get the error. Can anyone help? LotteryEntrance.js import { useEffect } from "react"
import { useMoralis, useWeb3Contract } from "react-moralis"
import {abi, contractAddresses} from "../constants"
export default function LotteryEntrance() {
const { chainId: chainIdHex, isWeb3Enabled } = useMoralis()
const chainId = parseInt(chainIdHex)
console.log(parseInt(chainIdHex))
const raffleAddress = chainIdHex in contractAddresses ? contractAddresses[chainId][0] : null
// const { runContractFunction: enterRaffle } = useWeb3Contract({
// abi: abi,
// contractAddress: raffleAddress,
// functionName: "enterRaffle",
// params: {},
// msgValue: //
// })
const { runContractFunction: getEntranceFee } = useWeb3Contract({
abi: abi,
contractAddress: raffleAddress,
functionName: "getEntranceFee",
params: {},
})
useEffect(() => {
if (isWeb3Enabled) {
// try to read the raffle entrance fee
async function updateUI() {
const something = await getEntranceFee()
console.log(something)
}
updateUI()
}
}, [isWeb3Enabled])
return (
<div>Hi from Lottery Entrance!</div>
)
} contractAddresses.json {"31337":["0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9"]} hardhat.config.js require("@nomiclabs/hardhat-waffle")
require("@nomiclabs/hardhat-etherscan")
require("hardhat-deploy")
require("solidity-coverage")
require("hardhat-gas-reporter")
require("hardhat-contract-sizer")
require("dotenv").config()
/** @type import('hardhat/config').HardhatUserConfig */
const COINMARKETCAP_API_KEY = process.env.COINMARKETCAP_API_KEY || ""
const KOVAN_RPC_URL =
process.env.KOVAN_RPC_URL || "https://eth-mainnet.alchemyapi.io/v2/your-api-key"
const RINKEBY_RPC_URL =
process.env.RINKEBY_RPC_URL ||
"https://eth-rinkeby.alchemyapi.io/v2/UoxMYEe56_n36Ya5-uavkt_TjPOSgMF0"
const PRIVATE_KEY =
process.env.PRIVATE_KEY || "0x11ee3108a03081fe260ecdc106554d09d9d1209bcafd46942b10e02943effc4a"
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY || ""
const API_URL =
process.env.API_URL || "https://eth-rinkeby.alchemyapi.io/v2/UoxMYEe56_n36Ya5-uavkt_TjPOSgMF0"
module.exports = {
solidity: "0.8.7",
defaultNetwork: "hardhat",
networks: {
hardhat: {
chainId: 31337,
blockConfirmations: 1,
},
rinkeby: {
chainId: 4,
blockConfirmations: 6,
url: RINKEBY_RPC_URL,
accounts: [PRIVATE_KEY],
},
},
etherscan: {
apiKey: {
rinkeby: ETHERSCAN_API_KEY,
},
},
gasReporter: {
enabled: false,
currency: "USD",
outputFile: "gas-report.txt",
noColors: true,
},
namedAccounts: {
deployer: {
default: 0,
1: 0,
},
player: {
default: 1,
},
},
mocha: {
timeout: 500000, // 300 seconds
},
} helper-hardhat-config.js const { network, ethers } = require("hardhat")
const networkConfig = {
default: {
name: "hardhat",
keepersUpdateInterval: "30",
},
31337: {
name: "hardhat",
entranceFee: ethers.utils.parseEther("0.01"),
gasLane: "0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc",
subscriptionId: "0",
callbackGasLimit: "500000",
interval: "30",
},
4: {
name: "rinkeby",
vrfCoordinatorV2: "0x6168499c0cFfCaCD319c818142124B7A15E857ab",
subscriptionId: "18416",
gasLane: "0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc", // 30 gwei
interval: "30",
entranceFee: ethers.utils.parseEther("0.01"), // 0.1 ETH
callbackGasLimit: "500000", // 500,000 gas
},
1: {
name: "mainnet",
keepersUpdateInterval: "30",
},
}
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,
} Please let me know if you need anything else! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@emmanuelf37 : You have to edit few changes I'm listing below : Change this line of code from this : To : Because in above line of code you have written : And also change this line of from this : To : Then do : |
Beta Was this translation helpful? Give feedback.
@emmanuelf37 : You have to edit few changes I'm listing below :
Change this line of code from this :
const raffleAddress = chainIdHex in contractAddresses ? contractAddresses[chainId][0] : null
To :
const raffleAddress = chainId in contractAddresses ? contractAddresses[chainId][0] : null
Because in above line of code you have written :
const chainId = parseInt(chainIdHex)
So, thats why it will bechainId
notchainIdHex
And also change this line of from this :
const something = await getEntranceFee()
To :
const something = (await getEntranceFee()).toString()
Then do :
console.log(something)