Refactoring

This commit is contained in:
viktor 2017-11-22 18:13:56 +03:00
parent 4bf3a99810
commit 28c6128c26
11 changed files with 879 additions and 102 deletions

790
assets/javascripts/application.js Normal file → Executable file

File diff suppressed because one or more lines are too long

View File

@ -29,48 +29,47 @@ function addValidator(web3, validatorViewObj, contractAddr, abi, cb) {
/*var funcParamsNumber = 7; /*var funcParamsNumber = 7;
var standardLength = 32; var standardLength = 32;
SHA3Encrypt(web3, func, function(funcEncode) { let funcEncode = SHA3Encrypt(web3, func)
var funcEncodePart = funcEncode.substring(0,10); var funcEncodePart = funcEncode.substring(0,10);
if (validatorViewObj.miningKey.indexOf("0x") > -1) { if (validatorViewObj.miningKey.indexOf("0x") > -1) {
validatorViewObj.miningKey = validatorViewObj.miningKey.substr(2); validatorViewObj.miningKey = validatorViewObj.miningKey.substr(2);
} }
validatorViewObj.miningKey = validatorViewObj.miningKey.toLowerCase(); validatorViewObj.miningKey = validatorViewObj.miningKey.toLowerCase();
var fullNameHex = "0x" + toUnifiedLengthRight(toHexString(toUTF8Array(validatorViewObj.fullName))); var fullNameHex = "0x" + toUnifiedLengthRight(toHexString(toUTF8Array(validatorViewObj.fullName)));
var streetNameHex = "0x" + toUnifiedLengthRight(toHexString(toUTF8Array(validatorViewObj.streetName))); var streetNameHex = "0x" + toUnifiedLengthRight(toHexString(toUTF8Array(validatorViewObj.streetName)));
var stateHex = "0x" + toUnifiedLengthRight(toHexString(toUTF8Array(validatorViewObj.state))); var stateHex = "0x" + toUnifiedLengthRight(toHexString(toUTF8Array(validatorViewObj.state)));
var parameterLocation1 = standardLength*funcParamsNumber; var parameterLocation1 = standardLength*funcParamsNumber;
var parameterLocation2 = parameterLocation1 + standardLength*(countRows(fullNameHex)); var parameterLocation2 = parameterLocation1 + standardLength*(countRows(fullNameHex));
var parameterLocation3 = parameterLocation2 + standardLength*(countRows(streetNameHex)); var parameterLocation3 = parameterLocation2 + standardLength*(countRows(streetNameHex));
var data = funcEncodePart var data = funcEncodePart
+ toUnifiedLengthLeft(validatorViewObj.miningKey) + toUnifiedLengthLeft(validatorViewObj.miningKey)
+ toUnifiedLengthLeft(validatorViewObj.zip.toString(16)) + toUnifiedLengthLeft(validatorViewObj.zip.toString(16))
+ toUnifiedLengthLeft(validatorViewObj.licenseID.toString(16)) + toUnifiedLengthLeft(validatorViewObj.licenseID.toString(16))
+ toUnifiedLengthLeft(validatorViewObj.licenseExpiredAt.toString(16)) + toUnifiedLengthLeft(validatorViewObj.licenseExpiredAt.toString(16))
+ toUnifiedLengthLeft(parameterLocation1.toString(16)) + toUnifiedLengthLeft(parameterLocation1.toString(16))
+ toUnifiedLengthLeft(parameterLocation2.toString(16)) + toUnifiedLengthLeft(parameterLocation2.toString(16))
+ toUnifiedLengthLeft(parameterLocation3.toString(16)) + toUnifiedLengthLeft(parameterLocation3.toString(16))
+ toUnifiedLengthLeft(bytesCount(validatorViewObj.fullName).toString(16)) + fullNameHex.substring(2) + toUnifiedLengthLeft(bytesCount(validatorViewObj.fullName).toString(16)) + fullNameHex.substring(2)
+ toUnifiedLengthLeft(bytesCount(validatorViewObj.streetName).toString(16)) + streetNameHex.substring(2) + toUnifiedLengthLeft(bytesCount(validatorViewObj.streetName).toString(16)) + streetNameHex.substring(2)
+ toUnifiedLengthLeft(bytesCount(validatorViewObj.state).toString(16)) + stateHex.substring(2); + toUnifiedLengthLeft(bytesCount(validatorViewObj.state).toString(16)) + stateHex.substring(2);
getGasPrice(function(gasPrice) { getGasPrice(function(gasPrice) {
console.log(gasPrice); console.log(gasPrice);
estimateGas(web3, address, contractAddr, data, null, function(estimatedGas, err) { estimateGas(web3, address, contractAddr, data, null, function(estimatedGas, err) {
if (err) {
cb(null, err);
return;
}
estimatedGas += 100000;
sendTx(web3, address, contractAddr, data, null, estimatedGas, gasPrice, function(txHash, err) {
if (err) { if (err) {
cb(null, err); cb(txHash, err);
return; return;
} }
estimatedGas += 100000; cb(txHash);
sendTx(web3, address, contractAddr, data, null, estimatedGas, gasPrice, function(txHash, err) {
if (err) {
cb(txHash, err);
return;
}
cb(txHash);
});
}); });
}); });
});*/ });*/

View File

