不受支持的键:rpcurl在使用ethers.js添加binance智能链时

发布于 2025-01-24 10:56:42 字数 1911 浏览 1 评论 0 原文

我写下了以下代码将/交换网络添加到Binance Mainnet,但它不起作用。 为什么不起作用?我使用了Web3提供商。 是否可以使用Web3添加Binance Main Net?

try {
    // check if the chain to connect to is installed
    await ethereum.request({
        method: 'wallet_switchEthereumChain',
        params: [{ chainId: '0x38' }], // chainId must be in hexadecimal numbers
    });
} catch (error) {
    // This error code indicates that the chain has not been added to MetaMask
    // if it is not, then install it into the user MetaMask
    
    if (error.code === 4902) {
        try {
            await ethereum.request({
                method: 'wallet_addEthereumChain',
                params: [
                    {
                        chainId: '0x38',
                        rpcUrl: 'https://bsc-dataseed.binance.org/',
                    },
                ],
            });
        } catch (addError) {
            console.error(addError)
            toast.error("Operation failed. Choose the Binance Smart Chain on your wallet")
            return
        }
    } else {
        console.error(error)
        toast.error("Operation failed. Choose the Binance Smart Chain on your wallet")
        return
    }
    
}

错误:

MetaMask - RPC Error: Received unexpected keys on object parameter. Unsupported keys:
rpcUrl Objectcode: -32602message: "Received unexpected keys on object parameter. Unsupported keys:\nrpcUrl"[[Prototype]]: Objectconstructor: ƒ Object()hasOwnProperty: ƒ hasOwnProperty()isPrototypeOf: ƒ isPrototypeOf()propertyIsEnumerable: ƒ propertyIsEnumerable()toLocaleString: ƒ toLocaleString()toString: ƒ toString()valueOf: ƒ valueOf()__defineGetter__: ƒ __defineGetter__()__defineSetter__: ƒ __defineSetter__()__lookupGetter__: ƒ __lookupGetter__()__lookupSetter__: ƒ __lookupSetter__()__proto__: (...)get __proto__: ƒ __proto__()set __proto__: ƒ __proto__()
(anonymous) @ inpage.js:1

I wrote below code to add/switch network to Binance mainnet but it is not working.
Why does not it work? I used web3 provider.
Is it possible to add binance main net using web3?

try {
    // check if the chain to connect to is installed
    await ethereum.request({
        method: 'wallet_switchEthereumChain',
        params: [{ chainId: '0x38' }], // chainId must be in hexadecimal numbers
    });
} catch (error) {
    // This error code indicates that the chain has not been added to MetaMask
    // if it is not, then install it into the user MetaMask
    
    if (error.code === 4902) {
        try {
            await ethereum.request({
                method: 'wallet_addEthereumChain',
                params: [
                    {
                        chainId: '0x38',
                        rpcUrl: 'https://bsc-dataseed.binance.org/',
                    },
                ],
            });
        } catch (addError) {
            console.error(addError)
            toast.error("Operation failed. Choose the Binance Smart Chain on your wallet")
            return
        }
    } else {
        console.error(error)
        toast.error("Operation failed. Choose the Binance Smart Chain on your wallet")
        return
    }
    
}

Error:

MetaMask - RPC Error: Received unexpected keys on object parameter. Unsupported keys:
rpcUrl Objectcode: -32602message: "Received unexpected keys on object parameter. Unsupported keys:\nrpcUrl"[[Prototype]]: Objectconstructor: ƒ Object()hasOwnProperty: ƒ hasOwnProperty()isPrototypeOf: ƒ isPrototypeOf()propertyIsEnumerable: ƒ propertyIsEnumerable()toLocaleString: ƒ toLocaleString()toString: ƒ toString()valueOf: ƒ valueOf()__defineGetter__: ƒ __defineGetter__()__defineSetter__: ƒ __defineSetter__()__lookupGetter__: ƒ __lookupGetter__()__lookupSetter__: ƒ __lookupSetter__()__proto__: (...)get __proto__: ƒ __proto__()set __proto__: ƒ __proto__()
(anonymous) @ inpage.js:1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太傻旳人生 2025-01-31 10:56:42

刚刚面对同样的问题。正确的参数为“ Wallet_addethereumchain”如下:

params: [
    {
        chainId: '0x38',
        rpcUrls: ['https://bsc-dataseed.binance.org/'],
        chainName: 'BSC Mainnet'
    }
]

更多:此处

Just faced the same problem. Correct params for 'wallet_addEthereumChain' are the following:

params: [
    {
        chainId: '0x38',
        rpcUrls: ['https://bsc-dataseed.binance.org/'],
        chainName: 'BSC Mainnet'
    }
]

More here https://docs.metamask.io/guide/rpc-api.html#unrestricted-methods

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文