diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-11-07 12:25:30 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-11-08 01:32:49 +0800 |
commit | dfbf10c94bfbbdbca353531c5cae6707e05981f0 (patch) | |
tree | 6b1f2832742517b280d7a7594adfa6fec4ea7bef /packages/instant/src/util/provider_factory.ts | |
parent | c30dca69619bf87ed198cf375d21593213798113 (diff) | |
download | dexon-0x-contracts-dfbf10c94bfbbdbca353531c5cae6707e05981f0.tar dexon-0x-contracts-dfbf10c94bfbbdbca353531c5cae6707e05981f0.tar.gz dexon-0x-contracts-dfbf10c94bfbbdbca353531c5cae6707e05981f0.tar.bz2 dexon-0x-contracts-dfbf10c94bfbbdbca353531c5cae6707e05981f0.tar.lz dexon-0x-contracts-dfbf10c94bfbbdbca353531c5cae6707e05981f0.tar.xz dexon-0x-contracts-dfbf10c94bfbbdbca353531c5cae6707e05981f0.tar.zst dexon-0x-contracts-dfbf10c94bfbbdbca353531c5cae6707e05981f0.zip |
feat(instant): fallback to an empty wallet provider when none is injected
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; + }, +}; |