我试图以编程方式切换用户的元问询网络。使用wallet_addEthereumChain
当请求对孟买多边形进行网络更改时,它工作得很好,但是在使用Polygon Mainet
时不起作用,我得到:Chain ID returned by RPC URL https://polygon-rpc.com does not match 0x137
如果我在元问题上手动添加网络,使用相同的信息就可以了。
const { ethereum } = window;
await ethereum.request({
id: 1,
jsonrpc: "2.0",
method: "wallet_addEthereumChain",
params: [
{
chainId: "0x13881",
rpcUrls: ["https://rpc-mumbai.maticvigil.com"],
chainName: "Polygon Testnet Mumbai",
nativeCurrency: {
name: "tMATIC",
symbol: "tMATIC", // 2-6 characters long
decimals: 18,
},
blockExplorerUrls: ["https://mumbai.polygonscan.com/"],
},
],
});
const { ethereum } = window;
await ethereum.request({
id: 1,
jsonrpc: "2.0",
method: "wallet_addEthereumChain",
params: [
{
chainId: "0x137",
rpcUrls:[ "https://polygon-rpc.com"],
chainName: "Polygon Mainnet",
nativeCurrency: {
name: "MATIC",
symbol: "MATIC", // 2-6 characters long
decimals: 18,
},
blockExplorerUrls: ["https://polygonscan.com/"],
},
],
});
发布于 2021-12-14 17:22:03
尝试使用web3.utils.toHex()函数将十六进制转换为web3.utils.toHex:
chainId = '137'; chainId = web3.utils.toHex(chainId);
chainData = [{
chainId: chainId,
chainName: 'Matic(Polygon) Mainnet',
nativeCurrency: { name: 'MATIC', symbol: 'MATIC', decimals: 18 },
rpcUrls: ['https://polygon-rpc.com'],
blockExplorerUrls: ['https://www.polygonscan.com'],
}];
发布于 2021-12-29 20:46:34
import { utils } from 'ethers';
const networkMap = {
POLYGON_MAINNET: {
chainId: utils.hexValue(137), // '0x89'
chainName: "Matic(Polygon) Mainnet",
nativeCurrency: { name: "MATIC", symbol: "MATIC", decimals: 18 },
rpcUrls: ["https://polygon-rpc.com"],
blockExplorerUrls: ["https://www.polygonscan.com/"],
},
MUMBAI_TESTNET: {
chainId: utils.hexValue(80001), // '0x13881'
chainName: "Matic(Polygon) Mumbai Testnet",
nativeCurrency: { name: "tMATIC", symbol: "tMATIC", decimals: 18 },
rpcUrls: ["https://rpc-mumbai.maticvigil.com"],
blockExplorerUrls: ["https://mumbai.polygonscan.com/"],
},
};
await window.ethereum.request({
method: "wallet_addEthereumChain",
params: [networkMap.MUMBAI_TESTNET],
});
https://ethereum.stackexchange.com/questions/115565
复制相似问题