From 66484c5106aece10f7c41e976beb5cb7993b3796 Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Tue, 5 Dec 2023 20:52:55 +0100 Subject: [PATCH] feat: added checking if ethereum provider is metamask --- src/app/hooks/use-ethereum.ts | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/app/hooks/use-ethereum.ts b/src/app/hooks/use-ethereum.ts index 42900ada..c112a6f9 100644 --- a/src/app/hooks/use-ethereum.ts +++ b/src/app/hooks/use-ethereum.ts @@ -132,8 +132,11 @@ export function useEthereum(): UseEthereumReturnType { newEthereumNetwork: EthereumNetwork, ): Promise { const { ethereum } = window; + const metamaskProvider = ethereum.providers.find( + (provider: any) => provider.isMetaMask, + ); try { - await ethereum.request({ + await metamaskProvider.request({ method: "wallet_addEthereumChain", params: addNetworkParams[newEthereumNetwork], }); @@ -146,8 +149,11 @@ export function useEthereum(): UseEthereumReturnType { newEthereumNetwork: EthereumNetwork, ): Promise { const { ethereum } = window; + const metamaskProvider = ethereum.providers.find( + (provider: any) => provider.isMetaMask, + ); try { - await ethereum.request({ + await metamaskProvider.request({ method: "wallet_switchEthereumChain", params: [{ chainId: hexChainIDs[newEthereumNetwork] }], }); @@ -178,7 +184,7 @@ export function useEthereum(): UseEthereumReturnType { try { const { ethereum } = window; const browserProvider = new ethers.providers.Web3Provider( - ethereum, + ethereum.providers.find((provider: any) => provider.isMetaMask), "any", ); const signer = browserProvider.getSigner(); @@ -244,12 +250,16 @@ export function useEthereum(): UseEthereumReturnType { try { const { ethereum } = window; - if (!ethereum) { + const metamaskProvider = ethereum.providers.find( + (provider: any) => provider.isMetaMask, + ); + + if (!metamaskProvider) { alert("Install MetaMask!"); return; } - const ethereumAccounts = await ethereum.request({ + const ethereumAccounts = await metamaskProvider.request({ method: "eth_requestAccounts", }); @@ -400,8 +410,11 @@ export function useEthereum(): UseEthereumReturnType { async function recommendTokenToMetamask(): Promise { try { const { ethereum } = window; - if (!ethereum) return false; - const response = await ethereum.request({ + const metamaskProvider = ethereum.providers.find( + (provider: any) => provider.isMetaMask, + ); + if (!metamaskProvider) return false; + const response = await metamaskProvider.request({ method: "wallet_watchAsset", params: { type: "ERC20",