Web3js

類型錯誤:getEnteranceFee 不是函式

  • October 3, 2022

我正在嘗試閱讀我的抽獎活動入場費,但我不斷收到此錯誤;

TypeError: getEnteranceFee is not a function

我知道有一個錯字,getEnteranceFee但它不應該影響任何事情,因為到處都是這樣。目前。

我已經嘗試過這裡給出的答案,但它對我不起作用。

我不明白為什麼useWeb3Contract無法獲得該功能。

RaffleEntrance.js 組件

import { useWeb3Contract, useMoralis } from "react-moralis"
import { abi, contractAddresses } from "../constants"
import { useEffect } from "react"

// have a funtion to enter the lottery

export default function RaffleEntrance() {
   const { chainId: chainIdHex, isWeb3Enabled } = useMoralis()
   const chainId = parseInt(chainIdHex)
   const raffleAddress = chainId in contractAddresses ? contractAddresses[chainId][0] : null
   /* const{runContractFuction: enterRaffle} = useWeb3Contract({
       abi: abi,
       contractAddress: raffleAddress, // specify networkId
       functionName: "enterRaffle",
       params: {},
       msgValue: 
   }) */

   const { runContractFuction: getEnteranceFee } = useWeb3Contract({
       abi: abi,
       contractAddress: raffleAddress, // specify networkId
       functionName: "getEnteranceFee",
       params: {},
   })

   useEffect(() => {
       if (isWeb3Enabled) {
           //try to read the raffle entrance fee
           async function updateUI() {
               const entranceFeeFromContract = await getEnteranceFee()
               console.log(entranceFeeFromContract)
           }
           updateUI()
       }
   }, [isWeb3Enabled])
   return <div>Hello from Lottery Entrance</div>
}

Raffle.sol 片段

   /* State Variables */
   uint256 private i_enteranceFee;
   

   /* Functions */
   constructor(
       address vrfCoordinatorV2, //address
       uint256 enteranceFee,
       bytes32 gasLane,
       uint64 subscriptionId,
       uint32 callbackGasLimit,
       uint256 interval
   ) VRFConsumerBaseV2(vrfCoordinatorV2) {
       i_vrfCoordinator = VRFCoordinatorV2Interface(vrfCoordinatorV2);
       i_enteranceFee = enteranceFee;
       i_gasLane = gasLane;
       i_subscriptionId = subscriptionId;
       i_callbackGasLimit = callbackGasLimit;
       s_raffleState = RaffleState.OPEN;
       s_lastTimeStamp = block.timestamp;
       i_interval = interval;
   }

   /* view / pure functions */
   function getEnteranceFee() public view returns (uint256) {
       return i_enteranceFee;
   }

ABI 片段

{
       "type": "function",
       "name": "getEnteranceFee",
       "constant": true,
       "stateMutability": "view",
       "payable": false,
       "gas": 29000000,
       "inputs": [],
       "outputs": [{ "type": "uint256" }]
   },

您好,您在這一行中拼錯了 runContractFunction:

const { runContractFuction: getEnteranceFee } = useWeb3Contract({

它應該是:

const { runContractFunction: getEnteranceFee } = useWeb3Contract({

我建議重命名getEnteranceFee,這樣你就不會規范程式碼中存在的拼寫錯誤。

引用自:https://ethereum.stackexchange.com/questions/136801