Commit b2b5a3cc authored by Zvezdin .'s avatar Zvezdin .

Create contract operations script

parent 83158fa2
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(){
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(){
init();
}
var inst;
var address = "0x5f9c1c404e70b85ace4ed48b6f7a40dd9dc89023";
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() {
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
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