aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/provider_state_factory.ts
blob: bd2d6dad5635c05fe9f740ed99f873c47ff2c239 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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,
        walletDisplayName?: string,
    ): ProviderState => {
        if (!_.isUndefined(provider)) {
            return providerStateFactory.getInitialProviderStateFromProvider(
                orderSource,
                network,
                provider,
                walletDisplayName,
            );
        }
        const providerStateFromWindowIfExits = providerStateFactory.getInitialProviderStateFromWindowIfExists(
            orderSource,
            network,
            walletDisplayName,
        );
        if (providerStateFromWindowIfExits) {
            return providerStateFromWindowIfExits;
        } else {
            return providerStateFactory.getInitialProviderStateFallback(orderSource, network, walletDisplayName);
        }
    },
    getInitialProviderStateFromProvider: (
        orderSource: OrderSource,
        network: Network,
        provider: Provider,
        walletDisplayName?: string,
    ): ProviderState => {
        const providerState: ProviderState = {
            name: envUtil.getProviderName(provider),
            displayName: walletDisplayName || envUtil.getProviderDisplayName(provider),
            provider,
            web3Wrapper: new Web3Wrapper(provider),
            assetBuyer: assetBuyerFactory.getAssetBuyer(provider, orderSource, network),
            account: LOADING_ACCOUNT,
        };
        return providerState;
    },
    getInitialProviderStateFromWindowIfExists: (
        orderSource: OrderSource,
        network: Network,
        walletDisplayName?: string,
    ): Maybe<ProviderState> => {
        const injectedProviderIfExists = providerFactory.getInjectedProviderIfExists();
        if (!_.isUndefined(injectedProviderIfExists)) {
            const providerState: ProviderState = {
                name: envUtil.getProviderName(injectedProviderIfExists),
                displayName: walletDisplayName || envUtil.getProviderDisplayName(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,
        walletDisplayName?: string,
    ): ProviderState => {
        const provider = providerFactory.getFallbackNoSigningProvider(network);
        const providerState: ProviderState = {
            name: 'Fallback',
            displayName: walletDisplayName || envUtil.getProviderDisplayName(provider),
            provider,
            web3Wrapper: new Web3Wrapper(provider),
            assetBuyer: assetBuyerFactory.getAssetBuyer(provider, orderSource, network),
            account: NO_ACCOUNT,
        };
        return providerState;
    },
};