get accounts and generate script with the right address

This commit is contained in:
Jonathan Rainville 2018-05-17 15:32:57 -04:00
parent 59c4456206
commit 94f0d98e28
5 changed files with 81 additions and 61 deletions

View File

@ -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();
})();

View File

@ -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');
}

View File

@ -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();
})();

View File

@ -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, "");
},

View File

@ -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) {