Skip to content

Commit f221397

Browse files
committed
first stab at funding accounts in geth given config
1 parent 4467874 commit f221397

File tree

5 files changed

+98
-4
lines changed

5 files changed

+98
-4
lines changed

lib/cmds/blockchain/blockchain.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ const fs = require('../../core/fs.js');
66
const constants = require('../../constants.json');
77

88
const GethCommands = require('./geth_commands.js');
9+
const DevFunds = require('./dev_funds.js');
910

1011
/*eslint complexity: ["error", 36]*/
1112
var Blockchain = function(options) {
1213
this.blockchainConfig = options.blockchainConfig;
1314
this.env = options.env || 'development';
1415
this.client = options.client;
1516
this.isDev = options.isDev;
16-
this.onReadyCallback = options.onReadyCallback;
17+
this.onReadyCallback = options.onReadyCallback || (() => {});
1718

1819
if ((this.blockchainConfig === {} || JSON.stringify(this.blockchainConfig) === '{"enabled":true}') && this.env !== 'development') {
1920
console.log("===> " + __("warning: running default config on a non-development environment"));
@@ -41,7 +42,7 @@ var Blockchain = function(options) {
4142
wsHost: this.blockchainConfig.wsHost || 'localhost',
4243
wsPort: this.blockchainConfig.wsPort || 8546,
4344
wsOrigins: this.blockchainConfig.wsOrigins || false,
44-
wsApi: (this.blockchainConfig.wsApi || ['eth', 'web3', 'net', 'shh', 'debug']),
45+
wsApi: (this.blockchainConfig.wsApi || ['eth', 'web3', 'net', 'shh', 'debug', 'personal']),
4546
vmdebug: this.blockchainConfig.vmdebug || false,
4647
targetGasLimit: this.blockchainConfig.targetGasLimit || false,
4748
syncMode: this.blockchainConfig.syncMode,
@@ -162,6 +163,12 @@ Blockchain.prototype.run = function() {
162163
self.child.stderr.on('data', (data) => {
163164
data = data.toString();
164165
if (self.onReadyCallback && !self.readyCalled && data.indexOf('WebSocket endpoint opened') > -1) {
166+
if (self.isDev) {
167+
return self.createFundAndUnlockAccounts(() => {
168+
self.readyCalled = true;
169+
self.onReadyCallback();
170+
});
171+
}
165172
self.readyCalled = true;
166173
self.onReadyCallback();
167174
}
@@ -175,6 +182,16 @@ Blockchain.prototype.run = function() {
175182
});
176183
};
177184

185+
Blockchain.prototype.createFundAndUnlockAccounts = function(cb) {
186+
console.dir('createFundAndUnlockAccounts');
187+
let devFunds = new DevFunds(this.config);
188+
devFunds.createFundAndUnlockAccounts(() => {
189+
console.dir("=====> done!");
190+
console.dir(arguments);
191+
cb();
192+
});
193+
}
194+
178195
Blockchain.prototype.kill = function() {
179196
if (this.child) {
180197
this.child.kill();

lib/cmds/blockchain/dev_funds.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const async = require('async');
2+
var Web3 = require('web3');
3+
var utils = require('../../utils/utils.js');
4+
5+
class DevFunds {
6+
constructor(blockchainConfig) {
7+
this.web3 = null;
8+
this.blockchainConfig = blockchainConfig;
9+
this.accounts = [];
10+
this.numAccounts = this.blockchainConfig.account.numAccounts || 0;
11+
//this.balance = this.blockchainConfig.account.balance || Web3.utils.toWei("1", "ether");
12+
this.balance = Web3.utils.toWei("1", "ether");
13+
this.password = "dev_password"
14+
}
15+
16+
connectToNode(cb) {
17+
const self = this;
18+
this.web3 = new Web3();
19+
this.web3.setProvider(new Web3.providers.WebsocketProvider(utils.buildUrl('ws', this.blockchainConfig.wsHost, this.blockchainConfig.wsPort), {headers: {Origin: "http://localhost:8000"}}));
20+
this.web3.eth.getAccounts().then((accounts) => {
21+
self.web3.eth.defaultAccount = accounts[0];
22+
cb();
23+
});
24+
}
25+
26+
createAccounts(numAccounts, password, cb) {
27+
console.dir("creating " + numAccounts + " with password " + password);
28+
const self = this;
29+
async.timesLimit(numAccounts, 1, (_, next) => {
30+
console.dir("--- creating new account");
31+
self.web3.eth.personal.newAccount(password, next);
32+
}, (err, accounts) => {
33+
console.dir("-- accounts created are ");
34+
console.dir(accounts);
35+
this.accounts = accounts;
36+
cb(err);
37+
});
38+
}
39+
40+
unlockAccounts(password, cb) {
41+
const self = this;
42+
async.each(this.accounts, (account, next) => {
43+
self.web3.eth.personal.unlockAccount(account, password).then(() => { next() });
44+
}, cb);
45+
}
46+
47+
fundAccounts(balance, cb) {
48+
const self = this;
49+
async.each(this.accounts, (account, next) => {
50+
self.web3.eth.sendTransaction({to: account, value: balance}).then(() => {
51+
next();
52+
});
53+
}, cb);
54+
}
55+
56+
createFundAndUnlockAccounts(cb) {
57+
const self = this;
58+
async.waterfall([
59+
function connect(next) {
60+
self.connectToNode(next);
61+
},
62+
function create(next) {
63+
self.createAccounts(self.numAccounts, self.password, next)
64+
},
65+
function unlock(next) {
66+
self.unlockAccounts(self.password, next);
67+
},
68+
function fund(next) {
69+
self.fundAccounts(self.balance, next);
70+
},
71+
], cb);
72+
}
73+
}
74+
75+
module.exports = DevFunds;

lib/cmds/blockchain/geth_commands.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class GethCommands {
123123
let self = this;
124124
let config = this.config;
125125
let rpc_api = (this.config.rpcApi || ['eth', 'web3', 'net', 'debug']);
126-
let ws_api = (this.config.wsApi || ['eth', 'web3', 'net', 'debug']);
126+
let ws_api = (this.config.wsApi || ['eth', 'web3', 'net', 'debug', 'personal']);
127127

128128
let args = [];
129129

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Embark {
4545

4646
blockchain(env, client) {
4747
this.context = [constants.contexts.blockchain];
48-
return require('./cmds/blockchain/blockchain.js')(this.config.blockchainConfig, client, env, this.isDev(env)).run();
48+
return require('./cmds/blockchain/blockchain.js')(this.config.blockchainConfig, client, env, this.isDev(env), () => {}).run();
4949
}
5050

5151
simulator(options) {

test_apps/test_app/config/blockchain.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"rpcPort": 8545,
1414
"rpcCorsDomain": "auto",
1515
"account": {
16+
"numAccounts": 3,
17+
"balance": "5 ether",
1618
"password": "config/development/password"
1719
},
1820
"targetGasLimit": 8000000,

0 commit comments

Comments
 (0)