@ -1,6 +1,6 @@
function SHA3Encrypt(web3, str, cb) { function SHA3Encrypt(web3, str) {
var strEncode = web3.sha3(str); var strEncode = web3.sha3(str);
cb(strEncode); return strEncode;
} }
function estimateGas(web3, from, to, data, val, cb) { function estimateGas(web3, from, to, data, val, cb) {

View File

@ -2,19 +2,18 @@ function checkInitialKey(web3, func, initialKey, contractAddr, cb) {
var funcParamsNumber = 1; var funcParamsNumber = 1;
var standardLength = 32; var standardLength = 32;
SHA3Encrypt(web3, func, function(funcEncode) { let funcEncode = SHA3Encrypt(web3, func)
var funcEncodePart = funcEncode.substring(0,10); var funcEncodePart = funcEncode.substring(0,10);
var data = funcEncodePart var data = funcEncodePart
+ toUnifiedLengthLeft(initialKey); + toUnifiedLengthLeft(initialKey);
console.log(data); console.log(data);
console.log("0x" + initialKey); console.log("0x" + initialKey);
console.log(contractAddr); console.log(contractAddr);
call(web3, "0x" + initialKey, contractAddr, data, function(respHex) { call(web3, "0x" + initialKey, contractAddr, data, function(respHex) {
console.log(respHex); console.log(respHex);
cb(parseInt(respHex, 16)); cb(parseInt(respHex, 16));
});
}); });
} }

View File

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

View File

@ -27,24 +27,23 @@ function createKeys(web3, keys, contractAddr, abi, cb) {
var parameterLocation = standardLength*funcParamsNumber; var parameterLocation = standardLength*funcParamsNumber;
SHA3Encrypt(web3, func, function(funcEncode) { let funcEncode = SHA3Encrypt(web3, func)
var funcEncodePart = funcEncode.substring(0,10); var funcEncodePart = funcEncode.substring(0,10);
var data = funcEncodePart var data = funcEncodePart
+ toUnifiedLengthLeft(keys.miningKey.miningKeyObject.address) + toUnifiedLengthLeft(keys.miningKey.miningKeyObject.address)
+ toUnifiedLengthLeft(keys.payoutKey.payoutKeyObject.address) + toUnifiedLengthLeft(keys.payoutKey.payoutKeyObject.address)
+ toUnifiedLengthLeft(keys.votingKey.votingKeyObject.address); + toUnifiedLengthLeft(keys.votingKey.votingKeyObject.address);
getGasPrice(function(gasPrice) { getGasPrice(function(gasPrice) {
estimateGas(web3, address, contractAddr, data, null, function(estimatedGas) { estimateGas(web3, address, contractAddr, data, null, function(estimatedGas) {
estimatedGas += 100000; estimatedGas += 100000;
sendTx(web3, address, contractAddr, data, null, estimatedGas, gasPrice, function(txHash, err) { sendTx(web3, address, contractAddr, data, null, estimatedGas, gasPrice, function(txHash, err) {
if (err) { if (err) {
cb(txHash, err); cb(txHash, err);
return; return;
} }
cb(txHash); cb(txHash);
});
}); });
}); });
});*/ });*/

View File

@ -1,8 +1,14 @@
//gets config file with address of Oracles contract //gets config file with address of Oracles contract
function getConfig(cb) { async function getConfig(cb) {
$.getJSON("./assets/javascripts/config.json", function(config) { let config = await $.getJSON("./assets/javascripts/config.json")
var contractAddress = config.Ethereum[config.environment].contractAddress; let contractAddress = config.Ethereum[config.environment].contractAddress
var abi = config.Ethereum[config.environment].abi; let abi = config.Ethereum[config.environment].abi
cb(contractAddress, abi); let networkID = config.networkID
}); let configJSON = {
contractAddress,
networkID,
abi
}
if (cb) cb(configJSON)
return configJSON;
} }

View File

@ -15,8 +15,7 @@ function getWeb3(callback) {
// new instance // new instance
myWeb3.eth.defaultAccount = window.web3.eth.defaultAccount; myWeb3.eth.defaultAccount = window.web3.eth.defaultAccount;
checkNetworkVersion(myWeb3, function(isOraclesNetwork) { let isOraclesNetwork = checkNetworkVersion(myWeb3)
callback(myWeb3, isOraclesNetwork); callback(myWeb3, isOraclesNetwork);
});
} }
} }

View File

@ -13,10 +13,9 @@ function startDapp(web3, isOraclesNetwork) {
"votingKey": {} "votingKey": {}
}; };
getAccounts(function(accounts) { getAccounts(async function(accounts) {
getConfig(function(contractAddress, abi) { let config = await getConfig()
getConfigCallBack(web3, accounts, contractAddress, abi); getConfigCallBack(web3, accounts, config.contractAddress, config.abi)
});
}); });
//getting of config callback //getting of config callback
@ -308,4 +307,4 @@ function startDapp(web3, isOraclesNetwork) {
window.addEventListener('load', function() { window.addEventListener('load', function() {
getWeb3(startDapp); getWeb3(startDapp);
}); });

View File

@ -1,5 +1,6 @@
{ {
"environment": "live", "environment": "live",
"networkID": 12648430,
"Ethereum": { "Ethereum": {
"live": { "live": {
"contractAddress": "0xf472e0e43570b9afaab67089615080cf7c20018d", "contractAddress": "0xf472e0e43570b9afaab67089615080cf7c20018d",

View File

@ -22,7 +22,7 @@
"sass": "gulp sass", "sass": "gulp sass",
"coffee": "gulp javascript", "coffee": "gulp javascript",
"watch": "gulp watch", "watch": "gulp watch",
"start": "http-server -a localhost -p 8000" "start": "gulp javascript && http-server -a localhost -p 8000"
}, },
"repository": { "repository": {
"type": "git", "type": "git",