Hello, World!
Today we are going to see how we can obtain the Random number using VFR( Verifiably Random function) via chain link api . In this smart book we are gonna to see the basic defination of the VRF , randon gernator code , How to setup the Subscription manager and many more things. Before starting this smartbook i would suggest u all to take a look on the official docs of the chainlink VRF https://docs.chain.link/docs/get-a-random-number/
Chainlink VRF (Verifiable Random Function) is a provably fair and verifiable random number generator (RNG) that enables smart contracts to access random values without compromising security or usability.All the proof is stored and publish on-chain before any consuming applications can use it.
The main reason behind chossing the VRF as a api of the chainlink beacuse as we all know that all the proof is stored on-chain before any specific entity or application use it . VRF process ensures that the result can not be reviled by any miner , oracle operator or smart contract developer .
THere is also various application of the VRF like ,
Here the fundind basically means Your deployed smart contract should have the suitable Cainlink faucet for gernating randon number .
For this example, create a new subscription on the Rinkeby testnet.
If are facing any error or confusion regarding the subscription manager then go through the video attach in the last of this smart book .
// SPDX-License-Identifier: MIT
// An example of a consumer contract that also owns and manages the subscription
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/interfaces/LinkTokenInterface.sol";
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";
contract VRFv2SubscriptionManager is VRFConsumerBaseV2 {
VRFCoordinatorV2Interface COORDINATOR;
LinkTokenInterface LINKTOKEN;
// Rinkeby coordinator. For other networks,
// see https://docs.chain.link/docs/vrf-contracts/#configurations
address vrfCoordinator = 0x6168499c0cFfCaCD319c818142124B7A15E857ab;
// Rinkeby LINK token contract. For other networks, see
// https://docs.chain.link/docs/vrf-contracts/#configurations
address link_token_contract = 0x01BE23585060835E02B77ef475b0Cc51aA1e0709;
// The gas lane to use, which specifies the maximum gas price to bump to.
// For a list of available gas lanes on each network,
// see https://docs.chain.link/docs/vrf-contracts/#configurations
bytes32 keyHash = 0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc;
// A reasonable default is 100000, but this value could be different
// on other networks.
uint32 callbackGasLimit = 100000;
// The default is 3, but you can set this higher.
uint16 requestConfirmations = 3;
// For this example, retrieve 2 random values in one request.
// Cannot exceed VRFCoordinatorV2.MAX_NUM_WORDS.
uint32 numWords = 2;
// Storage parameters
uint256[] public s_randomWords;
uint256 public s_requestId;
uint64 public s_subscriptionId;
address s_owner;
constructor() VRFConsumerBaseV2(vrfCoordinator) {
COORDINATOR = VRFCoordinatorV2Interface(vrfCoordinator);
LINKTOKEN = LinkTokenInterface(link_token_contract);
s_owner = msg.sender;
//Create a new subscription when you deploy the contract.
createNewSubscription();
}
// Assumes the subscription is funded sufficiently.
function requestRandomWords() external onlyOwner {
// Will revert if subscription is not set and funded.
s_requestId = COORDINATOR.requestRandomWords(
keyHash,
s_subscriptionId,
requestConfirmations,
callbackGasLimit,
numWords
);
}
function fulfillRandomWords(
uint256, /* requestId */
uint256[] memory randomWords
) internal override {
s_randomWords = randomWords;
}
// Create a new subscription when the contract is initially deployed.
function createNewSubscription() private onlyOwner {
s_subscriptionId = COORDINATOR.createSubscription();
// Add this contract as a consumer of its own subscription.
COORDINATOR.addConsumer(s_subscriptionId, address(this));
}
// Assumes this contract owns link.
// 1000000000000000000 = 1 LINK
function topUpSubscription(uint256 amount) external onlyOwner {
LINKTOKEN.transferAndCall(address(COORDINATOR), amount, abi.encode(s_subscriptionId));
}
function addConsumer(address consumerAddress) external onlyOwner {
// Add a consumer contract to the subscription.
COORDINATOR.addConsumer(s_subscriptionId, consumerAddress);
}
function removeConsumer(address consumerAddress) external onlyOwner {
// Remove a consumer contract from the subscription.
COORDINATOR.removeConsumer(s_subscriptionId, consumerAddress);
}
function cancelSubscription(address receivingWallet) external onlyOwner {
// Cancel the subscription and send the remaining LINK to a wallet address.
COORDINATOR.cancelSubscription(s_subscriptionId, receivingWallet);
s_subscriptionId = 0;
}
// Transfer this contract's funds to an address.
// 1000000000000000000 = 1 LINK
function withdraw(uint256 amount, address to) external onlyOwner {
LINKTOKEN.transfer(to, amount);
}
modifier onlyOwner() {
require(msg.sender == s_owner);
_;
}
}
Note : The variable vrfCoordinator , link_token_contract , keyHash all these are suitabe only for the RInkeby test network . Don't try tp change all this the reason is very simple as they are not running in your local machine all these are api of the chain link that is already built and test on the chainlink , so that's why they are providing all these variables for various networks.
You can find all these on https://docs.chain.link/docs/vrf-contracts/
Click on the Video tutorial written below