You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// SPDX-License-Identifier: Apache-2.0pragma solidity^0.8.0;
import { IEntropyConsumer } from"@diceprotocol/sdk/solidity/IEntropyConsumer.sol";
import { IEntropy } from"@diceprotocol/sdk/solidity/IEntropy.sol";
contractMyGameisIEntropyConsumer {
IEntropy publicimmutable dice;
addresspublic provider;
mapping(uint64=>bytes32) public results;
mapping(uint64=>address) public requesters;
constructor(address_dice, address_provider) {
dice =IEntropy(_dice);
provider = _provider;
}
function roll() externalpayable {
// Generate user random contributionbytes32 userRandom =keccak256(abi.encodePacked(
msg.sender,
block.timestamp,
block.prevrandao,
gasleft()
));
// Request randomness - must send exact feeuint64 seqNum = dice.requestV2{value: msg.value}(
provider,
userRandom,
100000// callback gas limit
);
requesters[seqNum] =msg.sender;
}
function entropyCallback(
uint64sequenceNumber,
address,
bytes32randomNumber
) internaloverride {
results[sequenceNumber] = randomNumber;
// Use the random number in your game logic
}
function getEntropy() internalviewoverridereturns (address) {
returnaddress(dice);
}
}
3. Request Randomness (TypeScript)
import{DiceProtocol}from'@diceprotocol/sdk';import{ethers}from'ethers';constprovider=newethers.JsonRpcProvider('https://rpc.mainnet.chain.robinhood.com');constsigner=newethers.Wallet(process.env.PRIVATE_KEY!,provider);constdice=newDiceProtocol({rpcUrl: 'https://rpc.mainnet.chain.robinhood.com',contractAddress: '0xd8a0680e7699526b57140ed4eafdcc7219dc0a0c',});// Get current feeconstfee=awaitdice.getFee('0x8741b8a825644D9Ef18Faf2DAB5e9b47B900F2b6');console.log('Fee:',ethers.formatEther(fee),'ETH');// Request randomnessconstuserRandom=ethers.randomBytes(32);constseqNum=awaitdice.requestRandom(signer,'0x8741b8a825644D9Ef18Faf2DAB5e9b47B900F2b6',ethers.hexlify(userRandom),200000);console.log('Request sequence:',seqNum);// Tyche auto-reveals within ~20 seconds// Your contract's entropyCallback() fires automatically
Admin withdraws accumulated fees to the vault via withdrawFees()
The keeper is funded separately for gas costs
Gas Considerations
Operation
Gas
Notes
requestV2()
~125,000
Paid by requester
revealWithCallback()
~200,000
Paid by keeper
Callback execution
Configurable
Effective limit is the larger of the requested gas limit and the provider default, subject to MAX_GAS_LIMIT
The live provider default is 200,000 gas. A request may specify a larger callback gas limit up to MAX_GAS_LIMIT; the contract rounds it up to the nearest 10,000 gas.
Error Reference
Error
Cause
InsufficientFee
Sent ETH does not equal the required fee
NoSuchProvider
Provider not registered
NoSuchRequest
Sequence number doesn't exist
IncorrectRevelation
Provider reveal doesn't match commitment
OutOfRandomness
Hash chain exhausted
MaxGasLimitExceeded
Requested gas limit > MAX_GAS_LIMIT
Unauthorized
Caller lacks permission
Comparison
Feature
Dice Protocol
Other RNG
Deployment
Native to Robinhood Chain
External
Fee model
Flat 0.000025 ETH
Variable
Reveal time
~20 seconds
1-5 minutes
Verifiability
On-chain Keccak256
Varies
Callback
Automatic
Manual polling
Infrastructure
Self-hosted keeper
Third-party
Refunds (v10)
If a request is not revealed within about 60–90 seconds, the original requester can reclaim the exact fee: