Hands On Game Using VRF
You will be creating this game called Runners which will utilise Chainlink VRF

Deploying Our Game in Remix
For this demo, we will be using Remix IDE. Make sure you've completed Section 2 and Section 3 before attempting this exercise.
First create a file called Runners.sol and paste the following code (make sure to adjust your Remix compiler to refer to the compiler version in the pragma statement below):
In our constructor, you can see we are passing in a subscriptionId which means we first need to set up our VRF subscription here:
A VRF Subscription is a way to ensure that the decentralized Chainlink VRF service that sends your smart contract verifiably random numbers is "funded" with LINK.
For this exercise, let's use the Avalanche Fuji testnet.
First ensure you are connected to Avalanche Fuji network in your wallet. Then select Create Subscription. This subscription will be used to manage your VRF service for your smart contract.

On the Create Subscription page, enter a name for your subscription (optional) and click create subscription again.

Then you will sign and confirm the following metamask transactions. After you will see the following pop-up on your subscription page. You must wait for the transaction to be confirmed for a few seconds, after which you will see add funds which you must select.

After you click add funds you will be prompted to enter an amount of LINK tokens (if you need more testnet LINK tokens and AVAX tokens, head to https://faucets.chain.link/fuji).
For this demo we will be adding 10 LINK, then select confirm and confirm the following transaction.

Now we will be directed to the subscription page. From this page we need to copy our ID which is our subscriptionId.

Now we want to head back to our remix to the Deploy and run transactions tab, select environment Injected Provider - Metamask and make sure we are still on Avalanche Fuji Network (43113).
Then with our copied subscriptionId we will deploy the Runners.sol contract, then select the orange Deploy button

In our constructor we minted an NFT, so now we will go to Opensea and have a look at this NFT. To do this, you must copy your contract address. Note. Opensea does not support all testets, but it does support Avalanche Fuji. Here is a list of testnets that Opensea supports.
Next, paste your contract address in the Openseas search bar and select your collection. You should now see an NFT minted which is the Elf.

We now want to take our copied contract address, and head back to our VRF Subscription page. On this page we need to select Add Consumer.

This will prompt you to enter you consumer address, where you will paste your contract address and click add consumer.

Once this transaction is confirmed, we will head back to our contract in remix, open it, and call the orange run function passing in '0' for the parameter tokenId. Then click transact to execute this function.

Once the transaction has been executed, scroll down until you see the blue lastRequestId function and select this. This will return a long number which is the requestId for your call to VRF so copy this.

After some time has passed we will then paste this requestId into the blue s_requests function and we should expect to see the following as true

Now we will call the blue runners function, passing '0' as a parameter

You will notice that your value for distance may be different, mine was '50'. This is because Chainlink VRF was used to generate this number randomly - we cannot predict it or know it in advance! Random!
Lastly we will head back to Opensea and view the previously minted NFT. If we scroll down and open the Levels tab you will see that the NFT metadata has been updated with this change!

Last updated
