blob: 4077dc13b76aa4164a3a9391ad1dcb32f206ad34 (
plain) (
tree)
|
|
// HACK: web3 injects XMLHttpRequest into the global scope and ProviderEngine checks XMLHttpRequest
// to know whether it is running in a browser or node environment. We need it to be undefined since
// we are not running in a browser env.
// Filed issue: https://github.com/ethereum/web3.js/issues/844
(global as any).XMLHttpRequest = undefined;
import ProviderEngine = require('web3-provider-engine');
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
import { EmptyWalletSubprovider, FakeGasEstimateSubprovider, GanacheSubprovider } from '@0xproject/subproviders';
import * as fs from 'fs';
import * as _ from 'lodash';
import * as process from 'process';
import { constants } from './constants';
import { getCoverageSubproviderSingleton } from './coverage';
// HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang
// because they are using the wrong XHR package.
// importing web3 after subproviders fixes this issue
// Filed issue: https://github.com/ethereum/web3.js/issues/844
// tslint:disable-next-line:ordered-imports
import * as Web3 from 'web3';
export interface Web3Config {
hasAddresses?: boolean; // default: true
useInProcessGanache?: boolean; // default: false
}
export const web3Factory = {
create(config: Web3Config = {}): Web3 {
const provider = this.getRpcProvider(config);
const web3 = new Web3();
web3.setProvider(provider);
return web3;
},
getRpcProvider(config: Web3Config = {}): Web3.Provider {
const provider = new ProviderEngine();
if (process.env.COVERAGE) {
provider.addProvider(getCoverageSubproviderSingleton());
}
const hasAddresses = _.isUndefined(config.hasAddresses) || config.hasAddresses;
if (!hasAddresses) {
provider.addProvider(new EmptyWalletSubprovider());
}
provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_ESTIMATE));
const logger = {
log: (arg: any) => {
fs.appendFileSync('ganache.log', `${arg}\n`);
},
};
const useInProcessGanache = config.useInProcessGanache;
if (useInProcessGanache) {
provider.addProvider(
new GanacheSubprovider({
logger,
verbose: process.env.VERBOSE_GANACHE,
port: 8545,
networkId: 50,
mnemonic: 'concert load couple harbor equip island argue ramp clarify fence smart topic',
}),
);
} else {
provider.addProvider(
new RpcSubprovider({
rpcUrl: constants.RPC_URL,
}),
);
}
provider.start();
return provider;
},
};
|