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
1
fee :fee request
2
ORAI token : address token ORAI
3
VrfOracleOraichain: address oracle
Copied!

Example

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

Example BSC Mainnet

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

Example BSC Testnet

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