Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LuckySwap
interface
Commits
3c6e067e
Unverified
Commit
3c6e067e
authored
Jan 12, 2023
by
Mike Grabowski
Committed by
GitHub
Jan 12, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: less confusing MetaMask extension/connection detection (#5816)
* fix * chore: remove
parent
fd1ee61d
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
50 additions
and
32 deletions
+50
-32
index.tsx
src/components/AccountDetails/index.tsx
+11
-6
index.test.tsx
src/components/WalletModal/index.test.tsx
+10
-10
index.tsx
src/components/WalletModal/index.tsx
+15
-9
index.tsx
src/components/Web3Status/index.tsx
+3
-3
utils.ts
src/connection/utils.ts
+11
-4
No files found.
src/components/AccountDetails/index.tsx
View file @
3c6e067e
import
{
Trans
}
from
'
@lingui/macro
'
import
{
Trans
}
from
'
@lingui/macro
'
import
{
useWeb3React
}
from
'
@web3-react/core
'
import
{
useWeb3React
}
from
'
@web3-react/core
'
import
{
getConnection
,
getConnectionName
,
getIsCoinbaseWallet
,
getIsMetaMask
}
from
'
connection/utils
'
import
{
getConnection
,
getConnectionName
,
getHasCoinbaseExtensionInstalled
,
getHasMetaMaskExtensionInstalled
,
}
from
'
connection/utils
'
import
{
useCallback
}
from
'
react
'
import
{
useCallback
}
from
'
react
'
import
{
ExternalLink
as
LinkIcon
}
from
'
react-feather
'
import
{
ExternalLink
as
LinkIcon
}
from
'
react-feather
'
import
{
useAppDispatch
}
from
'
state/hooks
'
import
{
useAppDispatch
}
from
'
state/hooks
'
...
@@ -210,14 +215,14 @@ export default function AccountDetails({
...
@@ -210,14 +215,14 @@ export default function AccountDetails({
const
theme
=
useTheme
()
const
theme
=
useTheme
()
const
dispatch
=
useAppDispatch
()
const
dispatch
=
useAppDispatch
()
const
isMetaMask
=
getIsMetaMask
()
const
hasMetaMaskExtension
=
getHasMetaMaskExtensionInstalled
()
const
isCoinbaseWallet
=
getIsCoinbaseWallet
()
const
hasCoinbaseExtension
=
getHasCoinbaseExtensionInstalled
()
const
isInjectedMobileBrowser
=
(
isMetaMask
||
isCoinbaseWallet
)
&&
isMobile
const
isInjectedMobileBrowser
=
(
hasMetaMaskExtension
||
hasCoinbaseExtension
)
&&
isMobile
function
formatConnectorName
()
{
function
formatConnectorName
()
{
return
(
return
(
<
WalletName
>
<
WalletName
>
<
Trans
>
Connected with
</
Trans
>
{
getConnectionName
(
connectionType
,
isMetaMask
)
}
<
Trans
>
Connected with
</
Trans
>
{
getConnectionName
(
connectionType
,
hasMetaMaskExtension
)
}
</
WalletName
>
</
WalletName
>
)
)
}
}
...
@@ -246,7 +251,7 @@ export default function AccountDetails({
...
@@ -246,7 +251,7 @@ export default function AccountDetails({
<
WalletAction
<
WalletAction
style=
{
{
fontSize
:
'
.825rem
'
,
fontWeight
:
400
,
marginRight
:
'
8px
'
}
}
style=
{
{
fontSize
:
'
.825rem
'
,
fontWeight
:
400
,
marginRight
:
'
8px
'
}
}
onClick=
{
()
=>
{
onClick=
{
()
=>
{
const
walletType
=
getConnectionName
(
getConnection
(
connector
).
type
,
getIsMetaMask
()
)
const
walletType
=
getConnectionName
(
getConnection
(
connector
).
type
)
if
(
connector
.
deactivate
)
{
if
(
connector
.
deactivate
)
{
connector
.
deactivate
()
connector
.
deactivate
()
}
else
{
}
else
{
...
...
src/components/WalletModal/index.test.tsx
View file @
3c6e067e
...
@@ -70,8 +70,8 @@ it('loads Wallet Modal on desktop', async () => {
...
@@ -70,8 +70,8 @@ it('loads Wallet Modal on desktop', async () => {
it
(
'
loads Wallet Modal on desktop with generic Injected
'
,
async
()
=>
{
it
(
'
loads Wallet Modal on desktop with generic Injected
'
,
async
()
=>
{
jest
.
spyOn
(
connectionUtils
,
'
getIsInjected
'
).
mockReturnValue
(
true
)
jest
.
spyOn
(
connectionUtils
,
'
getIsInjected
'
).
mockReturnValue
(
true
)
jest
.
spyOn
(
connectionUtils
,
'
get
IsMetaMask
'
).
mockReturnValue
(
false
)
jest
.
spyOn
(
connectionUtils
,
'
get
HasMetaMaskExtensionInstalled
'
).
mockReturnValue
(
false
)
jest
.
spyOn
(
connectionUtils
,
'
get
IsCoinbaseWallet
'
).
mockReturnValue
(
false
)
jest
.
spyOn
(
connectionUtils
,
'
get
HasCoinbaseExtensionInstalled
'
).
mockReturnValue
(
false
)
render
(<
WalletModal
pendingTransactions=
{
[]
}
confirmedTransactions=
{
[]
}
/>)
render
(<
WalletModal
pendingTransactions=
{
[]
}
confirmedTransactions=
{
[]
}
/>)
expect
(
screen
.
getByText
(
'
Browser Wallet
'
)).
toBeInTheDocument
()
expect
(
screen
.
getByText
(
'
Browser Wallet
'
)).
toBeInTheDocument
()
...
@@ -82,8 +82,8 @@ it('loads Wallet Modal on desktop with generic Injected', async () => {
...
@@ -82,8 +82,8 @@ it('loads Wallet Modal on desktop with generic Injected', async () => {
it
(
'
loads Wallet Modal on desktop with MetaMask installed
'
,
async
()
=>
{
it
(
'
loads Wallet Modal on desktop with MetaMask installed
'
,
async
()
=>
{
jest
.
spyOn
(
connectionUtils
,
'
getIsInjected
'
).
mockReturnValue
(
true
)
jest
.
spyOn
(
connectionUtils
,
'
getIsInjected
'
).
mockReturnValue
(
true
)
jest
.
spyOn
(
connectionUtils
,
'
get
IsMetaMask
'
).
mockReturnValue
(
true
)
jest
.
spyOn
(
connectionUtils
,
'
get
HasMetaMaskExtensionInstalled
'
).
mockReturnValue
(
true
)
jest
.
spyOn
(
connectionUtils
,
'
get
IsCoinbaseWallet
'
).
mockReturnValue
(
false
)
jest
.
spyOn
(
connectionUtils
,
'
get
HasCoinbaseExtensionInstalled
'
).
mockReturnValue
(
false
)
render
(<
WalletModal
pendingTransactions=
{
[]
}
confirmedTransactions=
{
[]
}
/>)
render
(<
WalletModal
pendingTransactions=
{
[]
}
confirmedTransactions=
{
[]
}
/>)
expect
(
screen
.
getByText
(
'
MetaMask
'
)).
toBeInTheDocument
()
expect
(
screen
.
getByText
(
'
MetaMask
'
)).
toBeInTheDocument
()
...
@@ -96,8 +96,8 @@ it('loads Wallet Modal on mobile', async () => {
...
@@ -96,8 +96,8 @@ it('loads Wallet Modal on mobile', async () => {
UserAgentMock
.
isMobile
=
true
UserAgentMock
.
isMobile
=
true
jest
.
spyOn
(
connectionUtils
,
'
getIsInjected
'
).
mockReturnValue
(
false
)
jest
.
spyOn
(
connectionUtils
,
'
getIsInjected
'
).
mockReturnValue
(
false
)
jest
.
spyOn
(
connectionUtils
,
'
get
IsMetaMask
'
).
mockReturnValue
(
false
)
jest
.
spyOn
(
connectionUtils
,
'
get
HasMetaMaskExtensionInstalled
'
).
mockReturnValue
(
false
)
jest
.
spyOn
(
connectionUtils
,
'
get
IsCoinbaseWallet
'
).
mockReturnValue
(
false
)
jest
.
spyOn
(
connectionUtils
,
'
get
HasCoinbaseExtensionInstalled
'
).
mockReturnValue
(
false
)
render
(<
WalletModal
pendingTransactions=
{
[]
}
confirmedTransactions=
{
[]
}
/>)
render
(<
WalletModal
pendingTransactions=
{
[]
}
confirmedTransactions=
{
[]
}
/>)
expect
(
screen
.
getByText
(
'
Open in Coinbase Wallet
'
)).
toBeInTheDocument
()
expect
(
screen
.
getByText
(
'
Open in Coinbase Wallet
'
)).
toBeInTheDocument
()
...
@@ -109,8 +109,8 @@ it('loads Wallet Modal on MetaMask browser', async () => {
...
@@ -109,8 +109,8 @@ it('loads Wallet Modal on MetaMask browser', async () => {
UserAgentMock
.
isMobile
=
true
UserAgentMock
.
isMobile
=
true
jest
.
spyOn
(
connectionUtils
,
'
getIsInjected
'
).
mockReturnValue
(
true
)
jest
.
spyOn
(
connectionUtils
,
'
getIsInjected
'
).
mockReturnValue
(
true
)
jest
.
spyOn
(
connectionUtils
,
'
get
IsMetaMask
'
).
mockReturnValue
(
true
)
jest
.
spyOn
(
connectionUtils
,
'
get
HasMetaMaskExtensionInstalled
'
).
mockReturnValue
(
true
)
jest
.
spyOn
(
connectionUtils
,
'
get
IsCoinbaseWallet
'
).
mockReturnValue
(
false
)
jest
.
spyOn
(
connectionUtils
,
'
get
HasCoinbaseExtensionInstalled
'
).
mockReturnValue
(
false
)
render
(<
WalletModal
pendingTransactions=
{
[]
}
confirmedTransactions=
{
[]
}
/>)
render
(<
WalletModal
pendingTransactions=
{
[]
}
confirmedTransactions=
{
[]
}
/>)
expect
(
screen
.
getByText
(
'
MetaMask
'
)).
toBeInTheDocument
()
expect
(
screen
.
getByText
(
'
MetaMask
'
)).
toBeInTheDocument
()
...
@@ -121,8 +121,8 @@ it('loads Wallet Modal on Coinbase Wallet browser', async () => {
...
@@ -121,8 +121,8 @@ it('loads Wallet Modal on Coinbase Wallet browser', async () => {
UserAgentMock
.
isMobile
=
true
UserAgentMock
.
isMobile
=
true
jest
.
spyOn
(
connectionUtils
,
'
getIsInjected
'
).
mockReturnValue
(
true
)
jest
.
spyOn
(
connectionUtils
,
'
getIsInjected
'
).
mockReturnValue
(
true
)
jest
.
spyOn
(
connectionUtils
,
'
get
IsMetaMask
'
).
mockReturnValue
(
false
)
jest
.
spyOn
(
connectionUtils
,
'
get
HasMetaMaskExtensionInstalled
'
).
mockReturnValue
(
false
)
jest
.
spyOn
(
connectionUtils
,
'
get
IsCoinbaseWallet
'
).
mockReturnValue
(
true
)
jest
.
spyOn
(
connectionUtils
,
'
get
HasCoinbaseExtensionInstalled
'
).
mockReturnValue
(
true
)
render
(<
WalletModal
pendingTransactions=
{
[]
}
confirmedTransactions=
{
[]
}
/>)
render
(<
WalletModal
pendingTransactions=
{
[]
}
confirmedTransactions=
{
[]
}
/>)
expect
(
screen
.
getByText
(
'
Coinbase Wallet
'
)).
toBeInTheDocument
()
expect
(
screen
.
getByText
(
'
Coinbase Wallet
'
)).
toBeInTheDocument
()
...
...
src/components/WalletModal/index.tsx
View file @
3c6e067e
...
@@ -7,7 +7,13 @@ import { sendEvent } from 'components/analytics'
...
@@ -7,7 +7,13 @@ import { sendEvent } from 'components/analytics'
import
{
AutoColumn
}
from
'
components/Column
'
import
{
AutoColumn
}
from
'
components/Column
'
import
{
AutoRow
}
from
'
components/Row
'
import
{
AutoRow
}
from
'
components/Row
'
import
{
networkConnection
}
from
'
connection
'
import
{
networkConnection
}
from
'
connection
'
import
{
getConnection
,
getConnectionName
,
getIsCoinbaseWallet
,
getIsInjected
,
getIsMetaMask
}
from
'
connection/utils
'
import
{
getConnection
,
getConnectionName
,
getHasCoinbaseExtensionInstalled
,
getHasMetaMaskExtensionInstalled
,
getIsInjected
,
}
from
'
connection/utils
'
import
usePrevious
from
'
hooks/usePrevious
'
import
usePrevious
from
'
hooks/usePrevious
'
import
{
useCallback
,
useEffect
,
useState
}
from
'
react
'
import
{
useCallback
,
useEffect
,
useState
}
from
'
react
'
import
{
ArrowLeft
}
from
'
react-feather
'
import
{
ArrowLeft
}
from
'
react-feather
'
...
@@ -204,7 +210,7 @@ export default function WalletModal({
...
@@ -204,7 +210,7 @@ export default function WalletModal({
// When new wallet is successfully set by the user, trigger logging of Amplitude analytics event.
// When new wallet is successfully set by the user, trigger logging of Amplitude analytics event.
useEffect
(()
=>
{
useEffect
(()
=>
{
if
(
account
&&
account
!==
lastActiveWalletAddress
)
{
if
(
account
&&
account
!==
lastActiveWalletAddress
)
{
const
walletType
=
getConnectionName
(
getConnection
(
connector
).
type
,
getIsMetaMask
()
)
const
walletType
=
getConnectionName
(
getConnection
(
connector
).
type
)
const
isReconnect
=
const
isReconnect
=
connectedWallets
.
filter
((
wallet
)
=>
wallet
.
account
===
account
&&
wallet
.
walletType
===
walletType
).
length
>
0
connectedWallets
.
filter
((
wallet
)
=>
wallet
.
account
===
account
&&
wallet
.
walletType
===
walletType
).
length
>
0
sendAnalyticsEventAndUserInfo
(
account
,
walletType
,
chainId
,
isReconnect
)
sendAnalyticsEventAndUserInfo
(
account
,
walletType
,
chainId
,
isReconnect
)
...
@@ -238,7 +244,7 @@ export default function WalletModal({
...
@@ -238,7 +244,7 @@ export default function WalletModal({
sendAnalyticsEvent
(
InterfaceEventName
.
WALLET_CONNECT_TXN_COMPLETED
,
{
sendAnalyticsEvent
(
InterfaceEventName
.
WALLET_CONNECT_TXN_COMPLETED
,
{
result
:
WalletConnectionResult
.
FAILED
,
result
:
WalletConnectionResult
.
FAILED
,
wallet_type
:
getConnectionName
(
connectionType
,
getIsMetaMask
()
),
wallet_type
:
getConnectionName
(
connectionType
),
})
})
}
}
},
},
...
@@ -247,11 +253,11 @@ export default function WalletModal({
...
@@ -247,11 +253,11 @@ export default function WalletModal({
function
getOptions
()
{
function
getOptions
()
{
const
isInjected
=
getIsInjected
()
const
isInjected
=
getIsInjected
()
const
isMetaMask
=
getIsMetaMask
()
const
hasMetaMaskExtension
=
getHasMetaMaskExtensionInstalled
()
const
isCoinbaseWallet
=
getIsCoinbaseWallet
()
const
hasCoinbaseExtension
=
getHasCoinbaseExtensionInstalled
()
const
isCoinbaseWalletBrowser
=
isMobile
&&
isCoinbaseWallet
const
isCoinbaseWalletBrowser
=
isMobile
&&
hasCoinbaseExtension
const
isMetaMaskBrowser
=
isMobile
&&
isMetaMask
const
isMetaMaskBrowser
=
isMobile
&&
hasMetaMaskExtension
const
isInjectedMobileBrowser
=
isCoinbaseWalletBrowser
||
isMetaMaskBrowser
const
isInjectedMobileBrowser
=
isCoinbaseWalletBrowser
||
isMetaMaskBrowser
let
injectedOption
let
injectedOption
...
@@ -259,8 +265,8 @@ export default function WalletModal({
...
@@ -259,8 +265,8 @@ export default function WalletModal({
if
(
!
isMobile
)
{
if
(
!
isMobile
)
{
injectedOption
=
<
InstallMetaMaskOption
/>
injectedOption
=
<
InstallMetaMaskOption
/>
}
}
}
else
if
(
!
isCoinbaseWallet
)
{
}
else
if
(
!
hasCoinbaseExtension
)
{
if
(
isMetaMask
)
{
if
(
hasMetaMaskExtension
)
{
injectedOption
=
<
MetaMaskOption
tryActivation=
{
tryActivation
}
/>
injectedOption
=
<
MetaMaskOption
tryActivation=
{
tryActivation
}
/>
}
else
{
}
else
{
injectedOption
=
<
InjectedOption
tryActivation=
{
tryActivation
}
/>
injectedOption
=
<
InjectedOption
tryActivation=
{
tryActivation
}
/>
...
...
src/components/Web3Status/index.tsx
View file @
3c6e067e
...
@@ -217,12 +217,12 @@ function Web3StatusInner() {
...
@@ -217,12 +217,12 @@ function Web3StatusInner() {
const
walletIsOpen
=
useModalIsOpen
(
ApplicationModal
.
WALLET_DROPDOWN
)
const
walletIsOpen
=
useModalIsOpen
(
ApplicationModal
.
WALLET_DROPDOWN
)
const
isClaimAvailable
=
useIsNftClaimAvailable
((
state
)
=>
state
.
isClaimAvailable
)
const
isClaimAvailable
=
useIsNftClaimAvailable
((
state
)
=>
state
.
isClaimAvailable
)
const
error
=
useAppSelector
((
state
)
=>
state
.
connection
.
errorByConnectionType
[
getConnection
(
connector
).
t
ype
])
const
error
=
useAppSelector
((
state
)
=>
state
.
connection
.
errorByConnectionType
[
connectionT
ype
])
useEffect
(()
=>
{
useEffect
(()
=>
{
if
(
getIsMetaMask
()
&&
error
)
{
if
(
getIsMetaMask
(
connectionType
)
&&
error
)
{
openMetamaskConnectionErrorModal
()
openMetamaskConnectionErrorModal
()
}
}
},
[
error
,
openMetamaskConnectionErrorModal
])
},
[
error
,
connectionType
,
openMetamaskConnectionErrorModal
])
const
allTransactions
=
useAllTransactions
()
const
allTransactions
=
useAllTransactions
()
...
...
src/connection/utils.ts
View file @
3c6e067e
...
@@ -12,14 +12,18 @@ export function getIsInjected(): boolean {
...
@@ -12,14 +12,18 @@ export function getIsInjected(): boolean {
return
Boolean
(
window
.
ethereum
)
return
Boolean
(
window
.
ethereum
)
}
}
export
function
get
IsMetaMask
():
boolean
{
export
function
get
HasMetaMaskExtensionInstalled
():
boolean
{
return
window
.
ethereum
?.
isMetaMask
??
false
return
window
.
ethereum
?.
isMetaMask
??
false
}
}
export
function
get
IsCoinbaseWallet
():
boolean
{
export
function
get
HasCoinbaseExtensionInstalled
():
boolean
{
return
window
.
ethereum
?.
isCoinbaseWallet
??
false
return
window
.
ethereum
?.
isCoinbaseWallet
??
false
}
}
export
function
getIsMetaMask
(
connectionType
:
ConnectionType
):
boolean
{
return
connectionType
===
ConnectionType
.
INJECTED
&&
getHasMetaMaskExtensionInstalled
()
}
const
CONNECTIONS
=
[
const
CONNECTIONS
=
[
gnosisSafeConnection
,
gnosisSafeConnection
,
injectedConnection
,
injectedConnection
,
...
@@ -50,10 +54,13 @@ export function getConnection(c: Connector | ConnectionType) {
...
@@ -50,10 +54,13 @@ export function getConnection(c: Connector | ConnectionType) {
}
}
}
}
export
function
getConnectionName
(
connectionType
:
ConnectionType
,
isMetaMask
?:
boolean
)
{
export
function
getConnectionName
(
connectionType
:
ConnectionType
,
hasMetaMaskExtension
:
boolean
=
getHasMetaMaskExtensionInstalled
()
)
{
switch
(
connectionType
)
{
switch
(
connectionType
)
{
case
ConnectionType
.
INJECTED
:
case
ConnectionType
.
INJECTED
:
return
isMetaMask
?
'
MetaMask
'
:
'
Browser Wallet
'
return
hasMetaMaskExtension
?
'
MetaMask
'
:
'
Browser Wallet
'
case
ConnectionType
.
COINBASE_WALLET
:
case
ConnectionType
.
COINBASE_WALLET
:
return
'
Coinbase Wallet
'
return
'
Coinbase Wallet
'
case
ConnectionType
.
WALLET_CONNECT
:
case
ConnectionType
.
WALLET_CONNECT
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment