Created
February 14, 2023 14:23
-
-
Save solangegueiros/fbc68f39465b8f2dc08433f0f2a6108d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: MIT | |
| 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/ConfirmedOwner.sol"; | |
| contract VRFSenderTest 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; | |
| // Your subscription ID. | |
| uint64 s_subscriptionId; | |
| // past requests Id. | |
| uint256[] public requestIds; | |
| uint256 public lastRequestId; | |
| address vrfCoordinator = 0x2Ca8E0C643bDe4C2E08ab1fA0da3401AdAD7734D; | |
| bytes32 keyHash = 0x79d3d8832d904592c0bf9818b621522c988bb8b0c05cdc3b15aea1b6e8db0c15; | |
| uint32 callbackGasLimit = 2500000; | |
| uint16 requestConfirmations = 3; | |
| uint32 numWords = 1; | |
| address public lastSender; | |
| address public lastReceiver; | |
| uint256 public number; | |
| constructor( | |
| uint64 subscriptionId | |
| ) | |
| VRFConsumerBaseV2(vrfCoordinator) | |
| ConfirmedOwner(msg.sender) | |
| { | |
| COORDINATOR = VRFCoordinatorV2Interface( | |
| vrfCoordinator | |
| ); | |
| s_subscriptionId = subscriptionId; | |
| } | |
| // Assumes the subscription is funded sufficiently. | |
| function requestRandomWords() | |
| external | |
| onlyOwner | |
| returns (uint256 requestId) | |
| { | |
| // Will revert if subscription is not set and funded. | |
| requestId = COORDINATOR.requestRandomWords( | |
| keyHash, | |
| s_subscriptionId, | |
| requestConfirmations, | |
| callbackGasLimit, | |
| numWords | |
| ); | |
| s_requests[requestId] = RequestStatus({ | |
| randomWords: new uint256[](0), | |
| exists: true, | |
| fulfilled: false | |
| }); | |
| requestIds.push(requestId); | |
| lastRequestId = requestId; | |
| emit RequestSent(requestId, numWords); | |
| lastSender = msg.sender; | |
| 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); | |
| lastReceiver = msg.sender; | |
| number = _randomWords[0] % 4; | |
| } | |
| 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); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment