Open
Description
Discussed in #6286
Originally posted by hoangglong-nguyen November 13, 2023
I've tried a lot of solutions on this thread relating to this bug, but none of them seem to work. My hardhat-localhost is running with metamask connected. I also console.log abi, raffleAddress, chainId and they all seem to have been connected correctly. I'm suspecting my runContractFunction is not working properly but I don't know how to fix it. Please help.
Here is my error and my console
Here is my 02-update-front-end.js
const { frontEndContractsFile, frontEndAbiFile } = require("../helper-hardhat-config")
const fs = require("fs")
const { network } = require("hardhat")
module.exports = async () => {
if (process.env.UPDATE_FRONT_END) {
console.log("Writing to front end...")
await updateContractAddresses()
await updateAbi()
console.log("Front end written!")
}
}
async function updateAbi() {
const raffle = await ethers.getContract("Raffle")
fs.writeFileSync(frontEndAbiFile, JSON.stringify(raffle.interface.fragments))
}
async function updateContractAddresses() {
const raffle = await ethers.getContract("Raffle")
const contractAddresses = JSON.parse(fs.readFileSync(frontEndContractsFile, "utf8"))
if (network.config.chainId.toString() in contractAddresses) {
if (!contractAddresses[network.config.chainId.toString()].includes(raffle.address)) {
contractAddresses[network.config.chainId.toString()] = raffle.address
}
} else {
contractAddresses[network.config.chainId.toString()] = [raffle.address]
}
fs.writeFileSync(frontEndContractsFile, JSON.stringify(contractAddresses))
}
module.exports.tags = ["all", "frontend"]
My LotteryEntrance.js
/*eslint-disable*/
import { contractAddresses, abi } from "../constants"
import { useMoralis, useWeb3Contract } from "react-moralis"
import { useEffect, useState } from "react"
import { useNotification } from "web3uikit"
import { ethers } from "ethers"
export default function LotteryEntrance() {
const { isWeb3Enabled, chainId: chainIdHex } = useMoralis()
const chainId = parseInt(chainIdHex)
const raffleAddress = chainId in contractAddresses ? contractAddresses[chainId] : null
const [entranceFee, setEntranceFee] = useState("0")
const [numberOfPlayers, setNumberOfPlayers] = useState("0")
const [recentWinner, setRecentWinner] = useState("0")
const dispatch = useNotification()
console.log(chainId)
console.log(raffleAddress)
console.log(abi)
const {
runContractFunction: enterRaffle,
data: enterTxResponse,
isLoading,
isFetching,
} = useWeb3Contract({
abi: abi,
contractAddress: raffleAddress,
functionName: "enterRaffle",
msgValue: entranceFee,
params: {},
})
/* View Functions */
const { runContractFunction: getEntranceFee } = useWeb3Contract({
abi: abi,
contractAddress: raffleAddress, // specify the networkId
functionName: "getEntranceFee",
params: {},
})
const { runContractFunction: getPlayersNumber } = useWeb3Contract({
abi: abi,
contractAddress: raffleAddress,
functionName: "getNumberOfPlayers",
params: {},
})
const { runContractFunction: getRecentWinner } = useWeb3Contract({
abi: abi,
contractAddress: raffleAddress,
functionName: "getRecentWinner",
params: {},
})
async function updateUIValues() {
//MY BUG HERE
const entranceFeeFromCall = (await getEntranceFee()).toString()
const numPlayersFromCall = (await getPlayersNumber()).toString()
const recentWinnerFromCall = (await getRecentWinner()).toString()
setEntranceFee(entranceFeeFromCall)
setNumberOfPlayers(numPlayersFromCall)
setRecentWinner(recentWinnerFromCall)
}
useEffect(() => {
if (isWeb3Enabled) {
updateUIValues()
}
}, [isWeb3Enabled])
const handleNewNotification = () => {
dispatch({
type: "info",
message: "Transaction Complete!",
title: "Transaction Notification",
position: "topR",
icon: "bell",
})
}
const handleSuccess = async (tx) => {
try {
await tx.wait(1)
handleNewNotification(tx)
updateUIValues()
} catch (error) {
console.log(error)
}
}
return (
<div className="p-5">
<h1 className="py-4 px-4 font-bold text-3xl">Lottery</h1>
{raffleAddress ? (
<>
<button
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded ml-auto"
onClick={async () =>
await enterRaffle({
onSuccess: handleSuccess,
onError: (error) => console.log(error),
})
}
disabled={isLoading || isFetching}
>
{isLoading || isFetching ? (
<div className="animate-spin spinner-border h-8 w-8 border-b-2 rounded-full"></div>
) : (
"Enter Raffle"
)}
</button>
<div>Entrance Fee: {ethers.utils.formatUnits(entranceFee, "ether")} ETH</div>
<div>The current number of players is: {numberOfPlayers}</div>
<div>The most previous winner was: {recentWinner}</div>
</>
) : (
<div>Please connect to a supported chain </div>
)}
</div>
)
}
Metadata
Metadata
Assignees
Labels
No labels