From 95a319836fb947d02e6209f7a28e8b999b23ea05 Mon Sep 17 00:00:00 2001 From: Eugene Chybisov Date: Tue, 23 Jul 2024 17:38:49 +0200 Subject: [PATCH] feat: allow syncing with mixed chains --- .../wallet-management/src/syncWagmiConfig.ts | 13 +++++++++---- .../wallet-management/src/useSyncWagmiConfig.ts | 3 ++- .../src/utils/convertExtendedChain.ts | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/wallet-management/src/syncWagmiConfig.ts b/packages/wallet-management/src/syncWagmiConfig.ts index 98faea92c..08e112162 100644 --- a/packages/wallet-management/src/syncWagmiConfig.ts +++ b/packages/wallet-management/src/syncWagmiConfig.ts @@ -3,17 +3,22 @@ import type { Chain } from 'viem'; import { mainnet } from 'viem/chains'; import type { Config, CreateConnectorFn } from 'wagmi'; import { reconnect } from 'wagmi/actions'; -import { convertExtendedChain } from './utils/convertExtendedChain.js'; +import { + convertExtendedChain, + isExtendedChain, +} from './utils/convertExtendedChain.js'; export const syncWagmiConfig = ( wagmiConfig: Config, connectors: CreateConnectorFn[], - chains: ExtendedChain[], + chains: (ExtendedChain | Chain)[], ) => { - const _chains = chains.map(convertExtendedChain) as [Chain, ...Chain[]]; + const _chains = chains.map((chain) => + isExtendedChain(chain) ? convertExtendedChain(chain) : chain, + ) as [Chain, ...Chain[]]; const _mainnet = _chains.find((chain) => chain.id === mainnet.id); if (_mainnet) { - _mainnet.contracts = mainnet.contracts; + _mainnet.contracts = { ...mainnet.contracts, ..._mainnet.contracts }; } wagmiConfig._internal.chains.setState(_chains); wagmiConfig._internal.connectors.setState(() => diff --git a/packages/wallet-management/src/useSyncWagmiConfig.ts b/packages/wallet-management/src/useSyncWagmiConfig.ts index 1b2f3cd72..d4deb47f9 100644 --- a/packages/wallet-management/src/useSyncWagmiConfig.ts +++ b/packages/wallet-management/src/useSyncWagmiConfig.ts @@ -1,12 +1,13 @@ import type { ExtendedChain } from '@lifi/sdk'; import { useEffect } from 'react'; +import type { Chain } from 'viem'; import type { Config, CreateConnectorFn } from 'wagmi'; import { syncWagmiConfig } from './syncWagmiConfig.js'; export const useSyncWagmiConfig = ( wagmiConfig: Config, connectors: CreateConnectorFn[], - chains?: ExtendedChain[], + chains?: (ExtendedChain | Chain)[], ) => { useEffect(() => { if (chains?.length) { diff --git a/packages/wallet-management/src/utils/convertExtendedChain.ts b/packages/wallet-management/src/utils/convertExtendedChain.ts index ca5d8eb84..cdcdd074f 100644 --- a/packages/wallet-management/src/utils/convertExtendedChain.ts +++ b/packages/wallet-management/src/utils/convertExtendedChain.ts @@ -35,3 +35,19 @@ export const convertExtendedChain = (chain: ExtendedChain): Chain => ({ : undefined), }, }); + +export function isExtendedChain(chain: any): chain is ExtendedChain { + return ( + typeof chain === 'object' && + chain !== null && + 'key' in chain && + 'chainType' in chain && + 'coin' in chain && + 'mainnet' in chain && + 'logoURI' in chain && + typeof chain.metamask === 'object' && + chain.metamask !== null && + typeof chain.nativeToken === 'object' && + chain.nativeToken !== null + ); +}