From 06359700de28decd2680ae010e851d0edd2719f6 Mon Sep 17 00:00:00 2001 From: dudu0506 Date: Wed, 20 Nov 2024 17:13:39 +0800 Subject: [PATCH] fix: timeout for switch chain --- src/connectors/ParticleConnector.ts | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/connectors/ParticleConnector.ts b/src/connectors/ParticleConnector.ts index e376e51d2..fbd7e8f2a 100644 --- a/src/connectors/ParticleConnector.ts +++ b/src/connectors/ParticleConnector.ts @@ -1,4 +1,5 @@ import { t } from '@lingui/macro'; +import { timeout } from '@masknet/kit'; import { EthereumMethodType, isValidAddress } from '@masknet/web3-shared-evm'; import { AuthType, connect, disconnect, EthereumProvider, particleAuth } from '@particle-network/auth-core'; import { type Address, type Chain, numberToHex, RpcError, SwitchChainError } from 'viem'; @@ -125,6 +126,19 @@ export function createParticleConnector(options: ConnectorOptions): CreateConnec try { const provider = await getProvider(); await Promise.all([ + timeout( + new Promise((resolve, reject) => { + const listener = ((data) => { + if ('chainId' in data && data.chainId === parameters.chainId) { + config.emitter.off('change', listener); + resolve(); + } + }) satisfies Parameters[1]; + config.emitter.on('change', listener); + }), + 60 * 1000 * 3, + '[particle] switchChain timeout', + ), provider .request({ method: EthereumMethodType.WALLET_SWITCH_ETHEREUM_CHAIN, @@ -140,15 +154,6 @@ export function createParticleConnector(options: ConnectorOptions): CreateConnec config.emitter.emit('change', { chainId: parameters.chainId }); } }), - new Promise((resolve) => { - const listener = ((data) => { - if ('chainId' in data && data.chainId === parameters.chainId) { - config.emitter.off('change', listener); - resolve(); - } - }) satisfies Parameters[1]; - config.emitter.on('change', listener); - }), ]); return chain;