Hey guys I am using react-moralis library to connect with the deployed smart contract.
The smart contract is a Raffle based contract
//SPDX-License-Identifier: MIT
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/KeeperCompatible.sol";
error Raffle__NotEnoughETHEntered();
error Raffle__TransferFailed();
error Raffle__LotteryClosed();
error Raffle__UpkeepNotNeeded(uint256 currentBalance, uint256 numPlayers, uint256 raffleState);
/**
* @title A Sample Raffle Contract
* @author Eik
* @notice This contract is for creating is untamperable decentralized smart contract
* @dev This implements Chainlink VRF V2 and Chainlink Keepers
*/
contract Raffle is VRFConsumerBaseV2, KeeperCompatible {
/* Types */
enum RaffleState {
OPEN,
CALCULATING
}
/* State Variables */
uint256 private immutable i_entranceFee;
address payable[] private s_players;
//For VRFConsumerBaseV2 Constructor
VRFCoordinatorV2Interface private immutable i_vrfCoordinator;
bytes32 private immutable i_gasLane;
uint64 private immutable i_subscriptionId;
uint16 private constant REQUEST_CONFIRMATIONS = 3;
uint32 private immutable i_callbackGasiLimit;
uint32 private constant NUM_WORDS = 1;
/* Lottery Variables */
address private s_recentWinner;
RaffleState private s_raffleState;
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,
uint256 entranceFee,
bytes32 gasLane,
uint64 subscriptionId,
uint32 callbackGasiLimit,
uint256 interval
) VRFConsumerBaseV2(vrfCoordinatorV2) {
i_entranceFee = entranceFee;
i_vrfCoordinator = VRFCoordinatorV2Interface(vrfCoordinatorV2);
i_gasLane = gasLane;
i_subscriptionId = subscriptionId;
i_callbackGasiLimit = callbackGasiLimit;
s_raffleState = RaffleState.OPEN;
s_lastTimeStamp = block.timestamp;
i_interval = interval;
}
function enterRaffle() public payable {
if (msg.value < i_entranceFee) {
revert Raffle__NotEnoughETHEntered();
}
if (s_raffleState != RaffleState.OPEN) {
revert Raffle__LotteryClosed();
}
s_players.push(payable(msg.sender)); //we type cast msg.sender to payable as s_players is payable
emit RaffleEnter(msg.sender);
}
/**
* @dev This is the function that Chainlink Keeper nodes call as they look for the
* 'checkUpkeep()' to return true.
* Following should be true in order to return true:
* 1. Our time inerval should have passed
* 2. The lottery should have atleast one player and some ETH
* 3. Our subscription is funded with LINK
* 4. Lottery should be in "Open" state
*/
function checkUpkeep(
bytes memory /*checkData*/
) public override returns (bool upkeepNeeded, bytes memory /*performData*/) {
bool isOpen = (RaffleState.OPEN == s_raffleState);
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);
}
function performUpkeep(bytes calldata /* performData */) external override {
(bool upkeepNeeded, ) = checkUpkeep("");
if (!upkeepNeeded) {
revert Raffle__UpkeepNotNeeded(
address(this).balance,
s_players.length,
uint256(s_raffleState)
);
}
s_raffleState = RaffleState.CALCULATING;
uint256 requestId = i_vrfCoordinator.requestRandomWords(
i_gasLane,
i_subscriptionId,
REQUEST_CONFIRMATIONS,
i_callbackGasiLimit,
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_raffleState = RaffleState.OPEN;
s_players = new address payable[](0);
s_lastTimeStamp = block.timestamp;
(bool success, ) = recentWinner.call{value: address(this).balance}("");
if (!success) {
revert Raffle__TransferFailed();
}
emit WinnerPicked(recentWinner);
}
/* Getter Functions */
function getEntranceFee() public view returns (uint256) {
return i_entranceFee;
}
function getPlayer(uint256 index) public view returns (address payable) {
return s_players[index];
}
function getRecenetWinner() public view returns (address) {
return s_recentWinner;
}
function getRaffleState() public view returns (RaffleState) {
return s_raffleState;
}
function getNumWords() public pure returns (uint256) {
return NUM_WORDS;
}
function getNumOfPlayers() 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;
}
function getInterval() public view returns (uint256) {
return i_interval;
}
}
In my component I am trying to fetch the entrance fee from my function but I am unable to do so as it returns undefined. I am getting the chainId and the raffleAddress correctly so I think the issue is in the getEntranceFee() function.
import { useWeb3Contract, useMoralis } from "react-moralis"
import { abi, contractAddress } from "../constants/index.js"
import { useEffect, useState } from "react"
import { ethers } from "ethers"
export default function LotteryEnterance() {
const { isWeb3Enabled, chainId: chainIdHex, enableWeb3 } = useMoralis()
const [chainId, setChainId] = useState(null)
const [fee, setFee] = useState("0")
useEffect(() => {
if (!isWeb3Enabled) {
enableWeb3()
}
}, [isWeb3Enabled, enableWeb3])
useEffect(() => {
if (chainIdHex) {
const chainIdDecimal = parseInt(chainIdHex, 16)
setChainId(chainIdDecimal)
console.log(chainIdDecimal)
}
}, [chainIdHex])
const raffleAddress = chainId in contractAddress ? contractAddress[chainId][0] : null
//runContractFunction can both send transactions and read states
const { runContractFunction: enterRaffle } = useWeb3Contract({
abi: abi,
contractAddress: raffleAddress,
functionName: "enterRaffle",
params: {},
msgValue: fee,
})
const { runContractFunction: getEntranceFee } = useWeb3Contract({
abi: abi,
contractAddress: raffleAddress,
functionName: "getEntranceFee",
params: {},
})
//PROBLEM HERE 👇
async function updateUI() {
const feeFromCall = (await getEntranceFee()).toString()
setFee(feeFromCall)
console.log(fee)
}
useEffect(() => {
if (isWeb3Enabled) {
updateUI()
}
}, [isWeb3Enabled])
return (
<div>
This project was made by Sarthak
<div>Entrance Fee : {fee} ETH</div>
</div>
)
}
My contract address :
{ "11155111": ["0xcD31ECC318Ec7b0Dae58fCFE112cCA6cF2A5E882"] }