Schedule Randomness with Gelato and Witnet, API3 & Chainlink Vrf

Javier Donoso
Coinmonks
Published in
9 min readSep 27, 2022

--

Learn how to use Gelato in conjunction with Witnet, API3 & Chainlink Vrf to automate the randomness

Schedule the randomness

Table of Contents

Randomness in Web3
- Current Solutions
- Automation and Randomness
Show Case Project
- Step 1) Gelato as the Master of Ceremony
- Step 2) API3 QRNG for random components
- Step 3) Witnet for random control type
- Step 4) Chainlink Vrf to choose employee
Conclusion

From roulette wheels to action-adventure games, randomness plays a major role in the gaming world. It is for any scenario where opponents or scenes are created dynamically and for those where users need “on the fly” randomness.

This is a far cry from my memories of the 1980s, when gaming was a lot less enjoyable precisely because everything was predictable- if your character went on a mission once, the next time you played the mission would be exactly the same. As you can imagine, this got very boring very quickly.

However, randomness has many other uses in the web3 ecosystem such as:

DAOs & Public participation processes: One of the greatest web3 characteristics is the existence of communities driving projects. In the long term, I think the projects that will succeed will be those with robust and active communities. In this case, randomness is required to ensure fairness in the reward and participation processes.

NFT generation: Over the past 5 years we have seen the exponential growth of NFTs and digital arts. In some cases, the NFT collections have random characteristics which influence the value.

Lottery: Beyond the traditional lottery games, the DeFi ecosystem allows for innovation such as “no-loss” lottery.

Due to the nature of the blockchain, where miners can create/discard blocks, and “re-roll” the dice until a specific value is found, no manipulable randomness can’t be created on-chain.

In all cases but especially in Lottery and NFT Generation where large value amounts are present, it is imperative to use rrandomness that cannot be manipulated.

Current Solutions

Most of the current solutions are using an Oracle off-chain. We must request numbers, which (once available) will enable us to execute our custom logic. In this article we will explore:

  • Witnet
  • API3 QRNG
  • Chainlink VRF

In this article, I will showcase how to implement the three solutions in conjunction with Gelato.

Automation and Randomness

The need for self-executing methods within smart contracts is growing every day. However the “Big Bang” will happen when blockchain technology will extend into non-blockchain business.

For instance, all processes (no matter the industry) are subject to quality control which relies on random sampling to run the controls to ensure that no bias is present.

Due to the specifics of acquiring random numbers in blockchain through an Oracle, two methods are needed:

  • One to request the random number to the oracle
  • The second is a callback for when the number is available. Chainlink Vrf and API3 QRNG execute the callback code in your contract, however, Witnet does not.

Gelato comes in handy to tackle this situation by creating a task, that will check when the random number is available and execute any custom logic. This task will be cancelled after one run

In our showcase project, we will demo this use case for Witnet, Chainlink and API3.

Show Case Project

Let’s imagine for a moment that we run an automobile factory, and that our main assembly machine has 20 components. Although we do periodic maintenance (one more example of automation), we are required to run random quality control every 10 minutes. Here’s what that would look like:

  1. Choosing 2 components, every component can be controlled once per hour.
  2. Choosing the level of control: express, medium or intensive
  3. Choosing one employee of the 500 to run the quality control

In order to run tamper-proof quality control and avoid bias, our system has to generate random results for all steps.

Landing page

Repo can be found here

We will use Gelato as the Master of Ceremony orchestrating all tasks to be run like calling oracles, checking whether results are available, and updating the components arrays. etc.. The deployed dapp on Goerli is live at https://gelato-vrf.web.app

The major contract “ScheduleTheRandomness” can be found here. Etherscan verified logs at:

Step 1) Gelato as the Master of Ceremony

We will use gelato to run the plan, every 15 min we will do quality control. In order to do that, we must:

a) Wire the Gelato Infrastructure, first, we have to copy IOPS.soland OpsReady.solboth files can be found at

b) After copying the files, we can then insert the import and inherit the contract:

c) Create a timed task:

This task will check whether the 15 minutes have passed and if an older control is still running checkQualityPlanIsActive() and then run the doQualityControl() method where the control flow starts with the different calls to request random numbers.

One very interesting usage of gelato is to execute the custom logic once the random number is available. In the case of Witnet we are obliged to do so as Witnet does not provide a callback, but also we can implement it in Chainlink or API3 to have more control over the callback execution.

We can use Gelato for creating requests to the randomness Oracles as well as to take control over the callback execution

Step 2) API3 QRNG for random components

In this step, we will use API3 QRNG by API3 to generate 2 random components to be controlled.

