Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure switch chain successfully in mpc wallet #3233

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@
"tipjar",
"elex",
"hlbl",
"pushswitch"
"pushswitch",
"Luma"
],
"flagWords": [],
"ignorePaths": [
Expand All @@ -232,4 +233,3 @@
],
"overrides": []
}

52 changes: 35 additions & 17 deletions src/connectors/ParticleConnector.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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, UserRejectedRequestError } from 'viem';
import { type Address, type Chain, numberToHex, RpcError, SwitchChainError } from 'viem';
import { ChainNotConfiguredError, createConnector, type CreateConnectorFn } from 'wagmi';
import { mainnet } from 'wagmi/chains';

Expand Down Expand Up @@ -51,7 +52,7 @@ export function createParticleConnector(options: ConnectorOptions): CreateConnec

console.info(`[particle] enabled`);

return createConnector(() => {
return createConnector((config) => {
return {
// override particle wallet from EIP6963
id: 'network.particle',
Expand Down Expand Up @@ -124,25 +125,42 @@ export function createParticleConnector(options: ConnectorOptions): CreateConnec

try {
const provider = await getProvider();
await provider.request({
method: EthereumMethodType.WALLET_SWITCH_ETHEREUM_CHAIN,
params: [
{
chainId: numberToHex(parameters.chainId),
},
],
});

const currentChainId = await this.getChainId();
if (currentChainId !== parameters.chainId) {
throw new UserRejectedRequestError(new Error('User rejected switch after adding network.'));
}
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,
params: [
{
chainId: numberToHex(parameters.chainId),
},
],
})
.then(async () => {
const currentChainId = await this.getChainId();
if (currentChainId === parameters.chainId) {
config.emitter.emit('change', { chainId: parameters.chainId });
}
}),
]);

return chain;
} catch (error) {
console.error(`[particle] switchChain error`, error);
throw new SwitchChainError(error as RpcError);
}

return chain;
},
async getChainId() {
const provider = await getProvider();
Expand Down