aboutsummaryrefslogtreecommitdiffstats
path: root/contracts/test-utils/src/web3_wrapper.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-12-04 20:53:15 +0800
committerGitHub <noreply@github.com>2018-12-04 20:53:15 +0800
commit79f5e36edbd8a4483aac46032092dece95bb0b4c (patch)
treeb9c86a0badb2f1e82efa6354eede587368449a87 /contracts/test-utils/src/web3_wrapper.ts
parenta1e985a1cac46ecbc54c7ef5b846fb5faf2bede2 (diff)
parenta87276341356e31e0712f42ada00ed5d55707a6b (diff)
downloaddexon-0x-contracts-79f5e36edbd8a4483aac46032092dece95bb0b4c.tar
dexon-0x-contracts-79f5e36edbd8a4483aac46032092dece95bb0b4c.tar.gz
dexon-0x-contracts-79f5e36edbd8a4483aac46032092dece95bb0b4c.tar.bz2
dexon-0x-contracts-79f5e36edbd8a4483aac46032092dece95bb0b4c.tar.lz
dexon-0x-contracts-79f5e36edbd8a4483aac46032092dece95bb0b4c.tar.xz
dexon-0x-contracts-79f5e36edbd8a4483aac46032092dece95bb0b4c.tar.zst
dexon-0x-contracts-79f5e36edbd8a4483aac46032092dece95bb0b4c.zip
Merge pull request #1311 from 0xProject/feature/contracts-monorepo-2
Contracts monorepo II
Diffstat (limited to 'contracts/test-utils/src/web3_wrapper.ts')
-rw-r--r--contracts/test-utils/src/web3_wrapper.ts85
1 files changed, 85 insertions, 0 deletions
diff --git a/contracts/test-utils/src/web3_wrapper.ts b/contracts/test-utils/src/web3_wrapper.ts
new file mode 100644
index 000000000..cb33476f3
--- /dev/null
+++ b/contracts/test-utils/src/web3_wrapper.ts
@@ -0,0 +1,85 @@
+import { devConstants, env, EnvVars, web3Factory } from '@0x/dev-utils';
+import { prependSubprovider, Web3ProviderEngine } from '@0x/subproviders';
+import { logUtils } from '@0x/utils';
+import { Web3Wrapper } from '@0x/web3-wrapper';
+import * as _ from 'lodash';
+
+import { coverage } from './coverage';
+import { profiler } from './profiler';
+import { revertTrace } from './revert_trace';
+
+enum ProviderType {
+ Ganache = 'ganache',
+ Geth = 'geth',
+}
+
+let testProvider: ProviderType;
+switch (process.env.TEST_PROVIDER) {
+ case undefined:
+ testProvider = ProviderType.Ganache;
+ break;
+ case 'ganache':
+ testProvider = ProviderType.Ganache;
+ break;
+ case 'geth':
+ testProvider = ProviderType.Geth;
+ break;
+ default:
+ throw new Error(`Unknown TEST_PROVIDER: ${process.env.TEST_PROVIDER}`);
+}
+
+const ganacheTxDefaults = {
+ from: devConstants.TESTRPC_FIRST_ADDRESS,
+ gas: devConstants.GAS_LIMIT,
+};
+const gethTxDefaults = {
+ from: devConstants.TESTRPC_FIRST_ADDRESS,
+};
+export const txDefaults = testProvider === ProviderType.Ganache ? ganacheTxDefaults : gethTxDefaults;
+
+const gethConfigs = {
+ shouldUseInProcessGanache: false,
+ rpcUrl: 'http://localhost:8501',
+ shouldUseFakeGasEstimate: false,
+};
+const ganacheConfigs = {
+ shouldUseInProcessGanache: true,
+};
+const providerConfigs = testProvider === ProviderType.Ganache ? ganacheConfigs : gethConfigs;
+
+export const provider: Web3ProviderEngine = web3Factory.getRpcProvider(providerConfigs);
+provider.stop();
+const isCoverageEnabled = env.parseBoolean(EnvVars.SolidityCoverage);
+const isProfilerEnabled = env.parseBoolean(EnvVars.SolidityProfiler);
+const isRevertTraceEnabled = env.parseBoolean(EnvVars.SolidityRevertTrace);
+const enabledSubproviderCount = _.filter(
+ [isCoverageEnabled, isProfilerEnabled, isRevertTraceEnabled],
+ _.identity.bind(_),
+).length;
+if (enabledSubproviderCount > 1) {
+ throw new Error(`Only one of coverage, profiler, or revert trace subproviders can be enabled at a time`);
+}
+if (isCoverageEnabled) {
+ const coverageSubprovider = coverage.getCoverageSubproviderSingleton();
+ prependSubprovider(provider, coverageSubprovider);
+}
+if (isProfilerEnabled) {
+ if (testProvider === ProviderType.Ganache) {
+ logUtils.warn(
+ "Gas costs in Ganache traces are incorrect and we don't recommend using it for profiling. Please switch to Geth",
+ );
+ process.exit(1);
+ }
+ const profilerSubprovider = profiler.getProfilerSubproviderSingleton();
+ logUtils.log(
+ "By default profilerSubprovider is stopped so that you don't get noise from setup code. Don't forget to start it before the code you want to profile and stop it afterwards",
+ );
+ profilerSubprovider.stop();
+ prependSubprovider(provider, profilerSubprovider);
+}
+if (isRevertTraceEnabled) {
+ const revertTraceSubprovider = revertTrace.getRevertTraceSubproviderSingleton();
+ prependSubprovider(provider, revertTraceSubprovider);
+}
+
+export const web3Wrapper = new Web3Wrapper(provider);