35
loading...
This website collects cookies to deliver better user experience
pragma solidity ^0.4.17;
contract Lottery {
}
contract
keyword to declare a Contract named Lottery, followed by the curly braces, where we'll write our Contract.manager
of datatype address
and players
of datatype address[]
. In Solidity, the address
datatype is used to store the wallet address.address public manager;
address[] public players;
Lottery()
function. As this function has the same name as the Contract name, it will eventually work as a constructor.function Lottery() public {
manager = msg.sender;
}
public
keyword after the function name which will make the function's visibility public and let anyone call the function.enter()
function.function enter() public payable {
require(msg.value > .01 ether);
players.push(msg.sender);
}
public
keyword, we have used the payable
keyword. It specifies that this function will require some amount of ethers to execute. So, whoever wants to enter our lottery, needs to pay a specified amount of ether, which also seems necessary, right? This ether will get stored as the Contract's ether in our Contract.enter()
function, we have written require(msg.value > .01 ether);
. Here, msg.value
denotes the amount of ether the user sends along with the function. It specifies that to enter our Lottery, the caller of the function must send ethers more than 0.01 ether and if the caller fulfills the specified condition, then only he is welcomed in the contract by pushing the caller's address to the players
array.enter()
function, comes the random()
function.function random() private view returns (uint) {
return uint(keccak256(block.difficulty, now, players));
}
random
method, thus we have to define our own.private
, view
, and returns
keywords following the function name.private
-> This keyword is the opposite of the public
keyword. By using the private
keyword, we are blocking public access to this function. As a result of which, this function can't be called from outside of the contract, even by the manager, and can only be called from inside of the Contract.
view
-> It will restrict the modification of the data of our Contract and will only allow reading our Contract data.
returns
-> It specifies that our function will return some data. Following it, we have written (uint)
which specifies that our function will return data of type uint
. (uint stands for unsigned integer).
random()
comes the pickWinner()
function, which will let the manager pick the winner of our Lottery.function pickWinner() public restricted {
uint index = random() % players.length;
players[index].transfer(this.balance);
players = new address[](0);
}
restricted
after the public
keyword.restricted
keyword specify? Well, we'll talk about this keyword after the upcoming function.pickWinner()
function, we are initializing the index
variable of type uint to store the array index of the winning player, which is obtained by the shown operation. This operation will return a random number from 0 to players.length() -1
.index
, we are transferring all the contract's ether to the address at the players[index]
. Followed by resetting the players
variable to an empty array, so that we can now accept entries for a new lottery.pickWinner()
, comes the restricted()
modifier.modifier restricted() {
require(msg.sender == manager);
_;
}
pickWinner()
function. By using modifier, all the conditions specified in the modifier will also be applicable for the function in which it is used.restricted()
modifier, we have specified the condition require(msg.sender == manager);
which specifies that sender must be the manager of the Contract._;
, which will execute all the function code after this.player()
function, which returns the players
array.function getPlayers() public view returns (address[]) {
return players;
}
players
array is also empty.