@@ -3,8 +3,17 @@ pragma solidity ^0.8.12;
3
3
4
4
import "@openzeppelin/contracts/utils/Strings.sol " ;
5
5
6
+ /**
7
+ * @title Lottery game
8
+ * @author Francisco Ramos
9
+ * @notice Simple web3 game based on Udemy course
10
+ * https://www.udemy.com/course/ethereum-and-solidity-the-complete-developers-guide/
11
+ */
6
12
contract Lottery {
13
+ // Stores the owner of the contract
7
14
address public owner;
15
+
16
+ // keeps track of the people who enter the game
8
17
address payable [] public players;
9
18
10
19
// We use it to emit some details about the pickWinner transaction
@@ -14,38 +23,61 @@ contract Lottery {
14
23
address winner
15
24
);
16
25
26
+ /**
27
+ * @dev Stores the address of the person deploying the contract
28
+ */
17
29
constructor () {
18
- // Address of the person deploying the contract
19
30
owner = msg .sender ;
20
31
}
21
32
22
- // Enforces a minimun amount of ether to be sent to a function
33
+ /**
34
+ * @dev Enforces a minimun amount of ether to be sent to a function
35
+ * @param value The minimum amount to send
36
+ */
23
37
modifier minimum (uint value ) {
24
38
string memory requiredMsg = string .concat ("The minimum value required is " , Strings.toString (value));
25
39
require (msg .value >= value, requiredMsg);
26
40
_;
27
41
}
28
42
29
- // Makes sure the owner is the only one who can call a function
43
+ /**
44
+ * @dev Makes sure the owner is the only one who can call a function
45
+ */
30
46
modifier restricted () {
31
47
require (msg .sender == owner, "Only the owner of this contract can call the function " );
32
48
_;
33
49
}
34
50
51
+ /**
52
+ * @dev Will be called by the player who enters de game sending ether
53
+ * and makes sure he/she is sending a minumun of 0.01 ether
54
+ */
35
55
function enter () public payable minimum (.01 ether) {
36
56
players.push (payable (msg .sender ));
37
57
}
38
58
59
+ /**
60
+ * @dev Generates a pseudo random number
61
+ * https://medium.com/0xcode/hashing-functions-in-solidity-using-keccak256-70779ea55bb0
62
+ * https://docs.soliditylang.org/en/v0.8.17/abi-spec.html
63
+ * @return index of the player within our list
64
+ */
39
65
function random () private view returns (uint ) {
40
- // See https://medium.com/0xcode/hashing-functions-in-solidity-using-keccak256-70779ea55bb0
41
- // See https://docs.soliditylang.org/en/v0.8.17/abi-spec.html
42
66
return uint (keccak256 (abi.encodePacked (block .difficulty , block .timestamp , players)));
43
67
}
44
68
69
+ /**
70
+ * @dev Gets the list of players currently in the game
71
+ * @return players
72
+ */
45
73
function getPlayers () public view returns (address payable [] memory ) {
46
74
return players;
47
75
}
48
76
77
+ /**
78
+ * @dev Called by the manager, it picks a winner
79
+ * emitting WinnerPicked event
80
+ */
49
81
function pickWinner () public restricted {
50
82
// Compute the (pseudo)random index of the winner
51
83
uint index = random () % players.length ;
0 commit comments