This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 363
fix: better network comparison + WC peer tracking #3729
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { Dispatch } from 'redux' | ||
import { Action } from 'redux-actions' | ||
import WalletConnectProvider from '@walletconnect/web3-provider' | ||
|
||
import { store as reduxStore } from 'src/store' | ||
import { enhanceSnackbarForAction, NOTIFICATIONS } from 'src/logic/notifications' | ||
|
@@ -10,48 +11,45 @@ import { providerSelector } from '../selectors' | |
import { trackEvent } from 'src/utils/googleTagManager' | ||
import { WALLET_EVENTS } from 'src/utils/events/wallet' | ||
import { instantiateSafeContracts } from 'src/logic/contracts/safeContracts' | ||
import { resetWeb3, setWeb3 } from '../../getWeb3' | ||
import onboard, { removeLastUsedProvider, saveLastUsedProvider } from '../../onboard' | ||
import { resetWeb3, setWeb3 } from 'src/logic/wallets/getWeb3' | ||
import onboard, { removeLastUsedProvider, saveLastUsedProvider } from 'src/logic/wallets/onboard' | ||
import { WALLET_CONNECT_MODULE_NAME } from 'src/logic/wallets/patchedWalletConnect' | ||
import { checksumAddress } from 'src/utils/checksumAddress' | ||
import { shouldSwitchNetwork } from 'src/logic/wallets/utils/network' | ||
|
||
let hasWalletName = false | ||
let hasAccount = false | ||
let hasNetwork = false | ||
const UNKNOWN_PEER = 'Unknown' | ||
|
||
const providerMiddleware = | ||
(store: ReturnType<typeof reduxStore>) => | ||
(next: Dispatch) => | ||
async (action: Action<ProviderPayloads>): Promise<Action<ProviderPayloads>> => { | ||
const handledAction = next(action) | ||
|
||
const { type, payload } = action | ||
|
||
// Onboard sends provider details via separate subscriptions: wallet, account, network | ||
// Payloads from all three need to be combined to be `loaded` and `available` | ||
if (type === PROVIDER_ACTIONS.WALLET_NAME) { | ||
hasWalletName = !!payload | ||
} else if (type === PROVIDER_ACTIONS.ACCOUNT) { | ||
hasAccount = !!payload | ||
} else if (type === PROVIDER_ACTIONS.NETWORK) { | ||
hasNetwork = !!payload | ||
} else { | ||
// Dispatched actions from reducers unrelated to wallet | ||
const isProviderAction = [ | ||
PROVIDER_ACTIONS.WALLET_NAME, | ||
PROVIDER_ACTIONS.ACCOUNT, | ||
PROVIDER_ACTIONS.NETWORK, | ||
].includes(action.type as PROVIDER_ACTIONS) | ||
|
||
// Prevent other dispatches from triggering this middleware | ||
if (!isProviderAction) { | ||
return handledAction | ||
} | ||
|
||
// No wallet is connected via onboard | ||
if (!hasWalletName && !hasAccount && !hasNetwork) { | ||
const state = store.getState() | ||
const { name, account, network, loaded, available } = providerSelector(state) | ||
|
||
// No wallet is connected via onboard, reset provider | ||
if (!name && !account && !network) { | ||
resetWeb3() | ||
removeLastUsedProvider() | ||
} | ||
|
||
// Wallet 'partially' connected: only a subset of onboard subscription(s) have fired | ||
if (!hasWalletName || !hasAccount || !hasNetwork) { | ||
if (!name || !account || !network) { | ||
return handledAction | ||
} | ||
|
||
const state = store.getState() | ||
const { available, loaded, name, account } = providerSelector(state) | ||
|
||
// @TODO: `loaded` flag that is/was always set to true - should be moved to wallet connection catch | ||
// Wallet, account and network did not successfully load | ||
if (!loaded) { | ||
|
@@ -64,18 +62,27 @@ const providerMiddleware = | |
return handledAction | ||
} | ||
|
||
const { wallet, address } = onboard().getState() | ||
|
||
if (name === wallet.name) { | ||
saveLastUsedProvider(name) | ||
} | ||
|
||
// Instantiate web3/contract | ||
const { wallet } = onboard().getState() | ||
if (wallet.provider) { | ||
setWeb3(wallet.provider) | ||
instantiateSafeContracts() | ||
} | ||
|
||
if (name) { | ||
saveLastUsedProvider(name) | ||
// Only track when account has been successfully saved to store | ||
if (payload === account) { | ||
trackEvent({ ...WALLET_EVENTS.CONNECT, label: name }) | ||
// Only track when store/UI is in sync with onboard | ||
if (account === checksumAddress(address) && !shouldSwitchNetwork(wallet)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
trackEvent({ ...WALLET_EVENTS.CONNECT, label: name }) | ||
// Track WalletConnect peer wallet | ||
if (name === WALLET_CONNECT_MODULE_NAME) { | ||
trackEvent({ | ||
...WALLET_EVENTS.WALLET_CONNECT, | ||
label: (wallet.provider as InstanceType<typeof WalletConnectProvider>)?.wc?.peerMeta?.name || UNKNOWN_PEER, | ||
}) | ||
} | ||
} | ||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was not the correct event key.