Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sub #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 98 additions & 3 deletions Betting Contract/contracts/betting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pragma solidity 0.4.19;
contract Betting {
/* Constructor function, where owner and outcomes are set */
function Betting(uint[] _outcomes) public {
owner = msg.sender;

}

/* Fallback function */
Expand All @@ -30,41 +32,134 @@ contract Betting {
mapping (address => uint) winnings;
/* Keep track of all outcomes (maps index to numerical outcome) */
mapping (uint => uint) public outcomes;
uint outLen;

/* Add any events you think are necessary */
event BetMade(address gambler);
event BetClosed();
event oracleSet();

/* Uh Oh, what are these? */
modifier ownerOnly() {_;}
modifier oracleOnly() {_;}
modifier outcomeExists(uint outcome) {_;}
modifier ownerOnly() {
require (msg.sender == owner);
_;
}

modifier oracleOnly() {
require (msg.sender == oracle);
_;
}

modifier outcomeExists(uint outcome) {
uint outcomeNum = 0;
while (outcomeNum++ && outcomeNum < outLen) {
if (outcomes[outcomeNum] == outcome) {
_;
}
}


}

/* Owner chooses their trusted Oracle */
function chooseOracle(address _oracle) public ownerOnly() returns (address) {
require (_oracle != gamblerA);
require (_oracle != gamblerB);
oracle = _oracle;

oracleSet();
return oracle;

}

/* Gamblers place their bets, preferably after calling checkOutcomes */
function makeBet(uint _outcome) public payable returns (bool) {
require (msg.sender != oracle);
require (bets[msg.sender] == false);

if (bets[gamblerA].initialized) {
bets[msg.sender] = Bet(_outcome,msg.value,true);
outLen+=1;
gamblerB = msg.sender;
BetMade(gamblerB);
return true;
}

else if (bets[gamblerB].initialized) {
bets[msg.sender] = Bet(_outcome,msg.value,true);
outLen+=1;
gamblerA = msg.sender;
BetMade(gamblerA);
return true;
}
else {
return false;
}

}

/* The oracle chooses which outcome wins */
function makeDecision(uint _outcome) public oracleOnly() outcomeExists(_outcome) {
require(oracleOnly());
require(bets[gamblerA].initialized);
require(bets[gamblerB].initialized);

if (bets[gamblerA].outcome == bets[gamblerB].outcome) {
winnings[gamblerA] += bets[gamblerA].amount;
winnings[gamblerB] += bets[gamblerB].amount;

}
else if (bets[gamblerA].outcome == _outcome) {
winnings[gamblerA] += bets[gamblerA].amount + bets[gamblerB].amount;

}

else if (bets[gamblerB].outcome == _outcome) {
winnings[gamblerB] += bets[gamblerA].amount + bets[gamblerB].amount;

}
else {
winnings[oracle] += bets[gamblerA].amount + bets[gamblerB].amount;
}

contractReset();
}

/* Allow anyone to withdraw their winnings safely (if they have enough) */
function withdraw(uint withdrawAmount) public returns (uint) {
require (winnings[msg.sender] >= withdrawAmount);


winnings[msg.sender] -= withdrawAmount;
if (msg.sender.send(withdrawAmount)) {
return winnings[msg.sender];
}
else {
winnings[msg.sender] += withdrawAmount;
return winnings[msg.sender]; //cant return -1 bc uint??? return this instead
}


}

/* Allow anyone to check the outcomes they can bet on */
function checkOutcomes(uint outcome) public view returns (uint) {
return outcomes[outcome];
}

/* Allow anyone to check if they won any bets */
function checkWinnings() public view returns(uint) {
return winnings[msg.sender];
}

/* Call delete() to reset certain state variables. Which ones? That's upto you to decide */
function contractReset() public ownerOnly() {
delete gamblerA;
delete gamblerB;
delete oracle;
delete bets[gamblerA];
delete bets[gamblerB];

BetClosed();
}
}
Loading