aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2018-11-07 14:33:58 +0800
committerBrandon Millman <brandon.millman@gmail.com>2018-11-08 06:25:23 +0800
commit28df5bfd94e817cb90689059d7255495ca6522ad (patch)
treef8368784de47d8fb73ebc6792100e2870ea0e815 /packages/instant
parentdfbf10c94bfbbdbca353531c5cae6707e05981f0 (diff)
downloaddexon-sol-tools-28df5bfd94e817cb90689059d7255495ca6522ad.tar
dexon-sol-tools-28df5bfd94e817cb90689059d7255495ca6522ad.tar.gz
dexon-sol-tools-28df5bfd94e817cb90689059d7255495ca6522ad.tar.bz2
dexon-sol-tools-28df5bfd94e817cb90689059d7255495ca6522ad.tar.lz
dexon-sol-tools-28df5bfd94e817cb90689059d7255495ca6522ad.tar.xz
dexon-sol-tools-28df5bfd94e817cb90689059d7255495ca6522ad.tar.zst
dexon-sol-tools-28df5bfd94e817cb90689059d7255495ca6522ad.zip
feat(instant): add Account to the ProviderState
Diffstat (limited to 'packages/instant')
-rw-r--r--packages/instant/src/components/zero_ex_instant_provider.tsx32
-rw-r--r--packages/instant/src/types.ts35
-rw-r--r--packages/instant/src/util/provider_state_factory.ts69
3 files changed, 97 insertions, 39 deletions
diff --git a/packages/instant/src/components/zero_ex_instant_provider.tsx b/packages/instant/src/components/zero_ex_instant_provider.tsx
index ed0fabdf3..1fb5cf64f 100644
--- a/packages/instant/src/components/zero_ex_instant_provider.tsx
+++ b/packages/instant/src/components/zero_ex_instant_provider.tsx
@@ -1,6 +1,5 @@
import { ObjectMap } from '@0x/types';
import { BigNumber } from '@0x/utils';
-import { Web3Wrapper } from '@0x/web3-wrapper';
import { Provider } from 'ethereum-types';
import * as _ from 'lodash';
import * as React from 'react';
@@ -11,12 +10,11 @@ import { asyncData } from '../redux/async_data';
import { DEFAULT_STATE, DefaultState, State } from '../redux/reducer';
import { store, Store } from '../redux/store';
import { fonts } from '../style/fonts';
-import { AffiliateInfo, AssetMetaData, Network, OrderSource, ProviderState, ProviderType } from '../types';
+import { AffiliateInfo, AssetMetaData, Network, OrderSource } from '../types';
import { assetUtils } from '../util/asset';
-import { assetBuyerFactory } from '../util/asset_buyer_factory';
import { errorFlasher } from '../util/error_flasher';
import { gasPriceEstimator } from '../util/gas_price_estimator';
-import { providerFactory } from '../util/provider_factory';
+import { providerStateFactory } from '../util/provider_state_factory';
fonts.include();
@@ -47,27 +45,11 @@ export class ZeroExInstantProvider extends React.Component<ZeroExInstantProvider
// use the networkId passed in with the props, otherwise default to that of the default state (1, mainnet)
const networkId = props.networkId || defaultState.network;
// construct the ProviderState
- let provider: Provider;
- let providerType: ProviderType;
- if (!_.isUndefined(props.provider)) {
- provider = props.provider;
- providerType = ProviderType.Props;
- } else {
- const injectedProviderIfExists = providerFactory.getInjectedProviderIfExists();
- if (!_.isUndefined(injectedProviderIfExists)) {
- provider = injectedProviderIfExists;
- providerType = ProviderType.Injected;
- } else {
- provider = providerFactory.getFallbackNoSigningProvider(networkId);
- providerType = ProviderType.FallbackEmptyWallet;
- }
- }
- const providerState: ProviderState = {
- provider,
- type: providerType,
- web3Wrapper: new Web3Wrapper(provider),
- assetBuyer: assetBuyerFactory.getAssetBuyer(provider, props.orderSource, networkId),
- };
+ const providerState = providerStateFactory.getInitialProviderState(
+ props.orderSource,
+ networkId,
+ props.provider,
+ );
// merge the additional additionalAssetMetaDataMap with our default map
const completeAssetMetaDataMap = {
...props.additionalAssetMetaDataMap,
diff --git a/packages/instant/src/types.ts b/packages/instant/src/types.ts
index 0ab227310..d65f70008 100644
--- a/packages/instant/src/types.ts
+++ b/packages/instant/src/types.ts
@@ -1,4 +1,4 @@
-import { AssetBuyer } from '@0x/asset-buyer';
+import { AssetBuyer, BigNumber } from '@0x/asset-buyer';
import { AssetProxyId, ObjectMap, SignedOrder } from '@0x/types';
import { Web3Wrapper } from '@0x/web3-wrapper';
import { Provider } from 'ethereum-types';
@@ -93,23 +93,30 @@ export interface AffiliateInfo {
feePercentage: number;
}
-/**
- * Our provider may be of 3 different types:
- * Props: the provider was passed in a prop to the component
- * Injected: the provider was injected into `window`
- * FallbackEmptyWallet: the provider has no wallet but responds to data requests using infura
- */
-export enum ProviderType {
- Props = 'PROPS',
- Injected = 'INJECTED',
- FallbackEmptyWallet = 'FALLBACK_EMPTY_WALLET',
-}
-
export interface ProviderState {
- type: ProviderType;
provider: Provider;
assetBuyer: AssetBuyer;
web3Wrapper: Web3Wrapper;
+ account: Account;
+}
+
+export enum AccountState {
+ Loading = 'LOADING',
+ Ready = 'READY',
+ Locked = 'LOCKED', // TODO(bmillman): break this up into locked / privacy mode enabled
+ Error = 'ERROR',
+ None = 'NONE,',
}
+export interface AccountReady {
+ state: AccountState.Ready;
+ address: string;
+ ethBalanceInWei?: BigNumber;
+}
+export interface AccountNotReady {
+ state: AccountState.None | AccountState.Loading | AccountState.Locked | AccountState.Error;
+}
+
+export type Account = AccountReady | AccountNotReady;
+
export type OrderSource = string | SignedOrder[];
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;
+ },
+};