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

@ -25,14 +25,20 @@ class DevFunds {
this.web3.eth.getAccounts().then((accounts) => {
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();
});
}
createAccounts(numAccounts, password, cb) {
console.dir("creating " + (numAccounts - this.accounts.length) + " new accounts with password " + password);
async.timesLimit((numAccounts - this.accounts.length), 1, (_, next) => {
const numAccountsToCreate = numAccounts - (this.accounts.length + 1);
if (numAccountsToCreate === 0) return cb();
console.dir("creating " + numAccountsToCreate + " new accounts with password " + password);
async.timesLimit(numAccountsToCreate, 1, (_, next) => {
console.dir("--- creating new account");
this.web3.eth.personal.newAccount(password, next);
}, (err, accounts) => {
@ -45,10 +51,17 @@ class DevFunds {
}
unlockAccounts(password, cb) {
console.dir('--- CURRENT ACCOUNTS ' + this.accounts);
async.each(this.accounts, (account, next) => {
console.dir('-- unlocking account ' + account + ' with password ' + password);
this.web3.eth.personal.unlockAccount(account, password).then(() => next()).catch(next);
}, cb);
this.web3.eth.personal.unlockAccount(account, password).then((result) => {
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) {
@ -57,8 +70,13 @@ class DevFunds {
async.each(this.accounts, (account, next) => {
console.dir("-- funding account " + account + " with balance " + balance);
this.web3.eth.sendTransaction({to: account, value: balance}).then((result) => {
this.web3.eth.getBalance(account).then(currBalance => {
const remainingBalance = balance - currBalance;
console.dir("---- account " + account + " balance needed = " + remainingBalance);
if (remainingBalance <= 0) return next();
console.dir("-- funding account " + account + " with balance " + remainingBalance);
this.web3.eth.sendTransaction({to: account, value: remainingBalance}).then((result) => {
console.dir('FUNDING ACCT result: ' + JSON.stringify(result));
next();
}).catch(next);
@ -66,6 +84,9 @@ class DevFunds {
console.dir('-- FINISHED FUNDING ACCOUNTS, err= ' + err);
cb(err);
});
});
}
createFundAndUnlockAccounts(cb) {
@ -88,7 +109,7 @@ class DevFunds {
}
], (err) => {
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) => {
// let numAccts = accounts.length;