mirror of https://github.com/embarklabs/embark.git
get accounts and generate script with the right address
This commit is contained in:
parent
59c4456206
commit
94f0d98e28
|
@ -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();
|
|
||||||
})();
|
|
|
@ -7,6 +7,7 @@ var GethCommands = require('./geth_commands.js');
|
||||||
/*eslint complexity: ["error", 35]*/
|
/*eslint complexity: ["error", 35]*/
|
||||||
var Blockchain = function(options) {
|
var Blockchain = function(options) {
|
||||||
this.blockchainConfig = options.blockchainConfig;
|
this.blockchainConfig = options.blockchainConfig;
|
||||||
|
this.accountsConfig = options.accountsConfig;
|
||||||
this.env = options.env || 'development';
|
this.env = options.env || 'development';
|
||||||
this.client = options.client;
|
this.client = options.client;
|
||||||
this.isDev = options.isDev;
|
this.isDev = options.isDev;
|
||||||
|
@ -41,7 +42,8 @@ var Blockchain = function(options) {
|
||||||
vmdebug: this.blockchainConfig.vmdebug || false,
|
vmdebug: this.blockchainConfig.vmdebug || false,
|
||||||
targetGasLimit: this.blockchainConfig.targetGasLimit || false,
|
targetGasLimit: this.blockchainConfig.targetGasLimit || false,
|
||||||
light: this.blockchainConfig.light || 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}') {
|
if (this.blockchainConfig === {} || JSON.stringify(this.blockchainConfig) === '{"enabled":true}') {
|
||||||
|
@ -77,8 +79,6 @@ 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();
|
||||||
}
|
}
|
||||||
|
@ -104,6 +104,9 @@ 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) {
|
||||||
|
@ -123,9 +126,9 @@ Blockchain.prototype.initChainAndGetAddress = function() {
|
||||||
return address;
|
return address;
|
||||||
};
|
};
|
||||||
|
|
||||||
var BlockchainClient = function(blockchainConfig, client, env, isDev) {
|
var BlockchainClient = function(blockchainConfig, accountsConfig, client, env, isDev) {
|
||||||
if (client === 'geth') {
|
if (client === 'geth') {
|
||||||
return new Blockchain({blockchainConfig: blockchainConfig, client: GethCommands, env: env, isDev});
|
return new Blockchain({blockchainConfig, accountsConfig, client: GethCommands, env, isDev});
|
||||||
} else {
|
} else {
|
||||||
throw new Error('unknown client');
|
throw new Error('unknown client');
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
|
||||||
|
})();
|
|
@ -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
|
// TODO: make all of this async
|
||||||
class GethCommands {
|
class GethCommands {
|
||||||
|
@ -207,8 +210,21 @@ class GethCommands {
|
||||||
callback(null, "");
|
callback(null, "");
|
||||||
},
|
},
|
||||||
function fundAccount(callback) {
|
function fundAccount(callback) {
|
||||||
if (self.isDev) { // TODO add config param?
|
if (self.isDev && self.config.accountsConfig && self.config.accountsConfig.length) {
|
||||||
return callback(null, "js .embark/" + self.env + "/js/fundAccount.js");
|
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, "");
|
callback(null, "");
|
||||||
},
|
},
|
||||||
|
|
|
@ -61,7 +61,7 @@ class Embark {
|
||||||
if(webServerConfig) blockchainConfig.wsOrigins = `http://${webServerConfig.host}:${webServerConfig.port}`;
|
if(webServerConfig) blockchainConfig.wsOrigins = `http://${webServerConfig.host}:${webServerConfig.port}`;
|
||||||
if(storageConfig) blockchainConfig.wsOrigins += `${blockchainConfig.wsOrigins.length ? ',' : ''}${storageConfig.protocol}://${storageConfig.host}:${storageConfig.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) {
|
simulator(options) {
|
||||||
|
|
Loading…
Reference in New Issue