Skip to content

Commit

Permalink
fix: timeout for switch chain
Browse files Browse the repository at this point in the history
  • Loading branch information
dudu0506 committed Nov 20, 2024
1 parent fa237fb commit 0635970
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/connectors/ParticleConnector.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -125,6 +126,19 @@ export function createParticleConnector(options: ConnectorOptions): CreateConnec
try {
const provider = await getProvider();
await Promise.all([
timeout(
new Promise<void>((resolve, reject) => {
const listener = ((data) => {
if ('chainId' in data && data.chainId === parameters.chainId) {
config.emitter.off('change', listener);
resolve();
}
}) satisfies Parameters<typeof config.emitter.on>[1];
config.emitter.on('change', listener);
}),
60 * 1000 * 3,
'[particle] switchChain timeout',
),
provider
.request({
method: EthereumMethodType.WALLET_SWITCH_ETHEREUM_CHAIN,
Expand All @@ -140,15 +154,6 @@ export function createParticleConnector(options: ConnectorOptions): CreateConnec
config.emitter.emit('change', { chainId: parameters.chainId });
}
}),
new Promise<void>((resolve) => {
const listener = ((data) => {
if ('chainId' in data && data.chainId === parameters.chainId) {
config.emitter.off('change', listener);
resolve();
}
}) satisfies Parameters<typeof config.emitter.on>[1];
config.emitter.on('change', listener);
}),
]);

return chain;
Expand Down

0 comments on commit 0635970

Please sign in to comment.