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_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_factory.ts')
-rw-r--r-- | packages/instant/src/util/provider_factory.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/packages/instant/src/util/provider_factory.ts b/packages/instant/src/util/provider_factory.ts new file mode 100644 index 000000000..603f7674d --- /dev/null +++ b/packages/instant/src/util/provider_factory.ts @@ -0,0 +1,34 @@ +import { EmptyWalletSubprovider, RPCSubprovider, Web3ProviderEngine } from '@0x/subproviders'; +import { Provider } from 'ethereum-types'; +import * as _ from 'lodash'; + +import { BLOCK_POLLING_INTERVAL_MS, ETHEREUM_NODE_URL_BY_NETWORK } from '../constants'; +import { Maybe, Network } from '../types'; + +export const providerFactory = { + getInjectedProviderIfExists: (): Maybe<Provider> => { + const injectedProviderIfExists = (window as any).ethereum; + if (!_.isUndefined(injectedProviderIfExists)) { + return injectedProviderIfExists; + } + const injectedWeb3IfExists = (window as any).web3; + if (!_.isUndefined(injectedWeb3IfExists) && !_.isUndefined(injectedWeb3IfExists.currentProvider)) { + return injectedWeb3IfExists.currentProvider; + } + return undefined; + }, + getFallbackNoSigningProvider: (network: Network): Provider => { + const providerEngine = new Web3ProviderEngine({ + pollingInterval: BLOCK_POLLING_INTERVAL_MS, + }); + // Intercept calls to `eth_accounts` and always return empty + providerEngine.addProvider(new EmptyWalletSubprovider()); + // Construct an RPC subprovider, all data based requests will be sent via the RPCSubprovider + // TODO(bmillman): make this more resilient to infura failures + const rpcUrl = ETHEREUM_NODE_URL_BY_NETWORK[network]; + providerEngine.addProvider(new RPCSubprovider(rpcUrl)); + // // Start the Provider Engine + providerEngine.start(); + return providerEngine; + }, +}; |