Site icon Tech Blog

Step-by-Step Guide on Integrating Witnet Randomness

Witnet the Ultimate Solution to the Oracle Problem

Witnet the Ultimate Solution to the Oracle Problem

Step-by-Step Guide on Integrating Witnet Randomness

In this article we will elaborate the Step-by-Step Guide on Integrating Witnet Randomness to your projects with some code examples.

Witnet Randomness

Firstly we should understand what is witnet?

A ground-breaking decentralized oracle network that seamlessly connects smart contracts on various blockchain platforms with reliable and verifiable data sources from the web2 is known as the Witnet blockchain, a layer 1 blockchain. Web 2.0 websites place an emphasis on user-generated content, ease of use, participatory culture, and interoperability for end users. By providing your smart contracts with access to accurate and reliable information, using Witnet ensures the utmost level of transparency and security for your blockchain efforts. Without having to worry about uncertainty or the oracle problem, Witnet allows you to read actual data from the blockchain. Thanks to Witnet, we now have the cutting-edge capacity to include randomization into our work. You can take your attempts to new heights by seamlessly integrating this capability, increasing security, and building enduring reliability. You may fully utilize Witnet randomness in your code by following the instructions in this Step-by-Step Guide on Integrating Witnet Randomness.

Before we get into this ensure you have this requirements in place;

  1. Basic knowledge of blockchain technology and smart contracts.
  2. Familiarity with a programming language that supports interacting with smart contracts (e.g., Solidity for Ethereum).
  3. Access to a blockchain development environment (e.g., Ethereum’s Remix or Truffle).
  4. Understanding how to use Solidity code on RemixIDE
  5. Test Tokens from etheruem, BNB, Conflux etc, depending on the network you want to deploy the smart contract

Step 1: Set Up Your Development Environment

Start by setting up your development environment for blockchain development. Install any necessary tools, such as Remix, Truffle, or a similar development platform. Ensure you have a local blockchain network or access to a testnet for testing purposes.

Step 2: There two methods you can create randomization in your contract.

  1. Utilizing the WitnetRandomness Contract, which is already pre-deployed on numerous EVM chains.
  2. Posting an entrypoint WitnetRequestRandomness with a low level.

The witnet-solidity-bridge npm package is always a useful tool.

You should use witnet.getRandomnessAfter() if you’re using the randomness oracle to randomly assign NFT attributes to each item in a collection. You can utilize each of the 32 random bytes to influence the various attributes you want to apply by sourcing them all at once.

Step 3: Deploy a Smart Contract

Create a new smart contract or use an existing one on the witnet documentation that requires random values. For this example, let’s assume you have a simple smart contract that needs a random number.

Here’s an example in Solidity:

// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

import “witnet-solidity-bridge/contracts/interfaces/IWitnetRandomness.sol”;

contract MyContract {

uint32 public randomness;
uint256 public latestRandomizingBlock;
IWitnetRandomness immutable public witnet;

/// @param _witnetRandomness Address of the WitnetRandomness contract.
constructor (IWitnetRandomness _witnetRandomness) {
assert(address(_witnetRandomness) != address(0));
witnet = _witnetRandomness;

receive () external payable {}

function requestRandomNumber() external payable {
latestRandomizingBlock = block.number;
uint _usedFunds = witnet.randomize{ value: msg.value }();
if (_usedFunds < msg.value) {
payable(msg.sender).transfer(msg.value – _usedFunds);}}

function fetchRandomNumber() external {

assert(latestRandomizingBlock > 0);
randomness = witnet.random(type(uint32).max, 0, latestRandomizingBlock);}}

Step 3: Deploy and Test

Compile and deploy your smart contract, and then test the random number generation function. Ensure that your metamask  is funded with enough test token to cover the gas costs of interacting with the Witnet network.

Please Note: Between the time you request the random number and the time you may use it, you must allow between 5 and 10 minutes for some common difficulties. Because it ensures that the number was only created after the request was received, Witnet’s random number function is asynchronous, protecting against manipulation and predictability.

For instance, if you have recently called getRandomNumber() or requestRandomness(), fetchRandomNumber() or fulfillRandomness() would likely revert.


NOTE: 18+ separate EVM compliant chains are currently receiving data and randomization from Witnet.

A lot of apps can use the same value that is stored on any of the EVM agnostic chains since instances of randomness can be employed without sacrificing unpredictability. In essence, you might be incorporating free randomness into your contract!

Like any other data feed, the Witnet Randomness Oracle relies on crowd-witnessing for its strength. By using randomization rather than a price feed, you are essentially querying the oracle in the same way. To create a random sequence of bytes that they will cryptographically commit to, nodes from the Witnet network are covertly and randomly chosen. Later, through sorting, concatenation, and hashing, the random sequences are exposed and deterministically aggregated.

Exit mobile version