Skip to content

Commit 251b503

Browse files
fix(docs): remove security issue in random number example
1 parent d7581dc commit 251b503

2 files changed

Lines changed: 5 additions & 20 deletions

File tree

examples/developer-hub-solidity/RandomNumberV2Lottery.sol

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity >=0.7.6 <0.9.0;
33

4+
import {ContractRegistry} from "@flarenetwork/flare-periphery-contracts/coston2/ContractRegistry.sol";
45
import {RandomNumberV2Interface} from "@flarenetwork/flare-periphery-contracts/coston2/RandomNumberV2Interface.sol";
56

67
/**
@@ -12,8 +13,6 @@ import {RandomNumberV2Interface} from "@flarenetwork/flare-periphery-contracts/c
1213
* @notice A lottery contract that utilizes a secure random number for determining winners.
1314
*/
1415
contract LotteryWithRandomNumber {
15-
RandomNumberV2Interface internal randomNumberGenerator;
16-
1716
address[] public participants;
1817
uint256 public lotteryId;
1918
uint256 public lotteryEndTimestamp;
@@ -26,14 +25,6 @@ contract LotteryWithRandomNumber {
2625
uint256 timestamp
2726
);
2827

29-
/**
30-
* @notice Initializes the contract with the address of the random number generator.
31-
* @param _randomNumberGenerator The address of the RandomNumberV2Interface contract.
32-
*/
33-
constructor(address _randomNumberGenerator) {
34-
randomNumberGenerator = RandomNumberV2Interface(_randomNumberGenerator);
35-
}
36-
3728
/**
3829
* @notice Enter the lottery.
3930
* Participants can enter the lottery before it ends.
@@ -69,6 +60,8 @@ contract LotteryWithRandomNumber {
6960
require(participants.length > 0, "No participants in the lottery");
7061

7162
// Get the current random number and its properties
63+
RandomNumberV2Interface randomNumberGenerator = ContractRegistry
64+
.getRandomNumberV2();
7265
(
7366
uint256 randomNumber,
7467
bool isSecureRandom,

examples/developer-hub-solidity/SecureRandomConsumer.sol

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ import {RandomNumberV2Interface} from "@flarenetwork/flare-periphery-contracts/c
1010
* @dev THIS IS AN EXAMPLE CONTRACT. DO NOT USE THIS EXACT CODE IN PRODUCTION.
1111
*/
1212
contract SecureRandomConsumer {
13-
/// @notice Random number provider (fetched from ContractRegistry at construction).
14-
RandomNumberV2Interface public immutable randomV2;
15-
16-
/**
17-
* @notice Initialize and grab the RandomNumberV2 instance from the ContractRegistry.
18-
*/
19-
constructor() {
20-
randomV2 = ContractRegistry.getRandomNumberV2();
21-
}
22-
2313
/**
2414
* @notice Returns the latest secure random number.
2515
* @dev The underlying provider returns `(uint256 random, bool isSecure, uint256 timestamp)`.
@@ -31,6 +21,7 @@ contract SecureRandomConsumer {
3121
view
3222
returns (uint256 randomNumber, bool isSecure, uint256 timestamp)
3323
{
24+
RandomNumberV2Interface randomV2 = ContractRegistry.getRandomNumberV2();
3425
(uint256 _randomNumber, bool _isSecure, uint256 _timestamp) = randomV2
3526
.getRandomNumber();
3627

@@ -51,6 +42,7 @@ contract SecureRandomConsumer {
5142
view
5243
returns (uint256 randomNumber, bool isSecure, uint256 timestamp)
5344
{
45+
RandomNumberV2Interface randomV2 = ContractRegistry.getRandomNumberV2();
5446
(randomNumber, isSecure, timestamp) = randomV2.getRandomNumber();
5547
}
5648
}

0 commit comments

Comments
 (0)