Skip to main content

Use onchain randomness

· One min read
Josh Crites

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

Randomness
/celo-codebase/protocol/identity/randomness

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();
}
}