-
Notifications
You must be signed in to change notification settings - Fork 2
/
VRFConsumer.sol
71 lines (59 loc) · 2.21 KB
/
VRFConsumer.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;
import {VRFConsumerBase} from "@bisonai/orakl-contracts/src/v0.1/VRFConsumerBase.sol";
import {IVRFCoordinator} from "@bisonai/orakl-contracts/src/v0.1/interfaces/IVRFCoordinator.sol";
import {IPrepayment} from "@bisonai/orakl-contracts/src/v0.1/interfaces/IPrepayment.sol";
contract VRFConsumer is VRFConsumerBase {
uint256 public sRandomWord;
address private sOwner;
IVRFCoordinator COORDINATOR;
error OnlyOwner(address notOwner);
modifier onlyOwner() {
if (msg.sender != sOwner) {
revert OnlyOwner(msg.sender);
}
_;
}
constructor(address coordinator) VRFConsumerBase(coordinator) {
sOwner = msg.sender;
COORDINATOR = IVRFCoordinator(coordinator);
}
// Receive remaining payment from requestRandomWordsPayment
receive() external payable {}
function requestRandomWords(
bytes32 keyHash,
uint64 accId,
uint32 callbackGasLimit,
uint32 numWords
) public onlyOwner returns (uint256 requestId) {
requestId = COORDINATOR.requestRandomWords(keyHash, accId, callbackGasLimit, numWords);
}
function requestRandomWordsDirect(
bytes32 keyHash,
uint32 callbackGasLimit,
uint32 numWords,
address refundRecipient
) public payable onlyOwner returns (uint256 requestId) {
requestId = COORDINATOR.requestRandomWords{value: msg.value}(
keyHash,
callbackGasLimit,
numWords,
refundRecipient
);
}
function fulfillRandomWords(
uint256 /* requestId */,
uint256[] memory randomWords
) internal override {
// requestId should be checked if it matches the expected request
// Generate random value between 1 and 50.
sRandomWord = (randomWords[0] % 50) + 1;
}
function cancelRequest(uint256 requestId) external onlyOwner {
COORDINATOR.cancelRequest(requestId);
}
function withdrawTemporary(uint64 accId) external onlyOwner {
address prepaymentAddress = COORDINATOR.getPrepaymentAddress();
IPrepayment(prepaymentAddress).withdrawTemporary(accId, payable(msg.sender));
}
}