Skip to content

Several VRF requests at once

When a contract has more than one VRF request open, store each request's context under the ID that the request call returns, and in the callback look up that ID, finish that request and delete it. Never keep "the" pending request in a single variable, and never let an outcome depend on which seed arrives first: nothing on chain makes fulfillments arrive in request order. The contract below lets any number of players keep any number of rolls open at once on Ronin, Base or Robinhood with the coordinator 0xC66eB2e7EE91145875000Ad46B55600000000001, and its tests deliver the seeds in reverse and shuffled order. Further down: the ID each request interface returns, why ten dice need one request rather than ten, and the order in which Randoo's oracle has actually fulfilled.

How contracts with several open requests break

  • One slot for "the" request. A contract that saves lastRequestId or lastPlayer when it requests, and reads it in the callback, works in a demo with one player. With two requests open, the second overwrites the first. In our test of that pattern, Alice rolls, then Bob rolls, both seeds arrive, and Alice never gets a result while Bob's is written twice.
  • One shared result. A latestSeed or lastResult that a later transaction reads gives that transaction whichever seed landed last, which may belong to someone else's request.
  • Assuming the order. Requests A, B and C can be fulfilled C, A, B. Chainlink's VRF v2.5 security page says so and recommends matching each fulfillment to its request by requestId. On Randoo, fulfill(requestId, proof) accepts any open request, and which oracle is in turn is computed per request from its own ID (contracts/src/RandooVRFCoordinator.sol), so no request waits for an older one.
  • An outcome that depends on the order. If the callback hands out the next token ID, the next prize in a list, or a jackpot to "the first roll over 90", then whoever orders the fulfillments picks the winner: the oracle, or the block builder when two fulfillments share a block. Fix everything that order would decide (the player's slot, the token ID, the prize) when the request is made, and let the seed choose only within it.
  • Player state read in the callback. If the outcome reads something another open roll can change, such as an inventory or a balance, either copy it into the request's record when the player asks, or allow one open request per player. When nothing is shared, as below, no per-player lock is needed.
  • msg.sender in the callback. The callback is called by the coordinator, so msg.sender there is the coordinator, never the player. Store the player with the request.

D20DAO's SDK README gives the same rule as rule 4 of its consumer checklist: "Map each request id to your own context, and reject anything else" (@d20dao/vrf-sdk 0.6.2).

The request ID each interface returns

Randoo serves three request interfaces at the same address. Key your mapping by what your interface returns:

InterfaceThe request returnsYour callback getsKey the mapping by
Ronin VRF, requestRandomSeedbytes32 reqHashrawFulfillRandomSeed(bytes32 reqHash, uint256 seed)reqHash
Chainlink VRF v2.5 wrapper, requestRandomWordsInNativeuint256 requestIdrawFulfillRandomWords(uint256 requestId, uint256[] randomWords)requestId
D20DAO, requestRandomnessuint256 requestIdrawFulfillRandomness(uint256 requestId, bytes32 randomness)requestId

A Ronin-interface request has both: reqHash is keccak256(abi.encode(requestId, consumer, requestBlock)), and RandomSeedRequested emits both. The coordinator's requestId goes up by one for every request, so two requests from the same contract in the same block get different hashes. The VRF input includes the requestId too, so their seeds are independent of each other.

The contract

Put the two files from the quickstart at src/RandooConsumerRonin.sol and src/interfaces/IRoninVRFCoordinatorForConsumers.sol, then add this one and deploy it with 0xC66eB2e7EE91145875000Ad46B55600000000001. It needs no other library.

src/Rolls.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {RandooConsumerRonin} from "./RandooConsumerRonin.sol";
import {IRoninVRFCoordinatorForConsumers} from "./interfaces/IRoninVRFCoordinatorForConsumers.sol";

interface IRandooSeeds {
    function seedOf(bytes32 reqHash) external view returns (uint256 seed, bool fulfilled, bool callbackOk);
}

/// Any number of players, each with any number of rolls waiting at once. Each roll is stored
/// under its own request hash, so the seeds can arrive in any order and each finishes its own roll.
contract Rolls is RandooConsumerRonin {
    uint256 public constant CALLBACK_GAS = 60_000;
    uint256 public constant MAX_DICE = 10;

    struct Roll {
        address player;
        uint32 index; // the player's roll number, fixed when they asked
        uint8 dice;
        uint8 sides;
    }

    // Rolls waiting for their seed, by request hash.
    mapping(bytes32 => Roll) public pending;
    // How many rolls each player has asked for, which is also the next roll's number.
    mapping(address => uint32) public rollCount;
    // Each finished roll's total, by player and roll number. 0 means not rolled yet.
    mapping(address => mapping(uint256 => uint256)) public totalOf;

    event Asked(bytes32 indexed reqHash, address indexed player, uint256 index, uint256 dice, uint256 sides);
    event Rolled(bytes32 indexed reqHash, address indexed player, uint256 index, uint256 total);

    constructor(address coordinator) RandooConsumerRonin(coordinator) {}

    /// Rolls `dice` dice with `sides` sides each from one request. Send at least
    /// estimateRequestRandomFee(CALLBACK_GAS, gasPrice); the unused part goes back to the player.
    function roll(uint8 dice, uint8 sides) external payable returns (bytes32 reqHash) {
        require(dice >= 1 && dice <= MAX_DICE && sides >= 2, "dice");
        uint256 gasPrice = _declaredGasPrice();
        require(
            msg.value >= IRoninVRFCoordinatorForConsumers(vrfCoordinator).estimateRequestRandomFee(CALLBACK_GAS, gasPrice),
            "fee"
        );
        reqHash = _requestRandomness(msg.value, CALLBACK_GAS, gasPrice, msg.sender);
        uint32 index = rollCount[msg.sender]++;
        pending[reqHash] = Roll(msg.sender, index, dice, sides);
        emit Asked(reqHash, msg.sender, index, dice, sides);
    }

    /// Finishes a roll whose callback failed, from the seed the coordinator stored.
    function settle(bytes32 reqHash) external {
        (uint256 seed, bool fulfilled,) = IRandooSeeds(vrfCoordinator).seedOf(reqHash);
        require(fulfilled, "pending");
        _finish(reqHash, seed);
    }

    function _fulfillRandomSeed(bytes32 reqHash, uint256 seed) internal override {
        _finish(reqHash, seed);
    }

    function _finish(bytes32 reqHash, uint256 seed) private {
        Roll memory r = pending[reqHash];
        // A hash this contract never asked for, or a roll already finished.
        require(r.player != address(0), "unknown");
        delete pending[reqHash];
        uint256 total;
        for (uint256 i = 0; i < r.dice; i++) {
            total += uint256(keccak256(abi.encode(seed, i))) % r.sides + 1;
        }
        totalOf[r.player][r.index] = total;
        emit Rolled(reqHash, r.player, r.index, total);
    }

    /// Ronin and Saigon: 23 gwei. Base and Robinhood: the transaction's gas price.
    function _declaredGasPrice() private view returns (uint256) {
        if (block.chainid == 2020 || block.chainid == 202601) return 23 gwei;
        return tx.gasprice;
    }
}
  • Everything the outcome needs is stored when the player asks. pending[reqHash] holds the player, the dice and the player's roll number. The callback reads only that record and its own seed, so the order in which seeds arrive changes nothing.
  • The roll number is fixed at request time. A player's third roll is totalOf(player, 2) whether its seed arrives first or last, so a frontend can show each roll in the order the player made it.
  • Unknown and finished hashes are refused. _finish deletes the record before it writes the result, so a second delivery or a second settle for the same hash reverts with unknown. The coordinator never delivers a seed twice; the check costs one storage read and does not rely on that.
  • No per-player lock. Nothing one roll does changes another roll's inputs, so a player can have ten rolls open at once. Add a cap only if your outcome reads player state that an open roll could change (see above).
  • settle covers a failed callback. If the callback reverts or runs out of gas, the request still counts as fulfilled and the coordinator stores the seed. Anyone can then call settle(reqHash), which finishes the roll from seedOf(reqHash) (When a VRF callback reverts).
  • On the Chainlink wrapper or D20DAO interface the pattern is the same with a uint256 key: mapping(uint256 => Roll) pending, filled with the requestId the request returns and read in fulfillRandomWords or fulfillRandomness.

Callback gas

We searched for the smallest callback gas limit at which the coordinator's call still succeeds, with the contract's storage cold and each roll the first one of a new player (a new storage slot for the result), 20 rolls per row:

Dice in one rollWorst case, Solidity 0.8.38, via-IR, 200 runsWorst case, 0.8.30, no optimizer
132,50735,661
232,88036,667
534,00039,686
1035,86744,718

Each extra die adds about 370 gas with the optimizer and 1,000 without. CALLBACK_GAS = 60_000 covers ten dice with room to spare; the coordinator's minimum is 50,000.

Ten dice cost one request

One seed is 256 bits, so one request can roll any number of dice: die i is keccak256(abi.encode(seed, i)) % sides + 1, independent of the others. Ten separate requests cost ten service fees and wait for ten fulfillments. estimateRequestRandomFee(60000, gasPrice) on each mainnet on 2026-11-23:

NetworkBlockDeclared gas priceOne request, prepaidService fee in thatTen requests, service fees
Ronin Mainnet63,974,45223 gwei0.1558 RON (≈ $0.0055)0.1416 RON ($0.005)$0.05
Base61,136,5980.006 gwei0.00000537 ETH (≈ $0.0092)0.00000116 ETH ($0.002)$0.02
Robinhood121,230,5030.020 gwei0.0000344 ETH (≈ $0.059)0.0000204 ETH ($0.035)$0.35

The rest of each prepayment is gas for the fulfillment at the declared price with the coordinator's buffer, plus the L1 data fee on Base and Robinhood; the coordinator bills the gas actually used and refunds the rest to the player. Dollar amounts convert at the coordinator's own price feed, which sets each service fee from the list price on Pricing.

Use one request per decision, not one per value. Rolls that players make at different times are separate decisions and need separate requests, which is what the mapping is for. Values that belong to one decision, such as the dice of one throw, the cards of one deal or the drops of one chest, come from one seed. On the Chainlink wrapper interface, numWords can be up to 10 for the same price (requestRandomWordsInNative reference). Dice game with VRF shuffles a 52-card deck from one seed.

Does Randoo fulfill in request order?

So far, yes, but do not build on it. We read every request the Randoo subgraphs keep on 2026-11-23: the 200 most recent fulfilled requests on Ronin Mainnet (request IDs 92,305 to 92,504, made 2026-11-11 to 2026-11-19) and every request on Base (21) and Robinhood (11). None of the 232 was fulfilled in an earlier block than an older request, and each was fulfilled in its own transaction. On Ronin, 7 of them arrived while the same contract had another request open. On Ronin the median request was fulfilled 1 block later and the slowest 11 blocks later.

The order comes from how the oracle works today, not from the coordinator. The coordinator accepts any open request whose proof checks out from the oracle in turn for that request, and if an oracle misses its turn the next one in the set fulfills it. A missed turn on one request, a retry with more gas after NotEnoughGas, or a block builder reordering two fulfillments in one block can each put a newer request ahead of an older one. Chainlink's docs say the same of Chainlink VRF: validators control the order in which fulfillments appear on chain, so there is no guarantee that A, B, C come back as A, B, C.

Tests we ran

Rolls compiles with Solidity 0.8.30 and 0.8.38. Against the mock coordinator from the integration playbook, extended with seedOf and a fulfillment with a chosen gas limit, these 13 tests pass with both compilers:

  • Three players make two rolls each; the six seeds arrive in reverse order, and every total lands on the right player and roll number and matches the dice computed from that request's seed.
  • Five players make 40 rolls of 1 to 10 dice; the seeds arrive in a shuffled order, and all 40 totals match. Each player's rollCount is 8.
  • Two rolls by the same player in the same block get different request hashes and roll numbers 0 and 1.
  • A callback for a hash the contract never asked for reverts with unknown, and only the coordinator can call rawFulfillRandomSeed.
  • After a callback starved of gas, the roll has no total; settle finishes it from the stored seed once, and a second settle reverts with unknown. settle on a pending roll reverts with pending, and after a successful callback with unknown.
  • roll with 0 or 11 dice or with 1 side reverts with dice, and with one wei less than the quote with fee. On chain ID 2020 (Ronin) the contract declares 23 gwei even when the transaction's gas price is zero.
  • The one-slot pattern above (lastPlayer): with Alice's and Bob's requests open, Alice ends with no result and Bob's result is written twice.
  • Over 60,000 seeds, each face of a six-sided die came up between 9,760 and 10,141 times (expected 10,000). Ten six-sided dice over 5,000 seeds totalled between 16 and 54.
  • The callback gas table above.

Try it on a testnet

Roll a free seed on Base Sepolia in the playground without a wallet and check it on /verify. Then deploy Rolls there with the quickstart's forge create command, with src/Rolls.sol:Rolls in place of the template, and call roll(2, 6) a few times in a row with at least the quoted fee each time. Each Rolled event carries its request hash, which /verify checks. For outcomes with money attached, see Dice game with VRF, Raffle with VRF and Loot box with VRF.

Updated 2026-11-23.