-
Notifications
You must be signed in to change notification settings - Fork 14
/
BoxingBets.sol
139 lines (110 loc) · 4.73 KB
/
BoxingBets.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
pragma solidity ^0.4.17;
import "./OracleInterface.sol";
import "./Ownable.sol";
/// @title BoxingBets
/// @author John R. Kosinski
/// @notice Takes bets and handles payouts for boxing matches
contract BoxingBets is Ownable {
//mappings
mapping(address => bytes32[]) private userToBets;
mapping(bytes32 => Bet[]) private matchToBets;
//boxing results oracle
address internal boxingOracleAddr = 0;
OracleInterface internal boxingOracle = OracleInterface(boxingOracleAddr);
//constants
uint internal minimumBet = 1000000000000;
struct Bet {
address user;
bytes32 matchId;
uint amount;
uint8 chosenWinner;
}
enum BettableOutcome {
Fighter1,
Fighter2
}
/// @notice determines whether or not the user has already bet on the given match
/// @param _user address of a user
/// @param _matchId id of a match
/// @param _chosenWinner the index of the participant to bet on (to win)
/// @return true if the given user has already placed a bet on the given match
function _betIsValid(address _user, bytes32 _matchId, uint8 _chosenWinner) private view returns (bool) {
return true;
}
/// @notice determines whether or not bets may still be accepted for the given match
/// @param _matchId id of a match
/// @return true if the match is bettable
function _matchOpenForBetting(bytes32 _matchId) private view returns (bool) {
return true;
}
/// @notice sets the address of the boxing oracle contract to use
/// @dev setting a wrong address may result in false return value, or error
/// @param _oracleAddress the address of the boxing oracle
/// @return true if connection to the new oracle address was successful
function setOracleAddress(address _oracleAddress) external onlyOwner returns (bool) {
boxingOracleAddr = _oracleAddress;
boxingOracle = OracleInterface(boxingOracleAddr);
return boxingOracle.testConnection();
}
/// @notice gets the address of the boxing oracle being used
/// @return the address of the currently set oracle
function getOracleAddress() external view returns (address) {
return boxingOracleAddr;
}
/// @notice gets a list ids of all currently bettable matches
/// @return array of match ids
function getBettableMatches() public view returns (bytes32[]) {
return boxingOracle.getPendingMatches();
}
/// @notice returns the full data of the specified match
/// @param _matchId the id of the desired match
/// @return match data
function getMatch(bytes32 _matchId) public view returns (
bytes32 id,
string name,
string participants,
uint8 participantCount,
uint date,
OracleInterface.MatchOutcome outcome,
int8 winner) {
return boxingOracle.getMatch(_matchId);
}
/// @notice returns the full data of the most recent bettable match
/// @return match data
function getMostRecentMatch() public view returns (
bytes32 id,
string name,
string participants,
uint participantCount,
uint date,
OracleInterface.MatchOutcome outcome,
int8 winner) {
return boxingOracle.getMostRecentMatch(true);
}
/// @notice places a non-rescindable bet on the given match
/// @param _matchId the id of the match on which to bet
/// @param _chosenWinner the index of the participant chosen as winner
function placeBet(bytes32 _matchId, uint8 _chosenWinner) public payable {
//bet must be above a certain minimum
require(msg.value >= minimumBet, "Bet amount must be >= minimum bet");
//make sure that match exists
require(boxingOracle.matchExists(_matchId), "Specified match not found");
//require that chosen winner falls within the defined number of participants for match
require(_betIsValid(msg.sender, _matchId, _chosenWinner), "Bet is not valid");
//match must still be open for betting
require(_matchOpenForBetting(_matchId), "Match not open for betting");
//transfer the money into the account
//address(this).transfer(msg.value);
//add the new bet
Bet[] storage bets = matchToBets[_matchId];
bets.push(Bet(msg.sender, _matchId, msg.value, _chosenWinner))-1;
//add the mapping
bytes32[] storage userBets = userToBets[msg.sender];
userBets.push(_matchId);
}
/// @notice for testing; tests that the boxing oracle is callable
/// @return true if connection successful
function testOracleConnection() public view returns (bool) {
return boxingOracle.testConnection();
}
}