SharedConsumer: An Out-of-the-Box Randomness Consumer Contract for Lottery and Gaming Demonstration
https://github.com/ARPA-Network/Randcast-User-Contract
Overview
SharedConsumer is a comprehensive, out-of-the-box smart contract built on top of Randcast that provides ready-to-use randomness solutions for various lottery and gaming scenarios. It serves as both a practical tool for communities seeking immediate randomness functionality and a reference implementation for developers looking to integrate Randcast into their projects.
The contract has been deployed across multiple major blockchain networks as part of the Randcast Playground, demonstrating its reliability and versatility in real-world applications.
Key Features
🎯 Three Built-in Play Types
SharedConsumer supports three distinct randomness use cases:
Draw Tickets - Fair lottery ticket drawing from a pool
Roll Dice - Multiple dice rolls with customizable dice sizes
Gacha - Weighted random selection with rarity tiers (perfect for NFT drops, loot boxes, and card games)
💰 Free Response Callbacks
One of the standout features of SharedConsumer is that response callbacks from trial subscription are completely free. Users only pay for the initial randomness request, making it cost-effective for lottery and gaming demonstrations.
📚 Developer Reference
As a demonstration project, SharedConsumer showcases best practices for:
Integrating with Randcast Adapter
Implementing randomness callbacks
Managing subscriptions and fees
Handling different randomness request types
Gas optimization strategies
Deployment Status
The SharedConsumer contract has been deployed as part of the Randcast Playground on multiple major blockchain networks, including:
Ethereum Mainnet(https://etherscan.io/address/0x8acfc64bF976488E9B83c517D4185Fd4D8A9D683)
BSC Mainnet(https://bscscan.com/address/0x9a599D28907780289bB980ddFA38A17B1176FC29)
Base Mainnet(https://basescan.org/address/0xC9519853F9E9576303dB70a054d320aCA82005Ad)
Optimism Mainnet(https://optimistic.etherscan.io/address/0x9B9b0ea8b7a565dB81D3C78129626f077D47f7B9)
Taiko Mainnet(https://taikoscan.io/address/0x9A6E06aa83eBF588c136dD7991d9002DF1E181CF)
Ethereum Hoodi Testnet(https://hoodi.etherscan.io/address/0x1D2c2d06e6d923B2B88B1CDDb0955d418fae48a8)
And more...
The Playground serves as:
Live Deployment: Real-world testing and usage by communities across multiple networks
Reference Implementation: Code examples and integration patterns
Cost Demonstration: Showcasing the efficiency of Randcast's subscription model
Integration Guide
For Communities (Quick Start)
Connect to Playground: Open the Playground website at https://www.arpanetwork.io/play
Select a Network: Choose the network you want to use from the dropdown menu
Read the Rules: Read the rules(
helpcommand) for the play type and the parameters you want to useSend the Request: In a simulated cmd window, enter the command
cast/draw/gachato send the requestWait for the result: The result will be displayed in the console
Pay Only for Requests: Response callbacks are free!
For Developers (Custom Integration)
Study the Contract: Review
SharedConsumer.solas a reference implementation
Understand Patterns: Learn how to:
Integrate with
BasicRandcastConsumerBaseImplement
_fulfillRandomWordsand_fulfillRandomnesscallbacksCalculate gas limits dynamically
Manage subscriptions
Adapt to Your Needs: Customize the contract for your specific use case
Deploy Your Own: Use the patterns learned to build your own consumer contract
Contract Architecture
Inheritance Structure
RequestIdBase: Provides request ID generation utilities
BasicRandcastConsumerBase: Base contract for Randcast integration
UUPSUpgradeable: Allows contract upgrades via UUPS proxy pattern
OwnableUpgradeable: Provides access control for administrative functions
Core Components
Play Types
Subscription Management
The contract:
Supports trial subscription for demo purposes
Allows users to use their own subscriptions
Allows users to cancel their subscriptions
Functionality Details
1. Draw Tickets (drawTickets)
drawTickets)Fairly selects winners from a pool of tickets.
Use Cases:
Community giveaways
Airdrop distribution
Contest winner selection
Token distribution lotteries
Parameters:
totalNumber: Total number of tickets in the pool (max 1000)winnerNumber: Number of winners to selectsubId: Subscription ID (0 for auto-creation)seed: Random seed for requestrequestConfirmations: Number of block confirmations (0 for default)message: Optional message (often merkle root of ticket list)
Example:
Events:
DrawTicketsRequest: Emitted when request is madeDrawTicketsResult: Emitted with winner ticket numbers
2. Roll Dice (rollDice)
rollDice)Performs multiple dice rolls with customizable dice sizes.
Use Cases:
Gaming mechanics
Random number generation
Decision making tools
Multi-outcome randomness
Parameters:
bunch: Number of dice rolls (max 100)size: Number of sides on each diesubId: Subscription ID (0 for auto-creation)seed: Random seed for requestrequestConfirmations: Number of block confirmations (0 for default)message: Optional message
Example:
Events:
RollDiceRequest: Emitted when request is madeRollDiceResult: Emitted with dice roll results (1-indexed)
3. Gacha (gacha)
gacha)Performs weighted random selection with rarity tiers and upper limits.
Use Cases:
NFT drops with rarity tiers
Loot box mechanics
Card pack opening
Item rarity distribution
Parameters:
count: Number of items to draw (max 100)weights: Array of weights for each rarity tierupperLimits: Array of upper limits for each tier (index range)subId: Subscription ID (0 for auto-creation)seed: Random seed for requestrequestConfirmations: Number of block confirmations (0 for default)message: Optional message
Example:
Events:
GachaRequest: Emitted when request is madeGachaResult: Emitted with weight results (tier indices) and index results (item IDs)
Result Interpretation:
weightResults: Array indicating which rarity tier was selected for each drawindexResults: Array indicating the specific item ID within the selected tier
Events Reference
Request Events
DrawTicketsRequest: Lottery ticket drawing requestRollDiceRequest: Dice rolling requestGachaRequest: Gacha/loot box request
Result Events
DrawTicketsResult: Winner ticket numbersRollDiceResult: Dice roll resultsGachaResult: Weight and index results
All events include requestId for easy tracking and correlation.
Limitations
Draw Tickets: Maximum 1000 tickets, winner count must be ≤ total tickets
Roll Dice: Maximum 100 rolls per request
Gacha: Maximum 100 items per draw, weights and upperLimits arrays must match length
Disclaimer
Important: While SharedConsumer is designed to be functional and has been tested in various scenarios, users are strongly advised to conduct their own research (DYOR) before deploying or using this contract in production environments.
This contract is provided as-is without warranties of any kind
Users should review the code, understand the risks, and assess suitability for their specific use case
The contract authors and maintainers are not responsible for any losses or damages resulting from the use of this contract
Always audit smart contracts before deploying to mainnet with significant value
Test thoroughly in testnet environments before production use
Conclusion
SharedConsumer represents an out-of-the-box solution for randomness needs in Web3 applications. Whether you're a community looking for immediate lottery functionality or a developer seeking integration patterns, SharedConsumer provides a robust foundation built on Randcast's decentralized randomness infrastructure.
The contract's deployment across multiple networks in the Randcast Playground demonstrates its reliability and versatility, while its free response callbacks from trial subscription make it economically viable for lottery and gaming demonstrations.
Remember: Always conduct your own research and security audits before deploying to production environments with significant value.
For more information, code examples, and deployment addresses, visit the Randcast Documentation and Randcast Playground.
Last updated