diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-11-09 03:04:16 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-11-09 03:04:16 +0800 |
commit | c27194a35783d966b27deeb1654a077b8c1ba1c8 (patch) | |
tree | 2b1deb57dcddf1c52bb7155836971be52d0b9fa3 /packages/instant/src/util/provider_state_factory.ts | |
parent | 6d5f65b77ede962b96ca59f30df1679a8216bd06 (diff) | |
parent | adcfe51190c8ca5cd4d11e49eb7e100c80fbd16c (diff) | |
download | dexon-sol-tools-c27194a35783d966b27deeb1654a077b8c1ba1c8.tar dexon-sol-tools-c27194a35783d966b27deeb1654a077b8c1ba1c8.tar.gz dexon-sol-tools-c27194a35783d966b27deeb1654a077b8c1ba1c8.tar.bz2 dexon-sol-tools-c27194a35783d966b27deeb1654a077b8c1ba1c8.tar.lz dexon-sol-tools-c27194a35783d966b27deeb1654a077b8c1ba1c8.tar.xz dexon-sol-tools-c27194a35783d966b27deeb1654a077b8c1ba1c8.tar.zst dexon-sol-tools-c27194a35783d966b27deeb1654a077b8c1ba1c8.zip |
Merge branch 'development' of https://github.com/0xProject/0x-monorepo into feature/instant/prevent-css-leakage
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; + }, +}; |