diff --git a/packages/web3-react/src/components/AlephiumConnect.tsx b/packages/web3-react/src/components/AlephiumConnect.tsx index e2e088235..875bc5f8d 100644 --- a/packages/web3-react/src/components/AlephiumConnect.tsx +++ b/packages/web3-react/src/components/AlephiumConnect.tsx @@ -34,7 +34,7 @@ import { subscribeToTxStatus, web3 } from '@alephium/web3' -import { Theme, Mode, CustomTheme, connectorIds, ProviderTheme } from '../types' +import { Theme, Mode, CustomTheme, ProviderTheme } from '../types' import { routes } from './Common/Modal' import { AlephiumBalanceContext, @@ -48,6 +48,8 @@ import { import { getLastConnectedAccount, removeLastConnectedAccount } from '../utils/storage' import { ConnectResult, getConnectorById } from '../utils/connector' import { useInjectedProviders } from '../hooks/useInjectedProviders' +import { injectedProviderStore } from '../utils/providers' +import { AlephiumWindowObject } from '@alephium/get-extension-wallet' export const ConnectSettingProvider: React.FC<{ theme?: Theme @@ -176,39 +178,57 @@ export const AlephiumConnectProvider: React.FC<{ ) useEffect(() => { - const func = async () => { - const onDisconnected = () => { - removeLastConnectedAccount() - updateAccount(undefined) - updateSignerProvider(undefined) - } - const onConnected = (result: ConnectResult) => { - updateAccount(result.account) - updateSignerProvider(result.signerProvider) + const onDisconnected = () => { + removeLastConnectedAccount() + updateAccount(undefined) + updateSignerProvider(undefined) + } + const onConnected = (result: ConnectResult) => { + updateAccount(result.account) + updateSignerProvider(result.signerProvider) + } + + const reconnectByInjected = async (providers: AlephiumWindowObject[], times = 0) => { + if (times >= 20) { + onDisconnected() + return } + const result = await getConnectorById('injected').autoConnect?.({ + network, + addressGroup, + keyType, + onDisconnected, + onConnected, + allInjectedProviders: providers + }) + if (result !== undefined) return + setTimeout(() => reconnectByInjected(injectedProviderStore.getProviders(), times + 1), 100) + } + const func = async () => { try { const lastConnectorId = lastConnectedAccount?.connectorId - const allConnectorIds = Array.from(connectorIds) - const sortedConnectorIds = - lastConnectorId === undefined - ? allConnectorIds - : [lastConnectorId].concat(allConnectorIds.filter((c) => c !== lastConnectorId)) - - for (const connectorId of sortedConnectorIds) { - const connector = getConnectorById(connectorId) - if (connector.autoConnect !== undefined) { - const result = await connector.autoConnect({ - network, - addressGroup, - keyType, - onDisconnected, - onConnected, - allInjectedProviders: connectorId === 'injected' ? allInjectedProviders : undefined - }) - if (result !== undefined) { - return - } + if (lastConnectorId === undefined) { + onDisconnected() + return + } + + if (lastConnectorId === 'injected') { + reconnectByInjected(allInjectedProviders) + return + } + + const connector = getConnectorById(lastConnectorId) + if (connector.autoConnect !== undefined) { + const result = await connector.autoConnect({ + network, + addressGroup, + keyType, + onDisconnected, + onConnected + }) + if (result !== undefined) { + return } } } catch (error) { diff --git a/packages/web3-react/src/utils/connector.ts b/packages/web3-react/src/utils/connector.ts index 2d70cfb33..594decdf3 100644 --- a/packages/web3-react/src/utils/connector.ts +++ b/packages/web3-react/src/utils/connector.ts @@ -19,7 +19,7 @@ along with the library. If not, see . import { Account, NetworkId, SignerProvider, KeyType } from '@alephium/web3' import { WalletConnectProvider } from '@alephium/walletconnect-provider' import QRCodeModal from '@alephium/walletconnect-qrcode-modal' -import { AlephiumWindowObject, getDefaultAlephiumWallet } from '@alephium/get-extension-wallet' +import { alephiumProvider, AlephiumWindowObject, getWalletObject } from '@alephium/get-extension-wallet' import { setLastConnectedAccount } from './storage' import { ConnectorId } from '../types' @@ -130,7 +130,7 @@ const wcDisconnect = async (signerProvider: SignerProvider): Promise => { const injectedConnect = async (options: InjectedConnectOptions): Promise => { try { - const windowAlephium = options.injectedProvider ?? (await getDefaultAlephiumWallet()) + const windowAlephium = options.injectedProvider ?? getWalletObject(alephiumProvider.id) const enableOptions = { addressGroup: options.addressGroup, keyType: options.keyType, @@ -159,7 +159,7 @@ const injectedAutoConnect = async (options: InjectedAutoConnectOptions): Promise try { const allProviders = options.allInjectedProviders ?? [] if (allProviders.length === 0) { - const windowAlephium = await getDefaultAlephiumWallet() + const windowAlephium = getWalletObject(alephiumProvider.id) if (windowAlephium !== undefined) { allProviders.push(windowAlephium) }