Skip to main content

πŸ’» Tutorial

As a decentralized oracle service provider, Chainlink provides 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 this tutorial, you will use Chainlink VRF V2 contract to programmatically create a subscription, add a consumer contract and request random values on the Kaia blockchain.

Prerequisites

Make sure you have configured Kaia Kairos 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.

To view your LINK Tokens in your wallet, You have to import the token by following the guide in LINK Token Contracts. Click the Add to wallet button.

A new window pop-up will appear asking you to β€œAdd token”. Follow through this process, and you should see your tokens successfully imported.

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.

After compiling, click on the Kaia 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 VRFv2SubscriptionManager 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.

To cover the gas fee, make sure you also have testnet KAIA 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 toUpSubscription and enter the amount, taking into account the 18 decimals. Then click Transact and confirm the transaction.

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 an argument.

Congratulations on successfully requesting for random numbers using ChainLink VRF V2 Contracts.

Conclusion

In this tutorial, we saw how to use Chainlink oracle to get the Random Numbers on Kaia Testnet (Kairos). If you want more information, visit Kaia Docs and Chainlink Oracle Docs. If you have any questions, visit Kaia Forum.

Chainlink integration is available only in the Kairos testnet.