diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-03-05 11:05:26 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-03-12 10:37:27 +0800 |
commit | 13299158d1e22d1af1cd36434fc403a74743ecb1 (patch) | |
tree | 9b35435c6f8641d2dc3d7bfd530c7c4f040a1f51 /packages/dev-utils/src/web3_factory.ts | |
parent | a6571b09d2087ffb9a4860c0db3d7344321fe2c3 (diff) | |
download | dexon-sol-tools-13299158d1e22d1af1cd36434fc403a74743ecb1.tar.gz dexon-sol-tools-13299158d1e22d1af1cd36434fc403a74743ecb1.tar.zst dexon-sol-tools-13299158d1e22d1af1cd36434fc403a74743ecb1.zip |
Add sol-cover implementation
Diffstat (limited to 'packages/dev-utils/src/web3_factory.ts')
-rw-r--r-- | packages/dev-utils/src/web3_factory.ts | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts index b0e0e4d3f..74359d4f6 100644 --- a/packages/dev-utils/src/web3_factory.ts +++ b/packages/dev-utils/src/web3_factory.ts @@ -6,9 +6,13 @@ import ProviderEngine = require('web3-provider-engine'); import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); -import { EmptyWalletSubprovider, FakeGasEstimateSubprovider } from '@0xproject/subproviders'; +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 { getCoverageSubprovider } from './coverage'; // HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang // because they are using the wrong XHR package. @@ -17,24 +21,51 @@ import { constants } from './constants'; // 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(hasAddresses: boolean = true): Web3 { - const provider = this.getRpcProvider(hasAddresses); + create(config: Web3Config = {}): Web3 { + const provider = this.getRpcProvider(config); const web3 = new Web3(); web3.setProvider(provider); return web3; }, - getRpcProvider(hasAddresses: boolean = true): Web3.Provider { + getRpcProvider(config: Web3Config = {}): Web3.Provider { const provider = new ProviderEngine(); + if (process.env.COVERAGE) { + provider.addProvider(getCoverageSubprovider()); + } + const hasAddresses = _.isUndefined(config.hasAddresses) || config.hasAddresses; if (!hasAddresses) { provider.addProvider(new EmptyWalletSubprovider()); } provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_ESTIMATE)); - provider.addProvider( - new RpcSubprovider({ - rpcUrl: constants.RPC_URL, - }), - ); + 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; }, |