Get a Random Number

To use it, the contract needs to inherit VrfOracleOraichain and define 2 required functions

  1. randomnessRequest: initiate a request to generate random numbers

  2. fulfillRandomness: will perform receiving and verifying data.

  • Your contract needs to have enough ORAI fee to make the request to generate random number.

  • Note: Some of the values in the example below need to be correct for each network

fee :fee request
ORAI token : address token ORAI
VrfOracleOraichain: address oracle

Example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
interface IVRFOracleOraichain {
function randomnessRequest(uint256 _seed, bytes calldata _data) external returns (bytes32);
function fee() external returns (uint256);
}
contract VrfOracleOraichainExample {
using SafeERC20 for IERC20;
uint256 public fee = 0;
address public orai = address("token orai of network");
address public oracle = address("oracle of networ );
uint256 public random;
bytes32 public reqId;
function randomnessRequest(uint256 _seed) public{
IERC20(orai).approve(oracle, IVRFOracleOraichain(oracle).fee());
bytes memory data = abi.encode(address(this), this.fulfillRandomness.selector);
reqId = IVRFOracleOraichain(oracle).randomnessRequest(_seed, data);
}
function fulfillRandomness(bytes32 _random) external {
random = uint(_random);
}
function transferOnlyOwner(IERC20 token, address to, uint256 amount) public {
token.safeTransfer(to, amount);
}
}

Example BSC Mainnet

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
interface IVRFOracleOraichain {
function randomnessRequest(uint256 _seed, bytes calldata _data) external returns (bytes32);
function fee() external returns (uint256);
}
contract VrfOracleOraichainExample {
using SafeERC20 for IERC20;
uint256 public fee = 0;
address public orai = address(0xA325Ad6D9c92B55A3Fc5aD7e412B1518F96441C0);
address public oracle = address(0x1e19249980542E9BFbBABbB0D2E15f7BB114A90b);
uint256 public random;
bytes32 public reqId;
function randomnessRequest(uint256 _seed) public{
IERC20(orai).approve(oracle, IVRFOracleOraichain(oracle).fee());
bytes memory data = abi.encode(address(this), this.fulfillRandomness.selector);
reqId = IVRFOracleOraichain(oracle).randomnessRequest(_seed, data);
}
function fulfillRandomness(bytes32 _reqId, bytes32 _random) external {
random = uint(_random);
}
function transferOnlyOwner(IERC20 token, address to, uint256 amount) public {
token.safeTransfer(to, amount);
}
}

Example BSC Testnet

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
interface IVRFOracleOraichain {
function randomnessRequest(uint256 _seed, bytes calldata _data) external returns (bytes32);
function fee() external returns (uint256);
}
contract VrfOracleOraichainExample {
using SafeERC20 for IERC20;
uint256 public fee = 0;
address public orai = address(0xE6487f7BdEB798e2457E44570f367a3059Ed9F93);
address public oracle = address(0x4144Dfd7Df97839507c404d1CA79b47aA227dDA2);
uint256 public random;
bytes32 public reqId;
function randomnessRequest(uint256 _seed) public{
IERC20(orai).approve(oracle, IVRFOracleOraichain(oracle).fee());
bytes memory data = abi.encode(address(this), this.fulfillRandomness.selector);
reqId = IVRFOracleOraichain(oracle).randomnessRequest(_seed, data);
}
function fulfillRandomness(bytes32 _reqId, bytes32 _random) external {
random = uint(_random);
}
function transferOnlyOwner(IERC20 token, address to, uint256 amount) public {
token.safeTransfer(to, amount);
}
}