Practise projects after Ethereum Smart Contract Programming 101

It was a great course, and I really wanted to write my own smart contract for the first time, and this is what i could produce.

My first idea was to create a smart contract that will pay my kid 0.5 ether, when he gets good grades. This project idea came from Ivan, in the course ethereum 101. Here is the code. The Ownable.sol file is the same as discussed by @filip in the course.

pragma solidity 0.5.12;

import "./Ownable.sol";

contract rewardKidFroGoodGrdaes is Ownable{
    address payable Receiver = 0xdd445B306473b01DF0aA50090DB618718C4b202F;
    uint public Grades;
    uint public balance;
    
    bool pay = false;
    string  errorMessage = "Your grades are low";
    string  successMessage = "You are awarded";
    
    event throwSuccessMessgae(address sender, string successMessage);
    event throwErrorMessgae(string errorMessage);
    
    function setGrades()private {
        if (Grades >= 8){
            pay = true;
        }
    }
    
    
    modifier costs(uint cost){
        require(msg.value >= cost);
        _;
    }
    
    function getGrades(uint grades)public onlyOwner{
        Grades = grades;
    }
    
    function rewardKiddo()public onlyOwner payable costs(0.1 ether){
        setGrades();
        require(pay == true);
        if (Receiver.send(msg.value)){
            balance += msg.value;
            emit throwSuccessMessgae(owner, successMessage);
        }
        else{
            emit throwErrorMessgae(errorMessage);
        }
    }
}

Second project is, suppose we organise a competition. There is an open window for competetors to take part. The cose is 1 wei. Based on their grades, the owner will choose a winner, and reward him as much he wish. And others can view the winner through a public struct variable. But only the winner can choose the winner. Here is the code.

pragma solidity 0.5.12;
import "./Ownable.sol";

contract Competition is Ownable{
    
    struct Competitor{
        address CompetitorAddress;
        string name;
        uint grade;
    }
    
    uint public totalCompetitors;
    Competitor public Winner;
    bool public CompetitionOver = false;
    
    // mapping (address => Competitor) private Competitors;
    Competitor[] Competitors;
    modifier costs(uint cost){
        require (msg.value >= cost);
        _;
    }
    
    function createCompetitor(string memory name, uint grade)public payable costs(1 wei){
        Competitor memory newCompetitor;
        newCompetitor.CompetitorAddress = msg.sender;
        newCompetitor.name = name;
        newCompetitor.grade = grade;
        
        insertCompetitor(newCompetitor);
    }
    
    function insertCompetitor(Competitor memory newCompetitor)private{
        Competitors.push(newCompetitor);
        totalCompetitors += 1;
    }
    
    function PredictWinner()public onlyOwner payable{
        // uint[] CompetitorsList;
        
        uint j;
        for(uint i = 0; i<totalCompetitors; i++){
            if (Competitors[j].grade <Competitors[i].grade){
                j = i;
            }
        }
        Winner = Competitors[j];
        payWinner(Winner.CompetitorAddress);
        
    }
    
    function payWinner(address CompetitorAddress)private onlyOwner{
        address(uint160(CompetitorAddress)).transfer(msg.value);
        CompetitionOver = true;
    }
    
}
2 Likes

I can’t wait to get home and check these out!

1 Like