aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/provider_state_factory.ts
blob: 452a71460dc352c0f693e9f4b202e29558c47ecd (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
import { Web3Wrapper } from '@0x/web3-wrapper';
import { Provider } from 'ethereum-types';
import * as _ from 'lodash';

import { LOADING_ACCOUNT, NO_ACCOUNT } from '../constants';
import { Maybe, Network, OrderSource, ProviderState } from '../types';
import { envUtil } from '../util/env';

import { assetBuyerFactory } from './asset_buyer_factory';
import { providerFactory } from './provider_factory';

export const providerStateFactory = {
    getInitialProviderState: (orderSource: OrderSource, network: Network, provider?: Provider): ProviderState => {
        if (!_.isUndefined(provider)) {
            return providerStateFactory.getInitialProviderStateFromProvider(orderSource, network, provider);
        }
        const providerStateFromWindowIfExits = providerStateFactory.getInitialProviderStateFromWindowIfExists(
            orderSource,
            network,
        );
        if (providerStateFromWindowIfExits) {
            return providerStateFromWindowIfExits;
        } else {
            return providerStateFactory.getInitialProviderStateFallback(orderSource, network);
        }
    },
    getInitialProviderStateFromProvider: (
        orderSource: OrderSource,
        network: Network,
        provider: Provider,
    ): ProviderState => {
        const providerState: ProviderState = {
            name: envUtil.getProviderName(provider),
            provider,
            web3Wrapper: new Web3Wrapper(provider),
            assetBuyer: assetBuyerFactory.getAssetBuyer(provider, orderSource, network),
            account: LOADING_ACCOUNT,
        };
        return providerState;
    },
    getInitialProviderStateFromWindowIfExists: (orderSource: OrderSource, network: Network): Maybe<ProviderState> => {
        const injectedProviderIfExists = providerFactory.getInjectedProviderIfExists();
        if (!_.isUndefined(injectedProviderIfExists)) {
            const providerState: ProviderState = {
                name: envUtil.getProviderName(injectedProviderIfExists),
                provider: injectedProviderIfExists,
                web3Wrapper: new Web3Wrapper(injectedProviderIfExists),
                assetBuyer: assetBuyerFactory.getAssetBuyer(injectedProviderIfExists, orderSource, network),
                account: LOADING_ACCOUNT,
            };
            return providerState;
        } else {
            return undefined;
        }
    },
    getInitialProviderStateFallback: (orderSource: OrderSource, network: Network): ProviderState => {
        const provider = providerFactory.getFallbackNoSigningProvider(network);
        const providerState: ProviderState = {
            name: envUtil.getProviderName(provider),
            provider,
            web3Wrapper: new Web3Wrapper(provider),
            assetBuyer: assetBuyerFactory.getAssetBuyer(provider, orderSource, network),
            account: NO_ACCOUNT,
        };
        return providerState;
    },
};