Commit 82d522a1 authored by Viktor Velev's avatar Viktor Velev

Merge branch 'master' of https://gitlab.com/VikVelev/pun into frontend

parents 13c77f9f 4fa664db
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
This diff is collapsed.
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"bignumber.js": {
"version": "git+https://github.com/frozeman/bignumber.js-nolookahead.git#57692b3ecfc98bbdd6b3a516cb2353652ea49934",
"from": "git+https://github.com/frozeman/bignumber.js-nolookahead.git"
},
"crypto-js": {
"version": "3.1.8",
"resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-3.1.8.tgz",
"integrity": "sha1-cV8HC/YBTyrpkqmLOSkli3E/CNU="
},
"utf8": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/utf8/-/utf8-2.1.2.tgz",
"integrity": "sha1-H6DZJw6b6FDZsFAn9jUZv0ZFfZY="
},
"web3": {
"version": "0.20.4",
"resolved": "https://registry.npmjs.org/web3/-/web3-0.20.4.tgz",
"integrity": "sha1-QA5leaZbtKPd5xpuv2UJr63DOgQ=",
"requires": {
"bignumber.js": "git+https://github.com/frozeman/bignumber.js-nolookahead.git#57692b3ecfc98bbdd6b3a516cb2353652ea49934",
"crypto-js": "^3.1.4",
"utf8": "^2.1.1",
"xhr2": "*",
"xmlhttprequest": "*"
}
},
"xhr2": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/xhr2/-/xhr2-0.1.4.tgz",
"integrity": "sha1-f4dliEdxbbUCYyOBL4GMras4el8="
},
"xmlhttprequest": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz",
"integrity": "sha1-Z/4HXFwk/vOfnWX197f+dRcZaPw="
}
}
}
var abi = [{"constant":true,"inputs":[{"name":"eventID","type":"uint256"},{"name":"idx","type":"uint256"}],"name":"getEventPlacedBetByIndex","outputs":[{"name":"amount","type":"uint256"},{"name":"betType","type":"uint256"},{"name":"values","type":"uint256[2]"},{"name":"odd","type":"uint256"},{"name":"user","type":"address"},{"name":"number","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"uint256"}],"name":"getEventByID","outputs":[{"name":"ts","type":"uint256"},{"name":"home","type":"string"},{"name":"away","type":"string"},{"name":"sport","type":"string"},{"name":"betTypes","type":"uint256[]"},{"name":"active","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getEventIDs","outputs":[{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"eventID","type":"uint256"},{"name":"betType","type":"uint256"}],"name":"closeBetType","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"eventID","type":"uint256"},{"name":"betID","type":"uint256"}],"name":"placeBet","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"eventID","type":"uint256"}],"name":"getEventPlacedBetsAmount","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"eventID","type":"uint256"},{"name":"betID","type":"uint256"}],"name":"getEventBetById","outputs":[{"name":"amount","type":"uint256"},{"name":"betType","type":"uint256"},{"name":"values","type":"uint256[2]"},{"name":"odd","type":"uint256"},{"name":"user","type":"address"},{"name":"number","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"eventID","type":"uint256"},{"name":"betType","type":"uint256"},{"name":"betID","type":"uint256"},{"name":"values","type":"uint256[2]"},{"name":"odd","type":"uint256"},{"name":"number","type":"uint256"}],"name":"updateBet","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"uint256"},{"name":"ts","type":"uint256"},{"name":"home","type":"string"},{"name":"away","type":"string"},{"name":"sport","type":"string"},{"name":"betTypes","type":"uint256[]"}],"name":"createEvent","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"eventID","type":"uint256"}],"name":"getEventAvailableBets","outputs":[{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"isCallerAdmin","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}];
function init(){
console.log("success")
if (typeof web3 === 'undefined') {
//if there is no web3 variable
displayMessage("Error! Are you sure that you are using metamask?");
} else {
displayMessage("Welcome to our DAPP!");
connectContract();
}
}
//this function will be called when the whole page is loaded
window.onload = function(){
}
var inst;
var address = "0x369a165581ba94afcced493d49dc339e13505f29";
var acc;
//temp fix:
//var Web3 = require("web3");
//var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
//init()
function connectContract(){
var Contract = web3.eth.contract(abi);
inst = Contract.at(address);
updateAccount();
}
function updateAccount(){
//in metamask, the accounts array is of size 1 and only contains the currently selected account. The user can select a different account and so we need to update our account variable
acc = web3.eth.accounts[0];
}
function displayMessage(message){
console.log(message);
}
async function test() {
let res = await getEventByID(1);
console.log(res, res[0].toFixed());
}
function isCallerAdmin() {
return new Promise((accept, reject) => {
inst.isCallerAdmin.call({"from": acc}, function(err, res){
if(!err) {
accept(res);
} else {
reject(err);
}
});
})
}
function getEventIDs() {
console.log(web3)
console.log(inst)
return new Promise((accept, reject) => {
inst.getEventIDs.call({"from": acc}, function(err, res){
if(!err) {
accept(res);
} else {
reject(err);
}
});
})
}
function getEventByID(id) {
return new Promise((accept, reject) => {
inst.getEventByID.call(id, {"from": acc}, function(err, res){
if(!err) {
accept(res);
} else {
reject(err);
}
});
})
}
//test();
function onButtonPressed(){
updateAccount();
var input = getTextInput();
contractInstance.transferOwnership(input, {"from": acc}, function(err, res){
if(!err){
displayMessage("Success! Transaction hash: " + res.valueOf());
} else {
displayMessage("Something went wrong. Are you sure that you are the current owner?");
}
});
}
function onSecondButtonPressed(){
updateAccount();
contractInstance.owner.call({"from": acc}, function(err, res) {
if(!err){
displayMessage("The current owner is " + res.valueOf());
} else {
displayMessage("Something went horribly wrong. Deal with it:", err);
}
});
}
\ No newline at end of file
......@@ -19,7 +19,8 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<script src="%PUBLIC_URL%/contract_operations.js"></script>
<title>Pun</title>
</head>
<body>
<noscript>
......
......@@ -5,6 +5,10 @@ import BetPage from './component/main'
import AdminPage from './component/admin'
class App extends Component {
componentDidMount() {
init(); //eslint-disable-line
}
routes = {
main: () => <BetPage/>,
admin: () => <AdminPage/>
......
......@@ -17,6 +17,11 @@ export default class BetPage extends Component {
constructor(props) {
super(props)
// Don't ask
setTimeout(() => getEventIDs(), 1) //eslint-disable-line
this.getAllEvents()
}
......
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