Created
December 4, 2017 00:52
-
-
Save q6r/958f30a19487b997612cf4146e48d4c4 to your computer and use it in GitHub Desktop.
Bet is a contract for betting on football matches between two teams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.17; | |
// Bet is a contract for betting on football matches between two teams | |
contract Bet { | |
address public owner; | |
struct Bets { | |
address addr; | |
uint amount; | |
} | |
struct Team { | |
bool locked; | |
string name; | |
string image; | |
Bets[] bets; | |
uint balance; | |
} | |
Team public A; | |
Team public B; | |
event NewBetA(address bidder, uint amount); | |
event NewBetB(address bidder, uint amount); | |
event NewWinner(address bidder, uint amount); | |
modifier onlyowner { | |
require(msg.sender == owner); | |
_; | |
} | |
function Bet(string aname, string aimage, string bname, string bimage) public { | |
owner = msg.sender; | |
A.name = aname; | |
A.image = aimage; | |
A.locked = false; | |
A.balance = 0; | |
B.name = bname; | |
B.image = bimage; | |
B.locked = false; | |
B.balance = 0; | |
} | |
function bet(uint choice) public payable { | |
require(choice == 0 || choice == 1); | |
require(msg.value > 0); | |
require(A.locked == false); | |
require(B.locked == false); | |
uint amount = msg.value; | |
uint idx; | |
if (choice == 0) { // 0=A | |
A.balance += msg.value; | |
idx = A.bets.length; | |
A.bets.length += 1; | |
A.bets[idx].addr = msg.sender; | |
A.bets[idx].amount = amount; | |
NewBetA(msg.sender, msg.value); | |
} else { // 1=B | |
B.balance += amount; | |
idx = B.bets.length; | |
B.bets.length += 1; | |
B.bets[idx].addr = msg.sender; | |
B.bets[idx].amount = amount; | |
NewBetB(msg.sender, msg.value); | |
} | |
} | |
function lock() public onlyowner { | |
A.locked = true; | |
B.locked = true; | |
} | |
function unlock() public onlyowner { | |
A.locked = false; | |
B.locked = false; | |
} | |
function returnMoney() public onlyowner { | |
lock(); | |
for (uint i = 0; i < A.bets.length; i++) { | |
A.bets[i].addr.transfer(A.bets[i].amount); | |
} | |
for (i = 0; i < B.bets.length; i++) { | |
B.bets[i].addr.transfer(B.bets[i].amount); | |
} | |
} | |
function payWinners(uint choice) public onlyowner { | |
require(choice == 0 || choice == 1); | |
uint collected = 0; | |
var winBalance = (choice == 0) ? A.balance : B.balance; | |
var lostBalance = (choice == 0) ? B.balance : A.balance; | |
var winnerBets = (choice == 0) ? A.bets : B.bets; | |
for (uint i = 0; i < winnerBets.length; i++) { | |
uint extra = winnerBets[i].amount * (lostBalance/winBalance); | |
uint amount = winnerBets[i].amount + extra; | |
if (!winnerBets[i].addr.send(amount)) { | |
collected += amount; | |
} else { | |
NewWinner(winnerBets[i].addr, amount); | |
} | |
} | |
if (collected != 0) { | |
owner.transfer(collected); | |
} | |
// lock the teams | |
lock(); | |
} | |
function setOwner(address _owner) public onlyowner { | |
owner = _owner; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, Additionally, I questioned if it was possible to profit from sports betting. I came upon betonline promo code it yesterday as I was browsing the web. By the way, when I compared it to well-known bookmakers, they offer some of the best odds. I have already received my welcome bonuses at the casino and the bookmaker. Those interested in betting, in my opinion, can watch it.