不受支持的键:rpcurl在使用ethers.js添加binance智能链时
我写下了以下代码将/交换网络添加到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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚面对同样的问题。正确的参数为“ Wallet_addethereumchain”如下:
更多:此处
Just faced the same problem. Correct params for 'wallet_addEthereumChain' are the following:
More here https://docs.metamask.io/guide/rpc-api.html#unrestricted-methods