rainbow-me / rainbowkit

The best way to connect a wallet 🌈 🧰
https://rainbowkit.com
MIT License
2.42k stars 643 forks source link

[bug] ChainNotConfiguredError: Chain not configured. #2075

Closed mits87 closed 2 months ago

mits87 commented 3 months ago

Is there an existing issue for this?

RainbowKit Version

2.1.3

wagmi Version

2.10.9

Current Behavior

Currently when I'm creating the RainbowKit configuration like this:

"use client";

import { GetSiweMessageOptions, RainbowKitSiweNextAuthProvider } from "@rainbow-me/rainbowkit-siwe-next-auth";
import {
  RainbowKitProvider as BaseRainbowKitProvider,
  getDefaultWallets,
  getDefaultConfig,
} from "@rainbow-me/rainbowkit";
import { mainnet } from "wagmi/chains";
import { WagmiProvider } from "wagmi";

const { wallets } = getDefaultWallets();

const getSiweMessageOptions: GetSiweMessageOptions = () => ({
  statement: process.env.NEXT_PUBLIC_WALLET_SIGN_MESSAGE,
});

export default function RainbowKitProvider({ children }: React.PropsWithChildren): React.ReactElement {
  const config = getDefaultConfig({
    ssr: true,
    appName: 'App Name',
    projectId: 'WALLET_CONNECT_PROJECT_ID',
    chains: [
      mainnet,
    ],
    wallets,
  });

  return (
    <WagmiProvider config={config}>
      <RainbowKitSiweNextAuthProvider getSiweMessageOptions={getSiweMessageOptions}>
        <BaseRainbowKitProvider modalSize="compact">
          {children}
        </BaseRainbowKitProvider>
      </RainbowKitSiweNextAuthProvider>
    </WagmiProvider>
  );
}

I'm getting an error: ChainNotConfiguredError: Chain not configured.

image

When I change my chains for example if I replace mainnet to arbitrum then everything works as expected.

Expected Behavior

Application should NOT crash.

Steps To Reproduce

Just pass only mainnet to the chains or an empty array.

Anything else?

Also would be great if you can allow to provide empty array in chains - in that case we should see Switch Networks window with message like "No available networks" etc.

Currently we can't provide empty array because application crash:

image

This could help to have better user experience and manage chains dynamically

magiziz commented 3 months ago

@mits87 Mhmm seems like you're using getDefaultConfig within the component itself and not defining it outside of the component.

Can you try this code ?

"use client";

import { GetSiweMessageOptions, RainbowKitSiweNextAuthProvider } from "@rainbow-me/rainbowkit-siwe-next-auth";
import {
  RainbowKitProvider as BaseRainbowKitProvider,
  getDefaultWallets,
  getDefaultConfig,
} from "@rainbow-me/rainbowkit";
import { mainnet } from "wagmi/chains";
import { WagmiProvider } from "wagmi";

const { wallets } = getDefaultWallets();

const getSiweMessageOptions: GetSiweMessageOptions = () => ({
  statement: process.env.NEXT_PUBLIC_WALLET_SIGN_MESSAGE,
});

const config = getDefaultConfig({
  ssr: true,
  appName: 'App Name',
  projectId: 'WALLET_CONNECT_PROJECT_ID',
  chains: [mainnet],
  wallets,
});

export default function RainbowKitProvider({ children }: React.PropsWithChildren): React.ReactElement {
  return (
    <WagmiProvider config={config}>
      <RainbowKitSiweNextAuthProvider getSiweMessageOptions={getSiweMessageOptions}>
        <BaseRainbowKitProvider modalSize="compact">
          {children}
        </BaseRainbowKitProvider>
      </RainbowKitSiweNextAuthProvider>
    </WagmiProvider>
  );
}

You can also check out our example dApp here if it helps https://github.com/rainbow-me/rainbowkit/blob/main/examples/with-next/src/pages/_app.tsx

magiziz commented 3 months ago

@mits87 Here is an example with app router as well https://github.com/rainbow-me/rainbowkit/blob/main/examples/with-next-app/src/app/providers.tsx

mits87 commented 3 months ago

Hi @magiziz yes I tried also as you suggested. For the case when we have only mainnet or only bsc we have error as above. But in the other hand if I use other chains for example only arbitrum then it works.

Also works when I use mainnet AND arbitrum. So for me it looks like a bug.

Btw. the example what you provided has more than one chain that's why it works correctly.

magiziz commented 3 months ago

@mits87 Can you provide a minimal reproducible example with codesandbox or github repo ? I also realised you didn't add <SessionProvider> to your <RainbowKitProvider> component.

magiziz commented 2 months ago

@mits87 Any updates ?

magiziz commented 2 months ago

Closing this issue for now. Feel free to re-open this if you're still stuck.