Skip to content

Commit

Permalink
fix adapter withConnection & selectNetwork action (#1392)
Browse files Browse the repository at this point in the history
Co-authored-by: Nikita-Polyakov <[email protected]>
  • Loading branch information
stefashkaa and Nikita-Polyakov authored Apr 25, 2024
1 parent b5132bf commit 5b615e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
25 changes: 12 additions & 13 deletions src/store/web3/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any, any>): Promise<void> {
const { state } = web3ActionContext(context);

if (state.networkType === BridgeNetworkType.Sub) {
autoselectSubAddress(context);
await connectSubNetwork(context);
}
}

async function connectSubNetwork(context: ActionContext<any, any>): Promise<void> {
const { getters, rootState } = web3ActionContext(context);
const subNetwork = getters.selectedNetwork;
Expand Down Expand Up @@ -80,13 +89,6 @@ async function autoselectSubAddress(context: ActionContext<any, any>): Promise<v
}
}

async function getRegisteredAssets(context: ActionContext<any, any>): Promise<void> {
const { rootDispatch } = web3ActionContext(context);

await rootDispatch.assets.getRegisteredAssets();
await autoselectBridgeAsset(context);
}

const actions = defineActions({
async selectEvmProvider(context, provider: Provider): Promise<void> {
const { commit, state } = web3ActionContext(context);
Expand Down Expand Up @@ -130,19 +132,16 @@ const actions = defineActions({
},

async selectExternalNetwork(context, { id, type }: { id: BridgeNetworkId; type: BridgeNetworkType }): Promise<void> {
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<void> {
Expand Down
10 changes: 8 additions & 2 deletions src/utils/bridge/sub/classes/adapters/substrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -61,7 +65,9 @@ export class SubAdapter {
};

protected async withConnection<T>(onSuccess: AsyncFnWithoutArgs<T> | FnWithoutArgs<T>, fallback: T) {
if (!this.connected && !this.connection.loading) return fallback;
if (this.closed) {
return fallback;
}

await this.api.isReady;

Expand All @@ -74,7 +80,7 @@ export class SubAdapter {
}

public async connect(): Promise<void> {
if (!this.connected && !this.api && !this.connection.loading) {
if (this.closed && !this.api) {
try {
await this.subNetworkConnection.connect();
} catch {}
Expand Down

0 comments on commit 5b615e0

Please sign in to comment.