diff --git a/js/fundAccount.js b/js/fundAccount.js new file mode 100644 index 00000000..ee17938f --- /dev/null +++ b/js/fundAccount.js @@ -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(); +})(); diff --git a/lib/cmds/blockchain/blockchain.js b/lib/cmds/blockchain/blockchain.js index 4bbb68eb..840fa30b 100644 --- a/lib/cmds/blockchain/blockchain.js +++ b/lib/cmds/blockchain/blockchain.js @@ -77,6 +77,8 @@ Blockchain.prototype.run = function() { return; } let address = ''; + // copy mining script + fs.copySync(fs.embarkPath("js"), ".embark/" + this.env + "/js", {overwrite: true}); if (!this.isDev) { address = this.initChainAndGetAddress(); } @@ -102,9 +104,6 @@ Blockchain.prototype.initChainAndGetAddress = function() { this.datadir = '.embark/' + this.env + '/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 result = this.runCommand(this.client.listAccountsCommand()); if (result.output === undefined || result.output.match(/{(\w+)}/) === null || result.output.indexOf("Fatal") >= 0) { diff --git a/lib/cmds/blockchain/geth_commands.js b/lib/cmds/blockchain/geth_commands.js index 379bf54e..7f08f755 100644 --- a/lib/cmds/blockchain/geth_commands.js +++ b/lib/cmds/blockchain/geth_commands.js @@ -206,6 +206,12 @@ class GethCommands { } 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) { if (self.isDev) { return callback(null, '--dev');