Day 9: Lottery Project

[UD2:49 – UD2:65]

Today I spent about six hours of studying the UD2 course (Module 46 through Module 65).

I probably would have a record day in terms of the amount of hours in one day studying if it weren’t for a storm that knocked the power out.

Anyway, today the instructor broke down the lines of code in two smart contracts – a lottery smart contract and an auction smart contract.

Below is the lottery smart contract.

You’ll see that in some of the comments that are bold notes (NOTE #1, NOTE #2, etc.). Those are key points where I would like to expand more about what I learned below the smart contract instead of putting it in the code.)

//SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;
contract Lottery{
    // declaring the state variables 
    address payable[] public players; // NOTE #1 - dynamic array of type address payable
    address public manager; 
    // declaring the constructor
        // initializing the owner to the address that deploys the contract
        manager = msg.sender; 
    // declaring the receive() function that is necessary to receive ETH
    receive () payable external{
        // each player sends exactly 0.1 ETH 
        require(msg.value == 0.1 ether);
        // appending the player to the players array
    // returning the contract's balance in wei
    function getBalance() public view returns(uint){
        // only the manager is allowed to call it
        require(msg.sender == manager);
        return address(this).balance;
    // helper function that returns a big random integer
    function random() internal view returns(uint){
       return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players.length)));
    // selecting the winner
    function pickWinner() public{
        // only the manager can pick a winner if there are at least 3 players in the lottery
        require(msg.sender == manager);
        require (players.length >= 3);
        uint r = random();
        address payable winner;
        // computing a random index of the array
        uint index = r % players.length;
        winner = players[index]; // this is the winner
        // transferring the entire contract's balance to the winner
        // resetting the lottery for the next round
        players = new address payable[](0);

As you can see, the code has a lot of descriptive comments. I’ll provide

Key Takeaways:

>> address payable[] public players; // NOTE #1 – dynamic array of type address payable. This allows lottery players to have their addresses payable in the event that they win.

>> There are two types of addresses. (1) Payable – ETH can be sent from the smart contract to the address. (2) Non-payable – ETH can not be sent.

(( I actually took lots of screen shot notes. At some point, I might post them in the post. ))

I feel great because I’m at a point where I can understand 95% or more of the code.

I now realize that it’s a good idea when you’re just starting out to just ‘get the gist’ of what’s going on and trust that everything will become more clear as you move on with the lessons.

Another thing: I’ve started to focus more on UD2. At first I was going back and forth between UD1 and UD2, but with UD2, I like Andrei’s teaching style a little bit better. Mainly because he explains things better. He breaks down the code.

Even though, he can be a little hard to understand at times, I’m enjoying his course a lot.

Also, we went over part of an auction smart contract today. I’ll post below what was covered.

//SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;
contract Auction{
    address payable public owner;
    uint public startBlock;
    uint public endBlock;
    string public ipfsHash;
    enum State {Started, Running, Ended, Canceled}
    State public auctionState;
    uint public highestBindingBid;
    address payable public highestBidder;
    mapping(address => uint) public bids;
    uint bidIncrement;


NOTE: this is just the first 21 lines of code of a smart contract that is 139 lines.

Leave a Reply