-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChainlinkVRFProxy.sol
executable file
·117 lines (96 loc) · 3.53 KB
/
ChainlinkVRFProxy.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.17;
import "./VRFCoordinatorV2Interface.sol";
import "./VRFConsumerBaseV2.sol";
import "./ConfirmedOwner.sol";
contract ChainlinkVRFProxy is VRFConsumerBaseV2, ConfirmedOwner {
struct RequestStatus {
bool fulfilled;
bool exists;
uint256[] randomWords;
}
event RequestRandomWords(uint256 requestId);
event FulfilledRandomWords(uint256 requestId, uint256[] randomWords);
/**
* @dev controller of current proxy. onlyOwner means only controller
* @notice must be contract with implament function
* `fulfillRandomWordsCallback(uint256 requestId,uint256[] memory randomWords)`
*/
address controller;
uint64 subscriptionId;
uint16 requestConfirmations;
VRFCoordinatorV2Interface public coordinator;
bytes32 public keyHash;
uint256 public latestRequestId;
uint256[] public requestIds;
mapping(uint256 => RequestStatus) public requests;
constructor(
address controller_, // control contract
address consumer_,
address coordinator_,
bytes32 keyHash_
) VRFConsumerBaseV2(consumer_) ConfirmedOwner(controller_) {
require(
controller_ != address(0),
"Ownable: controller is the zero address"
);
require(coordinator_ != address(0), "coordinator is the zero address");
require(keyHash != bytes32(0), "keyHash is bytes32(0)");
controller = controller_;
coordinator = VRFCoordinatorV2Interface(coordinator_);
keyHash = keyHash_;
}
function requestRandomWords(
uint32 numWords,
uint32 callbackGasLimit
) external onlyOwner returns (uint256 requestId) {
requestId = coordinator.requestRandomWords(
keyHash,
subscriptionId,
requestConfirmations,
callbackGasLimit,
numWords
);
requests[requestId] = RequestStatus({
fulfilled: false,
exists: true,
randomWords: new uint256[](0)
});
requestIds.push(requestId);
latestRequestId = requestId;
emit RequestRandomWords(requestId);
}
/**
* @notice owner must implament
* fulfillRandomWordsCallback(uint256 _requestId,uint256[] memory randomWords) function to receive randomWords
* @dev receive randomWords from chainlink and notify owner
* @param requestId requestId of randomwords
* @param randomWords random words of requestId return from chainlink vrf
*/
function fulfillRandomWords(
uint256 requestId,
uint256[] memory randomWords
) internal override {
require(requests[requestId].exists, "request not found");
requests[requestId].fulfilled = true;
requests[requestId].randomWords = randomWords;
controller.call(
abi.encodeWithSignature(
"fulfillRandomWordsCallback(uint256 requestId,uint256[] memory randomWords)",
requestId,
randomWords
)
);
emit FulfilledRandomWords(requestId, randomWords);
}
function getRequestIds() external view returns (uint256[] memory) {
return requestIds;
}
function getReuqestStatus(
uint256 requestId
) external view returns (bool fulfilled, uint256[] memory randomWords) {
require(requests[requestId].exists, "requestId not exists");
RequestStatus memory status = requests[requestId];
return (status.fulfilled, status.randomWords);
}
}