Witnet is a multichain decentralized oracle network that enables smart contracts to connect to any online valuable data source, e.g., sports results, stock prices, weather forecasts, and also randomness sources. As a permissionless blockchain oracle, it relies on Witnesses – a distributed peer of nodes, to request data and report it directly to the smart contract. This makes it a reliable source of randomness for smart contract use-cases that require some degree of unpredictability. Lotteries, NFTs, gaming, and other applications requiring a degree of randomness are examples of such use-cases.
The Witnet Foundation provides us with a `WitnetRandomness` Contract(also known as the Witnet Randomness Oracle) that can be used by practically all dApps. The instance of this randomness contract is live on the Klaytn blockchain, both on the mainnet (Cypress) and the testnet (Baobab). In this tutorial, you will use the Witnet Randomness Oracle to generate random numbers in your smart contract.
Randomness in Smart Contract
Getting random numbers on chain is a bit of a hurdle because of the deterministic nature of smart contracts, and smart contract developers do not have access to native random number generation functions in Solidity. Mechanisms to build randomness purely on-chain are prone to front-running by miners, manipulations, and abuse. This is where an oracle comes in: the job of an oracle is to connect smart contracts to real-world data like numbers in a tamper-proof way.
Witnet provides a solution to generate reliable randomness for smart contracts in EVM-compatible chains, such as Klaytn. A random number request is completed by a number of witnessing nodes that have been randomly selected. This process is reliable because it is cryptographically done by multiple anonymous, randomly selected oracle nodes, who are incentivized to remain honest and to compete for rewards, and whose stake in the protocol will be slashed in the case of misreporting.
To generate random numbers, the Witnet oracle provides us with two methods, viz.,
- Using the WitnetRandomness Contract
- Posting a low-level WitnetRequestRandomness to the WitnetRequestBoard entrypoint
In this tutorial, you will interact with the WitnetRandomness contract through the IWitnetRandomness interface, which is available through the witnet-solidity-bridge npm package.
Generate Random Numbers with Witnet
In this section, you will use the IWitnetRandomness contract interface to generate random numbers from the WitnetRandomness contract.
In order to follow along with this tutorial, you need the following configuration:
Step 1 – Opening WitnetRandomness Sample Contract
In this guide, we would use a sample contract as provided by the WItnet team here.
Click to open the WitnetRandomness sample contract on Remix IDE.
Step 2 – Compiling and Deploying Smart Contract
Now that we have a contract, let’s first compile it. You can either select the “Auto Compile” checkbox or the “Compile” button to compile manually.
After compiling, click on the Klaytn logo on the side panel, and change the environment to Injected Web3. A popup will appear to connect to your MetaMask wallet. Select the account you want to connect to and click Next to proceed.
Choose the WitnetRandomness Contract shown below.
Before deploying, the contract constructor requires the address of the WitnetRandomness contract on the Klaytn network. You can retrieve the addresses here. For this example, you can use the Klaytn Testnet address (0xB4B2E2e00e9d6E5490d55623E4F403EC84c6D33f).
Now that you have set the constructor argument, click Deploy.
Step 3 – Getting a random number
To view your already deployed contract, open the Deployed Contracts tab.
Now let’s generate random numbers. To generate random numbers, you need to undergo this two-step process, viz:
- requestRandomNumber(): This function initiates a random number request to be completed by the Oracle nodes. Users pay the gas cost of the randomization request and are required to deposit an amount of the native token (KLAY/ test KLAY), which is why the function is marked payable. All unused funds shall be transferred back to the sender.
To execute the `requestRandomNumber` function, you need to:
- Set the value field. For good measure, let’s set it to 0.5 KLAY. This value is meant to be the transaction cost when requesting random numbers. Click on requestRandomNumber button to request a random number.
Note: Randomisation requests take some time to complete, so calling fetchRandomNumber() right after requestRandomNumber() will most likely cause the transaction to revert. Therefore you have to wait 5 to 10 minutes before executing fetchRandomNumber().
- fetchRandomNumber(): This function retrieves the random number from the WitnetRandomness contract and stores it in the randomness variable in your smart contract.
It is important to wait for 5 –10 minutes before executing fetchRandomNumber(). Calling this function right after executing requestRandomNumber() might cause a gas estimation error as seen below:
You should be able to get your random number after your fetchRandomNumber transaction is successful by clicking on the randomness button.
Congratulations on getting your random number by interacting with the WitnetRandomness Contract.
In this article, we explained Witnet as a decentralized oracle network, enumerated the significance of random numbers, and explained why oracles are the best suit for connecting smart contracts to off-chain data as random numbers. We explained how to request random numbers using the Witnet Randomness Contract. You can begin developing exciting blockchain applications on Klaytn using Oracles such as Witnet.