Accessing a Random Seed with a Smart Contract
The RandomAura contract allows smart contracts to access a random number generated on-chain by the protocol.
Public getters:
currentSeed
: access the network's random seed.collectRoundLength
: length in blocks for each collection round (commit + reveal phases)isCommitPhase
: returns true if current block is in commit phasenextCommitPhaseStartBlock
: retrieve block number of first block in next commit phase.nextRevealPhaseStartBlock
: retrieve block number of first block in next reveal phase.
The public getter currentSeed
is used to access the network's random seed. Its value is only updated when the revealNumber
function is called. This should occur at least once per collection round. The length in blocks of each collection round can be retrieved with the collectRoundLength
public getter.
There are two phases in each round, a commit phase
and a reveal phase
. Since the revealing validator always knows the next random number before sending it, a DApp should prohibit business logic actions that depend on a random value during the reveal phase
.
For example, a gambling application that relies on a random value should only allow bets to be placed during the commit phase
. This type of application must prevent users from placing new bets during the entire reveal phase.
To determine the current phase, use the isCommitPhase
public getter: it returns true
if the current block is in the commit phase
and false
if the block is in the reveal phase
.
To retrieve the number of the first block of the next commit phase
, use the nextCommitPhaseStartBlock
public getter. To do the same for the reveal phase
, use the nextRevealPhaseStartBlock
public getter.
Example code to retrieve a random seed
Note: we are currently testing on Sokol and will provide a detailed example when the randomness contract is deployed.
Last updated