SimpleVRF

Examples

Examples

Quick Start: Minimal Integration

Here's a minimal example of how to request verifiable randomness from SimpleVrf in your Fuel contract:

use simplevrf_fuel_abi::{SimpleVrf, SimpleVrfCallback};
 
impl SimpleVrfCallback for Contract {
    #[storage(read, write)]
    fn simple_callback(seed: b256, proof: b256) {
        // Store or use the proof as needed
        storage.latest_proof.write(proof);
    }
}
 
fn request_random_number(seed: b256) -> u64 {
    let simple_vrf = abi(SimpleVrf, vrf_id);
    let fee = simple_vrf.get_fee(AssetId::base());
    let request_id = simple_vrf.request{
        asset_id: AssetId::base().bits(),
        coins: fee,
    }(seed);
    request_id
}
  • Implement the callback to receive the proof.
  • Call request_random_number(seed) to request randomness.

Full Example: Advanced Contract Integration

Below is a full example of a Fuel contract integrating with SimpleVrf for verifiable randomness, including storage and helper functions:

contract;
 
use simplevrf_fuel_abi::{SimpleVrf, SimpleVrfCallback};
use std::constants::{ZERO_B256};
use std::address::Address;
 
abi SampleVrfContract {
    #[storage(read, write)]
    fn simplevrf_callback(seed: b256, proof: b256);
 
    #[payable]
    #[storage(read, write)]
    fn request(seed: b256) -> u64;
 
    #[storage(read)]
    fn get_latest_proof() -> b256;
 
    #[storage(read)]
    fn get_seed() -> b256;
 
    #[storage(read, write)]
    fn set_vrf_id(vrf_id: ContractId);
}
 
storage {
    latest_proof: b256 = ZERO_B256,
    seed: b256 = ZERO_B256,
    vrf_id: b256 = ZERO_B256
}
 
impl SimpleVrfCallback for Contract {
    #[storage(read, write)]
    fn simple_callback(seed: b256, proof: b256) {
        storage.latest_proof.write(proof);
    }
}
 
impl SampleVrfContract for Contract {
    #[storage(read, write)]
    fn simplevrf_callback(seed: b256, proof: b256) {
        storage.latest_proof.write(proof);
    }
 
    #[payable]
    #[storage(read, write)]
    fn request(seed: b256) -> u64 {
        let vrf_id = storage.vrf_id.try_read().unwrap();
        storage.seed.write(seed);
        let simple_vrf = abi(SimpleVrf, vrf_id);
        let fee = simple_vrf.get_fee(AssetId::base());
        let request_id = simple_vrf.request{
            asset_id: AssetId::base().bits(),
            coins: fee, 
        }(seed);
        request_id
    }
 
    #[storage(read)]
    fn get_latest_proof() -> b256 {
        storage.latest_proof.try_read().unwrap()
    }
 
    #[storage(read)]
    fn get_seed() -> b256 {
        storage.seed.try_read().unwrap()
    }
 
    #[storage(read, write)]
    fn set_vrf_id(vrf_id: ContractId) {
        storage.vrf_id.write(vrf_id.bits());
    }
}

How it works

  • The contract implements a callback interface (SimpleVrfCallback) to receive the random proof.
  • The request(seed) function stores the seed, fetches the fee, and calls the SimpleVrf contract to request randomness.
  • The callback (simplevrf_callback or simple_callback) is triggered by SimpleVrf when the proof is ready, storing the result in contract storage.
  • Helper functions allow reading the latest proof and seed, and setting the VRF contract address.

You can use this pattern to integrate verifiable randomness into your own Fuel contracts. For more, see the SimpleVrf ABI documentation.

On this page