-
Hi I am following lesson 9 on 14:47:45
this is my code //Raffle
//Enter by paying some amount
//pick a random winner verifiyably random
///winner to be selected every X minutes -> completely automated
//Chainlink oracle -> Randomness outside the blockchain, Automated Execution
// 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 Raffle__NotEnoughETHEntered();
error Raffle__TransferFailed();
error Raffle__NotOpen();
contract Raffle is VRFConsumerBaseV2 {
/*Types declarations */
enum RaffleState {
OPEN,
CALCULATING
}
/*State Variables*/
uint256 private immutable i_entranceFee;
address payable[] private s_players;
VRFCoordinatorV2Interface private immutable i_vrfCoordinator;
bytes32 private immutable i_gasLane;
uint64 private immutable i_subscriptionId;
uint32 private immutable i_callBackGacLimit;
uint16 private constant REQUEST_CONFIRMATIONS = 3;
uint32 private constant NUM_WORDS = 1;
uint256 private i_interval;
//Lottery variables
address private s_recentWinner;
RaffleState private s_raffleState; //to pending , open, closed, calculation
uint256 private s_lastTimeStamp;
/*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 callBackGacLimit,
uint256 interval
) VRFConsumerBaseV2(vrfCoordinatorV2) {
i_entranceFee = entranceFee;
i_vrfCoordinator = VRFCoordinatorV2Interface(vrfCoordinatorV2);
i_gasLane = gasLane;
i_subscriptionId = subscriptionId;
i_callBackGacLimit = callBackGacLimit;
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__NotOpen();
}
s_players.push(payable(msg.sender));
//Emit an event when we update a dinamic array or mapping
//named events with the function name reversed
emit RaffleEnter(msg.sender);
}
/**
*@dev This is the function that the chainlink node calls to
*They look for the `upkeepNeeded` to return true
*The following should be true in order to return truth
1. Our time interval should have passed
2. The lottery should have at least one player
3.Our subscription is funded with LINK
4. The lottery should be in an "open" state
*/
function checkUpkeep(
bytes calldata /* checkData*/
)
external
override
returns (
bool upkeepNeeded,
bytes memory /**peformData */
)
{
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);
//(block.timestamp - last block.timestamp) > interval
}
function requestRandomWinner() external {
//Request hte random number
//Once we get it do somethig with it
//2 transaction proccess
s_raffleState = RaffleState.CALCULATING;
uint256 requestId = i_vrfCoordinator.requestRandomWords(
i_gasLane, // gasLane
i_subscriptionId, //
REQUEST_CONFIRMATIONS, // the amount of blocks to be hashed
i_callBackGacLimit, // the maximum amoint of gas to be usedx
NUM_WORDS // the amount of random words that we need
);
emit RequestedRaffleWinner(requestId);
}
function fulfillRandomWords(
uint256, /*requestId*/
uint256[] memory randomWords
) internal override {
//s_player = 10
//randomNumber 202
//202 % 10 = 2
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);
(bool success, ) = recentWinner.call{value: address(this).balance}("");
//require success gas efficient way
if (!success) {
revert Raffle__TransferFailed();
}
emit WinnerPicked(recentWinner);
}
/*View/Pure functions */
function getEntranceFee() public view returns (uint256) {
return i_entranceFee;
}
function getPlayer(uint256 index) public view returns (address) {
return s_players[index];
}
function getRecentWinner() public view returns (address) {
return s_recentWinner;
}
} Could someone please have look at my code :) |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
You imported Change:
To:
Let me know if this solve your problem. |
Beta Was this translation helpful? Give feedback.
-
@pacelliv
|
Beta Was this translation helpful? Give feedback.
-
The fixes below should resolve the problem.
contract Raffle is VRFConsumerBaseV2, KeeperCompatibleInterface { Note: There is no abstract needed.
function checkUpkeep(
bytes memory /* checkData */
)
public
view
override
returns (
bool upkeepNeeded,
bytes memory /* performData */
)
function performUpkeep(
bytes calldata /* performData */
) external override {
(bool upkeepNeeded, ) = checkUpkeep("");
// require(upkeepNeeded, "Upkeep not needed");
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_callbackGasLimit,
NUM_WORDS
);
// Quiz... is this redundant?
emit RequestedRaffleWinner(requestId);
} |
Beta Was this translation helpful? Give feedback.
-
I have a similar question This is my code : // SPDX-License-Identifier: MIT pragma solidity ^0.8.16; import '@openzeppelin/contracts/token/ERC1155/ERC1155.sol'; /// @title Crown contract Crown is ERC1155, Ownable, ERC1155Supply { uint256 public constant MAX_ATTACK_DEFEND_STRENGTH = 10; enum BattleStatus{ PENDING, STARTED, ENDED } /// @dev GameToken struct to store player token info /// @dev Player struct to store player info /// @dev Battle struct to store battle info mapping(address => uint256) public playerInfo; // Mapping of player addresses to player index in the players array Player[] public players; // Array of players function isPlayer(address addr) public view returns (bool) { function getPlayer(address addr) public view returns (Player memory) { function getAllPlayers() public view returns (Player[] memory) { function isPlayerToken(address addr) public view returns (bool) { function getPlayerToken(address addr) public view returns (GameToken memory) { function getAllPlayerTokens() public view returns (GameToken[] memory) { // Battle getter function function getBattle(string memory _name) public view returns (Battle memory) { function getAllBattles() public view returns (Battle[] memory) { function updateBattle(string memory _name, Battle memory _newBattle) private { // Events /// @dev Initializes the contract by setting a function setURI(string memory newuri) public onlyOwner { function initialize() private { /// @dev Registers a player
} /// @dev internal function to generate random number; used for Battle Card Attack and Defense Strength
} /// @dev internal function to create a new Battle Card
} /// @dev Creates a new game token
} function getTotalSupply() external view returns (uint256) { /// @dev Creates a new battle
} /// @dev Player joins battle
} // Read battle move info for player 1 and player 2
} function _registerPlayerMove(uint256 _player, uint8 _choice, string memory _battleName) internal { // User chooses attack or defense move for battle card
} // Awaits battle results
} struct P { /// @dev Resolve battle function to determine winner and loser of battle
} function quitBattle(string memory _battleName) public {
} /// @dev internal function to end the battle
} // Turns uint256 into string // Token URI getter function // The following functions are overrides required by Solidity. } |
Beta Was this translation helpful? Give feedback.
You imported
KeepersCompatibleInterface
but forgot to inherit it to your contractChange:
To:
Let me know if this solve your problem.