aboutsummaryrefslogtreecommitdiffstats
path: root/packages/metacoin/test/utils/web3_wrapper.ts
blob: 258cf560f6334e764da42e9a26daa068484ac2eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { env, EnvVars } from '@0x/dev-utils';
import { GanacheSubprovider, prependSubprovider, RPCSubprovider, Web3ProviderEngine } from '@0x/subproviders';
import { errorUtils, logUtils } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
import * as fs from 'fs';

import { config } from './config';
import { coverage } from './coverage';
import { profiler } from './profiler';

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 errorUtils.spawnSwitchErr('TEST_PROVIDER', process.env.TEST_PROVIDER);
}

export const provider = new Web3ProviderEngine();
if (testProvider === ProviderType.Ganache) {
    provider.addProvider(
        new GanacheSubprovider({
            logger: {
                log: (arg: any) => {
                    fs.appendFileSync(config.ganacheLogFile, `${arg}\n`);
                },
            },
            verbose: env.parseBoolean(EnvVars.SolidityCoverage),
            networkId: config.networkId,
            mnemonic: config.mnemonic,
        }),
    );
} else {
    provider.addProvider(new RPCSubprovider('http://localhost:8501'));
}
provider.start();

const isCoverageEnabled = env.parseBoolean(EnvVars.SolidityCoverage);
const isProfilerEnabled = env.parseBoolean(EnvVars.SolidityProfiler);
if (isCoverageEnabled && isProfilerEnabled) {
    throw new Error(
        `Unfortunately for now you can't enable both coverage and profiler at the same time. They both use coverage.json file and there is no way to configure that.`,
    );
}
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. Check README for more details",
        );
        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);
}

export const web3Wrapper = new Web3Wrapper(provider);