|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity 0.8.24; |
| 3 | + |
| 4 | +import {IVRFCoordinatorV2Plus} from "@chainlink/contracts/src/v0.8/vrf/dev/VRFConsumerBaseV2Plus.sol"; |
| 5 | +import {VRFV2PlusClient} from "@chainlink/contracts/src/v0.8/vrf/dev/libraries/VRFV2PlusClient.sol"; |
| 6 | + |
| 7 | +/// @title A mock for testing code that relies on VRFCoordinatorV2. |
| 8 | +contract ChainlinkVRFCoordinatorV2Mock is IVRFCoordinatorV2Plus { |
| 9 | + // ************************************* // |
| 10 | + // * Storage * // |
| 11 | + // ************************************* // |
| 12 | + |
| 13 | + uint256 nextRequestId = 1; |
| 14 | + mapping(uint256 requestID => VRFV2PlusClient.RandomWordsRequest request) requests; |
| 15 | + |
| 16 | + // ************************************* // |
| 17 | + // * Events * // |
| 18 | + // ************************************* // |
| 19 | + |
| 20 | + event RandomWordsRequested( |
| 21 | + bytes32 indexed keyHash, |
| 22 | + uint256 requestId, |
| 23 | + uint256 indexed subId, |
| 24 | + uint16 minimumRequestConfirmations, |
| 25 | + uint32 callbackGasLimit, |
| 26 | + uint32 numWords, |
| 27 | + address indexed sender |
| 28 | + ); |
| 29 | + event RandomWordsFulfilled(uint256 indexed requestId, bool success); |
| 30 | + |
| 31 | + // ************************************* // |
| 32 | + // * Function Modifiers * // |
| 33 | + // ************************************* // |
| 34 | + |
| 35 | + function fulfillRandomWords(uint256 _requestId, address _consumer, uint256[] memory _words) public { |
| 36 | + if (requests[_requestId].subId == 0) { |
| 37 | + revert("nonexistent request"); |
| 38 | + } |
| 39 | + VRFV2PlusClient.RandomWordsRequest memory req = requests[_requestId]; |
| 40 | + |
| 41 | + if (_words.length == 0) { |
| 42 | + _words = new uint256[](req.numWords); |
| 43 | + for (uint256 i = 0; i < req.numWords; i++) { |
| 44 | + _words[i] = uint256(keccak256(abi.encode(_requestId, i))); |
| 45 | + } |
| 46 | + } else if (_words.length != req.numWords) { |
| 47 | + revert InvalidRandomWords(); |
| 48 | + } |
| 49 | + |
| 50 | + bytes4 FULFILL_RANDOM_WORDS_SELECTOR = bytes4(keccak256("rawFulfillRandomWords(uint256,uint256[])")); |
| 51 | + bytes memory callReq = abi.encodeWithSelector(FULFILL_RANDOM_WORDS_SELECTOR, _requestId, _words); |
| 52 | + (bool success, ) = _consumer.call{gas: req.callbackGasLimit}(callReq); |
| 53 | + |
| 54 | + delete (requests[_requestId]); |
| 55 | + emit RandomWordsFulfilled(_requestId, success); |
| 56 | + } |
| 57 | + |
| 58 | + function requestRandomWords(VRFV2PlusClient.RandomWordsRequest calldata req) external returns (uint256) { |
| 59 | + uint256 requestId = nextRequestId++; |
| 60 | + |
| 61 | + requests[requestId] = req; |
| 62 | + |
| 63 | + emit RandomWordsRequested( |
| 64 | + req.keyHash, |
| 65 | + requestId, |
| 66 | + req.subId, |
| 67 | + req.requestConfirmations, |
| 68 | + req.callbackGasLimit, |
| 69 | + req.numWords, |
| 70 | + msg.sender |
| 71 | + ); |
| 72 | + return requestId; |
| 73 | + } |
| 74 | + |
| 75 | + // ************************************* // |
| 76 | + // * Public Views * // |
| 77 | + // ************************************* // |
| 78 | + |
| 79 | + function fundSubscription(uint256, uint96) public pure { |
| 80 | + revert("not implemented"); |
| 81 | + } |
| 82 | + |
| 83 | + function createSubscription() external pure returns (uint256) { |
| 84 | + revert("not implemented"); |
| 85 | + } |
| 86 | + |
| 87 | + function fundSubscriptionWithNative(uint256) external payable { |
| 88 | + revert("not implemented"); |
| 89 | + } |
| 90 | + |
| 91 | + function getActiveSubscriptionIds(uint256, uint256) external pure returns (uint256[] memory) { |
| 92 | + revert("not implemented"); |
| 93 | + } |
| 94 | + |
| 95 | + function getSubscription(uint256) external pure returns (uint96, uint96, uint64, address, address[] memory) { |
| 96 | + revert("not implemented"); |
| 97 | + } |
| 98 | + |
| 99 | + function cancelSubscription(uint256, address) external pure { |
| 100 | + revert("not implemented"); |
| 101 | + } |
| 102 | + |
| 103 | + function addConsumer(uint256, address) external pure { |
| 104 | + revert("not implemented"); |
| 105 | + } |
| 106 | + |
| 107 | + function removeConsumer(uint256, address) external pure { |
| 108 | + revert("not implemented"); |
| 109 | + } |
| 110 | + |
| 111 | + function getRequestConfig() external pure returns (uint16, uint32, bytes32[] memory) { |
| 112 | + return (3, 2000000, new bytes32[](0)); |
| 113 | + } |
| 114 | + |
| 115 | + function getConfig() |
| 116 | + external |
| 117 | + pure |
| 118 | + returns ( |
| 119 | + uint16 minimumRequestConfirmations, |
| 120 | + uint32 maxGasLimit, |
| 121 | + uint32 stalenessSeconds, |
| 122 | + uint32 gasAfterPaymentCalculation |
| 123 | + ) |
| 124 | + { |
| 125 | + return (4, 2_500_000, 2_700, 33285); |
| 126 | + } |
| 127 | + |
| 128 | + function getFeeConfig() |
| 129 | + external |
| 130 | + pure |
| 131 | + returns ( |
| 132 | + uint32 fulfillmentFlatFeeLinkPPMTier1, |
| 133 | + uint32 fulfillmentFlatFeeLinkPPMTier2, |
| 134 | + uint32 fulfillmentFlatFeeLinkPPMTier3, |
| 135 | + uint32 fulfillmentFlatFeeLinkPPMTier4, |
| 136 | + uint32 fulfillmentFlatFeeLinkPPMTier5, |
| 137 | + uint24 reqsForTier2, |
| 138 | + uint24 reqsForTier3, |
| 139 | + uint24 reqsForTier4, |
| 140 | + uint24 reqsForTier5 |
| 141 | + ) |
| 142 | + { |
| 143 | + return ( |
| 144 | + 100000, // 0.1 LINK |
| 145 | + 100000, // 0.1 LINK |
| 146 | + 100000, // 0.1 LINK |
| 147 | + 100000, // 0.1 LINK |
| 148 | + 100000, // 0.1 LINK |
| 149 | + 0, |
| 150 | + 0, |
| 151 | + 0, |
| 152 | + 0 |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + function getFallbackWeiPerUnitLink() external pure returns (int256) { |
| 157 | + return 4000000000000000; // 0.004 Ether |
| 158 | + } |
| 159 | + |
| 160 | + function requestSubscriptionOwnerTransfer(uint256, address) external pure { |
| 161 | + revert("not implemented"); |
| 162 | + } |
| 163 | + |
| 164 | + function acceptSubscriptionOwnerTransfer(uint256) external pure { |
| 165 | + revert("not implemented"); |
| 166 | + } |
| 167 | + |
| 168 | + function pendingRequestExists(uint256) public pure returns (bool) { |
| 169 | + revert("not implemented"); |
| 170 | + } |
| 171 | + |
| 172 | + // ************************************* // |
| 173 | + // * Errors * // |
| 174 | + // ************************************* // |
| 175 | + |
| 176 | + error InvalidRandomWords(); |
| 177 | +} |
0 commit comments