smartcontractkit / full-blockchain-solidity-course-js

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

Lesson 10: TypeError: can't access property "toString", _ctx.sent is undefined #6323

Open hoangglong-nguyen opened 7 months ago

hoangglong-nguyen commented 7 months ago

Discussed in https://github.com/smartcontractkit/full-blockchain-solidity-course-js/discussions/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 ![image](https://github.com/smartcontractkit/full-blockchain-solidity-course-js/assets/148770033/2eee34d5-5205-4ac9-a966-994a192f2df4) Here is my 02-update-front-end.js ```javascript 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 ```javascript /*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 (

Lottery

{raffleAddress ? ( <>
Entrance Fee: {ethers.utils.formatUnits(entranceFee, "ether")} ETH
The current number of players is: {numberOfPlayers}
The most previous winner was: {recentWinner}
) : (
Please connect to a supported chain
)}
) } ```