Skip to main content

chainlink-vrf

Testnet VRF Contracts:

  • Rewarding Token (ERC677) WTLOSERC677 0xc9894A9B9Fa32865e13a9297a55aBfed18300b99
  • VRFCoordinatorV2: 0x9a27A847629646aB29BafAd15a2777fb8D7dda98
  • VRFV2Wrapper: 0x3451eF0a630c13F5976769404E16C8397D4773fb

VRF service contracts

  • BlockhashStore: 0x061838A75239a73019B8a2Ab988a35A77de5CA06
  • Mock Oracle LINK/Native: 0x69a537c28553d58a0055568972bcaFF75e8C50bE

VRF testing consumer contracts

  • Test VRF Coordinator consumer: 0x4Df4Cbaf5b6300a6E97C25fa8F4056b9ae6A09e9
  • Test VRF Wrapper consumer: 0x4F81B0064bdB110DdE5234CC7cB27fdA77a7381B

Consuming VRF

In order to consume VRF user can choose one of the two methods: Subscription and Direct Funding, which are basically means either consuming directly from VRFCoordinatorV2 or through VRFV2Wrapper contract.

Subscription Method

This method requires the user to subscribe to the VRF Coordinator contract and to fund the created subscription:

  1. Subscribe to the VRF Coordinator contract by calling the createSubscription method, either in Explorer or in a smart contract; the ID of the subscription, of type uint64, will be returned and made available in the transaction logs;
  2. Fund the subscription: call the transferAndCall method of the LINK token contract with these parameters:
  • receiver - address of the VRF Coordinator contract;
  • amount - amount of LINK tokens in wei to fund the subscription;
  • data - encoded subscription ID: abi.encode(subscriptionID);

Then, the user has to deploy their VRF Consumer contract, aligning with the following requirements:

  1. It must inherit VRFConsumerBaseV2;
  2. It must implement the fulfillRandomness method, which will be called by the VRFCoordinatorV2 contract when the randomness is ready.

Example VRF consumer contract: VRFConsumerTest.sol.

And finally, the user has to add their VRF Consumer contract to the created subscription of the VRF Coordinator contract by calling the addConsumer method with the following parameters:

  • subID - ID of the subscription;
  • consumer - address of the VRF Consumer contract.

Once everything is set up, the user can submit their VRF request by calling requestRandomWords of the VRF Coordinator, including the following parameters in their request:

  • keyHash - identifier that maps to a job and a private key on the VRF service and that represents a specified gas lane; if the request is urgent, a gas lane with a higher gas price limit can be specified;
  • subscriptionId - subscription ID that the consuming contract is registered to; LINK funds are deducted from this subscription;
  • requestConfirmations - number of block confirmations the VRF service will wait to respond; this value must align with the minimum and maximum confirmations set for the VRF Coordinator;
  • callbackGasLimit: maximum amount of gas a user is willing to pay for completing the callback VRF function; user cannot put a value larger than maxGasLimit of the VRF Coordinator contract;
  • numWords - number of random numbers to request; it must align with the maximum random values set in the VRF Coordinator.

The VRF Coordinator contract will call the fulfillRandomWords method of the VRF Consumer contract when the randomness is ready.

You can refer to the official Chainlink documentation to get more information about the Subscription method:
https://docs.chain.link/vrf/v2/subscription

Direct Funding Method

This method does not require the user to create subscriptions and pre-fund them. Instead, the user must directly fund consuming contracts with LINK tokens before they request randomness.

The user has to deploy their VRF Consumer contract, aligning with the following requirements:

  1. It must inherit VRFV2WrapperConsumerBase;
  2. It must implement the fulfillRandomness method, which will be called by the VRFCoordinatorV2 contract when the randomness is ready. Once deployed, the Consumer contract must be funded with LINK tokens.

Example VRF consumer contract: VRFWrapperConsumerTest.sol.

Once everything is set up, the Consumer contract can submit their VRF request by calling requestRandomWords of the VRF Wrapper, including the following parameters in their request:

  • requestConfirmations - number of block confirmations the VRF service will wait to respond; this value must align with the minimum and maximum confirmations set for the VRF Coordinator;
  • callbackGasLimit: maximum amount of gas a user is willing to pay for completing the callback VRF function; user cannot put a value larger than maxGasLimit of the VRF Coordinator contract;
  • numWords - number of random numbers to request; it must align with the maximum random values set in the VRF Coordinator and VRF Wrapper.

You can refer to the official Chainlink documentation to get more information about the Subscription method:
https://docs.chain.link/vrf/v2/direct-funding