Gelato
  • Introduction
    • Gelato, The Web3 Cloud Platform
  • Smart Wallets
    • Introduction
      • Understanding EIP-7702
      • Understanding ERC-4337
      • ERC-4337 vs EIP-7702
    • Templates & Examples
    • How-To Guides
      • Create a Sponsor API Key
      • Sponsor gas for your users
      • Allow users to pay gas with erc20
      • Allow users to pay gas with native
      • Create Dynamic's Environment Id
      • Use Dynamic/Privy signers with React SDK
      • Estimate Gas for your transactions
    • React SDK
    • Demo
    • Supported Networks
  • Rollup As A Service
    • Introduction
    • Rollup Stacks
      • Arbitrum Orbit
        • Run a Full Orbit Node
      • OP Stack
        • Run OP Node
    • Deploy your Rollup
    • Customization
      • Data Availability
        • Celestia
        • Avail
        • Eigen DA
      • Custom Gas Token
      • Marketplace
        • Gelato Services
        • Data Indexers
        • Block Explorers
        • Oracles
        • Bridges
        • Account Abstraction
        • On & Off-ramp
        • Community
        • Identity & KYC
        • Others
      • Verifier Node Package
    • Public Testnet
  • RPC Nodes
    • Introduction
    • Compute Units
    • Using RPC Nodes
    • Supported Networks
    • Pricing and Plans
    • FAQ
  • Web3 Services
    • Web3 Functions
      • Understanding Web3 Functions
        • Trigger Types
        • Typescript Function
        • Solidity Function
        • Automated Transactions
      • Security Considerations
      • Template & Use Cases
      • Quick Start
        • Writing Typescript Functions
          • Event Trigger
          • Private Typescript Functions
          • Callbacks
        • Test, Deploy & Run Typescript functions
        • Writing Solidity Functions
        • Test, Deploy & Run Solidity Functions
        • Initiate an Automated Transaction
      • Create a Web3 Function Task
        • Using the UI
        • Using the Safe App
        • Using a Smart Contract
        • Using the Automate SDK
      • Analytics & Monitoring
      • Supported Networks
      • Subscription & Payments
      • Legacy Automate Migration Guide
    • Relay
      • What is Relaying?
      • Security Considerations
        • ERC-2771 Delegatecall Vulnerability
      • Templates
      • Quick Start
        • Sponsored Calls
        • Non-Sponsored Calls
      • ERC-2771 (recommended)
        • SponsoredCallERC2771
        • CallWithSyncFeeERC2771
          • Relay Context Contracts ERC2771
      • Non-ERC-2771
        • SponsoredCall
        • CallWithSyncFee
          • Relay Context Contracts
      • Relay API
      • Gelato's Fee Oracle
      • Tracking your Relay Request
      • Supported Networks
      • Subscriptions and Payments
        • 1Balance & Relay
        • SyncFee Payment Tokens
        • Relay Pricing
      • ERC2771 Migration Guide
    • VRF
      • Understanding VRF
      • How does Gelato VRF Work?
      • Security Considerations
      • Template
      • Quick Start
      • Create a VRF Task
        • Create a Fallback VRF
        • Migrating from Chainlink VRF
      • Supported Networks
      • Pricing & Rate Limits
    • Oracles
      • Understanding Gelato Oracles
      • Quick Start
      • Data Providers
        • Stork
        • Choas Labs
      • Migrating from Chainlink Oracles
      • Available Price Feeds
      • Supported Networks
      • Pricing & Rate Limits
    • Account Abstraction
      • Understanding ERC-4337
      • Introduction to Gelato Bundler
      • Templates & Examples
      • Quick Start
      • Supported Networks
      • Bundler API Endpoints
        • eth_sendUserOperation
        • eth_estimateUserOperationGas
        • eth_getUserOperationByHash
        • eth_getUserOperationReceipt
        • eth_supportedEntryPoints
        • eth_maxPriorityFeePerGas
        • eth_chainId
    • 1Balance
      • 1Balance Alerts
      • Subscription Plans
      • Subscription Notifications
      • USDC Addresses
    • AI Agents
    • Teams
  • GELATO DAO
    • DAO & Token (GEL)
    • GEL Token Contracts
    • Governance Process
  • Social Media
