Skip to content

Commit

Permalink
fix: compatible wagmi config type for getDefaultConfig (#1893)
Browse files Browse the repository at this point in the history
* feat: compatible with wagmi config type

* chore: update getDefaultConfig and add changeset

* chore: add generic types

* chore: include generic type for stricter ts usage

* chore: update changeset

* Update swift-starfishes-appear.md

---------

Co-authored-by: Magomed Khamidov <[email protected]>
Co-authored-by: Daniel Sinclair <[email protected]>
  • Loading branch information
3 people authored Apr 4, 2024
1 parent 1a0f209 commit ec41346
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-starfishes-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rainbow-me/rainbowkit": patch
---

Amended the `getDefaultConfig` return type to prevent indirect type annotation errors and better infer type for parameters from Wagmi's `createConfig`.
56 changes: 31 additions & 25 deletions packages/rainbowkit/src/config/getDefaultConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Transport } from 'viem';
import type { Transport } from 'viem';
import { http, CreateConfigParameters } from 'wagmi';
import { WagmiProviderProps, createConfig } from 'wagmi';
import { type RainbowKitChain } from '../components/RainbowKitProvider/RainbowKitChainContext';
import { createConfig } from 'wagmi';
import type { RainbowKitChain } from '../components/RainbowKitProvider/RainbowKitChainContext';
import type { WalletList } from '../wallets/Wallet';
import { computeWalletConnectMetaData } from '../wallets/computeWalletConnectMetaData';
import { connectorsForWallets } from '../wallets/connectorsForWallets';
Expand All @@ -18,15 +18,16 @@ export type _chains = readonly [RainbowKitChain, ...RainbowKitChain[]];
// It maps each 'Chain' id to a 'Transport'
export type _transports = Record<_chains[number]['id'], Transport>;

type WagmiConfigParameters = Omit<
CreateConfigParameters<_chains, _transports>,
// If you use 'client' you can't use 'transports' (we force to use 'transports')
// More info here https://wagmi.sh/core/api/createConfig#client
// We will also use our own 'connectors' instead of letting user specifying it
'client' | 'connectors'
>;

interface GetDefaultConfigParameters extends WagmiConfigParameters {
interface GetDefaultConfigParameters<
chains extends _chains,
transports extends _transports,
> extends Omit<
CreateConfigParameters<chains, transports>,
// If you use 'client' you can't use 'transports' (we force to use 'transports')
// More info here https://wagmi.sh/core/api/createConfig#client
// We will also use our own 'connectors' instead of letting user specifying it
'client' | 'connectors'
> {
appName: string;
appDescription?: string;
appUrl?: string;
Expand All @@ -35,26 +36,34 @@ interface GetDefaultConfigParameters extends WagmiConfigParameters {
projectId: string;
}

const createDefaultTransports = (chains: _chains): _transports => {
const transportsObject = chains.reduce((acc: _transports, chain) => {
const key = chain.id as keyof _transports;
acc[key] = http() as _transports[keyof _transports]; // Type assertion here
const createDefaultTransports = <
chains extends _chains,
transports extends _transports,
>(
chains: chains,
): transports => {
const transportsObject = chains.reduce((acc: transports, chain) => {
const key = chain.id as keyof transports;
acc[key] = http() as transports[keyof transports]; // Type assertion here
return acc;
}, {} as _transports);
}, {} as transports);

return transportsObject;
};

export const getDefaultConfig = ({
export const getDefaultConfig = <
chains extends _chains,
transports extends _transports,
>({
appName,
appDescription,
appUrl,
appIcon,
wallets,
projectId,
...wagmiParameters
}: GetDefaultConfigParameters): WagmiProviderProps['config'] => {
let { transports, chains, ...restWagmiParameters } = wagmiParameters;
}: GetDefaultConfigParameters<chains, transports>) => {
const { transports, chains, ...restWagmiParameters } = wagmiParameters;

const metadata = computeWalletConnectMetaData({
appName,
Expand Down Expand Up @@ -85,14 +94,11 @@ export const getDefaultConfig = ({
},
);

if (!transports) {
transports = createDefaultTransports(chains);
}

return createConfig({
connectors,
chains,
transports,
transports:
transports || createDefaultTransports<chains, transports>(chains),
...restWagmiParameters,
});
};

0 comments on commit ec41346

Please sign in to comment.