Skip to content

Commit

Permalink
improve typesafetey for chains
Browse files Browse the repository at this point in the history
  • Loading branch information
KorbinianK committed Feb 26, 2024
1 parent 31665e5 commit 4f9586a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/bridge-ui/src/libs/chain/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
18 changes: 14 additions & 4 deletions packages/bridge-ui/src/libs/wagmi/client.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<number, ReturnType<typeof http>>,
);

return transports;
}

export const config = createConfig({
//@ts-ignore
chains: [...chains],
chains,
connectors: [walletConnect({ projectId })],
transports,
transports: createTransports(chains),
});

reconnect(config);

0 comments on commit 4f9586a

Please sign in to comment.