不支持的密钥:使用 ethers.js 将 Binance 智能链添加到元掩码时的 rpcUrl

Unsupported keys: rpcUrl when adding Binance smart chain to metamask using ethers.js

我写了下面的代码 add/switch 到币安主网的网络,但它不起作用。 为什么它不起作用?我使用了 web3 提供商。 是否可以使用 web3 添加 binance 主网?

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

刚遇到同样的问题。 'wallet_addEthereumChain' 的正确参数如下:

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

这里有更多内容https://docs.metamask.io/guide/rpc-api.html#unrestricted-methods