Secret Network offers verifiable on-chain randomness for the entire Cosmos Ecosystem via IBC.
We have two methods available, depending on whether your chain supports IBC-hooks or not.
In case your chain supports IBC hooks, please take a look here: Secret VRF for IBC with IBC-Hooks
In case your chain does not support IBC hooks, please take a look here:SecretVRF for IBC via proxy contracts
Need assistance using SecretVRF for your dApp with IBC ? Please ask in the Secret Network Telegram or Discord.
Verifiable on-chain random number generator for the entire Cosmos.
This VRF tutorial uses IBC Hooks, as introduced in IBC v3.4.0. If your chain is on a previous IBC version, see the SecretVRF tutorial using proxy contracts here.
Secret VRF is a provably fair and verifiable on-chain random number generator (RNG) that enables smart contracts to access random values without compromising security or usability. Coupled with cross-chain interoperable smart contracts using IBC hooks, Secret Network enables developers and projects across the Cosmos access to state-of-the-art on-chain RNG.
Use Secret VRF to build reliable smart contracts for any application that relies on unpredictable outcomes:
NFT Minting: Utilize randomness for features like unordered minting, trait randomization, and identity numbering, enhancing the authenticity and security of NFT collections.
Web3 Gaming: Apply randomness in gambling, damage calculation, loot boxes, and boss drops to build trust among players by ensuring fairness and preventing any player from having an unfair advantage.
DAO Operations: Employ randomness for wallet initialization, task assigning, unordered voting/liquidations, and order book ordering, facilitating equitable and secure decentralized governance and operations.
Learn more about how SecretVRF works in-depth here.
To use SecretVRF on any IBC-enabled chain with IBC hooks, all that is required is:
An IBC transfer channel between Secret Network and the consumer chain that receives randomness.
Uploading the RNG Consumer Contract to your chain and directing it to your chain's transfer channel
You can look up existing transfer channels between Secret Network on a block explorer such as Ping here.
Git clone
the IBC hooks randomness repository:
Update the SECRET_TRANSFER_CHANNEL_ID
and the CHAIN_TRANSFER_CHANNEL_ID
to the channel-id for your IBC-enabled chain:
For this example, we request randomness on Juno, but you can request randomness on any IBC-compatible chain that has a transfer channel established with Secret Network.
Once you have updated the transfer channels, compile the contract:
Upload the compiled contract:
Upon successful upload, a code_id
is returned:
Instantiate the contract with the returned code_id
:
Upon succesful instantiation, a contract_address
is returned:
Now that you've instantiated your randomness contract, all that's left is to request randomness! Simply execute request_random:
A transaction hash will be returned:
You can update the job_id
string to any string of your choosing
Navigate to the recently received transactions for your contract:
And then view the magic of cross-chain randomness with IBC hooks 😍:
Congrats! You've just sent a verifiable on-chain random byte
with SecretVRF 🙌.
Secret VRF revolutionizes blockchain applications by providing a secure and verifiable source of randomness, critical for fairness in NFT minting, gaming, and DAO operations. Its seamless integration with IBC hooks enables cross-chain interoperability, allowing developers across the Cosmos ecosystem to build more reliable and elegant smart contracts.
If you have any questions, join our discord and a Secret developer will assist you ASAP.
An example of how to use Secret VRF to fetch random numbers via IBC between Secret and Juno testnet
The design system we’ll be using consists of one Secret contract and two Juno contracts:
Secret Proxy Contract: A Secret contract that requests a random number and specifies the callback information for the response.
Juno Proxy Contract: A Juno contract that sends an IBC message to fetch the random number from the Secret Network proxy contract.
Juno Consumer Contract: A Juno contract that consumes the random numbers generated by the Secret proxy contract.
See here for a diagram of the system architecture:
There are two steps to configuring your environment:
Upload and instantiate the three smart contracts
then cd
into secret-ibc-rng-template/node:
Run npm i
to install the dependencies:
Then, create a .env
file and add your wallet mnemonic:
Compile the contract:
Open a new terminal window and cd
into secret-ibc-rng-template/proxy:
Run the Makefile compile script:
Then upload + instantiate the contract in secret-ibc-rng-template/node:
Now that we have our Secret proxy contract, let's upload and instantiate the two Juno smart contracts.
You can configure the consumer contracts for any IBC-compatible chain of your choosing. However, for this demo will be uploading and instantiating our contracts on Juno.
Compile Juno Proxy Contract
Open a new terminal window and cd
into secret-ibc-rng-template/consumer-side-proxy:
Run the Makefile compile script:
Compile Juno Consumer Contract
Open a new terminal window and cd
into secret-ibc-rng-template/consumer:
Run the Makefile compile script:
Upload Juno Contracts
You should see the transaction result
returned upon successful upload.
To upload the contracts to Juno testnet, you need Juno testnet tokens in your wallet. Visit faucet.reece.sh/uni-6/<your Juno address> to receive testnet tokens
Next, instantiate the Juno proxy contract by running the following in your terminal:
Then, to query that the instantiation was successful and find the contract address, query the returned txHash
with:
You should see the contract_address
variable:
Now, simply repeat the process for the Juno Consumer contract. The only difference is that the instantiation message is slightly modified because it needs to include the other Juno contract_address
that you instantiated as a pointer:
Query the returned txHash:
You should see the contract_address
variable:
Now that you have successfully uploaded and instantiated the three smart contracts, let's configure Hermes Relayer to relay packets between Secret test and Juno testnet.
Then, configure Hermes by navigating to the folder .hermes
and opening the config.toml
file.
If you're on a Mac, you may need to press Command + Shift + Period
to see hidden files, such as the .hermes
folder.
To relay packets between Secret Network testnet and Juno testnet, update the config.toml
file with this configuration:
If you are using Hermes Relayer, make sure your paths are set up correctly in both the config.toml and gm.toml
. See the image below for reference:
Now let's relay packets! 🎉
Start Gaiad Manager
Add your Secret and Juno testnet wallets to Hermes:
cd
into examples/secret-ibc-rng-template:
and update the a.mnemonic
file to include your testnet wallet address like so:
Then run the following to add your testnet wallet address to Hermes:
Create clients
Create connections
In place of 07-tendermint-235 and 07-tendermint-468
, use the client IDs returned to you in your terminal.
Upon success, you should see a message like so:
Now that a channel is established, let's create a channel identifier, which links the Juno proxy contract to the Secret proxy contract. Note that the ports listed below are the addresses of the Juno and Secret proxy contracts which we instantiated earlier.
Create channel identifier
After successfully creating a channel identifier, we can relay packets! Let's start Hermes and then execute the Juno consumer contract to send a random network from Secret Network to Juno 🤯
Start Hermes (open a new terminal window and then run the following)
Hermes will scan the chain for all clients, connections and channels. This might take some time, which is normal. If you want to specify which channels it scans, update the hermes config file to include the following at the end of the chain configuration:
After Hermes has started running, execute the Juno consumer contract to return a random number from Secret via IBC:
Then, query the smart contract to see if it returned the random number:
Upon successful execution, a random number will be returned:
Congrats! You now have the tools to implement cross-chain random number generation via IBC. By following these steps, you can facilitate and execute smart contracts and relay packets between Juno and Secret, as well as any other IBC-compatible chain, expanding the possibilities for blockchain interoperability and fostering new avenues for decentralized application development.
If you are brand new to IBC, here is a quick crash course!
To connect two CosmWasm contracts over IBC you must establish an IBC channel between them. The IBC channel establishment process uses a four way handshake. Here is a summary of the steps:
OpenInit
Hello chain B, here is information that you can use to verify I am chain A. Do you have information I can use?
OpenTry
Hello chain A, I have verified that you are who you say you are. Here is my verification information.
OpenAck
Hello chain B. Thank you for that information I have verified you are who you say you are. I am now ready to talk.
OpenConfirm
Hello chain A. I am also now ready to talk.
Once the handshake has been completed a channel will be established that the ibc messages may be sent over. In order to do a handshake and receive IBC messages your contract must implement the following entry points (which are implemented in our proxy contract):
ibc_channel_open
- Handles the OpenInit
and OpenTry
handshake steps.
ibc_channel_connect
- Handles the OpenAck
and OpenConfirm
handshake steps.
ibc_channel_close
- Handles the closing of an IBC channel by the counterparty.
ibc_packet_receive
- Handles receiving IBC packets from the counter-party.
ibc_packet_ack
- Handles ACK messages from the counter-party.
ibc_packet_timeout
- Handles packet timeouts.
This documentation serves as a demo on how to send cross-chain random numbers from Secret Network testnet to Juno testnet via IBC. The demo repository .
The Secret proxy contract will produce a different, non-predictable number for each request it receives. You can read more about random numbers on Secret Network in the
Configure an IBC relayer to properly relay packets between your chains of choice (for this demo we will use ).
To begin, let's upload and instantiate the Secret Network proxy smart contract with Secret.js. :
Next, upload the compiled wasm
files to Juno testnet using , and be sure to note the respective codeIds:
If using the CLI, update to junod v16.0. If using js/ts, update .
First, .
Next, configure Gaiad Manager by navigating to the folder .gm
, and then update the gm.toml file with the
Now it's time to execute our IBC smart contracts and relay packets between Juno testnet and Secret testnet. If you run into any issues at this step, refer to the for guidance, and also ask questions in the !
If you run into errors at this step, see the official Hermes docs for adding keys .
to learn more about IBC fundamentals