Skip to content

Commit

Permalink
fix switching chains in metamask (DimensionDev#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
artdgn committed Aug 24, 2021
1 parent 0f86ee5 commit ceba296
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions packages/react-app/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,28 @@ function App(props) {
},
];
console.log("data", data);
const tx = await ethereum.request({ method: "wallet_addEthereumChain", params: data }).catch();
if (tx) {
console.log(tx);

let switchTx;
// https://docs.metamask.io/guide/rpc-api.html#other-rpc-methods
try {
switchTx = await ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: data[0].chainId }],
});
} catch (switchError) {
// not checking specific error code, because maybe we're not using MetaMask
try {
switchTx = await ethereum.request({
method: 'wallet_addEthereumChain',
params: data,
});
} catch (addError) {
// handle "add" error
}
}

if (switchTx) {
console.log(switchTx);
}
}}
>
Expand Down

0 comments on commit ceba296

Please sign in to comment.