embark/lib/contracts/provider.js

128 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-06-18 13:26:17 +00:00
const ProviderEngine = require('embark-web3-provider-engine');
const RpcSubprovider = require('embark-web3-provider-engine/subproviders/rpc');
const WsSubprovider = require('embark-web3-provider-engine/subproviders/websocket');
2018-06-29 21:09:19 +00:00
const CacheSubprovider = require('embark-web3-provider-engine/subproviders/cache.js');
const FixtureSubprovider = require('embark-web3-provider-engine/subproviders/fixture.js');
const FilterSubprovider = require('embark-web3-provider-engine/subproviders/filters.js');
const VmSubprovider = require('embark-web3-provider-engine/subproviders/vm.js');
const NonceSubprovider = require('embark-web3-provider-engine/subproviders/nonce-tracker.js');
const SubscriptionSubprovider = require('embark-web3-provider-engine/subproviders/subscriptions');
const async = require('async');
2018-05-17 17:48:39 +00:00
const AccountParser = require('./accountParser');
const fundAccount = require('./fundAccount');
2018-06-29 15:24:20 +00:00
const EventEmitter = require('events');
2018-06-29 15:24:20 +00:00
EventEmitter.prototype._maxListeners = 300;
const NO_ACCOUNTS = 'noAccounts';
2018-05-10 18:52:30 +00:00
2018-06-29 15:24:20 +00:00
class Provider extends ProviderEngine {
2018-05-10 18:52:51 +00:00
constructor(options) {
2018-06-29 15:24:20 +00:00
super();
2018-05-10 18:52:51 +00:00
this.web3 = options.web3;
this.accountsConfig = options.accountsConfig;
2018-06-15 18:35:50 +00:00
this.blockchainConfig = options.blockchainConfig;
this.type = options.type;
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-14 16:08:03 +00:00
this.asyncMethods = {};
2018-06-29 15:24:20 +00:00
this.setMaxListeners(300);
}
2018-05-10 18:52:30 +00:00
startWeb3Provider(callback) {
const self = this;
2018-06-15 18:35:50 +00:00
2018-06-29 21:09:19 +00:00
// cache layer
// self.addProvider(new CacheSubprovider())
// self.addProvider(new NonceSubprovider())
2018-06-15 18:35:50 +00:00
if (this.type === 'rpc') {
2018-06-29 15:24:20 +00:00
self.addProvider(new RpcSubprovider({
2018-06-15 18:35:50 +00:00
rpcUrl: self.web3Endpoint
}));
} else if (this.type === 'ws') {
2018-06-29 21:09:19 +00:00
console.log('USing ws');
self.addProvider(new SubscriptionSubprovider());
2018-06-29 15:24:20 +00:00
self.addProvider(new WsSubprovider({
2018-06-15 18:35:50 +00:00
rpcUrl: self.web3Endpoint,
origin: this.blockchainConfig.wsOrigins.split(',')[0]
}));
} else {
return callback(__("contracts config error: unknown deployment type %s", this.type));
}
2018-05-10 18:52:30 +00:00
2018-06-29 21:09:19 +00:00
2018-05-10 18:52:30 +00:00
// network connectivity error
2018-06-29 15:24:20 +00:00
self.on('error', (err) => {
2018-06-29 21:09:19 +00:00
console.log('ERR', JSON.stringify(err));
// report connectivity errors as trace due to polling
self.logger.trace('web3 provider error: ', err);
self.logger.trace('stopping web3 provider due to error');
2018-06-29 21:09:19 +00:00
// prevent continuous polling errors
2018-06-29 21:09:19 +00:00
self.stop();
});
2018-06-29 21:09:19 +00:00
self.web3.setProvider(self);
2018-06-29 15:24:20 +00:00
self.start();
self.accounts = AccountParser.parseAccountsConfig(self.accountsConfig, self.web3, self.logger);
self.addresses = [];
async.waterfall([
function populateWeb3Wallet(next) {
if (!self.accounts.length) {
return next(NO_ACCOUNTS);
}
self.accounts.forEach(account => {
self.addresses.push(account.address);
self.web3.eth.accounts.wallet.add(account);
});
self.asyncMethods = {
eth_accounts: self.eth_accounts.bind(self)
};
next();
}
], function (err) {
if (err && err !== NO_ACCOUNTS) {
self.logger.error((err));
}
callback();
2018-05-10 18:52:30 +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) => {
fundAccount(self.web3, account.address, account.hexBalance, eachCb);
}, callback);
}
2018-05-11 15:41:54 +00:00
eth_accounts(payload, cb) {
return cb(null, this.addresses);
}
sendAsync(payload, callback) {
let method = this.asyncMethods[payload.method];
if (method) {
return method.call(method, payload, (err, result) => {
if (err) {
return callback(err);
}
let response = {'id': payload.id, 'jsonrpc': '2.0', 'result': result};
callback(null, response);
});
}
2018-06-29 15:24:20 +00:00
super.sendAsync(payload, callback);
2018-05-10 18:52:30 +00:00
}
}
module.exports = Provider;