Powered by GitBook
On this page
  • Step 1: Set Up Your Development Environment
  • Step 2: Install the Gelato VRF Contracts
  • Step 3: Inherit GelatoVRFConsumerBase Contract
  • Step 4: Request Randomness
  • Step 5: Implement the Callback function
  • Step 6: Pass dedicated msg.sender
  1. Web3 Services
  2. VRF

Quick Start

PreviousTemplateNextCreate a VRF Task

Last updated 11 months ago

Watch Now: Learn more by watching our video, , now availble on YouTube

In order to get your VRF up and running you will need to first make your contract VRF Compatible.

Step 1: Set Up Your Development Environment

Ensure you have either or set up in your development environment.

Step 2: Install the Gelato VRF Contracts

Depending on your environment, use the following commands to import the Gelato VRF contracts:

  • For Hardhat users:

    • Clone the repo

    • Install dependencies yarn install

    • Fill in .env with variables in .env.example

  • For Foundry users: forge install gelatodigital/vrf-contracts --no-commit

Step 3: Inherit Contract

The recommended approach to integrate Gelato VRF is by inheriting from the GelatoVRFConsumerBase smart contract. Here's a simple example to help you set it up:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

import {GelatoVRFConsumerBase} from "./GelatoVRFConsumerBase.sol";

contract YourContract is GelatoVRFConsumerBase {
    // Your contract's code goes here
}

Understanding 1Balance

Note: It's important to remember that the current 1Balance system does not support withdrawals after depositing funds. Ensure to deposit only the amount you plan to utilize for Gelato VRF operations.

Step 4: Request Randomness

To request randomness, call the _requestRandomness() function. You should protect the call since it will take from your 1Balance. The data argument will be passed back to you by the W3F.

    function requestRandomness(bytes memory data) external {
        require(msg.sender == ...);
        uint64 requestId = _requestRandomness(data);
    }

Step 5: Implement the Callback function

Finally, implement the callback function.

    function _fulfillRandomness(
        bytes32 randomness,
        uint64 requestId,
        bytes memory data,
    ) internal override {
    }
}

Best Practices for VRF Fulfillment Handlers

When integrating VRF (Verifiable Random Function) into your smart contracts, it is crucial to manage gas usage effectively to ensure reliable execution. Here are two key practices to follow:

  1. Monitor Gas Limits: Always ensure that the gas usage of your VRF fulfillment handler does not exceed the maximum block gas limit. If the handler requires more gas than the block limit allows, the transaction will revert, leading to failed executions.

  2. Cap Dynamic Actions: If your VRF usage involves a dynamic number of actions—where the actions could increase the gas used by the fulfillment transaction—it is advisable to set a cap on the number of actions. This will prevent the transaction from becoming too gas-intensive and ensure it remains below the block gas limit, guaranteeing successful execution.

Step 6: Pass dedicated msg.sender

When you're ready to deploy your Gelato VRF-compatible contract, an important step is to include the dedicated msg.sender as a constructor parameter. This ensures that your contract is set up to work with the correct operator for fulfilling the randomness requests.. It's crucial for ensuring that only authorized requests are processed.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

import {GelatoVRFConsumerBase} from "./GelatoVRFConsumerBase.sol";

contract YourContract is GelatoVRFConsumerBase {
    constructor(address operator)
        GelatoVRFConsumerBase(operator) {
        // Additional initializations
    }

    // The rest of your contract code
}

Before we dive into requesting randomness, it's crucial to understand the role of 1Balance in using Gelato VRF. The Gelato VRF services necessitate that your Gelato balance is sufficiently funded. This balance caters to Gas fees and rewards Gelato Nodes for their computational tasks. For details about costs and funding your account, do visit our .

Before deploying, visit the Gelato app . You will find the specific dedicated msg.sender address assigned for your deployer address. This address is crucial for the security and proper functioning of your VRF requests. Learn more about it at

and once you have your contract ready & deployed, grab the address and .

Gelato VRF Walkthrough
Foundry
Hardhat
here
GelatoVRFConsumerBase
1balance documentation
Deploy your VRF instance
here
Dedicated msg.sender