📌Random Number Contract
Introduction
The Random Number Oracle Contract is a decentralized smart contract designed to generate and provide random numbers securely. By utilizing an off-chain randomness provider, this contract allows users to request and retrieve random numbers in a transparent and tamper-proof manner.
How It Works
The user deploys and connects their Ethereum wallet to the contract.
The user calls
requestRandomness(minNumber, maxNumber)
, which:Generates a requestId.
Emits an event for an off-chain oracle to listen to.
The off-chain oracle listens for the event, generates a random number within the given range, and fulfils the request by calling
fulfillRandomness(requestId, randomNumber)
.The user can then retrieve their random number by calling
getRandomNumber(requestId)
.
Smart Contract Implementation
Contract Functions
1. Constructor
Initializes the contract and sets
requestIdCounter
to 0.
2. Request Random Number
Allows users to request a random number within a specified range.
Returns a unique
requestId
associated with the request.
3. Retrieve Random Number
Retrieves the random number for a given
requestId
.Only the request owner can access their random number.
4. Fulfill Randomness Request
Allows the contract owner (off-chain oracle) to provide the generated random number.
Marks the request as fulfilled.
Process Flow
User requests randomness: Calls
requestRandomness(minNumber, maxNumber)
, which returns arequestId
.Backend processes request: Listens for the
RandomnessRequested
event and generates a random number.Oracle fulfills request: Calls
fulfillRandomness(requestId, randomNumber)
to store the generated random number.User retrieves result: Calls
getRandomNumber(requestId)
to obtain the generated random number.
Last updated