Skip to content

Commit

Permalink
Fix: disconnect wallet if SDK fails to init (#1525)
Browse files Browse the repository at this point in the history
* Fix: disconnect wallet if SDK fails to init

* Rm initial wallet getter

* Fix test

* Fix e2e test

* Update src/hooks/coreSDK/useInitSafeCoreSDK.ts

Co-authored-by: Aaron Cook <[email protected]>

Co-authored-by: Aaron Cook <[email protected]>
  • Loading branch information
katspaugh and iamacook authored Jan 11, 2023
1 parent bd8c5b5 commit c233cbb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/components/common/ConnectWallet/AccountCenter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import css from '@/components/common/ConnectWallet/styles.module.css'
import EthHashInfo from '@/components/common/EthHashInfo'
import ExpandLessIcon from '@mui/icons-material/ExpandLess'
import ExpandMoreIcon from '@mui/icons-material/ExpandMore'
import useOnboard, { closeAccountSelectionModal, connectWallet } from '@/hooks/wallets/useOnboard'
import useOnboard, { switchWallet } from '@/hooks/wallets/useOnboard'
import { useAppSelector } from '@/store'
import { selectChainById } from '@/store/chainsSlice'
import Identicon from '@/components/common/Identicon'
Expand All @@ -23,8 +23,7 @@ const AccountCenter = ({ wallet }: { wallet: ConnectedWallet }) => {
const handleSwitchWallet = () => {
if (onboard) {
handleClose()
connectWallet(onboard)
closeAccountSelectionModal()
switchWallet(onboard)
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/hooks/coreSDK/__tests__/useInitSafeCoreSDK.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { useInitSafeCoreSDK } from '@/hooks/coreSDK/useInitSafeCoreSDK'
import * as useSafeInfo from '@/hooks/useSafeInfo'
import * as coreSDK from '@/hooks/coreSDK/safeCoreSDK'
import * as useWallet from '@/hooks/wallets/useWallet'
import * as useOnboard from '@/hooks/wallets/useOnboard'
import type { SafeInfo } from '@safe-global/safe-gateway-typescript-sdk'
import type { EIP1193Provider } from '@web3-onboard/core'
import type { EIP1193Provider, OnboardAPI } from '@web3-onboard/core'
import { act } from '@testing-library/react'
import type Safe from '@safe-global/safe-core-sdk'

Expand All @@ -30,11 +31,16 @@ describe('useInitSafeCoreSDK hook', () => {
provider: null as unknown as EIP1193Provider,
}

const mockOnboard = {
disconnectWallet: jest.fn(),
} as unknown as OnboardAPI

beforeEach(() => {
jest.clearAllMocks()

jest.spyOn(useSafeInfo, 'default').mockReturnValue(mockSafeInfo)
jest.spyOn(useWallet, 'default').mockReturnValue(mockWallet)
jest.spyOn(useOnboard, 'default').mockReturnValue(mockOnboard)
})

it('initializes a Core SDK instance', async () => {
Expand Down
11 changes: 8 additions & 3 deletions src/hooks/coreSDK/useInitSafeCoreSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import { trackError } from '@/services/exceptions'
import ErrorCodes from '@/services/exceptions/ErrorCodes'
import { useAppDispatch } from '@/store'
import { showNotification } from '@/store/notificationsSlice'
import useOnboard from '@/hooks/wallets/useOnboard'

export const useInitSafeCoreSDK = () => {
const wallet = useWallet()
const onboard = useOnboard()
const { safe, safeLoaded } = useSafeInfo()
const dispatch = useAppDispatch()

useEffect(() => {
if (!safeLoaded || !wallet?.provider || safe.chainId !== wallet.chainId || !safe.version) {
if (!onboard || !wallet?.provider || !safeLoaded || safe.chainId !== wallet.chainId || !safe.version) {
// If we don't reset the SDK, a previous Safe could remain in the store
setSafeSDK(undefined)
return
Expand All @@ -24,12 +26,15 @@ export const useInitSafeCoreSDK = () => {
.catch((e) => {
dispatch(
showNotification({
message: `The Safe SDK could not be initialized.`,
message: `The Safe SDK could not be initialized. Please try connecting the wallet again.`,
groupKey: 'core-sdk-init-error',
variant: 'error',
}),
)
trackError(ErrorCodes._105, (e as Error).message)

// Disconnect the wallet
onboard.disconnectWallet({ label: wallet.label })
})
}, [wallet?.provider, wallet?.chainId, safe.chainId, safe.address.value, safe.version, safeLoaded, dispatch])
}, [onboard, wallet, safe.chainId, safe.address.value, safe.version, safeLoaded, dispatch])
}
7 changes: 6 additions & 1 deletion src/hooks/wallets/useOnboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const connectWallet = async (onboard: OnboardAPI, options?: Parameters<On

// A workaround for an onboard "feature" that shows a defunct account select popup
// See https://github.com/blocknative/web3-onboard/issues/888
export const closeAccountSelectionModal = () => {
const closeAccountSelectionModal = () => {
const maxTries = 100
const modalText = 'Please switch the active account'
let tries = 0
Expand All @@ -127,6 +127,11 @@ export const closeAccountSelectionModal = () => {
}, 100)
}

export const switchWallet = (onboard: OnboardAPI) => {
connectWallet(onboard)
closeAccountSelectionModal()
}

// Disable/enable wallets according to chain and cache the last used wallet
export const useInitOnboard = () => {
const { configs } = useChains()
Expand Down

0 comments on commit c233cbb

Please sign in to comment.