Commit cdbc337a authored by Zvezdin .'s avatar Zvezdin .

More contract security and constraints

parent 677c3a47
......@@ -24,6 +24,8 @@ contract Better {
string away;
string sport;
uint[] betTypes; //0 = score, 1 = goals, 2 = result, 3 = penalty
uint[] closedBetTypes;
mapping(uint => bool) isBetTypeClosed;
mapping(uint => uint) idToIndex;
Bet[] availableBets;
uint[] availableBetsIds;
......@@ -45,6 +47,35 @@ contract Better {
_;
}
modifier eventExists(uint eventID) {
require(events[eventID].ts > 0);
_;
}
modifier betExists(uint eventID, uint betID) {
Event storage ev = events[eventID];
require(ev.availableBets[ev.idToIndex[betID]].odd > 0);
_;
}
modifier betOpen(uint eventID, uint betID) {
Event storage ev = events[eventID];
Bet storage bet = ev.availableBets[ev.idToIndex[betID]];
require(!ev.isBetTypeClosed[bet.betType]);
_;
}
modifier placedBetExists(uint eventID, uint betIndex) {
Event storage ev = events[eventID];
require(ev.placedBets[betIndex].odd > 0);
_;
}
modifier eventActive(uint eventID) {
require(events[eventID].ts > now);
_;
}
uint[] tmpTest;
constructor() public {
......@@ -63,7 +94,7 @@ contract Better {
return eventIDs;
}
function getEventByID(uint id) public view returns (uint ts, string home, string away, string sport, uint[] betTypes, bool active) {
function getEventByID(uint id) public eventExists(id) view returns (uint ts, string home, string away, string sport, uint[] betTypes, uint[] closedBetTypes, bool active) {
Event storage ev = events[id];
ts = ev.ts;
home = ev.home;
......@@ -71,6 +102,7 @@ contract Better {
sport = ev.sport;
betTypes = ev.betTypes;
active = ev.active;
closedBetTypes = ev.closedBetTypes;
}
function breakDownBet(Bet bet) private view returns (uint amount, uint betType, uint[2] values, uint odd, address user, uint number) {
......@@ -82,25 +114,25 @@ contract Better {
number = bet.number;
}
function getEventBetByID(uint eventID, uint betID) public view returns (uint amount, uint betType, uint[2] values, uint odd, address user, uint number) {
function getEventBetByID(uint eventID, uint betID) public betExists(eventID, betID) view returns (uint amount, uint betType, uint[2] values, uint odd, address user, uint number) {
Event storage ev = events[eventID];
Bet storage bet = ev.availableBets[ev.idToIndex[betID]];
return breakDownBet(bet);
}
function getEventPlacedBetByIndex(uint eventID, uint idx) public view returns (uint amount, uint betType, uint[2] values, uint odd, address user, uint number) {
function getEventPlacedBetByIndex(uint eventID, uint idx) public placedBetExists(eventID, idx) view returns (uint amount, uint betType, uint[2] values, uint odd, address user, uint number) {
Event storage ev = events[eventID];
Bet storage bet = ev.placedBets[idx];
return breakDownBet(bet);
}
function getEventAvailableBets(uint eventID) public view returns (uint[]) {
function getEventAvailableBets(uint eventID) public eventExists(eventID) view returns (uint[]) {
return events[eventID].availableBetsIds;
}
function getEventPlacedBetsAmount(uint eventID) public view returns (uint) {
function getEventPlacedBetsAmount(uint eventID) public eventExists(eventID) view returns (uint) {
return events[eventID].placedBets.length;
}
......@@ -121,7 +153,7 @@ contract Better {
eventIDs.push(id);
}
function updateBet(uint eventID, uint betType, uint betID, uint[2] values, uint odd, uint number) public onlyOwner {
function updateBet(uint eventID, uint betType, uint betID, uint[2] values, uint odd, uint number) public eventActive(eventID) onlyOwner {
Event storage ev = events[eventID];
uint idx = 0;
......@@ -146,7 +178,8 @@ contract Better {
}
//only if event is active
function placeBet(uint eventID, uint betID) public payable {
//if bet is not closed
function placeBet(uint eventID, uint betID) public eventActive(eventID) betExists(eventID, betID) betOpen(eventID, betID) payable {
require(msg.value > 0);
Event storage ev = events[eventID];
Bet storage avBet = ev.availableBets[ev.idToIndex[betID]];
......@@ -163,12 +196,12 @@ contract Better {
for(uint i=0; i<ev.placedBets.length; i++) {
Bet storage bet = ev.placedBets[i];
if(bet.betType == betType) {
if(bet.betType == betType && bet.values[0] == values[0] && bet.values[1] == values[1]) {
uint winnings = bet.amount * bet.odd;
winnings /= 100; //because odd is multiplied by 100;
withdrawals[bet.user] += winnings;
BetWon(bet.user, eventID, i, winnings);
emit BetWon(bet.user, eventID, i, winnings);
}
}
}
......@@ -180,9 +213,14 @@ contract Better {
msg.sender.transfer(value);
}
function closeBetType(uint eventID, uint betType) public onlyOwner {
function closeBetType(uint eventID, uint betType) public eventExists(eventID) onlyOwner {
Event storage ev = events[eventID];
if(!ev.isBetTypeClosed[betType]) {
ev.isBetTypeClosed[betType] = true;
ev.closedBetTypes.push(betType);
}
for(uint i=0; i < ev.betTypes.length; i++) {
//TODO
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment