Skip to content

Instantly share code, notes, and snippets.

@bet02024
Created June 17, 2019 02:34
Show Gist options
  • Save bet02024/4d174dd5fd1bef703a5d08829cfcbcaa to your computer and use it in GitHub Desktop.
Save bet02024/4d174dd5fd1bef703a5d08829cfcbcaa to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.21+commit.dfe3193c.js&optimize=false&gist=
pragma solidity ^0.4.21;
contract CovoirOracle {
function query(bytes _query) external returns (uint256 id);
}
contract CovoirLookup {
function getQueryAddress() constant public returns (address);
function getResponseAddress() constant public returns (address);
}
contract usingCovoirOracle {
address constant lookupContract = 0x6a6d85aa8a9c7e7d9bbaf25053c8f9a5dd616d3c;
modifier fromCovoirOracle() {
CovoirLookup lookup = CovoirLookup(lookupContract);
if (msg.sender != lookup.getResponseAddress())
revert();
_;
}
function queryCovoirOracle(bytes query) internal returns (uint256 id) {
CovoirLookup lookup = CovoirLookup(lookupContract);
CovoirOracle covoirOracle = CovoirOracle(lookup.getQueryAddress());
return covoirOracle.query(query);
}
}
pragma solidity ^0.4.21;
import "./api_covoir.sol";
contract BettingContract is usingCovoirOracle {
bytes public response;
uint256 public id_response;
uint256 public minimumBet;
uint256 public totalBetOne;
uint256 public totalBetTwo;
uint256 public numberOfBets;
uint256 public maxAmountOfBets = 1000;
address payable[] public players;
struct Player {
uint256 amountBet;
uint16 teamSelected;
}
mapping(address => Player) public playerInfo;
event resultEvent(uint256 id, bytes responseIpfs);
function _oracleCallback(uint256 id, bytes _response) fromCovoirOracle external {
response = _response;
id_response = id;
resultEvent(id_response, responseIpfs);
}
function queryOracle(string queryIpfs) public {
queryCovoirOracle(bytes(queryIpfs));
}
function() external payable {}
constructor() public {
owner = msg.sender;
minimumBet = 100000000000000;
}
function kill() public {
if(msg.sender == owner) selfdestruct(owner);
}
function checkPlayerExists(address payable player) public view returns(bool){
for(uint256 i = 0; i < players.length; i++){
if(players[i] == player) return true;
}
return false;
}
function bet(uint8 _teamSelected) public payable {
//The first require is used to check if the player already exist
require(!checkPlayerExists(msg.sender));
//The second one is used to see if the value sended by the player is
//Higher than the minimum value
require(msg.value >= minimumBet);
//We set the player informations : amount of the bet and selected team
playerInfo[msg.sender].amountBet = msg.value;
playerInfo[msg.sender].teamSelected = _teamSelected;
//then we add the address of the player to the players array
players.push(msg.sender);
//at the end, we increment the stakes of the team selected with the player bet
if ( _teamSelected == 1){
totalBetsOne += msg.value;
}
else{
totalBetsTwo += msg.value;
}
}
// Generates a number between 1 and 10 that will be the winner
function distributePrizes(uint16 teamWinner) public {
address payable[1000] memory winners;
uint256 count = 0; // This is the count for the array of winners
uint256 LoserBet = 0; //This will take the value of all losers bet
uint256 WinnerBet = 0; //This will take the value of all winners bet
address add;
uint256 bet;
address payable playerAddress;
for(uint256 i = 0; i < players.length; i++){
playerAddress = players[i];
if(playerInfo[playerAddress].teamSelected == teamWinner){
winners[count] = playerAddress;
count++;
}
}
//We define which bet sum is the Loser one and which one is the winner
if ( teamWinner == 1){
LoserBet = totalBetsTwo;
WinnerBet = totalBetsOne;
} else{
LoserBet = totalBetsOne;
WinnerBet = totalBetsTwo;
}
for(uint256 j = 0; j < count; j++){
if(winners[j] != address(0))
add = winners[j];
bet = playerInfo[add].amountBet;
//Transfer the money to the user
winners[j].transfer( (bet*(10000+(LoserBet*10000/WinnerBet)))/10000 );
}
delete playerInfo[playerAddress]; // Delete all the players
players.length = 0; // Delete all the players array
LoserBet = 0; //reinitialize the bets
WinnerBet = 0;
totalBetsOne = 0;
totalBetsTwo = 0;
}
function AmountOne() public view returns(uint256){
return totalBetsOne;
}
function AmountTwo() public view returns(uint256){
return totalBetsTwo;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment