Skip to content

Commit 3d1db2b

Browse files
committed
Merge branch 'fund_accounts' of github.com:embark-framework/embark into fund_accounts
2 parents 91f1748 + 7e12c5a commit 3d1db2b

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

lib/cmds/blockchain/dev_funds.js

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const async = require('async');
22
const Web3 = require('web3');
3-
const {getWeiBalanceFromString, buildUrl} = require('../../utils/utils.js');
4-
const {readFileSync, dappPath} = require('../../core/fs');
3+
const { getWeiBalanceFromString, buildUrl } = require('../../utils/utils.js');
4+
const { readFileSync, dappPath } = require('../../core/fs');
55

66
class DevFunds {
77
constructor(blockchainConfig) {
@@ -12,60 +12,81 @@ class DevFunds {
1212
this.password = readFileSync(dappPath('config/development/password'), 'utf8').replace('\n', '');
1313
this.web3 = new Web3();
1414
this.balance = Web3.utils.toWei("1", "ether");
15-
if(this.blockchainConfig.account.balance){
15+
if (this.blockchainConfig.account.balance) {
1616
console.dir('[blockchain/dev_funds]: converting balance from ' + this.blockchainConfig.account.balance);
1717
this.balance = getWeiBalanceFromString(this.blockchainConfig.account.balance, this.web3);
1818
console.dir('[blockchain/dev_funds]: converted balance to ' + this.balance);
1919
}
2020
}
2121

2222
connectToNode(cb) {
23-
24-
this.web3.setProvider(new Web3.providers.WebsocketProvider(buildUrl('ws', this.blockchainConfig.wsHost, this.blockchainConfig.wsPort), {headers: {Origin: "http://localhost:8000"}}));
25-
23+
24+
this.web3.setProvider(new Web3.providers.WebsocketProvider(buildUrl('ws', this.blockchainConfig.wsHost, this.blockchainConfig.wsPort), { headers: { Origin: "http://localhost:8000" } }));
25+
2626
this.web3.eth.getAccounts().then((accounts) => {
2727
this.web3.eth.defaultAccount = accounts[0];
28-
this.accounts = accounts;
28+
if (accounts.length > 1) {
29+
this.accounts = accounts.slice(1);
30+
}
31+
console.dir('----- CURRENT ACCOUNTS ' + this.accounts);
2932
cb();
3033
});
3134
}
3235

3336
createAccounts(numAccounts, password, cb) {
34-
console.dir("creating " + (numAccounts - this.accounts.length) + " new accounts with password " + password);
35-
async.timesLimit((numAccounts - this.accounts.length), 1, (_, next) => {
37+
const numAccountsToCreate = numAccounts - (this.accounts.length + 1);
38+
if (numAccountsToCreate === 0) return cb();
39+
40+
console.dir("creating " + numAccountsToCreate + " new accounts with password " + password);
41+
async.timesLimit(numAccountsToCreate, 1, (_, next) => {
3642
console.dir("--- creating new account");
3743
this.web3.eth.personal.newAccount(password, next);
3844
}, (err, accounts) => {
39-
if(err) console.error(err);
45+
if (err) console.error(err);
4046
console.dir("-- accounts created are ");
4147
console.dir(accounts);
4248
this.accounts = accounts;
4349
cb(err);
44-
});
50+
});
4551
}
4652

4753
unlockAccounts(password, cb) {
54+
console.dir('--- CURRENT ACCOUNTS ' + this.accounts);
4855
async.each(this.accounts, (account, next) => {
4956
console.dir('-- unlocking account ' + account + ' with password ' + password);
50-
this.web3.eth.personal.unlockAccount(account, password).then(() => next()).catch(next);
51-
}, cb);
57+
this.web3.eth.personal.unlockAccount(account, password).then((result) => {
58+
console.dir('-- unlocked account ' + account + ' with password ' + password + ' and result ' + result);
59+
next();
60+
}).catch(next);
61+
}, (err) => {
62+
console.dir('-- FINISHED UNLOCKING ACCOUNTS, err= ' + err);
63+
cb(err);
64+
});
5265
}
5366

5467
fundAccounts(balance, cb) {
5568
console.dir('-- funding accounts...');
56-
57-
69+
70+
5871

5972
async.each(this.accounts, (account, next) => {
60-
console.dir("-- funding account " + account + " with balance " + balance);
61-
this.web3.eth.sendTransaction({to: account, value: balance}).then((result) => {
62-
console.dir('FUNDING ACCT result: ' + JSON.stringify(result));
63-
next();
64-
}).catch(next);
65-
}, (err) => {
66-
console.dir('-- FINISHED FUNDING ACCOUNTS, err= ' + err);
67-
cb(err);
73+
this.web3.eth.getBalance(account).then(currBalance => {
74+
const remainingBalance = balance - currBalance;
75+
console.dir("---- account " + account + " balance needed = " + remainingBalance);
76+
if (remainingBalance <= 0) return next();
77+
78+
console.dir("-- funding account " + account + " with balance " + remainingBalance);
79+
this.web3.eth.sendTransaction({to: account, value: remainingBalance}).then((result) => {
80+
console.dir('FUNDING ACCT result: ' + JSON.stringify(result));
81+
next();
82+
}).catch(next);
83+
}, (err) => {
84+
console.dir('-- FINISHED FUNDING ACCOUNTS, err= ' + err);
85+
cb(err);
86+
});
6887
});
88+
89+
6990
}
7091

7192
createFundAndUnlockAccounts(cb) {
@@ -88,7 +109,7 @@ class DevFunds {
88109
}
89110
], (err) => {
90111
console.dir(`--- COMPLETED THE ACCOUNTS (${this.accounts.join(', ')} and funded with ${this.balance} wei)`);
91-
if(err) console.error('Error creating, unlocking, and funding accounts', err);
112+
if (err) console.error('Error creating, unlocking, and funding accounts', JSON.stringify(err));
92113

93114
// this.web3.eth.getAccounts().then((accounts) => {
94115
// let numAccts = accounts.length;

0 commit comments

Comments
 (0)