SecretVRF stands out in the competitive landscape of verifiable random functions (VRFs) by offering a significant cost and speed advantage over its principal competitors. Notably, it provides approximately a 4 times cost benefit, which is a massive saving for projects that rely on random number generation at scale. This economic efficiency is largely due to its optimized gas usage, which minimizes the on-chain transaction cost.
In addition to its cost benefits, SecretVRF is also twice as fast as its closest competitor. This speed improvement is critical for applications that require almost real-time randomness, such as gaming, lotteries, and various DeFi protocols.
To demonstrate, we have this small video here outlining all of the advantages of SecretVRF vs. its biggest competitor:
An example contract call using SecretVRF on Ethereum Sepolia which requests 20 random words is compared for its gas usage. Here, we use the example contracts provided in the documentation of Chainlink. In both cases, response time for Secret VRF is at around 1-2 blocks, so around 10s.
94k Gas with Secretpath: Etherscan Sepolia
300k+ Gas for Chainlink VRF with their example project: Etherscan Sepolia
55k gas with Secretpath: Etherscan Sepolia
200k+ gas for Chainlink VRF with their example project: Etherscan Sepolia
In total, we also do not need to pay in special LINK tokens and instead can just pay in the native gas token of the chain (here: Sepolia ETH), which saves you additional costs.
To summarize, SecretVRF's combination of cost efficiency, speed, and EVM chain compatibility makes it a compelling choice for developers and projects seeking reliable and economical verifiable random functions. Its technical innovations position it as a leader in the space, offering tangible benefits that can significantly enhance the performance and cost-effectiveness of a wide range of blockchain applications.
Got improvements or suggestions on how to convert your ChainlinkVRF contract to SecretVRF ? Please ask in the Secret Network or Discord.
Converting from Chainlink VRF to Secret VRF is easier than you expect. Within four easy steps you can free your contract from bloat and use the lightweight and faster Secret VRF solution.
We start off with the example code from Chainlink from :
In the first step, we remove the imports and the inheritance of the VRFConsumerBaseV2 and add our SecretVRF interface from this. There is no
to get to this:
Here, the behavior of the callbackGasLimit
is different than in ChainlinkVRF. The callback Gas limit is simply all of the gas that you need to pay in order to make callback, which includes the verification of the result as well. The callback gas is the amount of gas that you have to pay for the message coming on the way back. We recommend at least using 100_000+
in Callback gas to ensure that enough gas is available. In case you did not pay enough gas, the contract callback execution will fail.
Next, we can also simplify the constructor since we do not need to define any extra variables or subscriptionIds. Going from this:
to this
Next, we need to slightly adjust the behavior of the function rollDice(address roller)
function and how it calls the Request Randomness function within Secret VRF. Here, we need to use the Secret VRF gateway and call it directly instead.
Make sure to now mark this function as payable
!
Please make sure to actually prepay the right amount of callback gas directly as a value transfer into the contract. The callback gas is the amount of gas that you have to pay for the message coming on the way back. If you do pay less than the amount specified below, your Gateway TX will fail:
Lastly, we'll add a small check to ensure that we actually got an incoming call from the SecretVRF gateway contract. For this, remove the internal
and override
flags on the function and add the require:
That's all that you need to convert your contract from ChainlinkVRF to SecretVRF.
Since this check is dependent on the current block.basefee
of the block it is included in, it is recommended that you estimate the gas fee beforehand and add some extra overhead to it. An example of how this can be implemented in your frontend can be found in this and here:
Learn how to use SecretPath on EVM to access on-chain verifiable random numbers.
SecretVRF over SecretPath enables EVM developers to access on-chain verifiable random numbers at a fraction of the cost and block time of traditional RNG oracles such as ChainlinkVRF. With fewer than 100 lines of code, you will have access to an infinite supply of randomness.
See a fullstack cross-chain SecretVRF demo here
To learn how SecretVRF works underneath the hood, refer to the docs here. 🤓
To get started, clone the repo:
Install the node dependencies:
Update the env
file with your EVM wallet private key and Infura API key.
Make sure your Infura API key is configured for Amoy testnet 😎
Compile your Solidity smart contract:
Once the contract is compiled successfully, upload the contract to Polygon testnet:
Note the contract address:
Add the RandomnessReceiver
contract address to your env
file:
Now that you've uploaded your contract, it's time to set the SecretPath gateway address for Polygon Amoy and then request on-chain verifiable random numbers!
Gateways are the on-chain smart contracts that handle the broadcasting, receipt, packaging, and verification of messages.
First, set the gateway address for Polygon Amoy testnet. You can do this by executing set_gateway.js
:
This tutorial is for Polygon testnet, but you can find a list of additional EVM gateway contract addresses here.
Next, create an event listener so you can listen to when the random numbers that you request have been fulfilled.
Create the event listener by executing fulfill_randomness_event.js
:
Now it's time to request random numbers! Currently, request_random.js
is configured to request 3 random numbers, but you can update how many numbers you would like to request here (up to 2000 for this example).
Once you have configured how many random numbers you want to request, execute request_random.js
:
Upon successful execution, your terminal will log the following:
Navigate to your event listener terminal to see the returned random numbers:
Congrats! You've just used SecretPath to request your first verifiable on-chain random numbers! 🎉
Secret VRF offers an innovative and cost-effective solution for EVM developers seeking access to verifiable random numbers. By following this guide, you've successfully set up your environment, deployed the RandomnessReceiver.sol
contract, and interacted with the SecretPath network to request and receive random numbers. Dive into the world of decentralized randomness with SecretPath, where security meets simplicity. 🌟
Need help with using encrypted payloads with Secretpath or want to discuss use cases for your dApp? Please ask in the Secret Network Telegram or Discord.
First, install all of the the dependencies via NPM:
Next, import the following into your code:
In your vite.config.ts
in the project, you need to add the support for bigInt
into the esbuildOptions:
To start, we first define all of our variables that we need for the encryption, as well as the gateway information:
First, we define the Gateway address that is specific to each chain, which can you can look up here Supported Networks.
Second, you need to input the private contract that you are going to call, in our case the Secret VRF RNG contact on Secret Network. The code for this example contract can be found here in case you want to deploy it yourself.
Next, init the Ethereum client that you are using to call the contract with. Here, we init the chainId to use the Ethereum sepolia testnet and use ethers.js to retrieve the address.
Next, you generate ephermal keys and load in the public encryption key for the Secret Gateway that you can look up in Supported Networks. Then, use ECDH to create the encryption key:
Next, you define all of the information that you need for calling the private contract on Secret + add the callback information for the message on its way back.
We begin by defining the function that we are going to call on the private secret contract, here it's request_random
. Next, we add the parameters/calldata for this function, which is ("{ numWords: Number(numWords) }"
and convert it into a JSON string.
Next, we define the callback Information. In this case, we are using the gateway contract as an example callback. Here, you would typically put in your own custom callback address and callback selector in.
After defining the contract call and callback, we now construct the payload:
Next, we encrypt the payload using ChaCha20-Poly1305. Then, we hash the encrypted payload into a ciphertextHash
using Keccak256.
Next, we use Metamask to sign the ciphertextHash
using personal_sign
. Then, we recover the user_pubkey
from this signed message, which will be also passed into the Public Gateway.
Internally, Metamask takes the ciphertextHash
, preprends the "\x19Ethereum Signed Message:\n32"
string and then hashes it using Keccak256, which results the payloadHash
. Metamask actually signs the payloadHash
to get the signature. Keep this in mind when verifying the signature against the payloadHash
and NOT the ciphertextHash
.
The callback gas is the amount of gas that you have to pay for the message coming on the way back. If you do pay less than the amount specified below, your Gateway TX will fail:
Since this check is dependent on the current block.basefee
of the block it is included in, it is recommended that you estimate the gas fee beforehand and add some extra overhead to it. An example of how this can be implemented in your frontend can be found in this example and here:
Lastly, we pack all the information we collected during previous steps into an info
struct that we send into the Gateway contract. We the encode the function data. Finally, we set the tx_params. Please make sure to set an approiate gas amount for your contract call, here we used 150k gas. For the value of the TX, we send over the estimated callback gas that we calculated above.
Got improvements or suggestions on how to improve SecretVRF or this tutorial ? Please ask in the Secret Network Telegram or Discord.
For a detailed demonstration, you can watch our video tutorial available here:
After we've gone through an extensive example on how our example contract works, here's how to implement SecretVRF via SecretPath in your own contract in 4 easy steps:
First, import the ISecretVRF
interface into your Solidity Contract:
Second, set your gateway address to the Secret VRF Gateways. You only need to make sure that your contract knows the correct SecretVRF Gateway address, for example:
Now, we implement the function that calls the SecretVRF Gateway on EVM. Note that you have to pay an extra amount of your gas token as CallbackGas:
The callback gas is the amount of gas that you have to pay for the message coming on the way back. If you do pay less than the amount specified below, your Gateway TX will fail:
Since this check is dependent on the current block.basefee
of the block it is included in, it is recommended that you estimate the gas fee beforehand and add some extra overhead to it. An example of how this can be implemented in your frontend can be found in this example and here:
From here, the SecretVRF Gateway will take care of everything, just wait 1-2 blocks for Gateway to provide you the random number by getting it from the Secret Networks on chain VRF and do the callback.
The SecretVRF gateway contract will always call the contract that called the VRF contract (using msg.sender
) with the function selector bytes 0x38ba4614
, which is the function:
Now, the SecretVRF Gateway contract will verify the validity of the call and when all checks pass, it will call this function. In this case, we just emit a log as an example to finish our demo. Emitting a log is not obligatory and optional.