aboutsummaryrefslogtreecommitdiffstats
path: root/test/utils/web3_factory.ts
blob: 863fa1d072d124f63983ff3f5b481cd03bf7a60a (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
// 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 * as Web3 from 'web3';
import * as Web3_beta from 'web3_beta';
import {constants} from './constants';
import {EmptyWalletSubProvider} from '../../src/subproviders/empty_wallet_subprovider';

export const web3Factory = {
    create(hasAddresses: boolean = true): Web3 {
        const provider = this.getRpcProvider(hasAddresses);
        const web3 = new Web3();
        web3.setProvider(provider);
        return web3;
    },
    createBeta(): any {
        const provider_beta = this.getProviderBeta();
        const web3_beta = new Web3_beta();
        web3_beta.setProvider(provider_beta);
        return web3_beta;
    },
    getRpcProvider(hasAddresses: boolean = true): Web3.Provider {
        const provider = new ProviderEngine();
        const rpcUrl = `http://${constants.RPC_HOST}:${constants.RPC_PORT}`;
        if (!hasAddresses) {
            provider.addProvider(new EmptyWalletSubProvider());
        }
        provider.addProvider(new RpcSubprovider({
            rpcUrl,
        }));
        provider.start();
        return provider;
    },
    getProviderBeta(): any {
        const rpcUrl = `http://${constants.RPC_HOST}:${constants.RPC_PORT}`;
        const providerBeta = new Web3_beta.providers.HttpProvider(rpcUrl);
        return providerBeta;
    },
};