2018-05-18 13:25:44 +00:00
|
|
|
const async = require('async');
|
2018-05-17 17:48:39 +00:00
|
|
|
const AccountParser = require('./accountParser');
|
2018-05-18 13:25:44 +00:00
|
|
|
const fundAccount = require('./fundAccount');
|
|
|
|
|
2018-07-03 20:39:17 +00:00
|
|
|
class Provider {
|
2018-05-10 18:52:51 +00:00
|
|
|
constructor(options) {
|
|
|
|
this.web3 = options.web3;
|
|
|
|
this.accountsConfig = options.accountsConfig;
|
2018-06-15 18:35:50 +00:00
|
|
|
this.blockchainConfig = options.blockchainConfig;
|
|
|
|
this.type = options.type;
|
2018-05-18 13:25:44 +00:00
|
|
|
this.web3Endpoint = options.web3Endpoint;
|
2018-05-10 18:52:51 +00:00
|
|
|
this.logger = options.logger;
|
2018-05-18 13:46:39 +00:00
|
|
|
this.isDev = options.isDev;
|
2018-05-18 13:25:44 +00:00
|
|
|
}
|
2018-05-10 18:52:30 +00:00
|
|
|
|
2018-05-18 17:30:12 +00:00
|
|
|
startWeb3Provider(callback) {
|
2018-05-18 13:25:44 +00:00
|
|
|
const self = this;
|
2018-06-15 18:35:50 +00:00
|
|
|
|
|
|
|
if (this.type === 'rpc') {
|
2018-07-03 20:39:17 +00:00
|
|
|
self.provider = new this.web3.providers.HttpProvider(self.web3Endpoint);
|
2018-06-15 18:35:50 +00:00
|
|
|
} else if (this.type === 'ws') {
|
2018-07-03 20:39:17 +00:00
|
|
|
self.provider = new this.web3.providers.WebsocketProvider(self.web3Endpoint, {headers: {Origin: "embark"}});
|
2018-06-15 18:35:50 +00:00
|
|
|
} else {
|
|
|
|
return callback(__("contracts config error: unknown deployment type %s", this.type));
|
|
|
|
}
|
|
|
|
|
2018-07-03 20:39:17 +00:00
|
|
|
self.web3.setProvider(self.provider);
|
2018-05-18 13:25:44 +00:00
|
|
|
|
|
|
|
self.accounts = AccountParser.parseAccountsConfig(self.accountsConfig, self.web3, self.logger);
|
|
|
|
self.addresses = [];
|
2018-07-03 20:39:17 +00:00
|
|
|
if (!self.accounts.length) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
self.accounts.forEach(account => {
|
|
|
|
self.addresses.push(account.address);
|
|
|
|
self.web3.eth.accounts.wallet.add(account);
|
|
|
|
});
|
|
|
|
|
2018-07-05 12:38:19 +00:00
|
|
|
self.web3.eth.defaultAccount = self.addresses[0];
|
|
|
|
const realAccountFunction = self.web3.eth.getAccounts;
|
2018-07-03 20:39:17 +00:00
|
|
|
self.web3.eth.getAccounts = function (cb) {
|
2018-07-05 12:38:19 +00:00
|
|
|
cb = cb || function () {};
|
2018-07-03 20:39:17 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-07-05 12:38:19 +00:00
|
|
|
realAccountFunction((err, accounts) => {
|
2018-07-03 20:39:17 +00:00
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
return reject(err);
|
|
|
|
}
|
2018-07-05 12:38:19 +00:00
|
|
|
// console.log('ACOUNTS', accounts);
|
|
|
|
// console.log('My addresses', self.addresses);
|
|
|
|
// accounts = self.addresses;
|
|
|
|
accounts = [accounts[0]].concat(self.addresses);
|
|
|
|
// accounts = accounts.concat(self.addresses);
|
2018-07-03 20:39:17 +00:00
|
|
|
// accounts = self.addresses.concat(accounts);
|
|
|
|
cb(null, accounts);
|
|
|
|
resolve(accounts);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
callback();
|
2018-05-10 18:52:30 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 16:55:07 +00:00
|
|
|
fundAccounts(callback) {
|
|
|
|
const self = this;
|
|
|
|
if (!self.accounts.length) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
if (!self.isDev) {
|
|
|
|
return callback();
|
|
|
|
}
|
2018-08-14 18:05:21 +00:00
|
|
|
async.eachLimit(self.accounts, 1, (account, eachCb) => {
|
2018-06-07 17:06:09 +00:00
|
|
|
fundAccount(self.web3, account.address, account.hexBalance, eachCb);
|
2018-06-07 16:55:07 +00:00
|
|
|
}, callback);
|
|
|
|
}
|
2018-05-10 18:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Provider;
|