Commit 509ad7d9 authored by Zvezdin .'s avatar Zvezdin .

Create contract

parent 663bbb6e
pragma solidity 0.4.24;
//pragma experimental ABIEncoderV2;
contract Better {
struct Bet {
uint amount;
uint eventID;
uint id;
uint betType; //docs in 'betTypes' in Event struct
uint[2] values;
uint odd;
address user;
uint number;
}
struct Event {
uint id;
uint ts;
string home;
string away;
string sport;
uint[] betTypes; //0 = score, 1 = goals, 2 = result, 3 = penalty
mapping(uint => uint) idToIndex;
Bet[] availableBets;
uint[] availableBetsIds;
Bet[] placedBets;
bool active;
}
uint[] private eventIDs;
mapping(uint => Event) events;
address owner;
modifier onlyOwner() {
require(owner == msg.sender);
_;
}
uint[] tmpTest;
constructor() public {
owner = msg.sender;
//DEBUG temp adds:
tmpTest.length = 2;
tmpTest[0] = 1;
tmpTest[1] = 2;
createEvent(1, 100, "Bulg", "Germ", "Football", tmpTest);
updateBet(1, 2, 123123, [uint(1),3], 7043, 3);
}
function getEventIDs() public view returns (uint[]) {
return eventIDs;
}
function getEventByID(uint id) public view returns (uint ts, string home, string away, string sport, uint[] betTypes, bool active) {
Event storage ev = events[id];
ts = ev.ts;
home = ev.home;
away = ev.away;
sport = ev.sport;
betTypes = ev.betTypes;
active = ev.active;
}
function breakDownBet(Bet bet) private view returns (uint amount, uint betType, uint[2] values, uint odd, address user, uint number) {
amount = bet.amount;
betType = bet.betType;
values = bet.values;
odd = bet.odd;
user = bet.user;
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) {
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) {
Event storage ev = events[eventID];
Bet storage bet = ev.placedBets[idx];
return breakDownBet(bet);
}
function getEventAvailableBets(uint eventID) public view returns (uint[]) {
return events[eventID].availableBetsIds;
}
function getEventPlacedBetsAmount(uint eventID) public view returns (uint) {
return events[eventID].placedBets.length;
}
function isCallerAdmin() public view returns(bool) {
return msg.sender == owner;
}
function createEvent(uint id, uint ts, string home, string away, string sport, uint[] betTypes) onlyOwner public {
Event storage ev = events[id];
ev.id = id;
ev.ts = ts;
ev.away = away;
ev.home = home;
ev.sport = sport;
ev.betTypes = betTypes;
ev.active = true;
eventIDs.push(id);
}
function updateBet(uint eventID, uint betType, uint betID, uint[2] values, uint odd, uint number) public onlyOwner {
Event storage ev = events[eventID];
uint idx = 0;
if(ev.idToIndex[betID] == 0){
ev.availableBets.length++;
idx = ev.availableBets.length-1;
ev.availableBetsIds.push(betID);
ev.idToIndex[betID] = idx;
} else {
idx = ev.idToIndex[betID];
}
Bet storage bet = ev.availableBets[idx];
bet.eventID = eventID;
bet.id = betID;
bet.betType = betType;
bet.values = values;
bet.odd = odd;
bet.number = number;
}
function placeBet(uint eventID, uint betID) public payable {
require(msg.value > 0);
Event storage ev = events[eventID];
Bet storage avBet = ev.availableBets[ev.idToIndex[betID]];
Bet memory b = avBet; // copy to memory
b.amount = msg.value;
b.user = msg.sender;
ev.placedBets.push(b);
}
function closeBetType(uint eventID, uint betType) public onlyOwner {
Event storage ev = events[eventID];
for(uint i=0; i < ev.betTypes.length; i++) {
//TODO
}
}
}
\ No newline at end of file
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