// SPDX-License-Identifier: MIT // An example of a consumer contract that relies on a subscription for funding. pragma solidity 0.8.6; import "../../shared/access/ConfirmedOwner.sol"; import "../VRFConsumerBaseV2.sol"; import "../interfaces/VRFCoordinatorV2Interface.sol"; /** * THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED VALUES FOR CLARITY. * THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE. * DO NOT USE THIS CODE IN PRODUCTION. */ contract VRFv2Consumer is VRFConsumerBaseV2, ConfirmedOwner { event RequestSent(uint256 requestId, uint32 numWords); event RequestFulfilled(uint256 requestId, uint256[] randomWords); struct RequestStatus { bool fulfilled; // whether the request has been successfully fulfilled bool exists; // whether a requestId exists uint256[] randomWords; } mapping(uint256 => RequestStatus) public s_requests; /* requestId --> requestStatus */ VRFCoordinatorV2Interface COORDINATOR; // past requests Id. uint256[] public requestIds; uint256 public lastRequestId; constructor( address vrfCoordinator ) VRFConsumerBaseV2(vrfCoordinator) ConfirmedOwner(msg.sender) { COORDINATOR = VRFCoordinatorV2Interface(vrfCoordinator); } // Assumes the subscription is funded sufficiently. function requestRandomWords( uint64 subId, uint32 callbackGasLimit, uint16 requestConfirmations, uint32 numWords, bytes32 keyHash ) external onlyOwner returns (uint256 requestId) { // Will revert if subscription is not set and funded. requestId = COORDINATOR.requestRandomWords(keyHash, subId, requestConfirmations, callbackGasLimit, numWords); s_requests[requestId] = RequestStatus({randomWords: new uint256[](0), exists: true, fulfilled: false}); requestIds.push(requestId); lastRequestId = requestId; emit RequestSent(requestId, numWords); return requestId; } function fulfillRandomWords(uint256 _requestId, uint256[] memory _randomWords) internal override { require(s_requests[_requestId].exists, "request not found"); s_requests[_requestId].fulfilled = true; s_requests[_requestId].randomWords = _randomWords; emit RequestFulfilled(_requestId, _randomWords); } function getRequestStatus( uint256 _requestId ) external view returns (bool fulfilled, uint256[] memory randomWords) { require(s_requests[_requestId].exists, "request not found"); RequestStatus memory request = s_requests[_requestId]; return (request.fulfilled, request.randomWords); } }