Skip to content

Latest commit

 

History

History
49 lines (39 loc) · 1.56 KB

2022-01-07-on-chain-randomness.md

File metadata and controls

49 lines (39 loc) · 1.56 KB
title description slug authors tags image hide_table_of_contents
Use onchain randomness
How to use onchain randomness in your smart contracts.
/developer-guide/start/randomness
name title url image_url
Josh Crites
Developer Relations, cLabs
solidity
randomness
oracle
false

import PageRef from '@components/PageRef';

Onchain randomness is used for selecting validators to perform phone number verification. Read more about how onchain randomness is produced at the provided page.

This randomness can be used by any smart contracts deployed to a Celo network.

import "celo-monorepo/packages/protocol/identity/interfaces/IRandom.sol";
import "celo-monorepo/packages/protocol/common/interfaces/IRegistry.sol";

contract Example {
    function test() external view returns (bytes32 randomness) {
        randomness = IRandom(
            IRegistry(0x000000000000000000000000000000000000ce10)
                .getAddressFor(keccak256(abi.encodePacked("Random")))
        ).random();
    }
}

Alternatively, through inheritance of UsingRegistry.

import "celo-monorepo/packages/protocol/common/UsingRegistryV2.sol";

contract Example is UsingRegistryV2 {
    function test() external view returns (bytes32 randomness) {
        randomness = getRandom().random();
    }
}