From 4f9586a72b39e7ac2472b4c82b09bafd6e6a8c9d Mon Sep 17 00:00:00 2001 From: Korbinian Date: Mon, 26 Feb 2024 16:29:09 +0100 Subject: [PATCH] improve typesafetey for chains --- packages/bridge-ui/src/libs/chain/chains.ts | 4 ++-- packages/bridge-ui/src/libs/wagmi/client.ts | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/bridge-ui/src/libs/chain/chains.ts b/packages/bridge-ui/src/libs/chain/chains.ts index 8ccbbfed0bf..a6ca0f82298 100644 --- a/packages/bridge-ui/src/libs/chain/chains.ts +++ b/packages/bridge-ui/src/libs/chain/chains.ts @@ -27,9 +27,9 @@ export const chainIdToChain = (chainId: number): Chain => { return chain; }; -export const chains: Chain[] = Object.entries(chainConfig).map(([chainId, chainConfig]) => +export const chains: [Chain, ...Chain[]] = Object.entries(chainConfig).map(([chainId, chainConfig]) => mapChainConfigToChain(chainId, chainConfig), -); +) as [Chain, ...Chain[]]; export const getConfiguredChainIds = (): number[] => { return chains.map((chain) => Number(chain.id)); diff --git a/packages/bridge-ui/src/libs/wagmi/client.ts b/packages/bridge-ui/src/libs/wagmi/client.ts index 8f00a8071c0..0493882ff22 100644 --- a/packages/bridge-ui/src/libs/wagmi/client.ts +++ b/packages/bridge-ui/src/libs/wagmi/client.ts @@ -1,5 +1,6 @@ import { walletConnect } from '@wagmi/connectors'; import { createConfig, getPublicClient, http, reconnect } from '@wagmi/core'; +import type { Chain } from 'viem'; import { PUBLIC_WALLETCONNECT_PROJECT_ID } from '$env/static/public'; import { chains } from '$libs/chain'; @@ -10,13 +11,22 @@ export const publicClient = async (chainId: number) => { return await getPublicClient(config, { chainId }); }; -const transports = chains.reduce((acc, { id }) => ({ ...acc, [id]: http() }), {}); +function createTransports(chains: readonly Chain[]) { + const transports = chains.reduce( + (acc, chain) => { + const { id } = chain; + return { ...acc, [id]: http() }; + }, + {} as Record>, + ); + + return transports; +} export const config = createConfig({ - //@ts-ignore - chains: [...chains], + chains, connectors: [walletConnect({ projectId })], - transports, + transports: createTransports(chains), }); reconnect(config);