fix for fortmatic
This commit is contained in:
@@ -13,6 +13,7 @@ import { swapQuoteUpdater } from '../util/swap_quote_updater';
|
|||||||
|
|
||||||
import { actions } from './actions';
|
import { actions } from './actions';
|
||||||
import { State } from './reducer';
|
import { State } from './reducer';
|
||||||
|
import { providerStateFactory } from '../util/provider_state_factory';
|
||||||
|
|
||||||
export const asyncData = {
|
export const asyncData = {
|
||||||
fetchEthPriceAndDispatchToStore: async (dispatch: Dispatch) => {
|
fetchEthPriceAndDispatchToStore: async (dispatch: Dispatch) => {
|
||||||
@@ -71,18 +72,19 @@ export const asyncData = {
|
|||||||
try {
|
try {
|
||||||
// TODO(bmillman): Add support at the web3Wrapper level for calling `eth_requestAccounts` instead of calling enable here
|
// TODO(bmillman): Add support at the web3Wrapper level for calling `eth_requestAccounts` instead of calling enable here
|
||||||
const isPrivacyModeEnabled = (provider as any).enable !== undefined;
|
const isPrivacyModeEnabled = (provider as any).enable !== undefined;
|
||||||
if (providerState.name !== 'Fortmatic') {
|
availableAddresses =
|
||||||
availableAddresses =
|
isPrivacyModeEnabled && shouldAttemptUnlock
|
||||||
isPrivacyModeEnabled && shouldAttemptUnlock
|
? await (provider as any).enable()
|
||||||
? await (provider as any).enable()
|
: await web3Wrapper.getAvailableAddressesAsync();
|
||||||
: await web3Wrapper.getAvailableAddressesAsync();
|
console.log(availableAddresses);
|
||||||
} else {
|
|
||||||
availableAddresses = await web3Wrapper.getAvailableAddressesAsync();
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
analytics.trackAccountUnlockDenied();
|
analytics.trackAccountUnlockDenied();
|
||||||
if (e.message.includes('Fortmatic: User denied account access.')) {
|
if (e.message.includes('Fortmatic: User denied account access.')) {
|
||||||
dispatch(actions.setAccountStateNone());
|
// If Fortmatic is not used, revert to injected provider
|
||||||
|
const initialProviderState = providerStateFactory.getInitialProviderStateWithCurrentProviderState(
|
||||||
|
providerState,
|
||||||
|
);
|
||||||
|
dispatch(actions.setProviderState(initialProviderState));
|
||||||
} else {
|
} else {
|
||||||
dispatch(actions.setAccountStateLocked());
|
dispatch(actions.setAccountStateLocked());
|
||||||
}
|
}
|
||||||
|
@@ -115,6 +115,7 @@ export interface ProviderState {
|
|||||||
web3Wrapper: Web3Wrapper;
|
web3Wrapper: Web3Wrapper;
|
||||||
account: Account;
|
account: Account;
|
||||||
orderSource: OrderSource;
|
orderSource: OrderSource;
|
||||||
|
isProviderInjected: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum AccountState {
|
export enum AccountState {
|
||||||
|
@@ -51,6 +51,8 @@ export const envUtil = {
|
|||||||
return ProviderType.Parity;
|
return ProviderType.Parity;
|
||||||
} else if (anyProvider.isMetaMask) {
|
} else if (anyProvider.isMetaMask) {
|
||||||
return ProviderType.MetaMask;
|
return ProviderType.MetaMask;
|
||||||
|
} else if (anyProvider.isFortmatic) {
|
||||||
|
return ProviderType.Fortmatic;
|
||||||
} else if (_.get(window, 'SOFA') !== undefined) {
|
} else if (_.get(window, 'SOFA') !== undefined) {
|
||||||
return ProviderType.CoinbaseWallet;
|
return ProviderType.CoinbaseWallet;
|
||||||
} else if (_.get(window, '__CIPHER__') !== undefined) {
|
} else if (_.get(window, '__CIPHER__') !== undefined) {
|
||||||
@@ -58,8 +60,7 @@ export const envUtil = {
|
|||||||
} else if (envUtil.getBrowser() === Browser.Opera && !anyProvider.isMetaMask) {
|
} else if (envUtil.getBrowser() === Browser.Opera && !anyProvider.isMetaMask) {
|
||||||
return ProviderType.Opera;
|
return ProviderType.Opera;
|
||||||
}
|
}
|
||||||
// If the provider is not supported by 0x, use fortmatic
|
return undefined;
|
||||||
return ProviderType.Fortmatic;
|
|
||||||
},
|
},
|
||||||
getProviderName(provider: ZeroExProvider): string {
|
getProviderName(provider: ZeroExProvider): string {
|
||||||
const providerTypeIfExists = envUtil.getProviderType(provider);
|
const providerTypeIfExists = envUtil.getProviderType(provider);
|
||||||
|
@@ -11,7 +11,6 @@ export interface HeartbeatFactoryOptions {
|
|||||||
export const generateAccountHeartbeater = (options: HeartbeatFactoryOptions): Heartbeater => {
|
export const generateAccountHeartbeater = (options: HeartbeatFactoryOptions): Heartbeater => {
|
||||||
const { store, shouldPerformImmediatelyOnStart } = options;
|
const { store, shouldPerformImmediatelyOnStart } = options;
|
||||||
return new Heartbeater(async () => {
|
return new Heartbeater(async () => {
|
||||||
console.log('beat');
|
|
||||||
await asyncData.fetchAccountInfoAndDispatchToStore(store.getState().providerState, store.dispatch, false);
|
await asyncData.fetchAccountInfoAndDispatchToStore(store.getState().providerState, store.dispatch, false);
|
||||||
}, shouldPerformImmediatelyOnStart);
|
}, shouldPerformImmediatelyOnStart);
|
||||||
};
|
};
|
||||||
|
@@ -52,6 +52,7 @@ export const providerStateFactory = {
|
|||||||
swapQuoteConsumer: assetSwapperFactory.getSwapQuoteConsumer(provider, network),
|
swapQuoteConsumer: assetSwapperFactory.getSwapQuoteConsumer(provider, network),
|
||||||
account: LOADING_ACCOUNT,
|
account: LOADING_ACCOUNT,
|
||||||
orderSource,
|
orderSource,
|
||||||
|
isProviderInjected: false,
|
||||||
};
|
};
|
||||||
return providerState;
|
return providerState;
|
||||||
},
|
},
|
||||||
@@ -71,6 +72,7 @@ export const providerStateFactory = {
|
|||||||
swapQuoteConsumer: assetSwapperFactory.getSwapQuoteConsumer(injectedProviderIfExists, network),
|
swapQuoteConsumer: assetSwapperFactory.getSwapQuoteConsumer(injectedProviderIfExists, network),
|
||||||
account: LOADING_ACCOUNT,
|
account: LOADING_ACCOUNT,
|
||||||
orderSource,
|
orderSource,
|
||||||
|
isProviderInjected: true,
|
||||||
};
|
};
|
||||||
return providerState;
|
return providerState;
|
||||||
} else {
|
} else {
|
||||||
@@ -83,7 +85,6 @@ export const providerStateFactory = {
|
|||||||
walletDisplayName?: string,
|
walletDisplayName?: string,
|
||||||
): ProviderState => {
|
): ProviderState => {
|
||||||
const provider = providerFactory.getFallbackNoSigningProvider(network);
|
const provider = providerFactory.getFallbackNoSigningProvider(network);
|
||||||
console.log(walletDisplayName, envUtil.getProviderDisplayName(provider));
|
|
||||||
const providerState: ProviderState = {
|
const providerState: ProviderState = {
|
||||||
name: 'Fallback',
|
name: 'Fallback',
|
||||||
displayName: walletDisplayName || envUtil.getProviderDisplayName(provider),
|
displayName: walletDisplayName || envUtil.getProviderDisplayName(provider),
|
||||||
@@ -93,9 +94,32 @@ export const providerStateFactory = {
|
|||||||
swapQuoteConsumer: assetSwapperFactory.getSwapQuoteConsumer(provider, network),
|
swapQuoteConsumer: assetSwapperFactory.getSwapQuoteConsumer(provider, network),
|
||||||
account: NO_ACCOUNT,
|
account: NO_ACCOUNT,
|
||||||
orderSource,
|
orderSource,
|
||||||
|
isProviderInjected: true,
|
||||||
};
|
};
|
||||||
return providerState;
|
return providerState;
|
||||||
},
|
},
|
||||||
|
// function to call getInitialProviderState with parameters retreived from a provided ProviderState
|
||||||
|
getInitialProviderStateWithCurrentProviderState: (
|
||||||
|
currentProviderState: ProviderState,
|
||||||
|
): ProviderState => {
|
||||||
|
const orderSource = currentProviderState.orderSource;
|
||||||
|
const chainId = currentProviderState.swapQuoter.chainId;
|
||||||
|
// If provider is provided to instant, use that and the displayName
|
||||||
|
if (!currentProviderState.isProviderInjected) {
|
||||||
|
return providerStateFactory.getInitialProviderState(
|
||||||
|
orderSource,
|
||||||
|
chainId,
|
||||||
|
currentProviderState.provider,
|
||||||
|
currentProviderState.displayName,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const newProviderState = providerStateFactory.getInitialProviderState(
|
||||||
|
orderSource,
|
||||||
|
chainId,
|
||||||
|
);
|
||||||
|
newProviderState.account = NO_ACCOUNT;
|
||||||
|
return newProviderState;
|
||||||
|
},
|
||||||
getProviderStateBasedOnProviderType: (
|
getProviderStateBasedOnProviderType: (
|
||||||
currentProviderState: ProviderState,
|
currentProviderState: ProviderState,
|
||||||
providerType: ProviderType,
|
providerType: ProviderType,
|
||||||
@@ -115,6 +139,7 @@ export const providerStateFactory = {
|
|||||||
swapQuoteConsumer: assetSwapperFactory.getSwapQuoteConsumer(provider, chainId),
|
swapQuoteConsumer: assetSwapperFactory.getSwapQuoteConsumer(provider, chainId),
|
||||||
account: LOADING_ACCOUNT,
|
account: LOADING_ACCOUNT,
|
||||||
orderSource,
|
orderSource,
|
||||||
|
isProviderInjected: true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -130,6 +155,7 @@ export const providerStateFactory = {
|
|||||||
swapQuoteConsumer: assetSwapperFactory.getSwapQuoteConsumer(fmProvider, chainId),
|
swapQuoteConsumer: assetSwapperFactory.getSwapQuoteConsumer(fmProvider, chainId),
|
||||||
account: LOADING_ACCOUNT,
|
account: LOADING_ACCOUNT,
|
||||||
orderSource,
|
orderSource,
|
||||||
|
isProviderInjected: true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return providerStateFactory.getInitialProviderState(
|
return providerStateFactory.getInitialProviderState(
|
||||||
|
Reference in New Issue
Block a user