Get a Random Number
To use it, the contract needs to inherit VrfOracleOraichain and define 2 required functions
  1. 1.
    randomnessRequest: initiate a request to generate random numbers
  2. 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
1
Fee :fee request
2
ORAI token : address token ORAI
3
VrfOracleOraichain: address oracle
Copied!

Example

1
pragma solidity ^0.8.0;
2
3
import "@openzeppelin/contracts/utils/Address.sol";
4
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
5
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
6
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
7
8
interface IVRFOracleOraichain {
9
function randomnessRequest(uint256 _seed, bytes calldata _data) external returns (bytes32 reqId);
10
11
function getFee() external returns (uint256);
12
}
13
14
contract VrfOracleOraichainExample {
15
16
uint256 public fee;
17
18
19
address public orai = "address token orai of network";
20
21
address public oracle "address oracle of network";
22
23
uint256 public random;
24
25
bytes32 public reqId;
26
27
bytes32 public reqId2;
28
constructor (address _oraiToken, address _oracle) public {
29
orai = _oraiToken;
30
oracle = _oracle;
31
}
32
33
function randomnessRequest(uint256 _seed) public {
34
IERC20(orai).approve(oracle, IVRFOracleOraichain(oracle).getFee());
35
36
bytes memory data = abi.encode(address(this), this.fulfillRandomness.selector);
37
38
reqId = IVRFOracleOraichain(oracle).randomnessRequest(_seed, data);
39
}
40
41
function fulfillRandomness(bytes32 _reqId, uint256 _random) external {
42
random = _random;
43
reqId2 = _reqId;
44
}
45
46
}
47
Copied!

Example BSC Mainnet

1
pragma solidity ^0.8.0;
2
3
import "@openzeppelin/contracts/utils/Address.sol";
4
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
5
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
6
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
7
8
interface IVRFOracleOraichain {
9
function randomnessRequest(uint256 _seed, bytes calldata _data) external returns (bytes32 reqId);
10
11
function getFee() external returns (uint256);
12
}
13
14
contract VrfOracleOraichainExample {
15
16
uint256 public fee;
17
18
19
address public orai = address(0xA325Ad6D9c92B55A3Fc5aD7e412B1518F96441C0);
20
21
address public oracle = address(0x9da069777A07326659b715522b8458179AaE0BD8);
22
23
uint256 public random;
24
25
bytes32 public reqId;
26
27
bytes32 public reqId2;
28
constructor (address _oraiToken, address _oracle) public {
29
orai = _oraiToken;
30
oracle = _oracle;
31
}
32
33
function randomnessRequest(uint256 _seed) public {
34
IERC20(orai).approve(oracle, IVRFOracleOraichain(oracle).getFee());
35
36
bytes memory data = abi.encode(address(this), this.fulfillRandomness.selector);
37
38
reqId = IVRFOracleOraichain(oracle).randomnessRequest(_seed, data);
39
}
40
41
function fulfillRandomness(bytes32 _reqId, uint256 _random) external {
42
random = _random;
43
reqId2 = _reqId;
44
}
45
46
}
47
Copied!

Example BSC Testnet

1
pragma solidity ^0.8.0;
2
3
import "@openzeppelin/contracts/utils/Address.sol";
4
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
5
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
6
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
7
8
interface IVRFOracleOraichain {
9
function randomnessRequest(uint256 _seed, bytes calldata _data) external returns (bytes32 reqId);
10
11
function getFee() external returns (uint256);
12
}
13
14
contract VrfOracleOraichainExample {
15
16
uint256 public fee;
17
18
19
address public orai = address(0xc4D66c12086A0ed7c175B2C7317F48838235f3D9);
20
21
address public oracle = address(0x75aDaF97eAf5ad586c0c733852cdA62A1cdB8B0D);
22
23
uint256 public random;
24
25
bytes32 public reqId;
26
27
bytes32 public reqId2;
28
constructor (address _oraiToken, address _oracle) public {
29
orai = _oraiToken;
30
oracle = _oracle;
31
}
32
33
function randomnessRequest(uint256 _seed) public {
34
IERC20(orai).approve(oracle, IVRFOracleOraichain(oracle).getFee());
35
36
bytes memory data = abi.encode(address(this), this.fulfillRandomness.selector);
37
38
reqId = IVRFOracleOraichain(oracle).randomnessRequest(_seed, data);
39
}
40
41
function fulfillRandomness(bytes32 _reqId, uint256 _random) external {
42
random = _random;
43
reqId2 = _reqId;
44
}
45
46
}
47
Copied!

Important: In the code, there should be a function to withdraw token orai if the network you use charges fees

Last modified 1mo ago