From de5f52f482addaccafc908ed9034ff1194533bf4 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 12 Jan 2024 02:34:56 +0200 Subject: [PATCH] fix: not supported chain --- src/web3/connectors/index.ts | 63 +++++++------------- src/web3/providers/WagmiProvider.tsx | 4 +- src/web3/store/walletSlice.ts | 87 +++++++++++++++++++++------- 3 files changed, 89 insertions(+), 65 deletions(-) diff --git a/src/web3/connectors/index.ts b/src/web3/connectors/index.ts index 78e9fdd..6701ab8 100644 --- a/src/web3/connectors/index.ts +++ b/src/web3/connectors/index.ts @@ -34,22 +34,13 @@ export enum WalletType { } export const initAllConnectors = (props: AllConnectorsInitProps) => { - const injectedConnector = { - connector: injected(), - type: WalletType.Injected, - }; - const coinbaseConnector = { - connector: coinbaseWallet({ - appName: props.appName, - }), - type: WalletType.Coinbase, - }; - const gnosisSafeConnector = { - connector: safe({ - ...safeSdkOptions, - }), - type: WalletType.Safe, - }; + const injectedConnector = injected(); + const coinbaseConnector = coinbaseWallet({ + appName: props.appName, + }); + const gnosisSafeConnector = safe({ + ...safeSdkOptions, + }); const connectors = [ injectedConnector, @@ -59,36 +50,24 @@ export const initAllConnectors = (props: AllConnectorsInitProps) => { const wcParams = props.wcParams; if (wcParams && !props.getImpersonatedAccount) { - const walletConnectConnector = { - connector: walletConnect({ - projectId: wcParams.projectId, - metadata: wcParams.metadata, - }), - type: WalletType.WalletConnect, - }; + const walletConnectConnector = walletConnect({ + projectId: wcParams.projectId, + metadata: wcParams.metadata, + }); return [walletConnectConnector, ...connectors]; } else if (!wcParams && !!props.getImpersonatedAccount) { - const impersonatedConnector = { - connector: impersonated({ - getAccountAddress: props.getImpersonatedAccount, - }), - type: WalletType.Impersonated, - }; + const impersonatedConnector = impersonated({ + getAccountAddress: props.getImpersonatedAccount, + }); return [impersonatedConnector, ...connectors]; } else if (wcParams && !!props.getImpersonatedAccount) { - const walletConnectConnector = { - connector: walletConnect({ - projectId: wcParams.projectId, - metadata: wcParams.metadata, - }), - type: WalletType.WalletConnect, - }; - const impersonatedConnector = { - connector: impersonated({ - getAccountAddress: props.getImpersonatedAccount, - }), - type: WalletType.Impersonated, - }; + const walletConnectConnector = walletConnect({ + projectId: wcParams.projectId, + metadata: wcParams.metadata, + }); + const impersonatedConnector = impersonated({ + getAccountAddress: props.getImpersonatedAccount, + }); return [walletConnectConnector, impersonatedConnector, ...connectors]; } else { return connectors; diff --git a/src/web3/providers/WagmiProvider.tsx b/src/web3/providers/WagmiProvider.tsx index 02dd8a2..652669a 100644 --- a/src/web3/providers/WagmiProvider.tsx +++ b/src/web3/providers/WagmiProvider.tsx @@ -87,15 +87,13 @@ export function WagmiProvider({ ); }); - console.log('chains in config', chains); - return { wagmiConfig: createConfig({ chains: [ chains[formattedProps.defaultChainId || 0] || mainnet, ...chains, ], - connectors: connectors.map((connector) => connector.connector), + connectors, transports, }), queryClient: new QueryClient(), diff --git a/src/web3/store/walletSlice.ts b/src/web3/store/walletSlice.ts index 590bbbb..1801a06 100644 --- a/src/web3/store/walletSlice.ts +++ b/src/web3/store/walletSlice.ts @@ -34,6 +34,7 @@ import { TransactionsSliceBaseType } from './transactionsSlice'; export interface Wallet { walletType: WalletType; address: Hex; + chainId: number; chain?: Chain; publicClient?: PublicClient; walletClient?: WalletClient; @@ -115,37 +116,78 @@ export function createWalletSlice({ setActiveWallet: async (wallet) => { const config = get().wagmiConfig; + const setWallet = async ( + walletData: Omit, + publicClient: PublicClient, + walletClient: WalletClient, + ) => { + const walletWithClients = { + ...walletData, + publicClient, + walletClient, + }; + + const isContractAddress = + await get().checkIsContractWallet(walletWithClients); + const activeWallet = { ...walletWithClients, isContractAddress }; + + set({ activeWallet }); + + if (walletData.chain?.id) { + get().setClient(walletData.chain.id, publicClient); + } + + walletConnected(activeWallet); + set({ isActiveWalletSetting: false }); + }; + if (wallet.isActive && config) { if ( wallet.chain && - config.chains.some((chain) => chain.id === wallet.chain?.id) + config.chains.some((chain) => chain.id === wallet.chainId) ) { set({ isActiveWalletSetting: true }); const publicClient = getPublicClient(config); const walletClient = await getWalletClient(config); if (publicClient && walletClient) { - const walletWithClients = { - ...wallet, - publicClient, - walletClient, + await setWallet(wallet, publicClient, walletClient); + } + } else { + set({ isActiveWalletSetting: true }); + let walletData = wallet; + let newConfig = config; + + if (!wallet.chain) { + walletData = { + ...walletData, + chain: getChainByChainId(walletData.chainId), }; + } else if ( + config.chains.every((chain) => chain.id !== wallet.chainId) + ) { + newConfig = { + ...config, + chains: [ + getChainByChainId(wallet.chainId) || mainnet, + ...config.chains, + ], + }; + + set({ wagmiConfig: newConfig }); + } - const isContractAddress = - await get().checkIsContractWallet(walletWithClients); - const activeWallet = { ...walletWithClients, isContractAddress }; + if ( + walletData.chain && + newConfig.chains.some((chain) => chain.id === wallet.chainId) + ) { + const publicClient = getPublicClient(newConfig); + const walletClient = await getWalletClient(newConfig); - set({ activeWallet }); - get().setClient(wallet.chain.id, publicClient); - walletConnected(activeWallet); - set({ isActiveWalletSetting: false }); + if (publicClient && walletClient) { + await setWallet(walletData, publicClient, walletClient); + } } - } else { - set({ isActiveWalletSetting: true }); - const activeWallet = { ...wallet, isContractAddress: false }; - set({ activeWallet }); - walletConnected(activeWallet); - set({ isActiveWalletSetting: false }); } } }, @@ -197,6 +239,7 @@ export function createWalletSlice({ await get().setActiveWallet({ walletType, address: account.address, + chainId: chainId || 1, chain: account.chain || getChainByChainId(chainId || 1), isActive: account.isConnected, isContractAddress: false, @@ -287,14 +330,18 @@ export function createWalletSlice({ account?.address && activeWallet && (activeWallet.address !== account.address || - activeWallet.chain?.id !== account.chain?.id) && + activeWallet.chainId !== account.chainId) && !get().isActiveWalletAccountChanging ) { set({ isActiveWalletAccountChanging: true }); await get().setActiveWallet({ walletType: activeWallet.walletType, address: account.address, - chain: account.chain, + chainId: account.chainId || 1, + chain: + account.chain || activeWallet.chain === account.chainId + ? activeWallet.chain + : getChainByChainId(account.chainId || 1), isActive: activeWallet.isActive, isContractAddress: activeWallet.isContractAddress, });