-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
119 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,18 @@ | ||
import { Connector } from '@web3-react/types' | ||
import { useCallback } from 'react' | ||
|
||
import { coinbaseWallet, krystalWalletConnectV2, walletConnectV2 } from 'constants/connectors' | ||
import { LOCALSTORAGE_LAST_WALLETKEY_EVM, SUPPORTED_WALLETS } from 'constants/wallets' | ||
import { useActiveWeb3React, useWeb3React } from 'hooks' | ||
|
||
const disconnectEvmConnector: (connector: Connector | undefined) => void | Promise<void> = ( | ||
connector: Connector | undefined, | ||
) => { | ||
if (connector) { | ||
if (connector === coinbaseWallet) { | ||
return connector.resetState() | ||
} | ||
connector.deactivate?.() | ||
connector.resetState?.() | ||
if (connector === walletConnectV2 || connector === krystalWalletConnectV2) { | ||
// This key should be deleted when disconnected by walletconnect library | ||
// But it was deleted slowly, if user call connector.active() again before this key cleared, bug will appear | ||
// So we force remove it right after disconnected to preventing bug | ||
// todo: deep dive rootcause why it slowly delete | ||
localStorage.removeItem('wc@2:client:0.3//session') | ||
} | ||
} | ||
} | ||
import { useWeb3React } from 'hooks' | ||
import { useAppDispatch } from 'state/hooks' | ||
import { setRecentConnectionDisconnected } from 'state/user/actions' | ||
|
||
const useDisconnectWallet = () => { | ||
const { walletKey } = useActiveWeb3React() | ||
const dispatch = useAppDispatch() | ||
const { connector } = useWeb3React() | ||
return useCallback(async () => { | ||
const wallet = walletKey && SUPPORTED_WALLETS[walletKey] | ||
// If wallet support both network, disconnect to both | ||
if (wallet) { | ||
await disconnectEvmConnector(connector) | ||
localStorage.removeItem(LOCALSTORAGE_LAST_WALLETKEY_EVM) | ||
} else { | ||
await disconnectEvmConnector(connector) | ||
localStorage.removeItem(LOCALSTORAGE_LAST_WALLETKEY_EVM) | ||
} | ||
}, [connector, walletKey]) | ||
const disconnect = useCallback(() => { | ||
connector.deactivate?.() | ||
connector.resetState() | ||
dispatch(setRecentConnectionDisconnected()) | ||
}, [connector, dispatch]) | ||
|
||
return disconnect | ||
} | ||
export default useDisconnectWallet |
Oops, something went wrong.