walletconnect-monorepo icon indicating copy to clipboard operation
walletconnect-monorepo copied to clipboard

Error initializing WalletConnect SDK [TypeError: Cannot read property `'getDefaultChain'` of undefined]

Open ozgurrgul opened this issue 1 year ago • 1 comments
trafficstars

Describe the bug Getting the following error while trying to implement WC modal in react-native for Solana:

 INFO  {"context": "core"} Core Initialization Success
 INFO  {"context": "core"} {"context": "core/expirer"} Emitting expirer_deleted
 INFO  {"context": "client"} SignClient Initialization Success
 --> ERROR  Error initializing WalletConnect SDK [TypeError: Cannot read property 'getDefaultChain' of undefined]
 INFO  Cleaning up inactive pairings...
 INFO  Inactive pairings cleared: 0
 INFO  {"context": "core"} {"context": "core/relayer"} {"context": "core/relayer/subscription"} Emitting subscription_created
 INFO  {"context": "core"} {"context": "core/expirer"} Emitting expirer_created
 INFO  {"context": "core"} {"context": "core/history"} Emitting history_created
 INFO  {"context": "core"} {"context": "core/expirer"} Emitting expirer_created
 INFO  {"context": "client"} session request queue is empty.

Ethereum one works fine. What should I do to support Solana? I got the chain id from your SUPPORTED_CHAINS.md file

SDK Version (if relevant)

"ethers": "5.7.2",
"@walletconnect/encoding": "^1.0.2",
"@walletconnect/ethereum-provider": "2.15.1",
"@walletconnect/modal-react-native": "1.1.0",
"@walletconnect/react-native-compat": "2.15.1",
"@walletconnect/universal-provider": "2.15.1",
"react-native-url-polyfill": "^2.0.0",
"node-libs-react-native": "^1.2.1",
"react-native-get-random-values": "^1.11.0",

To Reproduce Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior It should successfully open the modal

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: iOS (probably android too)
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context

Session params:

import { IProviderMetadata } from '@walletconnect/modal-react-native';
import { ISessionParams } from '@walletconnect/modal-react-native/lib/typescript/types/coreTypes';
import { NAVIGATION_SCHEMA } from '~/shared/environment';

export const WALLET_CONNECT_PROJECT_ID = '';

export const WALLET_CONNECT_METADATA: IProviderMetadata = {
  name: 'Omitted',
  description: 'Connect your wallet',
  url: 'https://Omitted.com/',
  icons: ['https://Omitted'],
  redirect: {
    native: `${NAVIGATION_SCHEMA}://`
  }
};

export const WALLET_CONNECT_SESSION_PARAMS: ISessionParams = {
  namespaces: {
    // Ethereum works fine
    // eip155: {
    //   methods: ['eth_signTransaction', 'eth_sign', 'personal_sign', 'eth_signTypedData'],
    //   chains: ['eip155:1'],
    //   events: ['accountsChanged']
    // },
    solana: {
      chains: ['solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'],
      methods: ['solana_signMessage', 'solana_signTransaction'],
      events: [],
    }
  }
};

WalletConnectModal implementation (react native):

import React, { useMemo, useEffect, useState } from 'react';
import { useWalletConnectModal } from '@walletconnect/modal-react-native';
import { ethers } from 'ethers';
import { Props } from '@walletconnect/modal-react-native/lib/typescript/modal/wcm-modal';
import {
  WALLET_CONNECT_PROJECT_ID,
  WALLET_CONNECT_METADATA,
  WALLET_CONNECT_SESSION_PARAMS
} from '~/store/addressBook/web3/AddressBookWeb3Constants';
import { getSignPayload } from '~/store/addressBook/web3/AddressBookWeb3Utils';
import { actions } from '~/store/redux';
import { Steps } from '../ui/Steps';

export const WalletConnect = () => {
  const { open, isConnected, provider, address } = useWalletConnectModal();
  const [WalletConnectModal, setWalletConnectModal] = useState<((config: Props) => JSX.Element) | null>(null);
  const web3Provider = useMemo(() => (provider ? new ethers.providers.Web3Provider(provider) : undefined), [provider]);

  useEffect(() => {
    const loadWalletConnect = async () => {
      try {
        const { WalletConnectModal: WalletConnectModalComp } = await import('@walletconnect/modal-react-native');
        setWalletConnectModal(() => WalletConnectModalComp);
      } catch (error) {
        console.error('Failed to load WalletConnectModal:', error);
      }
    };

    setTimeout(loadWalletConnect, 500);
  }, []);

  const sign = async () => {
    if (!address) return;
    const signature = await web3Provider?.send('personal_sign', [getSignPayload(), address]);
    // make use of signature
  };

  const disconnect = () => {
    provider?.disconnect();
  };

  const getContent = () => {
    if (!WalletConnectModal) {
      return <Steps.Loading />;
    }

    if (isConnected) {
      return <Steps.Connected onPressOpenWallet={sign} onPressDisconnect={disconnect} />;
    }

    return <Steps.NotConnected onPressOpenWallet={open} />;
  };

  return (
    <>
      {getContent()}
      {WalletConnectModal ? (
        <WalletConnectModal
          projectId={WALLET_CONNECT_PROJECT_ID}
          providerMetadata={WALLET_CONNECT_METADATA}
          sessionParams={WALLET_CONNECT_SESSION_PARAMS}
        />
      ) : null}
    </>
  );
};

ozgurrgul avatar Aug 27 '24 18:08 ozgurrgul

closing since we will switch to web3modal (appKit)

ozgurrgul avatar Aug 29 '24 13:08 ozgurrgul

@ozgurrgul

I still get this error

Cannot read properties of undefined (reading 'getDefaultChain')).

I am using appkit v1.7.19. In the same trace I also get these errors:

Error: Couldn't establish socket connection to the relay server: wss://relay.walletconnect.org
Error: No matching key. session: 8a4cdd0702c97747615e6ea051a21504dee1563283b557027d094cee8dff64e3
Image

Sidharth1013 avatar Sep 01 '25 05:09 Sidharth1013