Skip to content

Commit fc6ed4d

Browse files
committed
fix: fixed WalletConnect connection and disconnection
WalletConnect stores data on localStorage and they become persistent. However, the connection with the app stops working after a while. To fix that we cleanup the data once the user disconnects
1 parent 7261a32 commit fc6ed4d

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/adapters/web3-react/createHooks.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useEffect, useCallback } from 'react';
22
import { useSelector, useDispatch } from 'react-redux';
33
import { useWeb3React } from '@web3-react/core';
4+
import { WalletConnectConnector } from '@web3-react/walletconnect-connector';
45
import {
56
selectCurrentConnector,
67
selectActivatingConnector,
@@ -44,9 +45,18 @@ export default function createHooks({ connectors = {} } = {}) {
4445

4546
return useCallback(
4647
async connectorName => {
48+
const connector = connectors[connectorName];
49+
/**
50+
* WalletConnect provider doesn't work after user rejects the request the first time:
51+
* @see { @link https://github.com/NoahZinsmeister/web3-react/issues/217 }
52+
*/
53+
if (connector instanceof WalletConnectConnector && connector.walletConnectProvider?.wc?.uri) {
54+
connector.walletConnectProvider = undefined;
55+
}
56+
4757
try {
4858
dispatch(actions.activate.start({ name: connectorName }));
49-
await activate(connectors[connectorName], undefined, true);
59+
await activate(connector, undefined, true);
5060
dispatch(actions.activate.success({ name: connectorName }));
5161
} catch (err) {
5262
setError(err);
@@ -58,13 +68,20 @@ export default function createHooks({ connectors = {} } = {}) {
5868
}
5969

6070
function useDisconnectFromProvider() {
61-
const { deactivate } = useWeb3React();
71+
const { connector, deactivate } = useWeb3React();
6272
const dispatch = useDispatch();
6373

6474
return useCallback(() => {
75+
if (connector instanceof WalletConnectConnector) {
76+
/**
77+
* Cleans up wallet connect, otherwise it won't show the QR code when connecting again.
78+
*/
79+
window.localStorage.removeItem('walletconnect');
80+
}
81+
6582
deactivate();
6683
dispatch(actions.deactivate());
67-
}, [dispatch, deactivate]);
84+
}, [dispatch, connector, deactivate]);
6885
}
6986

7087
function useSyncToStore() {

src/shared/EthValue.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { useSelector } from 'react-redux';
44
import Web3 from 'web3';
55
import { selectChainId } from '~/features/web3/web3Slice';
66
import FormattedNumber from './FormattedNumber';
7+
import { defaultChainId } from '~/features/web3/supportedChains';
8+
9+
const DEFAULT_CHAIN_ID = process.env.DEFAULT_CHAIN_ID;
710

811
const { fromWei, toWei, toBN } = Web3.utils;
912

@@ -147,10 +150,12 @@ const canDisplay = ({ amount, unit, maxIntDigits, decimals }) => {
147150
return isIntPartOk && isFrPartOk;
148151
};
149152

150-
export const getBestDisplayUnit = ({ chainId, amount, maxIntDigits = 3, decimals = 2 }) => {
153+
export const getBestDisplayUnit = ({ chainId = DEFAULT_CHAIN_ID, amount, maxIntDigits = 3, decimals = 2 }) => {
151154
const defaultUnit = {
152155
unit: indexedAvailableUnits.ether.unit,
153-
suffix: indexedAvailableUnits.ether.chainIdToSuffixes[chainId],
156+
suffix:
157+
indexedAvailableUnits.ether.chainIdToSuffixes[chainId] ??
158+
indexedAvailableUnits.ether.chainIdToSuffixes[defaultChainId],
154159
};
155160

156161
if (canDisplay({ amount, maxIntDigits, decimals, unit: indexedAvailableUnits.ether.unit })) {

0 commit comments

Comments
 (0)