funding dev account updates

Now funds accounts only if they have not been funded, and also funds with only the amount needed.

Also fixed bug with unlocking accounts when re-running `unlockAccounts` on already created accounts.
This commit is contained in:
emizzle 2018-07-17 10:57:31 +02:00
parent 296663edc7
commit 7e12c5a9f7
1 changed files with 45 additions and 24 deletions

View File

@ -1,7 +1,7 @@
const async = require('async'); const async = require('async');
const Web3 = require('web3'); const Web3 = require('web3');
const {getWeiBalanceFromString, buildUrl} = require('../../utils/utils.js'); const { getWeiBalanceFromString, buildUrl } = require('../../utils/utils.js');
const {readFileSync, dappPath} = require('../../core/fs'); const { readFileSync, dappPath } = require('../../core/fs');
class DevFunds { class DevFunds {
constructor(blockchainConfig) { constructor(blockchainConfig) {
@ -12,7 +12,7 @@ class DevFunds {
this.password = readFileSync(dappPath('config/development/password'), 'utf8').replace('\n', ''); this.password = readFileSync(dappPath('config/development/password'), 'utf8').replace('\n', '');
this.web3 = new Web3(); this.web3 = new Web3();
this.balance = Web3.utils.toWei("1", "ether"); this.balance = Web3.utils.toWei("1", "ether");
if(this.blockchainConfig.account.balance){ if (this.blockchainConfig.account.balance) {
console.dir('[blockchain/dev_funds]: converting balance from ' + this.blockchainConfig.account.balance); console.dir('[blockchain/dev_funds]: converting balance from ' + this.blockchainConfig.account.balance);
this.balance = getWeiBalanceFromString(this.blockchainConfig.account.balance, this.web3); this.balance = getWeiBalanceFromString(this.blockchainConfig.account.balance, this.web3);
console.dir('[blockchain/dev_funds]: converted balance to ' + this.balance); console.dir('[blockchain/dev_funds]: converted balance to ' + this.balance);
@ -20,52 +20,73 @@ class DevFunds {
} }
connectToNode(cb) { connectToNode(cb) {
this.web3.setProvider(new Web3.providers.WebsocketProvider(buildUrl('ws', this.blockchainConfig.wsHost, this.blockchainConfig.wsPort), {headers: {Origin: "http://localhost:8000"}})); this.web3.setProvider(new Web3.providers.WebsocketProvider(buildUrl('ws', this.blockchainConfig.wsHost, this.blockchainConfig.wsPort), { headers: { Origin: "http://localhost:8000" } }));
this.web3.eth.getAccounts().then((accounts) => { this.web3.eth.getAccounts().then((accounts) => {
this.web3.eth.defaultAccount = accounts[0]; this.web3.eth.defaultAccount = accounts[0];
this.accounts = accounts; if (accounts.length > 1) {
this.accounts = accounts.slice(1);
}
console.dir('----- CURRENT ACCOUNTS ' + this.accounts);
cb(); cb();
}); });
} }
createAccounts(numAccounts, password, cb) { createAccounts(numAccounts, password, cb) {
console.dir("creating " + (numAccounts - this.accounts.length) + " new accounts with password " + password); const numAccountsToCreate = numAccounts - (this.accounts.length + 1);
async.timesLimit((numAccounts - this.accounts.length), 1, (_, next) => { if (numAccountsToCreate === 0) return cb();
console.dir("creating " + numAccountsToCreate + " new accounts with password " + password);
async.timesLimit(numAccountsToCreate, 1, (_, next) => {
console.dir("--- creating new account"); console.dir("--- creating new account");
this.web3.eth.personal.newAccount(password, next); this.web3.eth.personal.newAccount(password, next);
}, (err, accounts) => { }, (err, accounts) => {
if(err) console.error(err); if (err) console.error(err);
console.dir("-- accounts created are "); console.dir("-- accounts created are ");
console.dir(accounts); console.dir(accounts);
this.accounts = accounts; this.accounts = accounts;
cb(err); cb(err);
}); });
} }
unlockAccounts(password, cb) { unlockAccounts(password, cb) {
console.dir('--- CURRENT ACCOUNTS ' + this.accounts);
async.each(this.accounts, (account, next) => { async.each(this.accounts, (account, next) => {
console.dir('-- unlocking account ' + account + ' with password ' + password); console.dir('-- unlocking account ' + account + ' with password ' + password);
this.web3.eth.personal.unlockAccount(account, password).then(() => next()).catch(next); this.web3.eth.personal.unlockAccount(account, password).then((result) => {
}, cb); console.dir('-- unlocked account ' + account + ' with password ' + password + ' and result ' + result);
next();
}).catch(next);
}, (err) => {
console.dir('-- FINISHED UNLOCKING ACCOUNTS, err= ' + err);
cb(err);
});
} }
fundAccounts(balance, cb) { fundAccounts(balance, cb) {
console.dir('-- funding accounts...'); console.dir('-- funding accounts...');
async.each(this.accounts, (account, next) => { async.each(this.accounts, (account, next) => {
console.dir("-- funding account " + account + " with balance " + balance); this.web3.eth.getBalance(account).then(currBalance => {
this.web3.eth.sendTransaction({to: account, value: balance}).then((result) => { const remainingBalance = balance - currBalance;
console.dir('FUNDING ACCT result: ' + JSON.stringify(result)); console.dir("---- account " + account + " balance needed = " + remainingBalance);
next(); if (remainingBalance <= 0) return next();
}).catch(next);
}, (err) => { console.dir("-- funding account " + account + " with balance " + remainingBalance);
console.dir('-- FINISHED FUNDING ACCOUNTS, err= ' + err); this.web3.eth.sendTransaction({to: account, value: remainingBalance}).then((result) => {
cb(err); console.dir('FUNDING ACCT result: ' + JSON.stringify(result));
next();
}).catch(next);
}, (err) => {
console.dir('-- FINISHED FUNDING ACCOUNTS, err= ' + err);
cb(err);
});
}); });
} }
createFundAndUnlockAccounts(cb) { createFundAndUnlockAccounts(cb) {
@ -88,7 +109,7 @@ class DevFunds {
} }
], (err) => { ], (err) => {
console.dir(`--- COMPLETED THE ACCOUNTS (${this.accounts.join(', ')} and funded with ${this.balance} wei)`); console.dir(`--- COMPLETED THE ACCOUNTS (${this.accounts.join(', ')} and funded with ${this.balance} wei)`);
if(err) console.error('Error creating, unlocking, and funding accounts', err); if (err) console.error('Error creating, unlocking, and funding accounts', JSON.stringify(err));
// this.web3.eth.getAccounts().then((accounts) => { // this.web3.eth.getAccounts().then((accounts) => {
// let numAccts = accounts.length; // let numAccts = accounts.length;