Skip to content

Instantly share code, notes, and snippets.

@seresistvanandras
Last active July 5, 2018 14:47
Show Gist options
  • Save seresistvanandras/50ec619de067edb78e72d1ec2e9a0e20 to your computer and use it in GitHub Desktop.
Save seresistvanandras/50ec619de067edb78e72d1ec2e9a0e20 to your computer and use it in GitHub Desktop.
contract BetOnBrazilvsBelgium is Ownable, usingOraclize {
    using SafeMath for uint;
    string public homeTeam="43924";
    string public awayTeam="43935";
    bool public winningTeamSet;
    bool public winningTeam;
    mapping(address => Bet) public bets;

    struct Bet {
      uint256 betValueHomeTeam;
      uint256 betValueAwayTeam;
    }

    uint256 public totalPotHomeTeam;
    uint256 public totalPotAwayTeam;

    event LogNewOraclizeQuery(string description);
    
    function bet(bool _team) public payable onlyBeforeMatch {
      require(msg.value > 0);
      if(_team) {
        totalPotHomeTeam = totalPotHomeTeam.add(msg.value);
        bets[msg.sender].betValueHomeTeam = bets[msg.sender].betValueHomeTeam.add(msg.value);
      } else {
        totalPotAwayTeam = totalPotAwayTeam.add(msg.value);
        bets[msg.sender].betValueAwayTeam = bets[msg.sender].betValueAwayTeam.add(msg.value);
      }
    }

    function setWinningTeam() public payable onlyAfterMatch {
      require(!winningTeamSet);
      if (oraclize_getPrice("URL") > this.balance) {
        LogNewOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
      } else {
        LogNewOraclizeQuery("Oraclize query was sent, standing by for the answer..");
        oraclize_query("URL", "json(https://api.fifa.com/api/v1/calendar/matches?idCompetition=17&idSeason=254645&language=en&count=500).Results.57.Winner");
      }
    }

    function __callback(bytes32 myid, string result) {
      if (msg.sender != oraclize_cbAddress()) revert();
      require(sha3(result) == sha3(homeTeam) || sha3(result) == sha3(awayTeam));
      if(sha3(result) == sha3(homeTeam)) {
        winningTeam = true;
      } else {
        winningTeam = false;
      }
      winningTeamSet = true;
    }

    function withdrawWinnings() public onlyAfterMatch  {
      require(winningTeamSet);
      uint256 yourBet;
      uint256 reward;
      if(winningTeam) {
        yourBet = bets[msg.sender].betValueHomeTeam;
        reward = totalPotAwayTeam.mul(yourBet).div(totalPotHomeTeam);
        bets[msg.sender].betValueHomeTeam = 0;
        msg.sender.transfer(yourBet.add(reward));
      } else {
        yourBet = bets[msg.sender].betValueAwayTeam;
        reward = totalPotHomeTeam.mul(yourBet).div(totalPotAwayTeam);
        bets[msg.sender].betValueAwayTeam = 0;
        msg.sender.transfer(yourBet.add(reward));
      }
    }

    //2018.07.06 21:00 UTC+3 (Moscow time) unix epoch timestamp of the start of the quarter-final
    modifier onlyBeforeMatch() {
      require(block.timestamp < 1530907200, "you can not bet once the match started");
      _;
    }

    //2018.07.06 23:59 UTC+3 (Moscow time) unix epoch timestamp of the end of the final
    modifier onlyAfterMatch() {
      require(block.timestamp > 1530918000, "you can not set the winning team before the end of the match");
      _;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment