Witnet VRF For Onchain NFTs on Conflux eSpace

Temitayo
Conflux Network
Published in
3 min readMay 23, 2022

--

Witnet+Conflux

On-Chain Randomness

Generating random values on the blockchain(primarily for use in smart contracts) has not been fairly easy because almost all parameters like blockNumber, blockTimestamp or even blockHash which are being used to increase entropy in randomness generation can be easily manipulated by network miners thus making them deterministic. Therefore, there is a need for a trusted, decentralized, and verifiable protocol that can provide random values to a client(in this case, a smart contract) asking for it, This is what decentralized oracles like witnet provide among other services like Price Feeds and Web Oracles.

Why is it needed?

A lot of NFT projects have numerous traits that are assigned to each NFT in that collection, however, it would be disastrous to make the project developers give these traits manually to these NFTs, therefore a decentralized, random and verifiable way to distribute/assign these traits to NFTs is needed. This is where the witnet randomness oracle comes in. The process is simple;

  • NFT contract is written to integrate with witnet’s WitnetRandomness contract
  • Whenever a user comes to mint an NFT, the contract makes a random bytes request to the oracle
  • The contract then uses this 32 bytes random value to calculate the traits for the user’s NFT.

This article aims to dive into one of such contracts. Live code has been deployed here if you want to follow through.

Note that the project uses foundry, you can check my previous article on foundry here

We will be focusing on NFTLIL.sol

First, we check out the structure of an NFT

Basic NFT Structure

L3 -L7 shows that each NFT has 3 main properties;

  • A name which will either be a King, Warrior, Knight, or Steed
  • A tag which is also equal to the random bytes32 value gotten from witnet
  • five props which contain 5 numbers between 0 and 100

Also note that all these metadata are stored onchain instead of something like ipfs

Core functions
  • mintNFT()is the core function that allows users to mint an NFT, before minting, it calls fetchRandomness() which requests the witnet randomness oracle to finally fetch the ready random value. Note that you should only call fetchRandomness()after you have executed requestRandomness() and waited for some minutes. It then goes on to use this value to calculate the trait values of the NFT. The first requestRandomness() is called in the contract constructor.

Note that the witnet randomness oracle can either provide a random number(uint256) or a random bytes32 value, we are using a bytes32 value for this code since we will be using it to generate other traits.

  • genProp() is a function that takes in the random value gotten and resolves them to the 5 props property of the NFT
  • genName() also resolves the random value to a number between 0 and 3 and uses it to choose the name property of the NFT

Finally, we mentioned that this is an onchain NFT where all the metadata is generated and stored onchain, the main function is located here. All it does is take the traits of an NFT and wrap it into an SVG container and finally encode it with Base64 so it can be displayed onchain.

That’s all folks. If you want to play with the NFT, you can mint it here. It has been deployed on the Conflux eSpace testnet.

--

--