How to use oracles on Klaytn – VRF’s Explained

With the advent of Verifiable Random Functions (VRFs) provided by decentralized oracles, getting random numbers has never been more easy and secure. VRFs are essential to many blockchain applications, as having access to tamper-proof randomization enables exciting blockchain games, rare NFTs, and efficient unbiased outcomes.

In this article, you will create a subscription, add a consumer contract programmatically and get random values using Chainlink VRF v2 on the Klaytn Blockchain.

Why VRFs

Before VRFs, getting random numbers on-chain was a major problem and has played a part in major protocol exploits. The following are the malicious or risky traditional random number generators (RNG) methods previously used and their shortcomings:

Using block variables 

Using block variables such as block.number, block.timestamp, or block.difficulty to simulate randomness is risky as it can be predicted to some degree.

Relying on centralized APIs

Centralized APIs can be hacked, or the source owner can manipulate the blockchain for malicious reasons.

These shortcomings eventually brought about the invention of VRFs, which provide random numbers by relying on decentralized oracles.

What are VRFs?

VRF (Verifiable Random Function) is a provably fair and verifiable random number generator (RNG) that enables smart contracts to access random values without compromising security or usability.

How it works

VRF uses the Request & Receive Data cycle. For each request, the VRF generates one or more random values and cryptographic proof of how those values were determined. The proof is published and verified on-chain before any consuming applications can use it. This process ensures that results cannot be tampered with or manipulated by any single entity including oracle operators, miners, users, or smart contract developers.

VRF Use Cases

We can use VRF to build smart contracts for any applications that rely on unpredictable outcomes, such as:

  • Blockchain games and NFTs. For example, assigning randomized attributes to NFTs
  • Random assignment of duties and resources. For example, selecting participants for an exclusive sale
  • Choosing a representative sample for consensus mechanisms

Background Concepts

LINK is the name of the Chainlink tokens, which serve as currency to pay Chainlink network operators for retrieving and preparing off-chain data and performing computations.

There are two methods for requesting randomness, which are:

Subscription: With this method, you create a subscription account and fund the balance with LINK. And then you can connect various consuming contracts to this account to make requests from multiple contracts from a single subscription.

Direct Funding: Compared to the subscription method, you must directly fund your consumer contract and ensure there are enough LINK tokens to pay for randomness requests. 


In order to follow along this tutorial, you need the following configuration:

Step 1 – Obtaining test LINK

Make sure you have configured Klaytn Baobab network on MetaMask. You need LINK tokens when requesting randomness, which are available in Chainlink Faucet.

Click on Connect wallet and select MetaMask.

Verify Captcha request and click on Send me 20 testnet LINK.

After your request is completed, you will receive a congratulations message.

But your testnet LINK won’t be visible in your MetaMask wallet. If you want to see it, you can import the token manually.

You will then see this screen pop up. Click Add token.

Now you can see that the LINK token has been added to your MetaMask wallet.

Step 2 – Opening Subscription Manager Contract

In this guide, we will use an example subscription contract. For more information, refer to Programmatic Subscription in Chainlink Docs. 

So first, click here to open the subscription manager contract directly on Remix IDE.

Step 3 – Compiling & Deploying Smart Contract

Now that we have a contract, let’s first compile it. You can check Auto compile or compile yourself.

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.

Select the VRF2SubscriptionManager Contract and click Deploy.

Step 4 — Getting a random number

Now that we have successfully deployed our contract, we need to fund our subscription with some LINK, which we will use to request random numbers.

You can copy the contract address under Deployed Contracts.

Let’s paste in the address on MetaMask.

Click Next.

To cover the gas fee, make sure you also have testnet KLAY from faucet. Click Confirm.

We now need to top up our subscription account from the contract to pay for the transaction cost when requesting random numbers. Let’s send 5 LINK for good measure. To do this, click topUpSubscription and enter the amount, taking into account the 18 decimals. Then click Transact.

Now let’s call the `requestRandomWords` function. It may take some time for nodes to verify and confirm the request.

Call the s_randomwords function and pass zero(0) as argument. You will see the random number generated below.


In our previous article, we explored using oracles to get price feeds and in this article, we explained Verifiable Random Functions and how to request for random numbers programmatically. With VRF’s, you can start building your exciting blockchain applications on Klaytn.

MORE FROM Technology


Klaytn and SORAMITSU successfully conclude public testing of open-source DEX



4Q22 KGF/KIR Activity Report



Klaytn Brings the Best of Web3 to AVATARA, NX3’s First Blockchain MMORPG



© Klaytn Foundation 2022. All rights reserved.
© Klaytn Foundation 2022. All rights reserved.