a) We are required to wire the API3 infrastructure into our contract meaning that:
- It is necessary to add npm i @api3/airnode-protocol , and import “@api3/airnode-protocol/contracts/rrp/requesters/RrpRequesterV0.sol"; into your contract.

- We must inherit the contract and pass in the constructor the airnode address for the respective chain, addresses can be found here.

b) To retrieve QRNG we must fund the airnode. To do this, we must create and fund a Sponsor Wallet, either through the API3 Admin CLI or by deriving the sponsor wallet address from the contract address:
- npm i @api3/airnode-adminto derive the sponsor wallet
- Call the deriveSponsorWalletAddress()

The xpub and airnode params can be found in the API3 provider section here

API3 QRNG Quick Recap: we have set and wired the infrastructure, and created(& funded) a sponsor wallet to pay for transactions. Now we will set the parameters and the respective methods

c) Setting the params. We need to pass the following params to our contract

Both endpointIdUint256 and endpointIdUintArray256 can also be found at the API3 provider section here

d) In this last step we will need to create the two methods airnodeRrp.makeFullRequest() and the callback once the random have been delivered, in our case fullfillRandomComponents() as defined in the request.

API3 extract snippet

STEP 2: Witnet for random control type

In this step, we will request a random number between 1 and 3 to represent the three different control types (express, medium, intensive). To do so we will use the Randomness Contract by the Witnet oracle.

a) We are required to wire the API3 infrastructure into our contract:
npm i witnet-solidity-bridge, and import “witnet-solidity-bridge/contracts/interfaces/IWitnetRandomess.sol";.

b) To interact with the Randomness Contract we will need to create an instance of it within our contract. First, we must copy the chain address we are working on (contract addresses). In this particular case, we are on Goerli, so we will pass the Goerli’s Randomness Contract address by deployment. After which, within the constructor, we will create an instance of the Randomness Contract

c) We will request a random number rom Witnet

d) Unlike API3 and Chainlink Vrf Witnet does not provide a callback, however, the Randomness Contract provides theisRandomized() , a method that returns whether the random number can be retrieved or not. Once isRandomized() is true, we call witnet.random(3,0,latestRandomizingBlok);

We will use Gelato to create an “on the fly” task, that will check when isRandomized() returns true, and then will retrieve the random number and continue the control flow

In the next image, we can see both tasks, the first one running every 15 minutes for overall quality control and the second one is active only when we are awaiting the Witnet random number.

STEP 3: Chainlink Vrf to choose employee

Our last step is to pick up an employee to run the controls. In order to do so, we will use Chainlink Vrf to get a random number between 1 and 500 (the total number of employees).

a) Chainlink Vrf requires us to create a funded subscription with Link that will later be used by our contract (consumer contract) to pay the fees for requesting random numbers. You can do that here

Once the subscription is created and funded, you can add your contract as a consumer.

Once your contract is added as a consumer to the subscription we can continue with the settings

b) We are required to wire the Chainlink Vrf infrastructure into our contract which means:

-npm i @chainlink/contracts , and

import “@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol”; 
import “@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol”

- Inherit the contract and pass in the constructor the subscription ID and the VRF Coordinator address for the respective chain. Addresses can be found here (in our case is Goerli)

c) Now that our funding mechanism is ready and contract infrastructure is wired, we must write our request and callback methods COORDINATOR.requestRandomWords() and fullfillRandomwords() . Chainlink Vrf offers us the ability to set different parameters when calling requestRandomWords(), the code and params can be found here:

Conclusion

In my personal opinion, all the technologies mentioned in this article (Gelato Network, Chainlink Vrf, Witnet and API3 QRNG) are incredibly user-friendly and easy to learn.

Step by step, the web3 community is solving in a very efficient way the blockchain inherent problems. In the same way, Gelato Network solves the problem of scheduled tasks, and the three Vrf technologies analyzed here offer a very efficient way to create and consume random numbers on-chain.

Gelato Network can be used in different ways when dealing with randomness, from calling a random number at a specific point in time, to once the random number has been delivered, waiting for other conditions to be met in order to execute additional logic.

In our showcase project, for the sake of simplicity, we have done three random consecutively, but we could also parallelize the random calls and create a task that checks whether all three numbers are already available and execute the custom logic.

Moving forward we can foresee automation protocols like Gelato Network as a perfect match for Vrf generation solutions by providing a very elegant way of scheduling random numbers requests and orchestrating the callbacks and execution logic.

Thank you for reading!!

If you have questions or comments, please ping me on twitter @donoso_eth

--

--