Refactoring, functions comments
This commit is contained in:
parent
de7f7aa6c0
commit
b67b42362f
File diff suppressed because one or more lines are too long
|
@ -8,6 +8,10 @@ function checkInitialKey(web3, func, initialKey, contractAddr, cb) {
|
|||
var data = funcEncodePart
|
||||
+ toUnifiedLengthLeft(initialKey);
|
||||
|
||||
console.log(data);
|
||||
console.log("0x" + initialKey);
|
||||
console.log(contractAddr);
|
||||
|
||||
call(web3, "0x" + initialKey, contractAddr, data, function(respHex) {
|
||||
console.log(respHex);
|
||||
cb(parseInt(respHex, 16));
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//gets web3 object from MetaMask or Parity
|
||||
function getWeb3(callback) {
|
||||
if (typeof window.web3 === 'undefined') {
|
||||
// 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) {
|
||||
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) {
|
||||
|
@ -55,6 +57,7 @@ function checkNetworkVersion(web3, cb) {
|
|||
})
|
||||
}
|
||||
|
||||
//launches main application
|
||||
function startDapp(web3, isOraclesNetwork) {
|
||||
$(function() {
|
||||
$(".loading-container").hide();
|
||||
|
@ -64,14 +67,30 @@ function startDapp(web3, isOraclesNetwork) {
|
|||
"payoutKey": {},
|
||||
"votingKey": {}
|
||||
};
|
||||
var config;
|
||||
|
||||
web3.eth.getAccounts(function(error, accounts) {
|
||||
console.log(accounts);
|
||||
//get current account chosen in MetaMask or opened at Parity
|
||||
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) {
|
||||
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) {
|
||||
var possibleInitialKey = accounts[0].substr(2);
|
||||
checkInitialKey(web3,
|
||||
|
@ -90,12 +109,22 @@ function startDapp(web3, isOraclesNetwork) {
|
|||
$("#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() {
|
||||
$(this).remove();
|
||||
//triggers, if initial key is chosen
|
||||
function initialKeyChosen(el, config, cb) {
|
||||
$(el).remove();
|
||||
$("<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();
|
||||
reader.readAsText(file, "UTF-8");
|
||||
reader.onload = function (evt) {
|
||||
|
@ -122,12 +151,7 @@ function startDapp(web3, isOraclesNetwork) {
|
|||
$(".loading-container").show();
|
||||
|
||||
setTimeout(function() {
|
||||
if (!config) {
|
||||
$.getJSON("./assets/javascripts/config.json", function(_config) {
|
||||
config = _config;
|
||||
configLoadedCallBack(web3, config, address);
|
||||
});
|
||||
} else configLoadedCallBack(web3, config, address);
|
||||
cb(address);
|
||||
}, 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;
|
||||
for (var i in keys) {
|
||||
keysCount++;
|
||||
|
@ -152,7 +176,7 @@ function startDapp(web3, isOraclesNetwork) {
|
|||
keys.miningKey.miningKeyObject = _miningKeyObject;
|
||||
keys.miningKey.password = password;
|
||||
|
||||
if (keysIterator == keysCount) addressesGeneratedCallBack(keys, address);
|
||||
if (keysIterator == keysCount) cb(keys);
|
||||
});
|
||||
generateAddress(function(_payoutKeyObject, password) {
|
||||
keysIterator++;
|
||||
|
@ -161,7 +185,7 @@ function startDapp(web3, isOraclesNetwork) {
|
|||
keys.payoutKey.payoutKeyObject = _payoutKeyObject;
|
||||
keys.payoutKey.password = password;
|
||||
|
||||
if (keysIterator == keysCount) addressesGeneratedCallBack(keys, address);
|
||||
if (keysIterator == keysCount) cb(keys);
|
||||
});
|
||||
generateAddress(function(_votingKeyObject, password) {
|
||||
keysIterator++;
|
||||
|
@ -170,11 +194,12 @@ function startDapp(web3, isOraclesNetwork) {
|
|||
keys.votingKey.votingKeyObject = _votingKeyObject;
|
||||
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 = {
|
||||
miningKey: "0x" + keys.miningKey.miningKeyObject.address,
|
||||
fullName: $("#full-name").val(),
|
||||
|
@ -184,6 +209,7 @@ function startDapp(web3, isOraclesNetwork) {
|
|||
licenseID: $("#license-id").val(),
|
||||
licenseExpiredAt: new Date($("#license-expiration").val()).getTime() / 1000,
|
||||
};
|
||||
//adds notary personal data to contract
|
||||
addValidator(web3,
|
||||
"addValidator(address,uint256,uint256,uint256,string,string,string)",
|
||||
validatorViewObj,
|
||||
|
@ -197,6 +223,7 @@ function startDapp(web3, isOraclesNetwork) {
|
|||
return;
|
||||
}
|
||||
|
||||
//activate generated production keys
|
||||
createKeys(web3,
|
||||
"createKeys(address,address,address)",
|
||||
keys,
|
||||
|
@ -210,38 +237,64 @@ function startDapp(web3, isOraclesNetwork) {
|
|||
return;
|
||||
}
|
||||
|
||||
addressesAddedToContractCallBack(address, err);
|
||||
cb(err, address);
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function addressesAddedToContractCallBack(address, error) {
|
||||
if (error) {
|
||||
//Production keys addition to contract callback
|
||||
function addressesAddedToContractCallBack(err, address, keys) {
|
||||
if (err) {
|
||||
$(".loading-container").hide();
|
||||
swal("Error", error.message, "error");
|
||||
swal("Error", err.message, "error");
|
||||
return;
|
||||
}
|
||||
|
||||
//send ether to payoutKey
|
||||
web3.eth.getBalance(address, function(err, balance){
|
||||
//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) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
$(".loading-container").hide();
|
||||
return;
|
||||
}
|
||||
|
||||
var to = "0x" + keys.payoutKey.payoutKeyObject.address;
|
||||
cb(balance);
|
||||
});
|
||||
}
|
||||
|
||||
function getGasPrice(cb) {
|
||||
web3.eth.getGasPrice(function(err, gasPriceObj) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
$(".loading-container").hide();
|
||||
return;
|
||||
}
|
||||
|
||||
var gasPrice = gasPriceObj.c[0];
|
||||
|
||||
cb(gasPrice);
|
||||
});
|
||||
}
|
||||
|
||||
function estimateGasForTx(address, to, balance, cb) {
|
||||
estimateGas(web3, address, to, null, parseInt(balance/2), function(estimatedGas, err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
|
@ -249,9 +302,22 @@ function startDapp(web3, isOraclesNetwork) {
|
|||
return;
|
||||
}
|
||||
|
||||
cb(estimatedGas);
|
||||
});
|
||||
}
|
||||
|
||||
function calculateAmmountToSend(estimatedGas, gasPrice, balance, cb) {
|
||||
var ammountToSend = balance - 20 * estimatedGas * gasPrice;
|
||||
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) {
|
||||
console.log(err);
|
||||
$(".loading-container").hide();
|
||||
|
@ -282,13 +348,8 @@ function startDapp(web3, isOraclesNetwork) {
|
|||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
window.addEventListener('load', function() {
|
||||
|
|
|
@ -77,7 +77,7 @@
|
|||
</div>
|
||||
</footer>
|
||||
<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>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in New Issue