Refactoring, functions comments

This commit is contained in:
viktor 2017-06-28 16:42:40 +03:00
parent de7f7aa6c0
commit b67b42362f
4 changed files with 261 additions and 196 deletions

File diff suppressed because one or more lines are too long

View File

@ -8,6 +8,10 @@ function checkInitialKey(web3, func, initialKey, contractAddr, cb) {
var data = funcEncodePart var data = funcEncodePart
+ toUnifiedLengthLeft(initialKey); + toUnifiedLengthLeft(initialKey);
console.log(data);
console.log("0x" + initialKey);
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,3 +1,4 @@
//gets web3 object from MetaMask or Parity
function getWeb3(callback) { function getWeb3(callback) {
if (typeof window.web3 === 'undefined') { if (typeof window.web3 === 'undefined') {
// no web3, use fallback // no web3, use fallback
@ -20,6 +21,7 @@ function getWeb3(callback) {
} }
} }
//check current network page is connected to. Alerts, if not Oracles network
function checkNetworkVersion(web3, cb) { 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."; 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) { web3.version.getNetwork(function(err, netId) {
@ -55,6 +57,7 @@ function checkNetworkVersion(web3, cb) {
}) })
} }
//launches main application
function startDapp(web3, isOraclesNetwork) { function startDapp(web3, isOraclesNetwork) {
$(function() { $(function() {
$(".loading-container").hide(); $(".loading-container").hide();
@ -64,14 +67,30 @@ function startDapp(web3, isOraclesNetwork) {
"payoutKey": {}, "payoutKey": {},
"votingKey": {} "votingKey": {}
}; };
var config;
web3.eth.getAccounts(function(error, accounts) { //get current account chosen in MetaMask or opened at Parity
console.log(accounts); web3.eth.getAccounts(function(err, accounts) {
if (err) {
$(".loading-container").hide();
swal("Error", err.message, "error");
return;
}
getConfig(err, function(config) {
getConfigCallBack(web3, accounts, config);
});
});
//gets config file with address of Oracles contract
function getConfig(err, cb) {
$.getJSON("./assets/javascripts/config.json", function(_config) { $.getJSON("./assets/javascripts/config.json", function(_config) {
config = _config; cb(_config);
});
}
//getting of config callback
function getConfigCallBack(web3, accounts, config) {
//checks if chosen account is valid initial key
if (accounts.length == 1) { if (accounts.length == 1) {
var possibleInitialKey = accounts[0].substr(2); var possibleInitialKey = accounts[0].substr(2);
checkInitialKey(web3, checkInitialKey(web3,
@ -90,12 +109,22 @@ function startDapp(web3, isOraclesNetwork) {
$("#initialKeySource").click(); $("#initialKeySource").click();
}) })
$("#initialKeySource").change(initialKeyChosen); $("#initialKeySource").change(function() {
initialKeyChosen(this, config, function(address) {
checkInitialKeyCallBack(keys, function(_keys) {
addressesGeneratedCallBack(config, _keys, address, function(err, address) {
addressesAddedToContractCallBack(err, address, _keys);
})
});
});
});
}
function initialKeyChosen() { //triggers, if initial key is chosen
$(this).remove(); function initialKeyChosen(el, config, cb) {
$(el).remove();
$("<input type='file' id='initialKeySource' />").change(initialKeyChosen).appendTo($(".create-keys")); $("<input type='file' id='initialKeySource' />").change(initialKeyChosen).appendTo($(".create-keys"));
var file = $(this).prop('files')[0]; var file = $(el).prop('files')[0];
var reader = new FileReader(); var reader = new FileReader();
reader.readAsText(file, "UTF-8"); reader.readAsText(file, "UTF-8");
reader.onload = function (evt) { reader.onload = function (evt) {
@ -122,12 +151,7 @@ function startDapp(web3, isOraclesNetwork) {
$(".loading-container").show(); $(".loading-container").show();
setTimeout(function() { setTimeout(function() {
if (!config) { cb(address);
$.getJSON("./assets/javascripts/config.json", function(_config) {
config = _config;
configLoadedCallBack(web3, config, address);
});
} else configLoadedCallBack(web3, config, address);
}, 500) }, 500)
} }
); );
@ -137,8 +161,8 @@ function startDapp(web3, isOraclesNetwork) {
} }
} }
function configLoadedCallBack(web3, config, address) { //validating of initial key callback: async generates 3 addresses: mining, payout, voting
function checkInitialKeyCallBack(keys, cb) {
var keysCount = 0; var keysCount = 0;
for (var i in keys) { for (var i in keys) {
keysCount++; keysCount++;
@ -152,7 +176,7 @@ function startDapp(web3, isOraclesNetwork) {
keys.miningKey.miningKeyObject = _miningKeyObject; keys.miningKey.miningKeyObject = _miningKeyObject;
keys.miningKey.password = password; keys.miningKey.password = password;
if (keysIterator == keysCount) addressesGeneratedCallBack(keys, address); if (keysIterator == keysCount) cb(keys);
}); });
generateAddress(function(_payoutKeyObject, password) { generateAddress(function(_payoutKeyObject, password) {
keysIterator++; keysIterator++;
@ -161,7 +185,7 @@ function startDapp(web3, isOraclesNetwork) {
keys.payoutKey.payoutKeyObject = _payoutKeyObject; keys.payoutKey.payoutKeyObject = _payoutKeyObject;
keys.payoutKey.password = password; keys.payoutKey.password = password;
if (keysIterator == keysCount) addressesGeneratedCallBack(keys, address); if (keysIterator == keysCount) cb(keys);
}); });
generateAddress(function(_votingKeyObject, password) { generateAddress(function(_votingKeyObject, password) {
keysIterator++; keysIterator++;
@ -170,11 +194,12 @@ function startDapp(web3, isOraclesNetwork) {
keys.votingKey.votingKeyObject = _votingKeyObject; keys.votingKey.votingKeyObject = _votingKeyObject;
keys.votingKey.password = password; keys.votingKey.password = password;
if (keysIterator == keysCount) addressesGeneratedCallBack(keys, address); if (keysIterator == keysCount) cb(keys);
}); });
} }
function addressesGeneratedCallBack(keys, address) { //Geeneration of all 3 addresses callback
function addressesGeneratedCallBack(config, keys, address, cb) {
var validatorViewObj = { var validatorViewObj = {
miningKey: "0x" + keys.miningKey.miningKeyObject.address, miningKey: "0x" + keys.miningKey.miningKeyObject.address,
fullName: $("#full-name").val(), fullName: $("#full-name").val(),
@ -184,6 +209,7 @@ function startDapp(web3, isOraclesNetwork) {
licenseID: $("#license-id").val(), licenseID: $("#license-id").val(),
licenseExpiredAt: new Date($("#license-expiration").val()).getTime() / 1000, licenseExpiredAt: new Date($("#license-expiration").val()).getTime() / 1000,
}; };
//adds notary personal data to contract
addValidator(web3, addValidator(web3,
"addValidator(address,uint256,uint256,uint256,string,string,string)", "addValidator(address,uint256,uint256,uint256,string,string,string)",
validatorViewObj, validatorViewObj,
@ -197,6 +223,7 @@ function startDapp(web3, isOraclesNetwork) {
return; return;
} }
//activate generated production keys
createKeys(web3, createKeys(web3,
"createKeys(address,address,address)", "createKeys(address,address,address)",
keys, keys,
@ -210,21 +237,39 @@ function startDapp(web3, isOraclesNetwork) {
return; return;
} }
addressesAddedToContractCallBack(address, err); cb(err, address);
} }
); );
} }
); );
} }
function addressesAddedToContractCallBack(address, error) { //Production keys addition to contract callback
if (error) { function addressesAddedToContractCallBack(err, address, keys) {
if (err) {
$(".loading-container").hide(); $(".loading-container").hide();
swal("Error", error.message, "error"); swal("Error", err.message, "error");
return; return;
} }
//send ether to payoutKey //chain:sends ether to payoutKey
var to = "0x" + keys.payoutKey.payoutKeyObject.address;
//gets balance of initial key
getBalance(address, function(balance) {
//gets gas price
getGasPrice(function(gasPrice) {
//estimates gas
estimateGasForTx(address, to, balance, function(estimatedGas) {
//calculates how many coins we can send from initial key to payout key
calculateAmmountToSend(estimatedGas, gasPrice, balance, function(ammountToSend) {
transferCoinsToPayoutKey(estimatedGas, gasPrice, address, to, ammountToSend);
});
});
});
});
}
function getBalance(address, cb) {
web3.eth.getBalance(address, function(err, balance) { web3.eth.getBalance(address, function(err, balance) {
if (err) { if (err) {
console.log(err); console.log(err);
@ -232,16 +277,24 @@ function startDapp(web3, isOraclesNetwork) {
return; return;
} }
var to = "0x" + keys.payoutKey.payoutKeyObject.address; cb(balance);
});
}
function getGasPrice(cb) {
web3.eth.getGasPrice(function(err, gasPriceObj) { web3.eth.getGasPrice(function(err, gasPriceObj) {
if (err) { if (err) {
console.log(err); console.log(err);
$(".loading-container").hide(); $(".loading-container").hide();
return; return;
} }
var gasPrice = gasPriceObj.c[0]; var gasPrice = gasPriceObj.c[0];
cb(gasPrice);
});
}
function estimateGasForTx(address, to, balance, cb) {
estimateGas(web3, address, to, null, parseInt(balance/2), function(estimatedGas, err) { estimateGas(web3, address, to, null, parseInt(balance/2), function(estimatedGas, err) {
if (err) { if (err) {
console.log(err); console.log(err);
@ -249,9 +302,22 @@ function startDapp(web3, isOraclesNetwork) {
return; return;
} }
cb(estimatedGas);
});
}
function calculateAmmountToSend(estimatedGas, gasPrice, balance, cb) {
var ammountToSend = balance - 20 * estimatedGas * gasPrice; var ammountToSend = balance - 20 * estimatedGas * gasPrice;
console.log("ammountToSend: " + ammountToSend); console.log("ammountToSend: " + ammountToSend);
web3.eth.sendTransaction({gas: estimatedGas, from: address, to: to, value: ammountToSend}, function(err, txHash) { cb(ammountToSend);
}
function transferCoinsToPayoutKey(estimatedGas, gasPrice, address, to, ammountToSend) {
web3.eth.sendTransaction({
"gas": estimatedGas,
"from": address,
"to": to,
"value": ammountToSend}, function(err, txHash) {
if (err) { if (err) {
console.log(err); console.log(err);
$(".loading-container").hide(); $(".loading-container").hide();
@ -282,13 +348,8 @@ function startDapp(web3, isOraclesNetwork) {
}); });
}); });
}); });
});
});
});
} }
}); });
});
});
} }
window.addEventListener('load', function() { window.addEventListener('load', function() {

View File

@ -77,7 +77,7 @@
</div> </div>
</footer> </footer>
<script src="./assets/javascripts/sweetalert2.min.js" type="text/javascript"></script> <script src="./assets/javascripts/sweetalert2.min.js" type="text/javascript"></script>
<script src="./assets/javascripts/application.js?v=1.22" type="text/javascript"></script> <script src="./assets/javascripts/application.js?v=1.23" type="text/javascript"></script>
<script src="./assets/javascripts/keythereum.min.js" type="text/javascript"></script> <script src="./assets/javascripts/keythereum.min.js" type="text/javascript"></script>
</body> </body>
</html> </html>