2018-06-26 12:59:14 +10:00
|
|
|
const async = require('async');
|
|
|
|
const AccountParser = require('./accountParser');
|
|
|
|
const fundAccount = require('./fundAccount');
|
|
|
|
|
2018-07-03 16:39:17 -04:00
|
|
|
class Provider {
|
2018-06-26 12:59:14 +10:00
|
|
|
constructor(options) {
|
|
|
|
this.web3 = options.web3;
|
|
|
|
this.accountsConfig = options.accountsConfig;
|
|
|
|
this.blockchainConfig = options.blockchainConfig;
|
|
|
|
this.type = options.type;
|
|
|
|
this.web3Endpoint = options.web3Endpoint;
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.isDev = options.isDev;
|
|
|
|
}
|
|
|
|
|
|
|
|
startWeb3Provider(callback) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
if (this.type === 'rpc') {
|
2018-07-03 16:39:17 -04:00
|
|
|
self.provider = new this.web3.providers.HttpProvider(self.web3Endpoint);
|
2018-06-26 12:59:14 +10:00
|
|
|
} else if (this.type === 'ws') {
|
2018-07-03 16:39:17 -04:00
|
|
|
self.provider = new this.web3.providers.WebsocketProvider(self.web3Endpoint, {headers: {Origin: "embark"}});
|
2018-06-26 12:59:14 +10:00
|
|
|
} else {
|
|
|
|
return callback(__("contracts config error: unknown deployment type %s", this.type));
|
|
|
|
}
|
|
|
|
|
2018-07-03 16:39:17 -04:00
|
|
|
self.web3.setProvider(self.provider);
|
2018-06-26 12:59:14 +10:00
|
|
|
|
|
|
|
self.accounts = AccountParser.parseAccountsConfig(self.accountsConfig, self.web3, self.logger);
|
|
|
|
self.addresses = [];
|
2018-07-03 16:39:17 -04:00
|
|
|
if (!self.accounts.length) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
self.accounts.forEach(account => {
|
|
|
|
self.addresses.push(account.address);
|
|
|
|
self.web3.eth.accounts.wallet.add(account);
|
|
|
|
});
|
|
|
|
|
|
|
|
self.realAccountFunction = self.web3.eth.getAccounts;
|
|
|
|
self.web3.eth.getAccounts = function (cb) {
|
|
|
|
if (!cb) {
|
|
|
|
cb = function () {
|
2018-06-26 12:59:14 +10:00
|
|
|
};
|
|
|
|
}
|
2018-07-03 16:39:17 -04:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
self.realAccountFunction((err, accounts) => {
|
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
accounts = accounts.concat(self.addresses);
|
|
|
|
// accounts = self.addresses.concat(accounts);
|
|
|
|
cb(null, accounts);
|
|
|
|
resolve(accounts);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
callback();
|
2018-06-26 12:59:14 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
fundAccounts(callback) {
|
|
|
|
const self = this;
|
|
|
|
if (!self.accounts.length) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
if (!self.isDev) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
async.each(self.accounts, (account, eachCb) => {
|
|
|
|
fundAccount(self.web3, account.address, account.hexBalance, eachCb);
|
|
|
|
}, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Provider;
|