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