VRFSubscriptionManagerV2

Git Source

Author: Simon Malatrait simon.malatrait@grenoble-inp.org

This contracts implements a subscription manager for using VRF v2 with the Subscription Method.

It allows to create subscriptions, manage them and consumers.

LINK Token Arbitrum: https://docs.chain.link/resources/link-token-contracts?parent=vrf#arbitrum

VRFCoordinatorV2 address: https://docs.chain.link/vrf/v2/subscription/supported-networks#arbitrum-mainnet

For SECURITY CONSIDERATIONS, you might also have a look to: https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/VRFCoordinatorV2.sol

State Variables

vrfCoordinator

VRFCoordinatorV2Interface public vrfCoordinator;

linkToken

LinkTokenInterface public linkToken;

subscriptionId

uint64 public subscriptionId;

governor

address public governor;

Functions

onlyByGovernor

modifier onlyByGovernor();

constructor

Constructs the Chainlink VRF v2 Subscription Manager.

constructor(address _governor, address _vrfCoordinator, address _linkToken);

Parameters

NameTypeDescription
_governoraddressThe Governor of the contract
_vrfCoordinatoraddressThe address of the VRFCoordinator contract.
_linkTokenaddressThe address of the LINK token.

changeVrfCoordinator

Changes the vrfCoordinator storage variable.

function changeVrfCoordinator(address _vrfCoordinator) external onlyByGovernor;

Parameters

NameTypeDescription
_vrfCoordinatoraddressThe new value for the vrfCoordinator storage variable.

requestSubscriptionOwnerTransfer

Request the ownership transfer of the current subscription to newOwner

The current owner of the subscription is the Subscription Manager contract

function requestSubscriptionOwnerTransfer(address newOwner) external onlyByGovernor;

Parameters

NameTypeDescription
newOwneraddressAddress of the proposed new owner of the current subscription Note: Transferring the ownership should be used when migrating a subscription from one Subscription Manager to another without removing the consumers nor cancelling the subscription

acceptSubscriptionOwnerTransfer

Accept the subscription transfer of ownership.

function acceptSubscriptionOwnerTransfer(uint64 _subscriptionId) external onlyByGovernor;

Parameters

NameTypeDescription
_subscriptionIduint64ID of the subscription to be accepted. It will override the current subscription if any.

createNewSubscription

Creates a new subscription, overriding the previous one to be manageable by the contract.

function createNewSubscription() public onlyByGovernor;

topUpSubscription

Funds the current subscription by amount LINK tokens.

function topUpSubscription(uint256 amount) external;

Parameters

NameTypeDescription
amountuint256Amount of LINK token in wei.

addConsumer

Add a Consumer to the subscription.

function addConsumer(address consumer) external onlyByGovernor;

Parameters

NameTypeDescription
consumeraddressAddress of the Consumer contract added to the subscription.

removeConsumer

Removes a Consumer to the subscription

function removeConsumer(address consumer) external onlyByGovernor;

Parameters

NameTypeDescription
consumeraddressAddress of the Consumer contract removed from the subscription.

cancelSubscriptionToGovernor

Cancel the current subscription and send the remaining LINK of the subscription to the governor.

function cancelSubscriptionToGovernor() external onlyByGovernor;

withdrawLinkToGovernor

Transfers amount LINK tokens of the Subscription Manager (this contract) to the governor.

function withdrawLinkToGovernor(uint256 amount) external onlyByGovernor;

Parameters

NameTypeDescription
amountuint256Amount of LINK token in wei.

getSubscription

Returns information on the current subscription

function getSubscription()
    external
    view
    returns (uint96 balance, uint64 reqCount, address owner, address[] memory consumers);

Returns

NameTypeDescription
balanceuint96LINK token balance of the current subscription.
reqCountuint64Number of requests made to the subscription.
owneraddressAddress of the current owner of the subscription.
consumersaddress[]List of consumers subscribed to the current subscription.

Events

SubscriptionFunded

Emitted when LINK tokens are sent from this contract to the current subscription.

event SubscriptionFunded(uint64 subscriptionId, uint256 amount);

LinkWithdrawn

Emitted when the governor withdraws amount LINK from the subscription manager.

event LinkWithdrawn(address indexed receiver, uint256 indexed amount);