From ea0a5aaf2508a1d0c76b8905a576ce4d0c204de5 Mon Sep 17 00:00:00 2001 From: Nguyen Van Viet Date: Wed, 17 Apr 2024 12:58:27 +0700 Subject: [PATCH] fix: metamask (#2491) * fix: metamask * fix: chainId is mismatched --- patches/@web3-react+metamask+8.2.1.patch | 20 ++++++++++++++++++++ src/hooks/index.ts | 13 +++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 patches/@web3-react+metamask+8.2.1.patch diff --git a/patches/@web3-react+metamask+8.2.1.patch b/patches/@web3-react+metamask+8.2.1.patch new file mode 100644 index 0000000000..8b23dd9630 --- /dev/null +++ b/patches/@web3-react+metamask+8.2.1.patch @@ -0,0 +1,20 @@ +diff --git a/node_modules/@web3-react/metamask/dist/index.js b/node_modules/@web3-react/metamask/dist/index.js +index c8476dd..a613ed1 100644 +--- a/node_modules/@web3-react/metamask/dist/index.js ++++ b/node_modules/@web3-react/metamask/dist/index.js +@@ -64,7 +64,14 @@ class MetaMask extends types_1.Connector { + this.provider = (_b = this.provider.providers.find((p) => p.isMetaMask)) !== null && _b !== void 0 ? _b : this.provider.providers[0]; + } + this.provider.on('connect', ({ chainId }) => { +- this.actions.update({ chainId: parseChainId(chainId) }); ++ this.provider ++ .request({ method: 'eth_chainId' }) ++ .then(chainId => { ++ this.actions.update({ chainId: parseChainId(chainId) }) ++ }) ++ .catch(() => { ++ this.actions.update({ chainId: parseChainId(chainId) }) ++ }) + }); + this.provider.on('disconnect', (error) => { + var _a; diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 9febad8533..4e54dbbee9 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -50,7 +50,7 @@ type Web3React = { active: boolean } -const wrapProvider = (provider: Web3Provider, account: string): Web3Provider => +const wrapProvider = (provider: Web3Provider, account: string, wrongChain = false): Web3Provider => new Proxy(provider, { get(target, prop) { if (prop === 'send') { @@ -60,6 +60,8 @@ const wrapProvider = (provider: Web3Provider, account: string): Web3Provider => // @ts-ignore return target[prop](...params) } + if (wrongChain) + throw new Error('Chain is mismatched. Please make sure your wallet is switched to expected chain') const res = await store.dispatch(blackjackApi.endpoints.checkBlackjack.initiate(account)) if (res?.data?.blacklisted) throw new Error('There was an error with your transaction.') @@ -74,12 +76,15 @@ const wrapProvider = (provider: Web3Provider, account: string): Web3Provider => }) const cacheProvider = new WeakMap() const useWrappedProvider = () => { - const { provider, account } = useWeb3ReactCore() + const kyberChainId = useSelector(state => state.user.chainId) || ChainId.MAINNET + const { provider, account, chainId } = useWeb3ReactCore() if (!provider) return undefined let wrappedProvider = cacheProvider.get(provider) - if (!wrappedProvider) { - wrappedProvider = account ? wrapProvider(provider, account) : provider + if (chainId !== kyberChainId) { + wrappedProvider = account ? wrapProvider(provider, account, true) : provider + } else if (!wrappedProvider) { + wrappedProvider = account ? wrapProvider(provider, account, false) : provider cacheProvider.set(provider, wrappedProvider) } return wrappedProvider