Skip to content

Commit

Permalink
feat: 'getChains' for custom rainbowkit chains with wagmi v2
Browse files Browse the repository at this point in the history
  • Loading branch information
magiziz committed Jan 22, 2024
1 parent 0f62e35 commit 4644434
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,27 @@ const chainMetadataById = Object.fromEntries(
.map(({ chainId, ...metadata }) => [chainId, metadata]),
);

export const provideRainbowKitChains = (chains: readonly [Chain, ...Chain[]]) =>
chains.map((chain) => {
const defaultMetadata = chainMetadataById[chain.id] ?? {};
return {
...chain,
name: defaultMetadata.name ?? chain.name, // Favor colloquial names
iconUrl: defaultMetadata.iconUrl,
iconBackground: defaultMetadata.iconBackground,
} as RainbowKitChain;
});
const transformChainMetaData = (chain: RainbowKitChain) => {
const defaultMetadata = chainMetadataById[chain.id] ?? {};
return {
...chain,
name: defaultMetadata.name ?? chain.name, // favor colloquial names
iconUrl: chain.iconUrl ?? defaultMetadata.iconUrl,
iconBackground: chain.iconBackground ?? defaultMetadata.iconBackground,
} as RainbowKitChain;
};

export const provideRainbowKitChains = (
chains: readonly [Chain, ...Chain[]],
) => {
return chains.map(transformChainMetaData);
};

export const getChains = (
chains: readonly [RainbowKitChain, ...RainbowKitChain[]],
): readonly [Chain, ...Chain[]] => {
const rainbowKitChains = provideRainbowKitChains(chains);

// Construct a tuple from the array as required by wagmi = readonly [Chain, ...Chain[]]
return [rainbowKitChains[0], ...rainbowKitChains.slice(1)];
};
2 changes: 2 additions & 0 deletions packages/rainbowkit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export {
RainbowKitAuthenticationProvider,
createAuthenticationAdapter,
} from './components/RainbowKitProvider/AuthenticationContext';
export { getChains } from './components/RainbowKitProvider/provideRainbowKitChains';
export type { RainbowKitChain as Chain } from './components/RainbowKitProvider/RainbowKitChainContext';
export type {
Wallet,
WalletList,
Expand Down

0 comments on commit 4644434

Please sign in to comment.