2018-07-13 12:56:59 +00:00
|
|
|
const async = require('async');
|
2018-07-16 16:48:32 +00:00
|
|
|
const Web3 = require('web3');
|
|
|
|
const {getWeiBalanceFromString, buildUrl} = require('../../utils/utils.js');
|
|
|
|
const {readFileSync, dappPath} = require('../../core/fs');
|
2018-07-13 12:56:59 +00:00
|
|
|
|
|
|
|
class DevFunds {
|
|
|
|
constructor(blockchainConfig) {
|
|
|
|
this.web3 = null;
|
|
|
|
this.blockchainConfig = blockchainConfig;
|
|
|
|
this.accounts = [];
|
|
|
|
this.numAccounts = this.blockchainConfig.account.numAccounts || 0;
|
2018-07-16 19:19:01 +00:00
|
|
|
this.password = readFileSync(dappPath('config/development/password'), 'utf8').replace('\n', '');
|
2018-07-16 16:48:32 +00:00
|
|
|
this.web3 = new Web3();
|
2018-07-13 12:56:59 +00:00
|
|
|
this.balance = Web3.utils.toWei("1", "ether");
|
2018-07-16 16:48:32 +00:00
|
|
|
if(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);
|
|
|
|
console.dir('[blockchain/dev_funds]: converted balance to ' + this.balance);
|
|
|
|
}
|
2018-07-13 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
connectToNode(cb) {
|
2018-07-16 16:48:32 +00:00
|
|
|
|
|
|
|
this.web3.setProvider(new Web3.providers.WebsocketProvider(buildUrl('ws', this.blockchainConfig.wsHost, this.blockchainConfig.wsPort), {headers: {Origin: "http://localhost:8000"}}));
|
|
|
|
|
2018-07-13 12:56:59 +00:00
|
|
|
this.web3.eth.getAccounts().then((accounts) => {
|
2018-07-16 16:48:32 +00:00
|
|
|
this.web3.eth.defaultAccount = accounts[0];
|
2018-07-16 19:19:01 +00:00
|
|
|
this.accounts = accounts;
|
2018-07-13 12:56:59 +00:00
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createAccounts(numAccounts, password, cb) {
|
2018-07-16 19:19:01 +00:00
|
|
|
console.dir("creating " + (numAccounts - this.accounts.length) + " new accounts with password " + password);
|
|
|
|
async.timesLimit((numAccounts - this.accounts.length), 1, (_, next) => {
|
2018-07-13 12:56:59 +00:00
|
|
|
console.dir("--- creating new account");
|
2018-07-16 16:48:32 +00:00
|
|
|
this.web3.eth.personal.newAccount(password, next);
|
2018-07-13 12:56:59 +00:00
|
|
|
}, (err, accounts) => {
|
2018-07-16 16:48:32 +00:00
|
|
|
if(err) console.error(err);
|
2018-07-13 12:56:59 +00:00
|
|
|
console.dir("-- accounts created are ");
|
|
|
|
console.dir(accounts);
|
|
|
|
this.accounts = accounts;
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
unlockAccounts(password, cb) {
|
|
|
|
async.each(this.accounts, (account, next) => {
|
2018-07-16 16:48:32 +00:00
|
|
|
console.dir('-- unlocking account ' + account + ' with password ' + password);
|
|
|
|
this.web3.eth.personal.unlockAccount(account, password).then(() => next()).catch(next);
|
2018-07-13 12:56:59 +00:00
|
|
|
}, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
fundAccounts(balance, cb) {
|
2018-07-16 16:48:32 +00:00
|
|
|
console.dir('-- funding accounts...');
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-13 12:56:59 +00:00
|
|
|
async.each(this.accounts, (account, next) => {
|
2018-07-16 16:48:32 +00:00
|
|
|
console.dir("-- funding account " + account + " with balance " + balance);
|
|
|
|
this.web3.eth.sendTransaction({to: account, value: balance}).then((result) => {
|
|
|
|
console.dir('FUNDING ACCT result: ' + JSON.stringify(result));
|
2018-07-13 12:56:59 +00:00
|
|
|
next();
|
2018-07-16 16:48:32 +00:00
|
|
|
}).catch(next);
|
|
|
|
}, (err) => {
|
|
|
|
console.dir('-- FINISHED FUNDING ACCOUNTS, err= ' + err);
|
|
|
|
cb(err);
|
|
|
|
});
|
2018-07-13 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createFundAndUnlockAccounts(cb) {
|
|
|
|
async.waterfall([
|
2018-07-16 16:48:32 +00:00
|
|
|
(next) => {
|
|
|
|
console.dir('--- CONNECTING TO NODE');
|
|
|
|
this.connectToNode(next);
|
2018-07-13 12:56:59 +00:00
|
|
|
},
|
2018-07-16 16:48:32 +00:00
|
|
|
(next) => {
|
|
|
|
console.dir('--- CREATING THE ACCOUNTS');
|
|
|
|
this.createAccounts(this.numAccounts, this.password, next);
|
2018-07-13 12:56:59 +00:00
|
|
|
},
|
2018-07-16 16:48:32 +00:00
|
|
|
(next) => {
|
|
|
|
console.dir('--- UNLOCKING THE ACCOUNTS');
|
|
|
|
this.unlockAccounts(this.password, next);
|
2018-07-13 12:56:59 +00:00
|
|
|
},
|
2018-07-16 16:48:32 +00:00
|
|
|
(next) => {
|
|
|
|
console.dir('--- FUNDING THE ACCOUNTS');
|
|
|
|
this.fundAccounts(this.balance, next);
|
|
|
|
}
|
|
|
|
], (err) => {
|
2018-07-16 19:19:01 +00:00
|
|
|
console.dir(`--- COMPLETED THE ACCOUNTS (${this.accounts.join(', ')} and funded with ${this.balance} wei)`);
|
2018-07-16 16:48:32 +00:00
|
|
|
if(err) console.error('Error creating, unlocking, and funding accounts', err);
|
|
|
|
|
|
|
|
// this.web3.eth.getAccounts().then((accounts) => {
|
|
|
|
// let numAccts = accounts.length;
|
|
|
|
// accounts.forEach((account) => {
|
|
|
|
// this.web3.eth.getBalance(account).then((balance) => {
|
|
|
|
// console.dir('[contracts/dev_funds]: account ' + account + ' has balance of ' + balance);
|
|
|
|
// if(--numAccts === 0) cb(err);
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
cb(err);
|
|
|
|
});
|
2018-07-13 12:56:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DevFunds;
|