Generating randomness with Chainlink VRF (v1) on Polygon costs 0.01 dollar with 30 seconds.
Chainlink provides Verifiable Randomness Function (VRF), which allows generating uncompromisable random values on the blockchain. Currently it's deployed on Ethereum, BSC (v2) and Polygon (v1 only).
In this experiemnt, I test Chainlink VRF on Polygon to figure out how much it costs and how long does it take.
- Contract Used for the Test - Source code below
- Supposing MATIC = $1.4, LINK = $13.29
- Request Random (View Txn):
- Gas: 0.005266463762570673 MATIC ($0.0073, 151827 gas)
- Fee: 0.0001 LINK ($0.001329)
- Total: $0.008629 (10원)
- Random Response (View Txn):
- Elapsed Time: 28 sec (12 blocks)
Note that official LINK token (
0xb0897686c545045aFc77CF20eC7A532E3120E0F1
) should be used instead of LINK from Polygon Bridge, which is more familiar. You can use Chainlink PegSwap to convert to the official token.
// SPDX-License-Identifier: MIT
pragma solidity 0.6.6;
import "@chainlink/contracts/src/v0.6/VRFConsumerBase.sol";
contract RandomNumber is VRFConsumerBase {
bytes32 internal keyHash;
uint256 internal fee;
uint256 public randomResult;
event RandomNumberGenerated(bytes32 indexed requestId, uint256 randomness);
/** @see https://docs.chain.link/docs/vrf-contracts/v1/ */
constructor()
VRFConsumerBase(
0x3d2341ADb2D31f1c5530cDC622016af293177AE0, // VRF Coordinator
0xb0897686c545045aFc77CF20eC7A532E3120E0F1 // LINK Token
) public
{
keyHash = 0xf86195cf7690c55907b2b611ebb7343a6f649bff128701cc542f0569e2c549da;
fee = 0.0001 * 10 ** 18; // 0.0001 LINK
}
/** Requests randomness */
function getRandomNumber() public returns (bytes32 requestId) {
require(LINK.balanceOf(address(this)) > fee, "Not enough LINK - fill contract with faucet");
return requestRandomness(keyHash, fee);
}
/** Callback function used by VRF Coordinator */
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
randomResult = randomness;
emit RandomNumberGenerated(requestId, randomness);
}
}