From 94f0d98e28d9127d8c51f3177c9f5ae9f846a77f Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 17 May 2018 15:32:57 -0400 Subject: [PATCH] get accounts and generate script with the right address --- js/fundAccount.js | 52 -------------------------- lib/cmds/blockchain/blockchain.js | 13 ++++--- lib/cmds/blockchain/fundAccout.js.ejs | 53 +++++++++++++++++++++++++++ lib/cmds/blockchain/geth_commands.js | 22 +++++++++-- lib/index.js | 2 +- 5 files changed, 81 insertions(+), 61 deletions(-) delete mode 100644 js/fundAccount.js create mode 100644 lib/cmds/blockchain/fundAccout.js.ejs diff --git a/js/fundAccount.js b/js/fundAccount.js deleted file mode 100644 index ee17938f..00000000 --- a/js/fundAccount.js +++ /dev/null @@ -1,52 +0,0 @@ -/*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 840fa30b..81785d1b 100644 --- a/lib/cmds/blockchain/blockchain.js +++ b/lib/cmds/blockchain/blockchain.js @@ -7,6 +7,7 @@ var GethCommands = require('./geth_commands.js'); /*eslint complexity: ["error", 35]*/ var Blockchain = function(options) { this.blockchainConfig = options.blockchainConfig; + this.accountsConfig = options.accountsConfig; this.env = options.env || 'development'; this.client = options.client; this.isDev = options.isDev; @@ -41,7 +42,8 @@ var Blockchain = function(options) { vmdebug: this.blockchainConfig.vmdebug || false, targetGasLimit: this.blockchainConfig.targetGasLimit || false, light: this.blockchainConfig.light || false, - fast: this.blockchainConfig.fast || false + fast: this.blockchainConfig.fast || false, + accountsConfig: this.accountsConfig }; if (this.blockchainConfig === {} || JSON.stringify(this.blockchainConfig) === '{"enabled":true}') { @@ -77,8 +79,6 @@ 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(); } @@ -104,6 +104,9 @@ 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) { @@ -123,9 +126,9 @@ Blockchain.prototype.initChainAndGetAddress = function() { return address; }; -var BlockchainClient = function(blockchainConfig, client, env, isDev) { +var BlockchainClient = function(blockchainConfig, accountsConfig, client, env, isDev) { if (client === 'geth') { - return new Blockchain({blockchainConfig: blockchainConfig, client: GethCommands, env: env, isDev}); + return new Blockchain({blockchainConfig, accountsConfig, client: GethCommands, env, isDev}); } else { throw new Error('unknown client'); } diff --git a/lib/cmds/blockchain/fundAccout.js.ejs b/lib/cmds/blockchain/fundAccout.js.ejs new file mode 100644 index 00000000..8a979722 --- /dev/null +++ b/lib/cmds/blockchain/fundAccout.js.ejs @@ -0,0 +1,53 @@ +/*global web3, eth*/ + +(function () { + +var workAccount = '<%- accountAddress %>'; + +// Blockchain process ends if Javascript ends +function keepAlive() { + setInterval(function () { + // Just stay alive + }, 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/geth_commands.js b/lib/cmds/blockchain/geth_commands.js index 7f08f755..c2bd8e70 100644 --- a/lib/cmds/blockchain/geth_commands.js +++ b/lib/cmds/blockchain/geth_commands.js @@ -1,4 +1,7 @@ -let async = require('async'); +const async = require('async'); +const Web3 = require('web3'); +const AccountParser = require('../../contracts/accountParser'); +const fs = require('../../core/fs'); // TODO: make all of this async class GethCommands { @@ -207,8 +210,21 @@ class GethCommands { callback(null, ""); }, function fundAccount(callback) { - if (self.isDev) { // TODO add config param? - return callback(null, "js .embark/" + self.env + "/js/fundAccount.js"); + if (self.isDev && self.config.accountsConfig && self.config.accountsConfig.length) { + const accounts = AccountParser.parseAccountsConfig(self.config.accountsConfig, new Web3()); + if (accounts.length) { + const fundAccountTemplate = require('./fundAccout.js.ejs'); + const code = fundAccountTemplate({accountAddress: accounts[0].address}); + const filePath = `.embark/${self.env}/js/fundAccount.js`; + fs.writeFile(fs.dappPath(filePath), code, (err) => { + if (err) { + console.error('Failed to created the script to fund the account'); + return callback(null, ''); + } + callback(null, filePath); + }); + return; + } } callback(null, ""); }, diff --git a/lib/index.js b/lib/index.js index 00dcb5f4..6a2e7d2e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -61,7 +61,7 @@ class Embark { if(webServerConfig) blockchainConfig.wsOrigins = `http://${webServerConfig.host}:${webServerConfig.port}`; if(storageConfig) blockchainConfig.wsOrigins += `${blockchainConfig.wsOrigins.length ? ',' : ''}${storageConfig.protocol}://${storageConfig.host}:${storageConfig.port}`; } - return require('./cmds/blockchain/blockchain.js')(blockchainConfig, client, env, this.isDev(env)).run(); + return require('./cmds/blockchain/blockchain.js')(blockchainConfig, this.config.contractsConfig.deployment.accounts, client, env, this.isDev(env)).run(); } simulator(options) {