-
can anyone please help me with this im not understanding what i did wrong and why im getting this error ? //Raffle
//Steps we want to integrate
//1.Users should be able to buy enter the raffle
//2.The raffle should be able to generate a random number
//3.The raffle should be able to generate a random winner
//4.The raffle should be able to run at a specific time to pick the winner
// SPDX-License-Identifier: ISC
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";
import "@chainlink/contracts/src/v0.8/interfaces/KeeperCompatibleInterface.sol";
error Raffle__NotEnoughETH();
error Raffle__TransferFailed();
error Raffle__NotOpen();
error Raffle__UpKeepNotNeeded(
uint currentBalance,
uint requiredPlayers,
uint raffleState
);
contract Raffle is VRFConsumerBaseV2, KeeperCompatibleInterface {
//type decleaeration
enum RaffleState {
OPEN,
CALCULATING
}
//State Variable
VRFCoordinatorV2Interface private immutable i_vrfCoordinator;
bytes32 private immutable i_gasLane;
uint64 private immutable i_subscriptionId;
uint16 private constant c_requestConformation = 3;
uint32 private immutable i_callbackGaslimit;
uint32 private constant c_numWords = 1;
//Lottery Variables
address private s_recentWinners;
RaffleState private s_state;
uint private s_lastTimeStamp;
uint private immutable i_interval;
uint private immutable i_entranceFee;
//we are making this address payable so when one of the players win we have to pay them
address payable[] private s_contestents;
//Events
event RaffleEnter(address indexed player);
event RequestedRaffleWinner(uint indexed requestId);
event WinnerPicked(address indexed winner);
constructor(
uint entranceFee,
address vrfCordinatorV2,
bytes32 gasLane,
uint64 subscriptionId,
uint32 callbackGaslimit,
uint interval
) VRFConsumerBaseV2(vrfCordinatorV2) {
i_entranceFee = entranceFee;
i_vrfCoordinator = VRFCoordinatorV2Interface(vrfCordinatorV2);
i_gasLane = gasLane;
i_subscriptionId = subscriptionId;
i_callbackGaslimit = callbackGaslimit;
s_state = RaffleState.OPEN;
s_lastTimeStamp = block.timestamp;
i_interval = interval;
}
function enterRaffle() public payable {
if (msg.value < i_entranceFee) {
revert Raffle__NotEnoughETH();
}
if (RaffleState.OPEN != s_state) {
revert Raffle__NotOpen();
}
s_contestents.push(payable(msg.sender));
emit RaffleEnter(msg.sender);
}
/**
* @dev this is the function that the chainlink keepers node call
*they look for the `upKeepNeed` to return true
*the following should be true to return true
*1.Our time interval should have passed
*2.Lottery should atleast have one player and have some ETH
*3.Our subscription should be funded with link
*4.Lottery should be in an "open" state
*/
function checkUpkeep(
bytes calldata /* checkData */
)
public
view
override
returns (
bool upkeepNeeded,
bytes memory /* performData */
)
{
bool isOpen = (RaffleState.OPEN == s_state);
//(blockTimeStamp - lastTimeStamp) > interval (this interval is in seconds)
bool timePassed = ((block.timestamp - s_lastTimeStamp) > i_interval);
bool hasEnoughPlayers = (s_contestents.length > 0);
bool hasEnoughBalance = address(this).balance > 0;
upkeepNeeded = (isOpen &&
timePassed &&
hasEnoughPlayers &&
hasEnoughBalance);
return (upkeepNeeded, "0x0");
}
function performUpkeep(
bytes calldata /* performData */
) external override {
(bool upkeepNeeded, ) = checkUpkeep("");
if (!upkeepNeeded) {
revert Raffle__UpKeepNotNeeded(
address(this).balance,
s_contestents.length,
uint(s_state)
);
}
//request a random number
//once we get it we can do anything with it
//it is a 2 transaction operation
s_state = RaffleState.CALCULATING;
uint requestedId = i_vrfCoordinator.requestRandomWords(
i_gasLane,
i_subscriptionId,
c_requestConformation,
i_callbackGaslimit,
c_numWords
);
emit RequestedRaffleWinner(requestedId);
}
function fulfillRandomWords(
uint, /* requestId */
uint[] memory randomWords
) internal override {
//assume our players array is of 6 and we are getting a very huge random number so we are using modular funtion
uint indexOfWinner = randomWords[0] % s_contestents.length;
address payable recentWinner = s_contestents[indexOfWinner];
s_recentWinners = recentWinner;
s_state = RaffleState.OPEN;
s_contestents = new address payable[](0);
s_lastTimeStamp = block.timestamp;
(bool success, ) = recentWinner.call{value: address(this).balance}("");
if (!success) {
revert Raffle__TransferFailed();
}
emit WinnerPicked(recentWinner);
}
function getEntranceFee() public view returns (uint) {
return i_entranceFee;
}
function getContestents(uint index) public view returns (address) {
return s_contestents[index];
}
function getWinner() public view returns (address) {
return s_recentWinners;
}
}
|
Beta Was this translation helpful? Give feedback.
Answered by
mattjaf
Jun 11, 2022
Replies: 2 comments 3 replies
-
something might be wrong with your maybe
|
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
SantanuDK
-
@ElainaRS |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
something might be wrong with your
checkUpkeep
functionmaybe