wevm / wagmi

React Hooks for Ethereum
https://wagmi.sh
MIT License
5.79k stars 968 forks source link

ChainID get set to Base testnet when only Base mainnet is passed into createWagmiConfig #4077

Closed blmalone closed 2 weeks ago

blmalone commented 2 weeks ago

Check existing issues

Describe the bug

Inconsistent behavior when you pass through networks to the create config functionality.

Link to Minimal Reproducible Example

No response

Steps To Reproduce

Step to reproduce:

Using this configuration will return the Base testnet chainId instead of Base mainnet.

import { createConfig, http } from 'wagmi';
import { base } from 'wagmi/chains';
import { coinbaseWallet } from 'wagmi/connectors';

export function createWagmiConfig(rpcUrl: string, projectId?: string) {
  // Keep this till we fully deprecated RK inside the template
  if (projectId) {
    console.log('projectId:', projectId);
  }

  // Temporary hack, until we configure a FE page in OnchainKit to copy just the API key
  const baseUrl = rpcUrl.replace(/\/v1\/(.+?)\//, '/v1/base/');

  return createConfig({
    chains: [base],
    connectors: [
      coinbaseWallet({
        appName: '0xCafeBabe',
        preference: 'all', // Setting this as all gives you the QR code for scanning with Coinbase wallet.
      }),
    ],
    ssr: true,
    transports: {
      [base.id]: http(baseUrl),
    },
  });
}

To fix it, you need to pass through both networks to the config.

import { createConfig, http } from 'wagmi';
import { base, baseSepolia } from 'wagmi/chains';
import { coinbaseWallet } from 'wagmi/connectors';

export function createWagmiConfig(rpcUrl: string, projectId?: string) {
  // Keep this till we fully deprecated RK inside the template
  if (projectId) {
    console.log('projectId:', projectId);
  }

  // Temporary hack, until we configure a FE page in OnchainKit to copy just the API key
  const baseUrl = rpcUrl.replace(/\/v1\/(.+?)\//, '/v1/base/');
  const baseSepoliaUrl = rpcUrl.replace(/\/v1\/(.+?)\//, '/v1/base-sepolia/');

  return createConfig({
    chains: [base, baseSepolia],
    connectors: [
      coinbaseWallet({
        appName: '0xCafeBabe',
        preference: 'all', // Setting this as all gives you the QR code for scanning with Coinbase wallet.
      }),
    ],
    ssr: true,
    transports: {
      [baseSepolia.id]: http(baseSepoliaUrl),
      [base.id]: http(baseUrl),
    },
  });
}

What Wagmi package(s) are you using?

wagmi

Wagmi Version

^2.9.2

Viem Version

2.16.1

TypeScript Version

~5.3.3

Anything else?

No response

blmalone commented 2 weeks ago

@oliviabarnett

github-actions[bot] commented 2 weeks ago

Hello @blmalone.

Please provide a minimal reproduction using StackBlitz, TypeScript Playground (for type issues), or a separate minimal GitHub repository.

Minimal reproductions are required as they save us a lot of time reproducing your config/environment and issue, and allow us to help you faster.

Once a minimal reproduction is added, a team member will confirm it works, then re-open the issue.