-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathVRFSubscriptionManagerV2Mock.sol
141 lines (121 loc) · 5.13 KB
/
VRFSubscriptionManagerV2Mock.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//SPDX-License-Identifier: MIT
/**
* @authors: [@malatrax]
* @reviewers: []
* @auditors: []
* @bounties: []
* @deployments: []
*/
pragma solidity 0.8.18;
import "./VRFCoordinatorV2InterfaceMock.sol";
/**
* @title VRF Coordinator Manager Mock
* @author Simon Malatrait <[email protected]>
* @dev This contracts implements a subscription manager for using VRF v2 with the Subscription Method.
* @dev It allows to create subscriptions, manage them and consumers.
* @dev VRFCoordinatorV2 address: https://docs.chain.link/vrf/v2/subscription/supported-networks#arbitrum-mainnet
* @dev For SECURITY CONSIDERATIONS, you might also have a look to: https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/VRFCoordinatorV2.sol
*/
contract VRFSubscriptionManagerV2Mock {
// ************************************* //
// * Events * //
// ************************************* //
/**
* Emitted when LINK tokens are sent from this contract to the current subscription.
* @param subscriptionId ID of the funded subscription
* @param amount Amount of LINK token, in wei.
*/
event SubscriptionFunded(uint64 subscriptionId, uint256 amount);
// ************************************* //
// * Storage * //
// ************************************* //
VRFCoordinatorV2InterfaceMock public vrfCoordinator;
uint64 public subscriptionId;
address public governor;
// ************************************* //
// * Function Modifiers * //
// ************************************* //
modifier onlyByGovernor() {
require(msg.sender == governor, "Access not allowed: Governor only");
_;
}
// ************************************* //
// * Constructor * //
// ************************************* //
/**
* @dev Constructs the Chainlink VRF v2 Subscription Manager.
* @param _governor The Governor of the contract
* @param _vrfCoordinator The address of the VRFCoordinator contract.
*/
constructor(address _governor, address _vrfCoordinator) {
vrfCoordinator = VRFCoordinatorV2InterfaceMock(_vrfCoordinator);
governor = _governor;
createNewSubscription();
}
// ************************************* //
// * Governance * //
// ************************************* //
/**
* @dev Changes the `vrfCoordinator` storage variable.
* @param _vrfCoordinator The new value for the `vrfCoordinator` storage variable.
*/
function changeVrfCoordinator(address _vrfCoordinator) external onlyByGovernor {
vrfCoordinator = VRFCoordinatorV2InterfaceMock(_vrfCoordinator);
}
// ************************************* //
// * State Modifiers * //
// ************************************* //
/**
* @dev Creates a new subscription, overriding the previous one to be manageable by the contract.
*/
function createNewSubscription() public onlyByGovernor {
subscriptionId = vrfCoordinator.createSubscription();
}
/**
* @dev Funds the current subscription by `amount` LINK tokens.
* @param amount Amount of LINK token in wei.
*/
function topUpSubscription(uint96 amount) external {
vrfCoordinator.fundSubscription(subscriptionId, amount);
}
/**
* @dev Add a Consumer to the subscription.
* @param consumer Address of the Consumer contract added to the subscription.
*/
function addConsumer(address consumer) external onlyByGovernor {
// Add a consumer contract to the subscription.
vrfCoordinator.addConsumer(subscriptionId, consumer);
}
/**
* @dev Removes a Consumer to the subscription
* @param consumer Address of the Consumer contract removed from the subscription.
*/
function removeConsumer(address consumer) external onlyByGovernor {
// Remove a consumer contract from the subscription.
vrfCoordinator.removeConsumer(subscriptionId, consumer);
}
/**
* @dev Cancel the current subscription and send the remaining LINK of the subscription to the governor.
*/
function cancelSubscriptionToGovernor() external onlyByGovernor {
vrfCoordinator.cancelSubscription(subscriptionId, governor);
subscriptionId = 0;
}
// ************************************* //
// * Public Views * //
// ************************************* //
/**
* @dev Returns information on the current subscription
* @return balance LINK token balance of the current subscription.
* @return reqCount Number of requests made to the subscription.
* @return owner Address of the current owner of the subscription.
* @return consumers List of consumers subscribed to the current subscription.
*/
function getSubscription()
external
view
returns (uint96 balance, uint64 reqCount, address owner, address[] memory consumers)
{
(balance, reqCount, owner, consumers) = vrfCoordinator.getSubscription(subscriptionId);
}
}