POA
English
English
  • Welcome to POA
  • Features
    • Known Validators
    • POADAO Consensus
    • Bridged Native Token
    • On-Chain Randomness
  • Use Cases
    • Scalability for BlockChain Games
    • Community-Based Currencies
    • Subsidized Transactions
    • Decentralized Finance (DeFi)
  • DApp Spotlights
    • Ethernal.World
  • Roadmap
  • For users
    • POA Tokens
      • POA Merger & STAKE Swap
      • POA - STAKE FAQs
      • POA Token Supply
      • POA & POA20 Exchanges
      • POA & POA20 on Binance
      • FAQ: POA20 General Questions
    • POA Token Use Cases
      • Utility Token
      • Currency Token
      • Collateral Token
      • Bridged Token
      • Staking Token
      • Stable Token
    • POA to POA20 Bridge
    • Accept POA20 Payments
      • Account Registration & Login (Merchant Setup)
      • Setup Merchant Account
      • Merchant Payment Method Setup
      • Making a Payment with POA20 (Customer Perspective)
    • Tutorials
      • Trading POA20 on DEX.AG
      • POA20 Token Swaps on 1inch.exchange
      • Getting Airdrops via Discord
    • Governance
      • Article: A Successful Year of On-Chain Governance
      • Governance Reports
        • April 2020 Report
        • December 2019 Report
      • Ballot Type, Lifecycle & Limits
    • ❌Nifty Wallet (Discontinued)
      • Getting Started
      • Import and Interact with Smart Contracts
      • Connect to a Hardware Wallet (Ledger & Trezor)
      • Connect to D'CENT Biometric Wallet
      • Connect to Pocket Decentralized Network
    • 3rd Party Wallets
      • Trust Wallet
      • MetaMask
    • POA Mania
      • News - Updates
      • Rules
      • POA Mania FAQs
      • Deposit & Withdraw
      • Winner Selection
      • Round Details
      • POA Mania vs PoolTogether
      • POA Mania on Trust Wallet
      • POA Mania Security Audit
      • POA Mania on MetaMask Mobile
    • Whitepaper
      • POADAO v1
        • Introduction
        • Proof of Authority
        • POA Network Functionality
        • Decentralized apps (DApps)
          • Initial ceremony DApp
          • Proof of Physical Address (PoPA) DApp
          • Proof of Bank Account DApp
          • Proof of Social Network DApp
          • Proof of Phone Number DApp
          • Governance DApp
        • Summary & Acknowledgements
        • References
        • Appendix A: Code Samples
          • Ballots manager
          • Validators manager
          • Deployment scripts for the mining node
  • For developers
    • Developer Resources
    • Getting Tokens For Tests
      • ERC20 Test Token Faucet
      • Sokol Testnet Faucet
    • Full Node Setup
      • Install OpenEthereum Client
      • Install Nethermind Client
    • DApp Deployment
    • TheGraph Data Indexing
    • On-Chain Random Numbers
      • RNG explainer (AuRa + RandomAura Contract)
      • Accessing a Random Seed with a Smart Contract
      • Randomness FAQs
    • API & SQL Access
    • Smart Contract Dashboard
    • Grants for Building on POA
  • For validators
    • Getting Started
      • Validator Resources
      • Becoming a Validator
    • Bootnode Setup
      • AWS Bootnode Setup
        • Prerequisites
        • Configure AWS
        • Download and Configure Playbook
        • Deploy
      • Non-AWS Bootnode Setup & Deployment
        • Local/Remote Machine System Requirements
        • Node Preparation
        • Configure node with Deployment Playbook
    • Validator Node Setup
      • Nethermind Upgrade
      • AWS VM for Validator Node Deployment
        • MoC: Master of Ceremony Key Exchange & Generation
        • Current Validators Vote in New Validators
        • Validator Node Setup Prerequisites
        • Configuring AWS
        • Download and Configure Playbook
        • Deployment
        • Upgrade Instance to a Larger Instance Type
      • Non-AWS Validator Node Setup
        • Local & Remote Machine System Requirements
        • Remote Machine Setup
        • Configure Node using Deployment Playbook
      • NetStats Dashboard
    • Hard Forks
      • Parity Upgrade Guide
      • POA Core
        • 2021-11-02 | #24090200
        • 2021-05-24 | #21364900
        • 2020-03-31 | #14350721
        • 2019-12-19 | #12598600
        • 2019-04-29 | #8582254
        • 2018-01-29 | # 772000
        • 2018-10-22 | #5329160
        • POA Core spec.json hard-fork update
      • Sokol
        • 2021-05-24 | #21050600
        • 2020-02-20 | #13391641
        • 2019-12-05 | #12095200
        • 2019-02-06 | #7026400
        • 2019-01-04 | #6464300
        • 2018-09-19 | #4622420
        • 2018-01-18 | #509355
        • 2018-01-08 | #362296
        • Sokol spec.json hard-fork update
    • Validator DApps
      • Validators MetaData DApp
      • Adding or Removing a Validator
  • Media
    • Social Media
    • Media Kit
    • Contact Us
Powered by GitBook
On this page

Was this helpful?

  1. For developers
  2. On-Chain Random Numbers

Accessing a Random Seed with a Smart Contract

PreviousRNG explainer (AuRa + RandomAura Contract)NextRandomness FAQs

Last updated 5 years ago

Was this helpful?

The 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 phase

  • nextCommitPhaseStartBlock : 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.

pragma solidity 0.5.16;


interface IPOSDAORandom {
    function collectRoundLength() external view returns(uint256);
    function currentSeed() external view returns(uint256);
}

contract Example {
    IPOSDAORandom private _posdaoRandomContract; // address of RandomAuRa contract
    uint256 private _seed;
    uint256 private _seedLastBlock;
    uint256 private _updateInterval;

    constructor(IPOSDAORandom _randomContract) public {
        require(_randomContract != IPOSDAORandom(0));
        _posdaoRandomContract = _randomContract;
        _seed = _randomContract.currentSeed();
        _seedLastBlock = block.number;
        _updateInterval = _randomContract.collectRoundLength();
        require(_updateInterval != 0);
    }

    function useSeed() public {
        if (_wasSeedUpdated()) {
            // using updated _seed ...
        } else {
            // using _seed ...
        }
    }

    function _wasSeedUpdated() private returns(bool) {
        if (block.number - _seedLastBlock <= _updateInterval) {
            return false;
        }

        _updateInterval = _posdaoRandomContract.collectRoundLength();

        uint256 remoteSeed = _posdaoRandomContract.currentSeed();
        if (remoteSeed != _seed) {
            _seed = remoteSeed;
            _seedLastBlock = block.number;
            return true;
        }
        return false;
    }
}
RandomAura