Code refactoring

This commit is contained in:
viktor 2017-06-28 20:28:17 +03:00
parent 72ab852576
commit e4d2407c62
9 changed files with 667 additions and 596 deletions

View File

@ -127,10 +127,9 @@ function estimateGas(web3, acc, contractAddr, data, cb) {
data: data,
to: contractAddr
}, function(err, estimatedGas) {
if (err)
console.log(err);
if (err) console.log(err);
console.log(estimatedGas);
cb(estimatedGas);
cb(estimatedGas, err);
});
}
@ -141,8 +140,7 @@ function sendTx(web3, acc, contractAddr, data, estimatedGas, cb) {
to: contractAddr,
gas: estimatedGas
}, function(err, txHash) {
if (err)
console.log(err);
if (err) console.log(err);
cb(txHash, err);
});
}
@ -153,16 +151,14 @@ function call(web3, acc, contractAddr, data, cb) {
else props = { data: data, to: contractAddr };
web3.eth.call(props, function(err, data) {
if (err)
console.log(err);
if (err) console.log(err);
cb(data);
});
}
function getTxCallBack(txHash, cb) {
web3.eth.getTransaction(txHash, function(err, txDetails) {
if (err)
console.log(err);
if (err) console.log(err);
if (!txDetails.blockNumber) {
setTimeout(function() {
@ -186,7 +182,6 @@ function getContractStringDataFromAddressKey(web3, acc, func, inputVal, i, contr
+ toUnifiedLengthLeft(inputVal);
call(web3, acc, contractAddr, data, function(respHex) {
console.log(respHex);
cb(i, hex2a(respHex));
});
});
@ -228,6 +223,41 @@ function getContractAddressDataFromAddressKey(web3, acc, func, inputVal, i, cont
});
}
//check current network page is connected to. Alerts, if not Oracles network
function checkNetworkVersion(web3, cb) {
var msgNotOracles = "You aren't connected to Oracles network. Please, switch on Parity or MetaMask client and choose Oracles network. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.";
web3.version.getNetwork(function(err, netId) {
if (err)
console.log(err);
console.log("netId: " + netId);
switch (netId) {
case "1": {
console.log('This is mainnet');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
case "2": {
console.log('This is the deprecated Morden test network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
case "3": {
console.log('This is the ropsten test network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
case "12648430": {
console.log('This is Oracles from Metamask');
cb(true);
} break;
default: {
console.log('This is an unknown network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
}
})
}
function checkVotingKey(web3, func, key, contractAddr, cb) {
var funcParamsNumber = 1;
var standardLength = 32;
@ -493,6 +523,18 @@ function validateSearch(ballot, searchInput) {
return false;
}
//get current account chosen in MetaMask or opened at Parity
function getAccounts(cb) {
web3.eth.getAccounts(function(err, accounts) {
if (err) {
$(".loading-container").hide();
showAlert(err, err.message);
return;
}
cb(accounts);
});
}
function getBallots(web3, func, acc, contractAddress, cb) {
SHA3Encrypt(web3, func, function(funcEncode) {
var funcEncodePart = funcEncode.substring(0,10);
@ -889,15 +931,23 @@ function ballotViewObject(ballotID, ballotPropsObj, isVotingEnabled) {
</div>
</div>`;
}
//gets config file with address of Oracles contract
function getConfig(cb) {
$.getJSON("./assets/javascripts/config.json", function(config) {
var contractAddress = config.Ethereum[config.environment].contractAddress;
cb(contractAddress);
});
}
//gets web3 object from MetaMask or Parity
function getWeb3(callback) {
if (typeof window.web3 === 'undefined') {
// no web3, use fallback
// no web3, use fallback
console.error("Please use a web3 browser");
var msgNotEthereum = "You aren't connected to Ethereum. Please, switch on Parity or MetaMask client and refresh the page. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.";
swal("Warning", msgNotEthereum, "warning");
callback(myWeb3, false);
} else {
// window.web3 == web3 most of the time. Don't override the provided,
// window.web3 == web3 most of the time. Don't override the provided,
// web3, just wrap it in your Web3.
var myWeb3 = new Web3(window.web3.currentProvider);
@ -906,59 +956,104 @@ function getWeb3(callback) {
myWeb3.eth.defaultAccount = window.web3.eth.defaultAccount;
checkNetworkVersion(myWeb3, function(isOraclesNetwork) {
callback(myWeb3, isOraclesNetwork);
callback(myWeb3, isOraclesNetwork);
});
}
}
function checkNetworkVersion(web3, cb) {
var msgNotOracles = "You aren't connected to Oracles network. Please, switch on Parity or MetaMask client and choose Oracles network. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.";
web3.version.getNetwork(function(err, netId) {
if (err)
console.log(err);
console.log("netId: " + netId);
switch (netId) {
case "1": {
console.log('This is mainnet');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
case "2": {
console.log('This is the deprecated Morden test network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
case "3": {
console.log('This is the ropsten test network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
case "12648430": {
console.log('This is Oracles from Metamask');
cb(true);
} break;
default: {
console.log('This is an unknown network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
}
})
}
//launches main application
function startDapp(web3, isOraclesNetwork) {
$(function() {
$(".loading-container").hide();
if (!isOraclesNetwork) return;
var config;
if (!isOraclesNetwork) {
$(".loading-container").hide();
return;
}
var ballotsArrayFiltered = [];
var votingKey;
web3.eth.getAccounts(function(error, accounts) {
console.log(accounts);
getAccounts(function(accounts) {
getConfig(function(contractAddress) {
getConfigCallBack(web3, accounts, contractAddress);
})
});
var votingKey;
if (readCookie('votingKey'))
//getting of config callback
function getConfigCallBack(web3, accounts, contractAddress) {
//checks if chosen account is valid voting key
if (accounts.length == 1) {
var possiblePayoutKey = accounts[0];
checkVotingKey(web3,
"checkVotingKeyValidity(address)",
possiblePayoutKey,
contractAddress,
function(_isActive) {
_isActive = !!+_isActive;
if (!_isActive) {
$(".loading-container").hide();
swal("Warning", "Current key isn't valid voting key. Please, choose your voting key in MetaMask client and reload the page. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.", "warning");
} else $(".choose-key-button").trigger("click");
});
} else if (accounts.length == 0) {
$(".loading-container").hide();
swal("Warning", "You haven't chosen any account in MetaMask. Please, choose your voting key in MetaMask client and reload the page. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.", "warning");
}
$(".loading-container").hide();
//choose key button onclick event
$(".choose-key-button").on("click", function() {
$(".loading-container").show();
ballotsNavPan();
$(".key-content").addClass("hidden");
$(".content").removeClass("hidden");
$(".container.vote").empty();
$(".container.new-ballot").addClass("hidden");
$(".container.vote").removeClass("hidden");
$(".loading-container").hide();
votingKey = $(".key-select").val();
getBallots(web3,
"getBallots()",
votingKey,
contractAddress,
function(_ballotsArray) {
$(".loading-container").hide();
ballotsArrayFiltered = _ballotsArray;
showBallotsPage(_ballotsArray, web3, contractAddress);
}
);
// ballots list nav filters onclick events
$(".nav-i").on("click", function() {
$(".search-input").val('');
$(".loading-container").show();
if ($(this).hasClass("nav-i_actual")) {
$(".nav-i").removeClass("nav-i_active");
$(this).addClass("nav-i_active");
getBallotsList(web3, contractAddress);
} else if ($(this).hasClass("nav-i_unanswered")) {
$(".nav-i").removeClass("nav-i_active");
$(this).addClass("nav-i_active");
getBallotsList(web3, contractAddress, {filter: "unanswered"});
} else if ($(this).hasClass("nav-i_expired")) {
$(".nav-i").removeClass("nav-i_active");
$(this).addClass("nav-i_active");
getBallotsList(web3, contractAddress, {filter: "expired"});
}
});
// search input onkeyup event
$(".search-input").on("keyup", function() {
var searchInput = $(this).val();
var ballotsArrayFiltered = filterBallots(searchInput);
$(".container.vote").empty();
showBallotsPage(ballotsArrayFiltered, web3, contractAddress);
});
});
if (readCookie('votingKey')) {
votingKey = readCookie('votingKey');
$(".choose-key-button").trigger("click");
}
for (var i = 0; i < accounts.length; i++) {
if (i == 0) votingKey = accounts[i];
if (readCookie('votingKey') == accounts[i] || (!readCookie('votingKey') && i == 0)) {
@ -969,80 +1064,6 @@ function startDapp(web3, isOraclesNetwork) {
$(".key-select").append($option);
}
//key select onchange event
$(".key-select").change(function() {
createCookie('votingKey', $(this).val(), 365);
votingKey = $(this).val();
});
$.getJSON("./assets/javascripts/config.json", function(_config) {
config = _config;
if (accounts.length == 1) {
var possiblePayoutKey = accounts[0];
checkVotingKey(web3,
"checkVotingKeyValidity(address)",
possiblePayoutKey,
config.Ethereum[config.environment].contractAddress,
function(_isActive) {
_isActive = !!+_isActive;
if (!_isActive) swal("Warning", "Current key isn't valid voting key. Please, choose your voting key in MetaMask client and reload the page. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.", "warning");
else $(".choose-key-button").trigger("click");
});
} else if (accounts.length == 0) {
swal("Warning", "You haven't chosen any account in MetaMask. Please, choose your voting key in MetaMask client and reload the page. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.", "warning");
}
//choose key button onclick event
$(".choose-key-button").on("click", function() {
ballotsNavPan();
$(".key-content").addClass("hidden");
$(".content").removeClass("hidden");
$(".container.vote").empty();
$(".container.new-ballot").addClass("hidden");
$(".container.vote").removeClass("hidden");
$(".loading-container").hide();
votingKey = $(".key-select").val();
getBallots(web3,
"getBallots()",
votingKey,
config.Ethereum[config.environment].contractAddress,
function(_ballotsArray) {
ballotsArrayFiltered = _ballotsArray;
getBallotsCallBack(_ballotsArray);
}
);
// ballots list nav filters onclick events
$(".nav-i").on("click", function() {
$(".search-input").val('');
$(".loading-container").show();
if ($(this).hasClass("nav-i_actual")) {
$(".nav-i").removeClass("nav-i_active");
$(this).addClass("nav-i_active");
getBallotsArray();
} else if ($(this).hasClass("nav-i_unanswered")) {
$(".nav-i").removeClass("nav-i_active");
$(this).addClass("nav-i_active");
getBallotsArray({filter: "unanswered"});
} else if ($(this).hasClass("nav-i_expired")) {
$(".nav-i").removeClass("nav-i_active");
$(this).addClass("nav-i_active");
getBallotsArray({filter: "expired"});
}
});
// search input onkeyup event
$(".search-input").on("keyup", function() {
var searchInput = $(this).val();
var ballotsArrayFiltered = filterBallots(searchInput);
$(".container.vote").empty();
getBallotsCallBack(ballotsArrayFiltered);
});
});
});
//back button onclick event
$(".back").on("click", function() {
if ($(".new-ballot-add").attr("step") == 2) {
@ -1052,10 +1073,16 @@ function startDapp(web3, isOraclesNetwork) {
$(".new-ballot-add").html("Continue");
} else {
ballotsNavPan();
getBallotsArray();
getBallotsList(web3, contractAddress);
}
});
//key select onchange event
$(".key-select").change(function() {
createCookie('votingKey', $(this).val(), 365);
votingKey = $(this).val();
});
//settings button onclick event
$(".header-settings").on("click", function() {
$(".key-content").removeClass("hidden");
@ -1072,7 +1099,6 @@ function startDapp(web3, isOraclesNetwork) {
$(".container.new-ballot").load("./newBallot.html", function() {
newBallotNavPan();
$("#type_add").click(function() {
$(".new-ballot-add").attr("step", 1);
$(".new-ballot-add").html("Continue");
@ -1113,7 +1139,7 @@ function startDapp(web3, isOraclesNetwork) {
}
if (!addAction) {
newBallotClickCallback(ballotViewObj, null);
addBallotClick(web3, ballotViewObj, null, contractAddress);
} else {
var validatorViewObj = {
miningKey: $("#mining-key").val(),
@ -1124,174 +1150,184 @@ function startDapp(web3, isOraclesNetwork) {
licenseID: $("#license-id").val(),
licenseExpiredAt: new Date($("#license-expiration").val()).getTime() / 1000,
};
newBallotClickCallback(ballotViewObj, validatorViewObj);
addBallotClick(web3, ballotViewObj, validatorViewObj, contractAddress);
}
});
});
});
}
function newBallotClickCallback(ballotViewObj, validatorViewObj) {
addBallot(web3,
"addBallot(uint256,address,address,address,uint256,bool,string)",
ballotViewObj,
//triggers after clicking "Add Ballot" button
function addBallotClick(web3, ballotViewObj, validatorViewObj, contractAddress) {
addBallot(web3,
"addBallot(uint256,address,address,address,uint256,bool,string)",
ballotViewObj,
votingKey,
contractAddress,
function(txHash, err) {
addBallotCallBack(err, web3, txHash, ballotViewObj.addAction, validatorViewObj, contractAddress);
}
);
}
//Adding of ballot to contract callback
function addBallotCallBack(err, web3, txHash, addAction, validatorViewObj, contractAddress) {
if (err) {
$(".loading-container").hide();
showAlert(err, err.message);
return;
}
if (!addAction) {
getTxCallBack(txHash, function() {
$(".loading-container").hide();
$(".back").trigger("click");
});
} else {
addValidator(web3,
"addValidator(address,uint256,uint256,uint256,string,string,string)",
validatorViewObj,
votingKey,
config.Ethereum[config.environment].contractAddress,
contractAddress,
function(txHash, err) {
if (err) {
$(".loading-container").hide();
showAlert(err, err.message);
return;
}
if (!ballotViewObj.addAction) {
getTxCallBack(txHash, function() {
$(".loading-container").hide();
$(".back").trigger("click");
});
} else {
addValidator(web3,
"addValidator(address,uint256,uint256,uint256,string,string,string)",
validatorViewObj,
votingKey,
config.Ethereum[config.environment].contractAddress,
function(txHash, err) {
if (err) {
$(".loading-container").hide();
showAlert(err, err.message);
return;
}
getTxCallBack(txHash, function() {
$(".loading-container").hide();
//$(".back").trigger("click");
ballotsNavPan();
getBallotsArray();
});
}
);
}
addValidatorCallBack(err, txHash, web3, contractAddress);
}
);
}
}
function getBallotsCallBack(_ballotsArray) {
for(var i = 0; i < _ballotsArray.length; i++) {
var ballot = _ballotsArray[i];
if (ballot) {
var ballotID = Object.keys(ballot)[0];
var ballotPropsObj = ballot[ballotID];
var ballotView = getBallotView(votingKey, ballotID, ballotPropsObj, false);
$(".container.vote").append(ballotView);
//Adding of validator to contract callback
function addValidatorCallBack(err, txHash, web3, contractAddress) {
if (err) {
$(".loading-container").hide();
showAlert(err, err.message);
return;
}
getTxCallBack(txHash, function() {
$(".loading-container").hide();
//$(".back").trigger("click");
ballotsNavPan();
getBallotsList(web3, contractAddress);
});
}
//shows page with list of ballots
function showBallotsPage(_ballotsArray, web3, contractAddress) {
for(var i = 0; i < _ballotsArray.length; i++) {
var ballot = _ballotsArray[i];
if (ballot) {
var ballotID = Object.keys(ballot)[0];
var ballotPropsObj = ballot[ballotID];
var ballotView = getBallotView(votingKey, ballotID, ballotPropsObj, false);
$(".container.vote").append(ballotView);
}
}
//vote now button onclick event
$(".vote-now").on("click", function() {
getBallotView(votingKey, $(this).attr("ballot-id"), null, true, web3, contractAddress, function(ballotView) {
showSingleBallotPage(ballotView, web3, contractAddress);
});
});
}
//shows page with single ballot
function showSingleBallotPage(ballotView, web3, contractAddress) {
$(".container.vote").empty();
$(".container.vote").append(ballotView);
newBallotNavPan();
//vote button onclick event
$(".vote-button").on("click", function(e) {
voteButtonClick(web3, contractAddress, e, $(this));
});
}
//triggers after .vote-button clicked
function voteButtonClick(web3, contractAddress, e, $this) {
$(".loading-container").show();
var voteFor = $this.hasClass("vote-rating-yes")?true:false;
var ballotID = $this.closest(".vote-i").attr("ballot-id");
vote(web3,
"vote(uint256,bool)",
ballotID,
voteFor,
votingKey,
contractAddress,
function(txHash, err) {
if (err) {
$(".loading-container").hide();
showAlert(err, "You are already voted or have no rights to vote");
return;
}
getTxCallBack(txHash, function() {
$(".loading-container").hide();
$(".back").trigger("click");
});
}
);
}
//change to new ballot navigation pan
function newBallotNavPan() {
$(".nav").addClass("hidden");
$(".search-form").addClass("hidden");
$(".back").removeClass("hidden");
}
//change to ballots navigation pan
function ballotsNavPan() {
$(".nav").removeClass("hidden");
$(".search-form").removeClass("hidden");
$(".back").addClass("hidden");
}
function getBallotsList(web3, contractAddress, filterObj) {
$(".container.new-ballot").addClass("hidden");
$(".container.vote").removeClass("hidden");
$(".container.vote").empty();
getBallots(web3,
"getBallots()",
votingKey,
contractAddress,
function(_ballotsArray) {
$(".loading-container").hide();
if (!filterObj) {
ballotsArrayFiltered = _ballotsArray;
showBallotsPage(ballotsArrayFiltered, web3, contractAddress);
return;
}
if (filterObj.filter == "expired") {
var _ballotsArrayFiltered = [];
for (var i = 0; i < _ballotsArray.length; i++) {
var ballot = _ballotsArray[i];
var ballotID = Object.keys(ballot)[0];
if (new Date(ballot[ballotID].votingEnd*1000) < new (Date)) { //expired
_ballotsArrayFiltered.push(ballot);
}
}
ballotsArrayFiltered = _ballotsArrayFiltered;
showBallotsPage(ballotsArrayFiltered, web3, contractAddress);
} else if (filterObj.filter == "unanswered") {
var _ballotsArrayFiltered = [];
for (var i = 0; i < _ballotsArray.length; i++) {
var ballot = _ballotsArray[i];
var ballotID = Object.keys(ballot)[0];
if (!ballot[ballotID].voted && (new Date(ballot[ballotID].votingEnd*1000) >= new (Date))) {
_ballotsArrayFiltered.push(ballot);
}
}
ballotsArrayFiltered = _ballotsArrayFiltered;
showBallotsPage(ballotsArrayFiltered, web3, contractAddress);
}
}
//vote now button onclick event
$(".vote-now").on("click", function() {
getBallotView(
votingKey,
$(this).attr("ballot-id"),
null,
true,
web3,
config.Ethereum[config.environment].contractAddress,
function(ballotView)
{
$(".container.vote").empty();
$(".container.vote").append(ballotView);
newBallotNavPan();
//vote button onclick event
$(".vote-button").on("click", function(e) {
voteButtonClick(e, $(this));
});
});
});
}
function voteButtonClick(e, $this) {
$(".loading-container").show();
var voteFor = $this.hasClass("vote-rating-yes")?true:false;
var ballotID = $this.closest(".vote-i").attr("ballot-id");
vote(web3,
"vote(uint256,bool)",
ballotID,
voteFor,
votingKey,
config.Ethereum[config.environment].contractAddress,
function(txHash, err) {
if (err) {
$(".loading-container").hide();
showAlert(err, "You are already voted or have no rights to vote");
return;
}
getTxCallBack(txHash, function() {
$(".loading-container").hide();
$(".back").trigger("click");
});
}
);
}
//change to new ballot navigation pan
function newBallotNavPan() {
$(".nav").addClass("hidden");
$(".search-form").addClass("hidden");
$(".back").removeClass("hidden");
}
//change to ballots navigation pan
function ballotsNavPan() {
$(".nav").removeClass("hidden");
$(".search-form").removeClass("hidden");
$(".back").addClass("hidden");
}
function getBallotsArray(filterObj) {
$(".container.new-ballot").addClass("hidden");
$(".container.vote").removeClass("hidden");
$(".container.vote").empty();
getBallots(web3,
"getBallots()",
votingKey,
config.Ethereum[config.environment].contractAddress,
function(_ballotsArray) {
$(".loading-container").hide();
if (!filterObj) {
ballotsArrayFiltered = _ballotsArray;
getBallotsCallBack(ballotsArrayFiltered);
return;
}
if (filterObj.filter == "expired") {
var _ballotsArrayFiltered = [];
for (var i = 0; i < _ballotsArray.length; i++) {
var ballot = _ballotsArray[i];
var ballotID = Object.keys(ballot)[0];
if (new Date(ballot[ballotID].votingEnd*1000) < new (Date)) { //expired
_ballotsArrayFiltered.push(ballot);
}
}
ballotsArrayFiltered = _ballotsArrayFiltered;
getBallotsCallBack(ballotsArrayFiltered);
} else if (filterObj.filter == "unanswered") {
var _ballotsArrayFiltered = [];
for (var i = 0; i < _ballotsArray.length; i++) {
var ballot = _ballotsArray[i];
var ballotID = Object.keys(ballot)[0];
if (!ballot[ballotID].voted && (new Date(ballot[ballotID].votingEnd*1000) >= new (Date))) {
_ballotsArrayFiltered.push(ballot);
}
}
ballotsArrayFiltered = _ballotsArrayFiltered;
getBallotsCallBack(ballotsArrayFiltered);
}
}
);
}
});
);
}
});
}

View File

@ -9,10 +9,9 @@ function estimateGas(web3, acc, contractAddr, data, cb) {
data: data,
to: contractAddr
}, function(err, estimatedGas) {
if (err)
console.log(err);
if (err) console.log(err);
console.log(estimatedGas);
cb(estimatedGas);
cb(estimatedGas, err);
});
}
@ -23,8 +22,7 @@ function sendTx(web3, acc, contractAddr, data, estimatedGas, cb) {
to: contractAddr,
gas: estimatedGas
}, function(err, txHash) {
if (err)
console.log(err);
if (err) console.log(err);
cb(txHash, err);
});
}
@ -35,16 +33,14 @@ function call(web3, acc, contractAddr, data, cb) {
else props = { data: data, to: contractAddr };
web3.eth.call(props, function(err, data) {
if (err)
console.log(err);
if (err) console.log(err);
cb(data);
});
}
function getTxCallBack(txHash, cb) {
web3.eth.getTransaction(txHash, function(err, txDetails) {
if (err)
console.log(err);
if (err) console.log(err);
if (!txDetails.blockNumber) {
setTimeout(function() {
@ -68,7 +64,6 @@ function getContractStringDataFromAddressKey(web3, acc, func, inputVal, i, contr
+ toUnifiedLengthLeft(inputVal);
call(web3, acc, contractAddr, data, function(respHex) {
console.log(respHex);
cb(i, hex2a(respHex));
});
});

View File

@ -0,0 +1,35 @@
//check current network page is connected to. Alerts, if not Oracles network
function checkNetworkVersion(web3, cb) {
var msgNotOracles = "You aren't connected to Oracles network. Please, switch on Parity or MetaMask client and choose Oracles network. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.";
web3.version.getNetwork(function(err, netId) {
if (err)
console.log(err);
console.log("netId: " + netId);
switch (netId) {
case "1": {
console.log('This is mainnet');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
case "2": {
console.log('This is the deprecated Morden test network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
case "3": {
console.log('This is the ropsten test network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
case "12648430": {
console.log('This is Oracles from Metamask');
cb(true);
} break;
default: {
console.log('This is an unknown network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
}
})
}

View File

@ -0,0 +1,12 @@
//get current account chosen in MetaMask or opened at Parity
function getAccounts(cb) {
web3.eth.getAccounts(function(err, accounts) {
if (err) {
$(".loading-container").hide();
showAlert(err, err.message);
return;
}
cb(accounts);
});
}

View File

@ -0,0 +1,7 @@
//gets config file with address of Oracles contract
function getConfig(cb) {
$.getJSON("./assets/javascripts/config.json", function(config) {
var contractAddress = config.Ethereum[config.environment].contractAddress;
cb(contractAddress);
});
}

View File

@ -0,0 +1,22 @@
//gets web3 object from MetaMask or Parity
function getWeb3(callback) {
if (typeof window.web3 === 'undefined') {
// no web3, use fallback
console.error("Please use a web3 browser");
var msgNotEthereum = "You aren't connected to Ethereum. Please, switch on Parity or MetaMask client and refresh the page. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.";
swal("Warning", msgNotEthereum, "warning");
callback(myWeb3, false);
} else {
// window.web3 == web3 most of the time. Don't override the provided,
// web3, just wrap it in your Web3.
var myWeb3 = new Web3(window.web3.currentProvider);
// the default account doesn't seem to be persisted, copy it to our
// new instance
myWeb3.eth.defaultAccount = window.web3.eth.defaultAccount;
checkNetworkVersion(myWeb3, function(isOraclesNetwork) {
callback(myWeb3, isOraclesNetwork);
});
}
}

View File

@ -1,73 +1,97 @@
function getWeb3(callback) {
if (typeof window.web3 === 'undefined') {
// no web3, use fallback
console.error("Please use a web3 browser");
var msgNotEthereum = "You aren't connected to Ethereum. Please, switch on Parity or MetaMask client and refresh the page. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.";
swal("Warning", msgNotEthereum, "warning");
callback(myWeb3, false);
} else {
// window.web3 == web3 most of the time. Don't override the provided,
// web3, just wrap it in your Web3.
var myWeb3 = new Web3(window.web3.currentProvider);
// the default account doesn't seem to be persisted, copy it to our
// new instance
myWeb3.eth.defaultAccount = window.web3.eth.defaultAccount;
checkNetworkVersion(myWeb3, function(isOraclesNetwork) {
callback(myWeb3, isOraclesNetwork);
});
}
}
function checkNetworkVersion(web3, cb) {
var msgNotOracles = "You aren't connected to Oracles network. Please, switch on Parity or MetaMask client and choose Oracles network. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.";
web3.version.getNetwork(function(err, netId) {
if (err)
console.log(err);
console.log("netId: " + netId);
switch (netId) {
case "1": {
console.log('This is mainnet');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
case "2": {
console.log('This is the deprecated Morden test network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
case "3": {
console.log('This is the ropsten test network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
case "12648430": {
console.log('This is Oracles from Metamask');
cb(true);
} break;
default: {
console.log('This is an unknown network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
} break;
}
})
}
//launches main application
function startDapp(web3, isOraclesNetwork) {
$(function() {
$(".loading-container").hide();
if (!isOraclesNetwork) return;
var config;
if (!isOraclesNetwork) {
$(".loading-container").hide();
return;
}
var ballotsArrayFiltered = [];
var votingKey;
web3.eth.getAccounts(function(error, accounts) {
console.log(accounts);
getAccounts(function(accounts) {
getConfig(function(contractAddress) {
getConfigCallBack(web3, accounts, contractAddress);
})
});
var votingKey;
if (readCookie('votingKey'))
//getting of config callback
function getConfigCallBack(web3, accounts, contractAddress) {
//checks if chosen account is valid voting key
if (accounts.length == 1) {
var possiblePayoutKey = accounts[0];
checkVotingKey(web3,
"checkVotingKeyValidity(address)",
possiblePayoutKey,
contractAddress,
function(_isActive) {
_isActive = !!+_isActive;
if (!_isActive) {
$(".loading-container").hide();
swal("Warning", "Current key isn't valid voting key. Please, choose your voting key in MetaMask client and reload the page. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.", "warning");
} else $(".choose-key-button").trigger("click");
});
} else if (accounts.length == 0) {
$(".loading-container").hide();
swal("Warning", "You haven't chosen any account in MetaMask. Please, choose your voting key in MetaMask client and reload the page. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.", "warning");
}
$(".loading-container").hide();
//choose key button onclick event
$(".choose-key-button").on("click", function() {
$(".loading-container").show();
ballotsNavPan();
$(".key-content").addClass("hidden");
$(".content").removeClass("hidden");
$(".container.vote").empty();
$(".container.new-ballot").addClass("hidden");
$(".container.vote").removeClass("hidden");
$(".loading-container").hide();
votingKey = $(".key-select").val();
getBallots(web3,
"getBallots()",
votingKey,
contractAddress,
function(_ballotsArray) {
$(".loading-container").hide();
ballotsArrayFiltered = _ballotsArray;
showBallotsPage(_ballotsArray, web3, contractAddress);
}
);
// ballots list nav filters onclick events
$(".nav-i").on("click", function() {
$(".search-input").val('');
$(".loading-container").show();
if ($(this).hasClass("nav-i_actual")) {
$(".nav-i").removeClass("nav-i_active");
$(this).addClass("nav-i_active");
getBallotsList(web3, contractAddress);
} else if ($(this).hasClass("nav-i_unanswered")) {
$(".nav-i").removeClass("nav-i_active");
$(this).addClass("nav-i_active");
getBallotsList(web3, contractAddress, {filter: "unanswered"});
} else if ($(this).hasClass("nav-i_expired")) {
$(".nav-i").removeClass("nav-i_active");
$(this).addClass("nav-i_active");
getBallotsList(web3, contractAddress, {filter: "expired"});
}
});
// search input onkeyup event
$(".search-input").on("keyup", function() {
var searchInput = $(this).val();
var ballotsArrayFiltered = filterBallots(searchInput);
$(".container.vote").empty();
showBallotsPage(ballotsArrayFiltered, web3, contractAddress);
});
});
if (readCookie('votingKey')) {
votingKey = readCookie('votingKey');
$(".choose-key-button").trigger("click");
}
for (var i = 0; i < accounts.length; i++) {
if (i == 0) votingKey = accounts[i];
if (readCookie('votingKey') == accounts[i] || (!readCookie('votingKey') && i == 0)) {
@ -78,80 +102,6 @@ function startDapp(web3, isOraclesNetwork) {
$(".key-select").append($option);
}
//key select onchange event
$(".key-select").change(function() {
createCookie('votingKey', $(this).val(), 365);
votingKey = $(this).val();
});
$.getJSON("./assets/javascripts/config.json", function(_config) {
config = _config;
if (accounts.length == 1) {
var possiblePayoutKey = accounts[0];
checkVotingKey(web3,
"checkVotingKeyValidity(address)",
possiblePayoutKey,
config.Ethereum[config.environment].contractAddress,
function(_isActive) {
_isActive = !!+_isActive;
if (!_isActive) swal("Warning", "Current key isn't valid voting key. Please, choose your voting key in MetaMask client and reload the page. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.", "warning");
else $(".choose-key-button").trigger("click");
});
} else if (accounts.length == 0) {
swal("Warning", "You haven't chosen any account in MetaMask. Please, choose your voting key in MetaMask client and reload the page. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.", "warning");
}
//choose key button onclick event
$(".choose-key-button").on("click", function() {
ballotsNavPan();
$(".key-content").addClass("hidden");
$(".content").removeClass("hidden");
$(".container.vote").empty();
$(".container.new-ballot").addClass("hidden");
$(".container.vote").removeClass("hidden");
$(".loading-container").hide();
votingKey = $(".key-select").val();
getBallots(web3,
"getBallots()",
votingKey,
config.Ethereum[config.environment].contractAddress,
function(_ballotsArray) {
ballotsArrayFiltered = _ballotsArray;
getBallotsCallBack(_ballotsArray);
}
);
// ballots list nav filters onclick events
$(".nav-i").on("click", function() {
$(".search-input").val('');
$(".loading-container").show();
if ($(this).hasClass("nav-i_actual")) {
$(".nav-i").removeClass("nav-i_active");
$(this).addClass("nav-i_active");
getBallotsArray();
} else if ($(this).hasClass("nav-i_unanswered")) {
$(".nav-i").removeClass("nav-i_active");
$(this).addClass("nav-i_active");
getBallotsArray({filter: "unanswered"});
} else if ($(this).hasClass("nav-i_expired")) {
$(".nav-i").removeClass("nav-i_active");
$(this).addClass("nav-i_active");
getBallotsArray({filter: "expired"});
}
});
// search input onkeyup event
$(".search-input").on("keyup", function() {
var searchInput = $(this).val();
var ballotsArrayFiltered = filterBallots(searchInput);
$(".container.vote").empty();
getBallotsCallBack(ballotsArrayFiltered);
});
});
});
//back button onclick event
$(".back").on("click", function() {
if ($(".new-ballot-add").attr("step") == 2) {
@ -161,10 +111,16 @@ function startDapp(web3, isOraclesNetwork) {
$(".new-ballot-add").html("Continue");
} else {
ballotsNavPan();
getBallotsArray();
getBallotsList(web3, contractAddress);
}
});
//key select onchange event
$(".key-select").change(function() {
createCookie('votingKey', $(this).val(), 365);
votingKey = $(this).val();
});
//settings button onclick event
$(".header-settings").on("click", function() {
$(".key-content").removeClass("hidden");
@ -181,7 +137,6 @@ function startDapp(web3, isOraclesNetwork) {
$(".container.new-ballot").load("./newBallot.html", function() {
newBallotNavPan();
$("#type_add").click(function() {
$(".new-ballot-add").attr("step", 1);
$(".new-ballot-add").html("Continue");
@ -222,7 +177,7 @@ function startDapp(web3, isOraclesNetwork) {
}
if (!addAction) {
newBallotClickCallback(ballotViewObj, null);
addBallotClick(web3, ballotViewObj, null, contractAddress);
} else {
var validatorViewObj = {
miningKey: $("#mining-key").val(),
@ -233,174 +188,184 @@ function startDapp(web3, isOraclesNetwork) {
licenseID: $("#license-id").val(),
licenseExpiredAt: new Date($("#license-expiration").val()).getTime() / 1000,
};
newBallotClickCallback(ballotViewObj, validatorViewObj);
addBallotClick(web3, ballotViewObj, validatorViewObj, contractAddress);
}
});
});
});
}
function newBallotClickCallback(ballotViewObj, validatorViewObj) {
addBallot(web3,
"addBallot(uint256,address,address,address,uint256,bool,string)",
ballotViewObj,
//triggers after clicking "Add Ballot" button
function addBallotClick(web3, ballotViewObj, validatorViewObj, contractAddress) {
addBallot(web3,
"addBallot(uint256,address,address,address,uint256,bool,string)",
ballotViewObj,
votingKey,
contractAddress,
function(txHash, err) {
addBallotCallBack(err, web3, txHash, ballotViewObj.addAction, validatorViewObj, contractAddress);
}
);
}
//Adding of ballot to contract callback
function addBallotCallBack(err, web3, txHash, addAction, validatorViewObj, contractAddress) {
if (err) {
$(".loading-container").hide();
showAlert(err, err.message);
return;
}
if (!addAction) {
getTxCallBack(txHash, function() {
$(".loading-container").hide();
$(".back").trigger("click");
});
} else {
addValidator(web3,
"addValidator(address,uint256,uint256,uint256,string,string,string)",
validatorViewObj,
votingKey,
config.Ethereum[config.environment].contractAddress,
contractAddress,
function(txHash, err) {
if (err) {
$(".loading-container").hide();
showAlert(err, err.message);
return;
}
if (!ballotViewObj.addAction) {
getTxCallBack(txHash, function() {
$(".loading-container").hide();
$(".back").trigger("click");
});
} else {
addValidator(web3,
"addValidator(address,uint256,uint256,uint256,string,string,string)",
validatorViewObj,
votingKey,
config.Ethereum[config.environment].contractAddress,
function(txHash, err) {
if (err) {
$(".loading-container").hide();
showAlert(err, err.message);
return;
}
getTxCallBack(txHash, function() {
$(".loading-container").hide();
//$(".back").trigger("click");
ballotsNavPan();
getBallotsArray();
});
}
);
}
addValidatorCallBack(err, txHash, web3, contractAddress);
}
);
}
}
function getBallotsCallBack(_ballotsArray) {
for(var i = 0; i < _ballotsArray.length; i++) {
var ballot = _ballotsArray[i];
if (ballot) {
var ballotID = Object.keys(ballot)[0];
var ballotPropsObj = ballot[ballotID];
var ballotView = getBallotView(votingKey, ballotID, ballotPropsObj, false);
$(".container.vote").append(ballotView);
//Adding of validator to contract callback
function addValidatorCallBack(err, txHash, web3, contractAddress) {
if (err) {
$(".loading-container").hide();
showAlert(err, err.message);
return;
}
getTxCallBack(txHash, function() {
$(".loading-container").hide();
//$(".back").trigger("click");
ballotsNavPan();
getBallotsList(web3, contractAddress);
});
}
//shows page with list of ballots
function showBallotsPage(_ballotsArray, web3, contractAddress) {
for(var i = 0; i < _ballotsArray.length; i++) {
var ballot = _ballotsArray[i];
if (ballot) {
var ballotID = Object.keys(ballot)[0];
var ballotPropsObj = ballot[ballotID];
var ballotView = getBallotView(votingKey, ballotID, ballotPropsObj, false);
$(".container.vote").append(ballotView);
}
}
//vote now button onclick event
$(".vote-now").on("click", function() {
getBallotView(votingKey, $(this).attr("ballot-id"), null, true, web3, contractAddress, function(ballotView) {
showSingleBallotPage(ballotView, web3, contractAddress);
});
});
}
//shows page with single ballot
function showSingleBallotPage(ballotView, web3, contractAddress) {
$(".container.vote").empty();
$(".container.vote").append(ballotView);
newBallotNavPan();
//vote button onclick event
$(".vote-button").on("click", function(e) {
voteButtonClick(web3, contractAddress, e, $(this));
});
}
//triggers after .vote-button clicked
function voteButtonClick(web3, contractAddress, e, $this) {
$(".loading-container").show();
var voteFor = $this.hasClass("vote-rating-yes")?true:false;
var ballotID = $this.closest(".vote-i").attr("ballot-id");
vote(web3,
"vote(uint256,bool)",
ballotID,
voteFor,
votingKey,
contractAddress,
function(txHash, err) {
if (err) {
$(".loading-container").hide();
showAlert(err, "You are already voted or have no rights to vote");
return;
}
getTxCallBack(txHash, function() {
$(".loading-container").hide();
$(".back").trigger("click");
});
}
);
}
//change to new ballot navigation pan
function newBallotNavPan() {
$(".nav").addClass("hidden");
$(".search-form").addClass("hidden");
$(".back").removeClass("hidden");
}
//change to ballots navigation pan
function ballotsNavPan() {
$(".nav").removeClass("hidden");
$(".search-form").removeClass("hidden");
$(".back").addClass("hidden");
}
function getBallotsList(web3, contractAddress, filterObj) {
$(".container.new-ballot").addClass("hidden");
$(".container.vote").removeClass("hidden");
$(".container.vote").empty();
getBallots(web3,
"getBallots()",
votingKey,
contractAddress,
function(_ballotsArray) {
$(".loading-container").hide();
if (!filterObj) {
ballotsArrayFiltered = _ballotsArray;
showBallotsPage(ballotsArrayFiltered, web3, contractAddress);
return;
}
if (filterObj.filter == "expired") {
var _ballotsArrayFiltered = [];
for (var i = 0; i < _ballotsArray.length; i++) {
var ballot = _ballotsArray[i];
var ballotID = Object.keys(ballot)[0];
if (new Date(ballot[ballotID].votingEnd*1000) < new (Date)) { //expired
_ballotsArrayFiltered.push(ballot);
}
}
ballotsArrayFiltered = _ballotsArrayFiltered;
showBallotsPage(ballotsArrayFiltered, web3, contractAddress);
} else if (filterObj.filter == "unanswered") {
var _ballotsArrayFiltered = [];
for (var i = 0; i < _ballotsArray.length; i++) {
var ballot = _ballotsArray[i];
var ballotID = Object.keys(ballot)[0];
if (!ballot[ballotID].voted && (new Date(ballot[ballotID].votingEnd*1000) >= new (Date))) {
_ballotsArrayFiltered.push(ballot);
}
}
ballotsArrayFiltered = _ballotsArrayFiltered;
showBallotsPage(ballotsArrayFiltered, web3, contractAddress);
}
}
//vote now button onclick event
$(".vote-now").on("click", function() {
getBallotView(
votingKey,
$(this).attr("ballot-id"),
null,
true,
web3,
config.Ethereum[config.environment].contractAddress,
function(ballotView)
{
$(".container.vote").empty();
$(".container.vote").append(ballotView);
newBallotNavPan();
//vote button onclick event
$(".vote-button").on("click", function(e) {
voteButtonClick(e, $(this));
});
});
});
}
function voteButtonClick(e, $this) {
$(".loading-container").show();
var voteFor = $this.hasClass("vote-rating-yes")?true:false;
var ballotID = $this.closest(".vote-i").attr("ballot-id");
vote(web3,
"vote(uint256,bool)",
ballotID,
voteFor,
votingKey,
config.Ethereum[config.environment].contractAddress,
function(txHash, err) {
if (err) {
$(".loading-container").hide();
showAlert(err, "You are already voted or have no rights to vote");
return;
}
getTxCallBack(txHash, function() {
$(".loading-container").hide();
$(".back").trigger("click");
});
}
);
}
//change to new ballot navigation pan
function newBallotNavPan() {
$(".nav").addClass("hidden");
$(".search-form").addClass("hidden");
$(".back").removeClass("hidden");
}
//change to ballots navigation pan
function ballotsNavPan() {
$(".nav").removeClass("hidden");
$(".search-form").removeClass("hidden");
$(".back").addClass("hidden");
}
function getBallotsArray(filterObj) {
$(".container.new-ballot").addClass("hidden");
$(".container.vote").removeClass("hidden");
$(".container.vote").empty();
getBallots(web3,
"getBallots()",
votingKey,
config.Ethereum[config.environment].contractAddress,
function(_ballotsArray) {
$(".loading-container").hide();
if (!filterObj) {
ballotsArrayFiltered = _ballotsArray;
getBallotsCallBack(ballotsArrayFiltered);
return;
}
if (filterObj.filter == "expired") {
var _ballotsArrayFiltered = [];
for (var i = 0; i < _ballotsArray.length; i++) {
var ballot = _ballotsArray[i];
var ballotID = Object.keys(ballot)[0];
if (new Date(ballot[ballotID].votingEnd*1000) < new (Date)) { //expired
_ballotsArrayFiltered.push(ballot);
}
}
ballotsArrayFiltered = _ballotsArrayFiltered;
getBallotsCallBack(ballotsArrayFiltered);
} else if (filterObj.filter == "unanswered") {
var _ballotsArrayFiltered = [];
for (var i = 0; i < _ballotsArray.length; i++) {
var ballot = _ballotsArray[i];
var ballotID = Object.keys(ballot)[0];
if (!ballot[ballotID].voted && (new Date(ballot[ballotID].votingEnd*1000) >= new (Date))) {
_ballotsArrayFiltered.push(ballot);
}
}
ballotsArrayFiltered = _ballotsArrayFiltered;
getBallotsCallBack(ballotsArrayFiltered);
}
}
);
}
});
);
}
});
}

View File

@ -2,7 +2,6 @@
"environment": "live",
"Ethereum": {
"live": {
"account": "0xDd0BB0e2a1594240fED0c2f2c17C1E9AB4F87126",
"contractAddress": "0xf472e0e43570b9afaab67089615080cf7c20018d"
}
}

View File

@ -78,6 +78,6 @@
</div>
</footer>
<script src="./assets/javascripts/sweetalert2.min.js" type="text/javascript"></script>
<script src="./assets/javascripts/application.js?v=1.66" type="text/javascript"></script>
<script src="./assets/javascripts/application.js?v=1.67" type="text/javascript"></script>
</body>
</html>