Using Chainlink VRF in a Smart Contract
Verifiable Random Functions and how to use them.
Chainlink (ChainLink on Medium) is slowly getting integrated into more and more blockchains, dApps, decentralized games, and even within de-fi applications. But what it is, and more importantly, how do I use it? I will try to explain both of those questions in this article.
What is Chainlink VRF
One of Chainlink’s main features is its VRF or Verifiable Random Function. This roughly means that you can get random values from Chainlinks contracts and that users could verify that your program isn’t giving you any better randomness than you claim to have.
An example
For example, you flip an online coin by searching ‘flip a coin’, or by using an app. You expect the chance for either heads or tails to be 50%, or at least after you flip a million coins it should be very close to that. But what if I skew the randomness in my favor?
I win €10 from you every time the program hits tails. But if I also programmed it to be ‘tails’ for 51% of the time, it will give me a huge benefit over those 1 million coin tosses.
To make it fair, I should provide documentation that I didn’t alter the randomness. And in a verified smart contract using VRF, you can prove that you are providing fair randomness.
How to implement Chainlink VRF in a Smart Contract
Now on to the technical side of things. We’re going to make a Solidity contract (for example for Ethereum or Polygon) to simulate coin tosses. I am going to use Truffle as my development suite and Remix to quickly deploy and test the contract in a visual way. You can also use HardHat.
Note: We’re gonna test on the Rinkeby testnet. You will need to have both Rinkeby ETH and LINK which you can both get from Chainlink’s faucet. You can Google to find the link to the faucets since there are a few available.
Setting things up
Let’s start off by adding some basic files and by initializing Truffle and installing Chainlink’s contracts from NPM.
truffle init
truffle create contract CoinToss
npm install @chainlink/contracts --save
This will initialize Truffle, and create a new empty contract called CoinToss.sol
that we will use for our verifiable fair coin toss dApp. Import Chainlink’s VRFConsumerBase contract to your own. Your code should roughly look like this:
Hint: Run truffle compile
to check everything is going right.
Implementing VRFConsumerBase
We need to implement VRFConsumerBase in our contract. We need to set it up properly depending on the testnet (or mainnet) that you’re going to use. So let’s do that. You can copy-paste most code from the official docs. You can also get the addresses and the key hash from the docs depending on the network you’re going to use.
So in theory, when we deploy this, fund the contract address with LINK and ETH, and call the getRandomNumber
function and the randomResult
variable, we should get a big random number.
So how can we use this number to check for specific values, like which number a single die rolls, or which value a coin toss returns?
The modulo operator of course!
You can of course change the number 2
to a 6 for dice, or to 100 for a random percentage. You can read about it in Chainlink’s VRF best practices.
The contract is now finished. It can be polished a lot more, we can return a string “heads” or “tails” instead of a number, but that’s something you can try yourself.
Running the contract in Remix
Open Remix and create a new file CoinToss.sol
in which you can copy-paste the contract. Simply paste it and compile it, it shouldn’t return any errors, although it might return a warning that you can ignore.
Go to the deploy tab and deploy the contract while using injected Web3
. Make sure you’re connected with MetaMask and that you’re using the correct network. Rinkeby in my case.
You will get a MetaMask popup to deploy the test contract, just accept it and wait for the tx to complete.
Funding the contract
We now need to fund the contract with LINK that you have on your testnet wallet. Copy the contract’s address first.
Open MetaMask and send some LINK to the contract. 1 LINK is more than enough. When you have funded the contract you can use Remix to call the getRandomNumber
function, and after that you can get the randomResult
variable to check the result. You will have to send a tx for the function call as well.
You should now get a random number. You can call getRandomNumber
again to get a new number.
Common pitfalls
If you’re having trouble with any of the steps, be sure to:
- Fund your contract with LINK.
- Check the addresses, fee, and key hash in the contract. They should match the ones given for your chosen network.
- Check that you’re testing on the right network.
If you don’t have any more issues, you can now learn to build even more amazing dApps with Chainlink’s verifiable random function.
Conclusion
For decentralized games, Chainlink’s VRF is a critical part. You cannot have a game without randomness (in most cases), and true randomness on Ethereum doesn’t exist. At least not without Chainlink.
You have now learned the very basics of using VRF in a smart contract. And I’m certain you will use this info to build amazing dApps.
Thank you so much for reading and have an excellent day.
Support me by supporting Medium and becoming a member. It helps me out a lot, it won’t cost you extra, and you can read as many Medium articles as you like!
Follow me on Twitter and gm.xyz to keep up with me.
Check out the project I’m the dev of here.
Check out my latest NFT collection on Polygon.