diff --git a/src/store/web3/actions.ts b/src/store/web3/actions.ts index a27708f88..f322bb3f9 100644 --- a/src/store/web3/actions.ts +++ b/src/store/web3/actions.ts @@ -12,6 +12,15 @@ import ethersUtil, { Provider, PROVIDER_ERROR } from '@/utils/ethers-util'; import type { SubNetwork } from '@sora-substrate/util/build/bridgeProxy/sub/types'; import type { ActionContext } from 'vuex'; +async function connectNetworkType(context: ActionContext): Promise { + const { state } = web3ActionContext(context); + + if (state.networkType === BridgeNetworkType.Sub) { + autoselectSubAddress(context); + await connectSubNetwork(context); + } +} + async function connectSubNetwork(context: ActionContext): Promise { const { getters, rootState } = web3ActionContext(context); const subNetwork = getters.selectedNetwork; @@ -80,13 +89,6 @@ async function autoselectSubAddress(context: ActionContext): Promise): Promise { - const { rootDispatch } = web3ActionContext(context); - - await rootDispatch.assets.getRegisteredAssets(); - await autoselectBridgeAsset(context); -} - const actions = defineActions({ async selectEvmProvider(context, provider: Provider): Promise { const { commit, state } = web3ActionContext(context); @@ -130,19 +132,16 @@ const actions = defineActions({ }, async selectExternalNetwork(context, { id, type }: { id: BridgeNetworkId; type: BridgeNetworkType }): Promise { - const { commit, dispatch } = web3ActionContext(context); + const { commit, dispatch, rootDispatch } = web3ActionContext(context); await dispatch.disconnectExternalNetwork(); commit.setNetworkType(type); commit.setSelectedNetwork(id); - getRegisteredAssets(context); + await Promise.allSettled([rootDispatch.assets.getRegisteredAssets(), connectNetworkType(context)]); - if (type === BridgeNetworkType.Sub) { - autoselectSubAddress(context); - await connectSubNetwork(context); - } + await autoselectBridgeAsset(context); }, async changeEvmNetworkProvided(context): Promise { diff --git a/src/utils/bridge/sub/classes/adapters/substrate.ts b/src/utils/bridge/sub/classes/adapters/substrate.ts index 782e436e5..b5290575c 100644 --- a/src/utils/bridge/sub/classes/adapters/substrate.ts +++ b/src/utils/bridge/sub/classes/adapters/substrate.ts @@ -40,6 +40,10 @@ export class SubAdapter { return !!this.api?.isConnected; } + get closed(): boolean { + return !this.connected && !this.subNetworkConnection.nodeAddressConnecting; + } + get chainSymbol(): string | undefined { return this.api?.registry.chainTokens[0]; } @@ -61,7 +65,9 @@ export class SubAdapter { }; protected async withConnection(onSuccess: AsyncFnWithoutArgs | FnWithoutArgs, fallback: T) { - if (!this.connected && !this.connection.loading) return fallback; + if (this.closed) { + return fallback; + } await this.api.isReady; @@ -74,7 +80,7 @@ export class SubAdapter { } public async connect(): Promise { - if (!this.connected && !this.api && !this.connection.loading) { + if (this.closed && !this.api) { try { await this.subNetworkConnection.connect(); } catch {}