Skip to content

[Bug]: Add new chain success but throw error #1272

@bongdungyeuem27

Description

@bongdungyeuem27

SDK

Web

Provide environment information

"react": "19.1.0"
"react-dom": "19.1.0"
"@metamask/sdk-react": "0.32.1"
"next": "15.3.1"

MetaMask SDK Version

0.32.1

MetaMask Mobile app Version

none

What browser are you using? (if relevant)

Chrome 135.0.7049.115 (Official Build) (arm64)

How are you deploying your application? (if relevant)

No response

Describe the Bug

error:

{
    "code": -32603,
    "message": "f is not a function",
    "data": {
        "cause": {
            "stack": "TypeError: f is not a function\n  at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background-0.js:1:99116\n  at async chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background-0.js:1:92354",
            "message": "f is not a function"
        }
    }
}

react code:

import { useToast } from "@chakra-ui/react";
import { useSDK } from "@metamask/sdk-react";
import { useCallback } from "react";
import { toHex } from "viem";
import { currentChainConfig } from "../../../lib/hooks/useCurrentChain";

export const useAddEvmChainToWallet = () => {
  const { provider } = useSDK();

  const toast = useToast();

  const addOrSwitchChain = useCallback(async () => {
    const config = currentChainConfig.config;
    const chainIdHex = toHex(config.id);
    const chainParams = {
      chainId: chainIdHex,
      chainName: currentChainConfig.chainPrettyName,
      iconUrls: [
        currentChainConfig.cosmosConfig.logo_URIs!.svg,
        currentChainConfig.cosmosConfig.logo_URIs!.png,
      ],
      nativeCurrency: {
        name: config.nativeCurrency.name,
        symbol: config.nativeCurrency.symbol,
        decimals: config.nativeCurrency.decimals,
      },
      rpcUrls: config.rpcUrls.default.http,
      blockExplorerUrls: [config.blockExplorers?.default.url],
    };

    if (!provider) {
      console.warn("Ethereum object not found");
      window.open("https://metamask.io/download.html", "_blank");
      return;
    }

    //   {
    //     "chainId": "0x531",
    //     "chainName": "Sei (mainnet)",
    //     "iconUrls": [
    //         "https://raw.githubusercontent.com/cosmos/chain-registry/master/sei/images/sei.svg",
    //         "https://raw.githubusercontent.com/cosmos/chain-registry/master/sei/images/sei.png"
    //     ],
    //     "nativeCurrency": {
    //         "name": "SEI",
    //         "symbol": "SEI",
    //         "decimals": 18
    //     },
    //     "rpcUrls": [
    //         "https://evm-rpc.sei-apis.com"
    //     ],
    //     "blockExplorerUrls": [
    //         "https://seitrace.com"
    //     ]
    // }
    console.log(chainParams);

    return await provider
      .request({
        method: "wallet_addEthereumChain",
        params: [chainParams],
      })
      .then(() => {
        toast({
          position: "top-right",
          title: "Success",
          description: "Successfully added network to your wallet",
          status: "success",
          variant: "subtle",
          isClosable: true,
        });
      })
      .catch((error: Error) => {
        console.log(error);
        toast({
          position: "top-right",
          title: "Error",
          description: (error as Error)?.message || "Something went wrong",
          status: "error",
          variant: "subtle",
          isClosable: true,
        });
      });
  }, [provider]);

  return {
    addOrSwitchChain: addOrSwitchChain,
  };
};

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
    at Function.invokeGetter (<anonymous>:3:28)
    at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/scripts/inpage.js:1:9489
    at new Promise (<anonymous>)
    at E.p (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/scripts/inpage.js:1:9468)
    at E.d (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/scripts/inpage.js:1:8853)
    at async E.l (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/scripts/inpage.js:1:8597)

Expected Behavior

Do not throw error when chain added

Link to reproduction - Issues with a link to complete (but minimal) reproduction code will be addressed faster

No response

To Reproduce

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions