How to Get a Random Number on Polygon

Polygon (formerly known as Matic Network) is a protocol and a framework for building and connecting Ethereum-compatible blockchain networks. Initially focusing on Ethereum scaling solutions that utilize Proof of Stake (PoS) sidechains and a Plasma framework, in rebranding to Polygon, they’ve also added other major scaling solutions such as zk-Rollups and Optimistic Rollups, as well as inter-chain communication protocols.

As the most widely adopted decentralized oracle network, Chainlink is the preferred oracle network within Polygon for securely obtaining external data and using tamper-proof data feeds. Recently, Chainlink VRF went live on Polygon mainnet, offering developers the ability to request verifiably random numbers in their smart contracts. In this technical tutorial, we’ll show you how to deploy a smart contract to Polygon and obtain a provably random number using Chainlink VRF.

The Need for Verifiable Randomness

Chainlink VRF is a provably fair and verifiable source of randomness designed for smart contracts. Solidity developers can use it as a tamper-proof random number generator to build safe and reliable smart contracts for Ethereum applications that rely on unpredictable outcomes. Critically, Chainlink VRF not only provides random numbers, but also cryptographic proof that the number has not been tampered with to generate certain predictable outcomes.

Having verifiably random numbers in smart contracts enables developers to build for a wide array of use cases, such as randomness in gaming, random rewards in NFTs, lotteries, and Fair Participant or Random Node Selection.

One noteworthy real-world example is the NFT gaming dApp Aavegotchi, which integrated Chainlink VRF as its source of provably fair randomness to help determine an Aavegotchi’s unique characteristics, generate unpredictable game scenarios, and randomly select DAO jurors. Aavegotchi has launched on Polygon’s layer 2 PoS chain, allowing them to cost-effectively scale to meet user demand thanks to near zero transaction fees and fast settlement times.

Now that we understand the use cases that benefit from verifiable randomness, we’ll walk through how to obtain a verifiable random number on a smart contract on the Polygon Mumbai Testnet.

Connecting to Polygon Mumbai Testnet

The first step in using Chainlink VRF on Polygon’s PoS chain is to set up your MetaMask wallet to connect to the Matic Mumbai Testnet. You can do this by selecting ‘Networks’, then choosing ‘Custom RPC’ and entering the following details:

Details on how to enter the Mumbai Testnet
Entering the Mumbai Testnet details

MATIC is the native token of the Polygon network and is needed to interact with the network, similar to how ETH is used as gas for Ethereum. To obtain Mumbai Testnet MATIC, head over to the MATIC Faucet, select the MATIC token and the Mumbai Testnet network, enter in your MetaMask Wallet address and press Submit.

Similar to the MATIC token, the LINK token is required as payment to the VRF coordinator for fulfilling a random number request. To obtain testnet LINK, simply choose the LINK token in the faucet instead of the MATIC token.

A picture showing how to obtain MATIC and LINK from the Mumbai testnet
Obtaining Mumbai Testnet MATIC and LINK

While the process for using the faucet to obtain LINK on the Mumbai Testnet is relatively simple, it’s a little more involved when moving to Polygon’s Mainnet. This is because the LINK provided by the Polygon Bridge is not ERC-677 compatible, so it cannot be used with Chainlink oracles. However, it can be converted to the official LINK token on Polygon using Chainlink’s PegSwap service, essentially creating a wrapped version of the LINK token.

Building the Smart Contract

The easiest way to start building a smart contract that uses Chainlink VRF on Polygon is to begin with the standard Chainlink VRFConsumer contract. This is a basic standardized contract for initiating requests for a random number via a Chainlink oracle. So we will open this contract in Remix via the link above and modify it accordingly.

  • The VRF Coordinator for Mumbai Testnet is 0x8C7382F9D8f56b33781fE506E897a4F1e2d17255
  • The address of the LINK token on the Mumbai Testnet is 0x326C977E6efc84E512bB9C30f76E30c160eD06FB
  • The KeyHash for Mumbai Testnet is 0x6e75b569a01ef56d18cab6a8e71e6600d6ce853834d4a5748b720d06f878b3a4
  • The fee for making a VRF request on the Mumbai Testnet is 100000000000000 (0.0001 LINK)
        ) public
        keyHash = 0x6e75b569a01ef56d18cab6a8e71e6600d6ce853834d4a5748b720d06f878b3a4;
        fee = 100000000000000; // 0.0001 LINK

Now our contract is ready to be compiled and deployed to the Mumbai Testnet.

Deploying and Testing the Smart Contract

Compile the contract in Remix, then on the deployment tab, change the environment to “Injected Web3”, and ensure the wallet address below is the one in your MetaMask wallet that contains your MATIC and LINK tokens, press the deploy button, and follow the steps. The end result is you have your smart contract deployed to the Mumbai Testnet. You should take note of the deployed contract address via the transaction output in the Remix console.

The next step is to fund the contract with LINK so it can send requests to the VRF Coordinator. To do this, simply take the deployed contract address from the step above, and then send it at least 0.0001 LINK  from your MetaMask wallet.

How to fund a contract with LINK.
Funding the contract with LINK

Once the contract is deployed and funded with LINK, we can request a random number by executing the `getRandomNumber` function, passing in a given seed as the parameter. This will send the request along with the seed to the VRF coordinator. Remember it’s extremely important to choose a seed that is difficult to influence or predict.

How to enter a seed and send a request to the VRF Coordinator
Entering a seed and sending a request to the VRF Coordinator

Once this transaction has been processed, we then wait a few seconds for the Chainlink oracle to fulfill the request for a random number, and then call the `fulfillRandomness` function we created earlier to return the random number back to our consumer contract.

We then call the `randomResult` getter function to see the result of the verifiable random number that was generated by the Chainlink oracle with the given seed. We now successfully have a random number in our smart contract deployed to the Polygon Mumbai Testnet that was verifiably created using the given seed.

An image showing the returned random number
Viewing the returned random number


Poygon’s PoS Chain offers a viable layer 2 solution with its fast, cheap, and reliable transactions, and with the addition of Chainlink VRF, developers can now build various scalable dApps that make use of verifiable random numbers.

If you’re a smart contract developer and want to take advantage of the Chainlink VRF feature, visit the Chainlink developer documentation and join the technical discussion on Discord.

More on This Topic

Website |  Twitter | Reddit | YouTube | Telegram | Events | GitHub | Price Feeds | DeFi

Need Integration Support?
Talk to an expert
Get testnet tokens
Read the Docs
Technical documentation