add fundAccount script that keeps the user's account funded

This commit is contained in:
Jonathan Rainville 2018-05-17 13:12:54 -04:00
parent c6593f6168
commit 668fd3a064
3 changed files with 60 additions and 3 deletions

52
js/fundAccount.js Normal file
View File

@ -0,0 +1,52 @@
/*global web3, eth*/
(function () {
var workAccount = '0xDf18Cb4F2005Bc52F94E9BD6C31f7B0C6394E2C2';
// Blockchain process ends if Javascript ends
function keepAlive() {
setInterval(function () {
// Do nothing
}, 999999);
}
var workAccountBalance = eth.getBalance(workAccount);
var TARGET = 15000000000000000000;
if (workAccountBalance >= TARGET) {
return keepAlive();
}
function getNonce() {
return web3.eth.getTransactionCount(eth.coinbase);
}
function getGasPrice() {
return web3.eth.getGasPrice();
}
function sendTransaction(nonce, gasPrice) {
web3.eth.sendTransaction({
from: eth.coinbase,
to: workAccount,
value: TARGET - workAccountBalance,
gasPrice: gasPrice,
nonce: nonce
}, function (err, _result) {
if (err) {
console.error('Error while transferring funds to user account', err);
}
});
}
try {
var nonce = getNonce();
var gasPrice = getGasPrice();
sendTransaction(nonce, gasPrice);
} catch (e) {
console.error('Error while getting nonce or gas price', e);
}
keepAlive();
})();

View File

@ -77,6 +77,8 @@ Blockchain.prototype.run = function() {
return; return;
} }
let address = ''; let address = '';
// copy mining script
fs.copySync(fs.embarkPath("js"), ".embark/" + this.env + "/js", {overwrite: true});
if (!this.isDev) { if (!this.isDev) {
address = this.initChainAndGetAddress(); address = this.initChainAndGetAddress();
} }
@ -102,9 +104,6 @@ Blockchain.prototype.initChainAndGetAddress = function() {
this.datadir = '.embark/' + this.env + '/datadir'; this.datadir = '.embark/' + this.env + '/datadir';
fs.mkdirpSync(this.datadir); fs.mkdirpSync(this.datadir);
// copy mining script
fs.copySync(fs.embarkPath("js"), ".embark/" + this.env + "/js", {overwrite: true});
// check if an account already exists, create one if not, return address // check if an account already exists, create one if not, return address
result = this.runCommand(this.client.listAccountsCommand()); result = this.runCommand(this.client.listAccountsCommand());
if (result.output === undefined || result.output.match(/{(\w+)}/) === null || result.output.indexOf("Fatal") >= 0) { if (result.output === undefined || result.output.match(/{(\w+)}/) === null || result.output.indexOf("Fatal") >= 0) {

View File

@ -206,6 +206,12 @@ class GethCommands {
} }
callback(null, ""); callback(null, "");
}, },
function fundAccount(callback) {
if (self.isDev) { // TODO add config param?
return callback(null, "js .embark/" + self.env + "/js/fundAccount.js");
}
callback(null, "");
},
function isDev(callback) { function isDev(callback) {
if (self.isDev) { if (self.isDev) {
return callback(null, '--dev'); return callback(null, '--dev');