githubEdit

Get a Random Value from API

This section showcases how to get a random value inside a smart contract using Oraichain VRF 2.0

Prerequisite

Before integrating Oraichain VRF 2.0 into your tech stack, you need to install Oraichain cosmosjs library. Kindly follow the steps as demonstrated herearrow-up-right.

Integration steps

For Back-end Usage

Step 1: Import Oraichain cosmosjs library

const Cosmos = require("@oraichain/cosmosjs").default;

const lcdUrl = "https://lcd.orai.io";
const chainId = "Oraichain";
const mnemonic = "foo bar";
const requestFees = “100”;

const message = Cosmos.message;
const cosmos = new Cosmos(lcdUrl, chainId);
const childKey = cosmos.getChildKey(mnemonic);
const sender = cosmos.getAddress(mnemonic);

cosmos.setBech32MainPrefix('orai');

Step 2: Request a random transaction

Enter a string value of your preference at userInput. You will see the fee for your request at requestFees. Also, maximum gas and tx fee need to be decided before each request.

⚠️ In the unlikely case that your request takes too long to respond, try switching "BROADCAST_MODE_BLOCK" to "BROADCAST_MODE_SYNC", and use tx hash to query until it is included on-chain.

Regarding how to query tx hash, please refer to this examplearrow-up-right or thisarrow-up-right.

Step 3: Get a random round number

Step 4: Get the information from a round

contract is the contract address of the VRF issued by Oraichain and can be reviewed on Oraiscan any time**.**

If the round result has not displayed the randomness output, you just need to wait for some time before querying again.

For browser usage

Step 1: Import Oraichain cosmosjs library

Step 2: Import keystation for wallet use by creating a new js file

Step 3: Create a randomness request

Function getChildKey() is as follow:

Step 4: Repeat the previously mentioned step 3 and 4 to get the random value and round information

Last updated

Was this helpful?