VRF Oracle V2
Get Random numbers from Chainlink's Oracle on Chain
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. For each request, Chainlink VRF generates one or more random values and cryptographic proof of how those values were determined. The proof is published and verified on-chain before any consuming applications can use it. This process ensures that results cannot be tampered with or manipulated by any single entity including oracle operators, miners, users, or smart contract developers.
This guide explains how to get random values using a simple contract to request and receive random values from Chainlink VRF v2
Using VRF v2
Start by importing the datum-contracts package
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.14;
import "datum-contracts/interfaces/OptimisticOracleV3Interface.sol";
Create the contracts instance with the VRF Oracle address
// For Sepolia
oracleAddress = 0x48749fde6370FBfc9a45C5EFB559253D72B7Cd53
VRFOracleV2 _vrfOracle =
VRFOracleV2(oracleAddress);
Next step it to request the Random numbers from contract with the numWords and Numconfirmations info , from Oracle using
requestRandomness
function requestRandomWords() public {
uint numWords = 2;
uint numConfirmations = 3;
vrfId = _vrfOracle.requestRandomness(numWords, numConfirmations);
}
The process will take place in backend and will probably take about 2-3 Minutes , so be patient
To get the Random Words from the Oracle , call
getRandomness
function getRandomWords() public view returns (uint[] memory randomWords) {
randomWords = _vrfOracle.getRandomness(vrfId);
}
Full Example Contract Code
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.14;
// *************************************
// * Minimum Viable VRF Oracle *
// *************************************
// This contract shows how to get up and running as quickly as posible with Chainlink's VRF oracle
// We will requestRandomness from the oracle and fetch it later
import "datum-contracts/interfaces/VRFv2Interface.sol";
contract VRFOracleTest {
VRFOracleV2 _vrfOracle =
VRFOracleV2(0x48749fde6370FBfc9a45C5EFB559253D72B7Cd53);
uint vrfId;
// Submit a random uint request to the Oracle
function requestRandomWords() public {
uint numWords = 2;
uint numConfirmations = 3;
vrfId = _vrfOracle.requestRandomness(numWords, numConfirmations);
}
// Fetch the random uint back
function getRandomWords() public view returns (uint[] memory randomWords) {
randomWords = _vrfOracle.getRandomness(vrfId);
}
}
In this way , you can use VRF Oracle in simple steps , extra info is available on the Chainlink Docs
Last updated