diff options
author | Brandon Millman <brandon@0xproject.com> | 2018-11-08 15:40:43 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-08 15:40:43 +0800 |
commit | f6abc007ffb249e4bbf85b8a7a77309d43e0a147 (patch) | |
tree | 0bf9d75eed4bc60d37b7aed47be6adc2b4551a40 /packages/instant/src/util/provider_state_factory.ts | |
parent | 771f8a6a6cff935631e8c6ebcdc012cdd3533de6 (diff) | |
parent | 54b51830d075fcfc9b9e3ce0f4f4a4ef26eaf036 (diff) | |
download | dexon-sol-tools-f6abc007ffb249e4bbf85b8a7a77309d43e0a147.tar dexon-sol-tools-f6abc007ffb249e4bbf85b8a7a77309d43e0a147.tar.gz dexon-sol-tools-f6abc007ffb249e4bbf85b8a7a77309d43e0a147.tar.bz2 dexon-sol-tools-f6abc007ffb249e4bbf85b8a7a77309d43e0a147.tar.lz dexon-sol-tools-f6abc007ffb249e4bbf85b8a7a77309d43e0a147.tar.xz dexon-sol-tools-f6abc007ffb249e4bbf85b8a7a77309d43e0a147.tar.zst dexon-sol-tools-f6abc007ffb249e4bbf85b8a7a77309d43e0a147.zip |
Merge pull request #1221 from 0xProject/feature/instant/fallback-provider
[instant] Ensure we always have a provider from initial state
Diffstat (limited to 'packages/instant/src/util/provider_state_factory.ts')
-rw-r--r-- | packages/instant/src/util/provider_state_factory.ts | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/packages/instant/src/util/provider_state_factory.ts b/packages/instant/src/util/provider_state_factory.ts new file mode 100644 index 000000000..18b188d89 --- /dev/null +++ b/packages/instant/src/util/provider_state_factory.ts @@ -0,0 +1,69 @@ +import { Web3Wrapper } from '@0x/web3-wrapper'; +import { Provider } from 'ethereum-types'; +import * as _ from 'lodash'; + +import { AccountNotReady, AccountState, Maybe, Network, OrderSource, ProviderState } from '../types'; + +import { assetBuyerFactory } from './asset_buyer_factory'; +import { providerFactory } from './provider_factory'; + +const LOADING_ACCOUNT: AccountNotReady = { + state: AccountState.Loading, +}; +const NO_ACCOUNT: AccountNotReady = { + state: AccountState.None, +}; + +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 = { + 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 = { + 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 = { + provider, + web3Wrapper: new Web3Wrapper(provider), + assetBuyer: assetBuyerFactory.getAssetBuyer(provider, orderSource, network), + account: NO_ACCOUNT, + }; + return providerState; + }, +}; |