SMART-CONTRACT-UB

Smart Contract PowerPoint Slides


BARTS

The Blockchain ART Simulation (BARTS) is used to illustrate the interrelationship of blockchain mining, cryptocurrency, and hashing concepts. The hash algorithm SHA3 is used in cryptocurrency mining to make sure that there is no cheating, double spending, and to eliminate denial of service attacks. A miners job in the blockchain is to find a nonce value that when appended with the other data comes up with a SHA3 hash that has a predetermined number of leading zeros.

BARTS Simulation:

Step 1: Identify Artists and Gallery Owners

We will be asking for  volunteers to be artists and gallery owners. Don’t worry if you’re not a good artist. Each artist will be given a copy of the BARTS transaction paper along with a number 2 pencil. The BARTS transaction sheet is on the back of this sheet.

Step 2: The instructor will tell the artists to start drawing

Each artist will start drawing their artwork, and then the gallery owners will bid anywhere from 1 to 5 BARTS coins for each drawing. The first artist and gallery owner that agree on the amount will then fill out the BARTS transaction sheet and deliver to the instructor.

Step 3: Start the Mining Process

The instructor announces the information that needs to be mined. Each member of the class that is not an artist or gallery owner will use the BARTS Mining Program at https://tinyurl.com/BARTSMining to generate a hash with a leading zero. The individual that finds a hash with a leading zero yells out “EUREKA.”

Step 5: Instructor Adds Valid Transaction to the Ledger

The instructor asks the individual that yelled Eureka what nonce value generated the leading zero. If the transaction is validated by at least two other miner pools, the instructor will add the transaction to the blockchain at https://tinyurl.com/BARTSBlockchain.


Smart Contract Template

Navigate to: Remix (Click on the word “Remix” to go to link)


Hello World Smart Contract

pragma solidity ^0.5.7;
contract HelloWorld{
    function Hello() public view returns (string memory){ 
		return ("Hello World");
	}
}

Betting Smart Contract

pragma solidity 0.5.7;
pragma experimental ABIEncoderV2;

contract Bid{
   address owner = msg.sender;
  
  struct User{
    string Name;
    uint Balance;
  }
  
  struct Bet{
      string Team1;
      uint ScoreTeam1;
      string Team2;
      uint ScoreTeam2;
      bool Team1Won;
      uint BidAmount;
      string WantsTeam1ToWin;
      string WantsTeam2ToWin;
  }
  
   mapping(uint => User) users;
   uint index = 0;
   
   mapping(uint => Bet) bets;
   uint indexBids = 0;
   
    function RegisterUserToLedger(string memory name) public{
        require(bytes(name).length > 0);
            User storage user = users[index];
            user.Name = name;
            user.Balance=10;
        
        index = index+1;
    }
    
    function GetAllUsersFromLedger()public view returns(string[] memory, uint[] memory){
    
        string[] memory Names = new string[](index);

        uint[] memory Balance = new uint[](index);
    
       for(uint i=0; i<index; i++){
         User storage user = users[i];
         Names[i]=user.Name;
         
         Balance[i] = user.Balance;
         
        }
        return (Names, Balance);
    }
    
    
    function SuperBowlAddBet(string memory Team1, string memory Team2,  uint BidAmount, string memory WantsTeam1Win, string memory WantsTeam2Win) public payable{
            require(BidAmount < 10);
            Bet storage bet = bets[indexBids];
            bet.Team1 = Team1;
            bet.Team2 = Team2;
            
            bet.BidAmount = BidAmount;
            bet.WantsTeam1ToWin = WantsTeam1Win;
            bet.WantsTeam2ToWin = WantsTeam2Win;
        indexBids = indexBids+1;
    }
    
    function SuperBowlGameCompleted(uint IndexVal, bool Team1Won, uint Team1Score, uint Team2Score) public{
        require(msg.sender == owner);
        
        Bet storage bet = bets[IndexVal];
        bet.Team1Won = Team1Won;
        
        bet.ScoreTeam1 = Team1Score;
        bet.ScoreTeam2 = Team2Score;
        
        if(Team1Won == true){
            User storage user = users[getUserIndexValue(bet.WantsTeam1ToWin)];
            user.Balance = user.Balance+bet.BidAmount;
            user = users[getUserIndexValue(bet.WantsTeam2ToWin)];
            user.Balance = user.Balance-bet.BidAmount;
        }
        else{
          User storage user = users[getUserIndexValue(bet.WantsTeam2ToWin)];
            user.Balance = user.Balance+bet.BidAmount;
            user = users[getUserIndexValue(bet.WantsTeam1ToWin)];
            user.Balance = user.Balance-bet.BidAmount;  
        }
    }
    
    function GetAllBetsFromLedger() public view returns(string[] memory, uint[] memory, string[] memory, uint[] memory, bool[] memory, uint[] memory, bool[] memory){
        string[] memory Team1 = new string[](indexBids);
        uint[] memory Team1Score = new uint[](indexBids);
        string[] memory Team2 = new string[](indexBids);
        uint[] memory Team2Score = new uint[](indexBids);
        bool[] memory Team1Won = new bool[](indexBids);
        bool[] memory CompletedGame = new bool[](indexBids);
        uint[] memory BidAmount = new uint[](indexBids);
        
        
       for(uint i=0; i<indexBids; i++){
         Bet storage bet = bets[i];
         Team1[i] = bet.Team1;
         Team1Score[i] = bet.ScoreTeam1;
         Team2[i] = bet.Team2;
         Team2Score[i] = bet.ScoreTeam2;
         Team1Won[i] = bet.Team1Won;
         BidAmount[i] = bet.BidAmount;
         
        }
        return (Team1, Team1Score, Team2, Team2Score, Team1Won, BidAmount, CompletedGame);
    }
    
     function GetIndividualsPlacingBet()public view returns(string[] memory, string[] memory){
        string[] memory PersonA = new string[](indexBids);
        string[] memory PersonB = new string[](indexBids); 
        for(uint i=0; i<indexBids; i++){
            Bet storage bet = bets[i];
            PersonA[i] = bet.WantsTeam1ToWin;
            PersonB[i] = bet.WantsTeam2ToWin;
            
            return (PersonA, PersonB);
        }
     }
    //USE BELOW FUNCTION FOR GIVING WINNING BID AND LOSING BIDS TO PROPER USERS
    function getUserIndexValue(string memory username) public view returns(uint){
        uint counter = 0;
         for(uint i=0; i<index; i++){
             User storage user = users[i];
             if(keccak256(abi.encodePacked(user.Name))==keccak256(abi.encodePacked(username))){
                 return counter;
             }
             else{
                counter = counter+1;
             }
        }
        
    }    
    
    //THIS FUNCTION IS USED FOR TESTING ONLY
    function SuperBowlAddBetToLedger() public{
        //Test 1
        RegisterUserToLedger("Colleen");
        RegisterUserToLedger("Larry");
        SuperBowlAddBet("Seahawks", "Patriots",  5,  "Colleen", "Larry");
        
        //Test 2
        RegisterUserToLedger("Cindy");
        SuperBowlAddBet("Chiefs", "Bills",  4,  "Cindy", "Larry");
    }
    
    function SuperBowlUpdateLedger() public{
        //Test 1
       
        SuperBowlGameCompleted(0, true, 7, 5);
        
        //Test 2
        SuperBowlGameCompleted(1, false, 8, 21);
    }
}