-
Hello, I was trying to get the entrance fee of the Lottery contract, but it prints out nothing in console of my browser. However, I did get something from the console, which is this-
I'm not sure if this has anything to do with this, but I am using Yarn Plug'n'Play, and my Yarn version is 3.4.1. Also, here is the part that is the most sus- const { runContractFunction: getEntranceFee } = useWeb3Contract({
abi: abi,
contractAddresses: contractAddress,
functionName: "getEntranceFee",
params: {},
}) {
"type": "function",
"name": "getEntranceFee",
"constant": true,
"stateMutability": "view",
"payable": false,
"gas": 29000000,
"inputs": [],
"outputs": [{ "type": "uint256" }]
}, And this is my LotteryEntrance, abi, package.json, and the contract itself- import { useWeb3Contract } from "react-moralis"
import { contractAddresses, abi } from "@/constants"
import { useMoralis } from "react-moralis"
import { useEffect } from "react"
export default function LotteryEntrance() {
const { chainId: chainIdHex, isWeb3Enabled } = useMoralis()
const chainId = parseInt(chainIdHex)
const contractAddress = chainId in contractAddresses ? contractAddresses[chainId][0] : null
// const { runContractFunction: enterLottery } = useWeb3Contract({
// abi: abi,
// contractAddresses: contractAddress,
// functionName: "enterLottery",
// params: {},
// msgValue,
// })
const { runContractFunction: getEntranceFee } = useWeb3Contract({
abi: abi,
contractAddresses: contractAddress,
functionName: "getEntranceFee",
params: {},
})
useEffect(() => {
if (isWeb3Enabled) {
async function updateUI() {
const entranceFee = await getEntranceFee()
console.log(entranceFee)
}
updateUI()
}
}, [isWeb3Enabled])
return <div></div>
} [
{
"type": "constructor",
"payable": false,
"inputs": [
{ "type": "address", "name": "vrfCoordinatorV2" },
{ "type": "uint256", "name": "entranceFee" },
{ "type": "bytes32", "name": "keyHash" },
{ "type": "uint64", "name": "subscriptionId" },
{ "type": "uint32", "name": "callbackGasLimit" },
{ "type": "uint256", "name": "interval" }
]
},
{ "type": "error", "name": "Lottery__NotEnoughETHEntered", "inputs": [] },
{ "type": "error", "name": "Lottery__NotOpen", "inputs": [] },
{ "type": "error", "name": "Lottery__TransferFailed", "inputs": [] },
{
"type": "error",
"name": "Lottery__UpkeepNotNeeded",
"inputs": [
{ "type": "uint256", "name": "balance" },
{ "type": "uint256", "name": "numberOfPlayers" },
{ "type": "uint256", "name": "LotteryState" }
]
},
{
"type": "error",
"name": "OnlyCoordinatorCanFulfill",
"inputs": [
{ "type": "address", "name": "have" },
{ "type": "address", "name": "want" }
]
},
{
"type": "event",
"anonymous": false,
"name": "LotteryEnter",
"inputs": [{ "type": "address", "name": "player", "indexed": true }]
},
{
"type": "event",
"anonymous": false,
"name": "RequestedLotteryWinner",
"inputs": [{ "type": "uint256", "name": "requestId", "indexed": true }]
},
{
"type": "event",
"anonymous": false,
"name": "WinnerPicked",
"inputs": [{ "type": "address", "name": "winner", "indexed": true }]
},
{
"type": "function",
"name": "checkUpkeep",
"constant": true,
"stateMutability": "view",
"payable": false,
"gas": 29000000,
"inputs": [{ "type": "bytes" }],
"outputs": [{ "type": "bool", "name": "upkeepNeeded" }, { "type": "bytes" }]
},
{
"type": "function",
"name": "enterLottery",
"constant": false,
"stateMutability": "payable",
"payable": true,
"gas": 29000000,
"inputs": [],
"outputs": []
},
{
"type": "function",
"name": "getEntranceFee",
"constant": true,
"stateMutability": "view",
"payable": false,
"gas": 29000000,
"inputs": [],
"outputs": [{ "type": "uint256" }]
},
{
"type": "function",
"name": "getInterval",
"constant": true,
"stateMutability": "view",
"payable": false,
"gas": 29000000,
"inputs": [],
"outputs": [{ "type": "uint256" }]
},
{
"type": "function",
"name": "getLatestTimeStamp",
"constant": true,
"stateMutability": "view",
"payable": false,
"gas": 29000000,
"inputs": [],
"outputs": [{ "type": "uint256" }]
},
{
"type": "function",
"name": "getLotteryState",
"constant": true,
"stateMutability": "view",
"payable": false,
"gas": 29000000,
"inputs": [],
"outputs": [{ "type": "uint8" }]
},
{
"type": "function",
"name": "getMostRecentWinner",
"constant": true,
"stateMutability": "view",
"payable": false,
"gas": 29000000,
"inputs": [],
"outputs": [{ "type": "address" }]
},
{
"type": "function",
"name": "getNumberOfPlayers",
"constant": true,
"stateMutability": "view",
"payable": false,
"gas": 29000000,
"inputs": [],
"outputs": [{ "type": "uint256" }]
},
{
"type": "function",
"name": "getNumberOfWords",
"constant": true,
"stateMutability": "pure",
"payable": false,
"gas": 29000000,
"inputs": [],
"outputs": [{ "type": "uint256" }]
},
{
"type": "function",
"name": "getPlayer",
"constant": true,
"stateMutability": "view",
"payable": false,
"gas": 29000000,
"inputs": [{ "type": "uint256", "name": "index" }],
"outputs": [{ "type": "address" }]
},
{
"type": "function",
"name": "getRequestConfirmations",
"constant": true,
"stateMutability": "pure",
"payable": false,
"gas": 29000000,
"inputs": [],
"outputs": [{ "type": "uint256" }]
},
{
"type": "function",
"name": "performUpkeep",
"constant": false,
"payable": false,
"gas": 29000000,
"inputs": [{ "type": "bytes" }],
"outputs": []
},
{
"type": "function",
"name": "rawFulfillRandomWords",
"constant": false,
"payable": false,
"gas": 29000000,
"inputs": [
{ "type": "uint256", "name": "requestId" },
{ "type": "uint256[]", "name": "randomWords" }
],
"outputs": []
}
] {
"name": "nextjs-lottery-fcc",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@babel/runtime": "^7.21.0",
"@walletconnect/web3-provider": "^1.8.0",
"@web3auth/web3auth": "^2.1.3",
"ethers": "5.7.2",
"magic-sdk": "^13.4.0",
"moralis": "^2.14.2",
"moralis-v1": "^1.12.0",
"next": "13.2.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-moralis": "^1.4.2",
"react-toastify": "^9.1.1",
"utf8": "^3.0.0",
"walletlink": "^2.5.0",
"web3modal": "^1.9.12"
},
"devDependencies": {
"autoprefixer": "^10.4.13",
"dotenv": "^16.0.3",
"postcss": "^8.4.21",
"prettier": "^2.8.4",
"tailwindcss": "^3.2.7",
"typescript": "^4.9.5"
}
} // 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/interfaces/KeeperCompatibleInterface.sol";
error Lottery__NotEnoughETHEntered();
error Lottery__TransferFailed();
error Lottery__NotOpen();
error Lottery__UpkeepNotNeeded(uint256 balance, uint256 numberOfPlayers, uint256 LotteryState);
/**
* @title A decentralized lottery that is 100% random and let players pay
* @author Undefined
* @notice Only an example smart contract
* @notice Selects a winner every certain amount of time -> fully automated
* @dev Uses Chainlink oracles and keepers for randomization and automated execution
*/
contract Lottery is VRFConsumerBaseV2, KeeperCompatibleInterface {
/* Type declarations */
enum LotteryState {
OPEN,
CALCULATING
}
/* State Variables */
uint256 private immutable i_entranceFee;
address payable[] private s_players;
VRFCoordinatorV2Interface private immutable i_vrfCoordinator;
bytes32 private immutable i_keyHash;
uint64 private immutable i_subscriptionId;
uint32 private immutable i_callbackGasLimit;
uint16 private constant REQUEST_CONFIRMATIONS = 3;
uint32 private constant NUM_WORDS = 1;
/* Lottery Variables */
address private s_mostRecentWinner;
LotteryState private s_LotteryState;
uint256 private s_lastTimeStamp;
uint256 private immutable i_interval;
/* Events */
event LotteryEnter(address indexed player);
event RequestedLotteryWinner(uint256 indexed requestId);
event WinnerPicked(address indexed winner);
constructor(
address vrfCoordinatorV2,
uint256 entranceFee,
bytes32 keyHash,
uint64 subscriptionId,
uint32 callbackGasLimit,
uint256 interval
) VRFConsumerBaseV2(vrfCoordinatorV2) {
i_entranceFee = entranceFee;
i_vrfCoordinator = VRFCoordinatorV2Interface(vrfCoordinatorV2);
i_keyHash = keyHash;
i_subscriptionId = subscriptionId;
i_callbackGasLimit = callbackGasLimit;
s_LotteryState = LotteryState.OPEN;
s_lastTimeStamp = block.timestamp;
i_interval = interval;
}
receive() external payable{
enterLottery();
}
fallback() external payable {
enterLottery();
}
function enterLottery() public payable {
if (msg.value < i_entranceFee) {
revert Lottery__NotEnoughETHEntered();
}
if (s_LotteryState != LotteryState.OPEN) {
revert Lottery__NotOpen();
}
s_players.push(payable(msg.sender));
emit LotteryEnter(msg.sender);
}
/**
* @dev This is the function that the Chainlink Keeper nodes call
* they look for the `upkeepNeeded` to return true.
* The following should be true in order to return true:
* 1) Our time interval should have passed
* 2) The lottery should have at least 1 player with ETH
* 3) Our subscription is funded with LINK
* 4) The lottery should be in an "open" state
*/
function checkUpkeep(
bytes memory /* checkData */
) public view override returns (bool upkeepNeeded, bytes memory /* performData */) {
bool isOpen = LotteryState.OPEN == s_LotteryState;
bool timePassed = ((block.timestamp - s_lastTimeStamp) > i_interval);
bool hasPlayers = s_players.length > 0;
bool hasBalance = address(this).balance > 0;
upkeepNeeded = (timePassed && isOpen && hasBalance && hasPlayers);
return (upkeepNeeded, "0x0");
}
function performUpkeep(bytes calldata /* performData */) external override {
(bool upkeepNeeded, ) = checkUpkeep("");
if (!upkeepNeeded) {
revert Lottery__UpkeepNotNeeded(
address(this).balance,
s_players.length,
uint256(s_LotteryState)
);
}
s_LotteryState = LotteryState.CALCULATING;
uint256 requestId = i_vrfCoordinator.requestRandomWords(
i_keyHash,
i_subscriptionId,
REQUEST_CONFIRMATIONS,
i_callbackGasLimit,
NUM_WORDS
);
emit RequestedLotteryWinner(requestId);
}
function fulfillRandomWords(
uint256 /* requestId */,
uint256[] memory randomWords
) internal override {
uint256 indexOfWinner = randomWords[0] % s_players.length;
address payable mostRecentWinner = s_players[indexOfWinner];
s_mostRecentWinner = mostRecentWinner;
s_LotteryState = LotteryState.OPEN;
s_players = new address payable[](0);
s_lastTimeStamp = block.timestamp;
(bool success, ) = mostRecentWinner.call{value: address(this).balance}("");
if (!success) {
revert Lottery__TransferFailed();
}
emit WinnerPicked(mostRecentWinner);
}
/* View/Pure Functions */
/**
* @dev @notice These view/pure functions are for showing the private variables in this contract.
* Private variables are variables in the contract that are only visible/accessible in the contract.
* The reason some of these variables are private is for saving gas- making the contract cheaper to call/deploy.
*/
function getEntranceFee() public view returns (uint256) {
return i_entranceFee;
}
function getPlayer(uint256 index) public view returns (address) {
return s_players[index];
}
function getMostRecentWinner() public view returns (address) {
return s_mostRecentWinner;
}
function getLotteryState() public view returns (LotteryState) {
return s_LotteryState;
}
function getNumberOfWords() public pure returns (uint256) {
return NUM_WORDS;
}
function getNumberOfPlayers() 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;
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
OK, so I figured out the problem, and it was very stupid. In my LotteryEntrance.js, I put contractAddresses instead of contractAddress. Sorry! |
Beta Was this translation helpful? Give feedback.
OK, so I figured out the problem, and it was very stupid. In my LotteryEntrance.js, I put contractAddresses instead of contractAddress. Sorry!