Skip to content

Commit

Permalink
fix: preserve Safe App connection after new EOA connections (balancer…
Browse files Browse the repository at this point in the history
  • Loading branch information
agualis authored Aug 27, 2024
1 parent 1a4cbe4 commit 7a66d62
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/modules/web3/UserAccountProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { setTag, setUser } from '@sentry/nextjs'
import { config, isProd } from '@/lib/config/app.config'
import { captureError, ensureError } from '@/lib/shared/utils/errors'
import { useIsMounted } from '@/lib/shared/hooks/useIsMounted'
import { useSafeAppConnectionGuard } from './useSafeAppConnectionGuard'

async function isAuthorizedAddress(address: Address): Promise<boolean> {
try {
Expand Down Expand Up @@ -71,6 +72,8 @@ export function _useUserAccount() {
isBlocked,
}

useSafeAppConnectionGuard(result.connector, result.chainId)

useEffect(() => {
if (result.userAddress) {
setUser({
Expand Down
17 changes: 17 additions & 0 deletions lib/modules/web3/useSafeAppConnectionGuard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable react-hooks/exhaustive-deps */
import { Connector, useConnect } from 'wagmi'
import { useEffect } from 'react'

/*
If the app is running as a Safe App and an EOA connection is created in another tab,
this hook will ensure that the Safe App tab keeps connected to the Safe account.
*/
export function useSafeAppConnectionGuard(newConnector?: Connector, chainId?: number) {
const { connect, connectors } = useConnect()
useEffect(() => {
const safeConnector = connectors.find(c => c.id === 'safe')
if (newConnector && newConnector?.id !== 'safe' && safeConnector) {
connect({ chainId, connector: safeConnector })
}
}, [newConnector])
}

0 comments on commit 7a66d62

Please sign in to comment.