Skip to content

Commit

Permalink
Fix: Switching network inconsistencies (#82)
Browse files Browse the repository at this point in the history
* fix switching networks

* deleted hook that caused multiple calls to connect

* remove ts-ignore
  • Loading branch information
ricardocr987 authored Feb 12, 2024
1 parent 18cba09 commit d17e03f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 45 deletions.
53 changes: 17 additions & 36 deletions src/hooks/common/useConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ export type UseConnectReturn = {
disconnect: () => Promise<void>
isConnected: boolean
account: Account | undefined
tryReconnect: () => Promise<void>
switchNetwork: (chain: Chain) => Promise<Account | undefined>
selectedNetwork: Chain
}

export function useConnect(): UseConnectReturn {
const [state, dispatch] = useAppState()
const noti = useNotification()
const [keepAccountAlive, setKeepAccountAlive] = useSessionStorage(
'keepAccountAlive',
false,
)
const [selectedNetwork, setSelectedNetwork] = useSessionStorage<Chain>(
'selectedNetwork',
Chain.ETH,
Expand Down Expand Up @@ -55,13 +50,10 @@ export function useConnect(): UseConnectReturn {

const connect = useCallback(
async (chain?: Chain, provider?: ExternalProvider) => {
if (!chain) return

let account
try {
if (chain) {
setSelectedNetwork(chain)
} else {
chain = selectedNetwork
}
if (!provider && window.ethereum) {
provider = window.ethereum
}
Expand All @@ -71,27 +63,12 @@ export function useConnect(): UseConnectReturn {
// provider = window.solana
// }
account = await web3Connect(chain, provider)
setSelectedNetwork(chain)
} catch (err) {
const e = err as Error

onError(e.message) // we assume because the user denied the connection
// @todo: remove ugly hack because of weird selectedNetwork behavior
try {
if (chain === Chain.ETH) {
account = await web3Connect(Chain.AVAX, provider)
setSelectedNetwork(Chain.AVAX)
} else {
account = await web3Connect(Chain.ETH, provider)
setSelectedNetwork(Chain.ETH)
}
} catch (err) {
const e = err as Error

onError(e.message) // we got fucked
}
}
if (!account) return
setKeepAccountAlive(true)

await Promise.all([getBalance(account)]).catch((err) => {
onError(err.message)
Expand All @@ -101,35 +78,39 @@ export function useConnect(): UseConnectReturn {

return account
},
[setKeepAccountAlive, getBalance, dispatch, onError],
[getBalance, dispatch, onError],
)

const disconnect = useCallback(async () => {
setKeepAccountAlive(false)
dispatch({ type: ActionTypes.disconnect, payload: null })
}, [dispatch, setKeepAccountAlive])
}, [dispatch])

const switchNetwork = useCallback(
async (chain: Chain) => {
return await connect(chain)
let account

try {
account = await web3Connect(chain, window.ethereum)
setSelectedNetwork(chain)
console.log('Account connected after switching network: ', account)
} catch (err) {
const e = err as Error
console.error('Error during network switch: ', e.message)
}

return account
},
[connect, setSelectedNetwork],
)

const { account } = state
const isConnected = !!account?.address

const tryReconnect = useCallback(async () => {
if (isConnected || !keepAccountAlive) return
await connect()
}, [isConnected, keepAccountAlive, connect])

return {
connect,
disconnect,
isConnected,
account,
tryReconnect,
switchNetwork,
selectedNetwork,
}
Expand Down
9 changes: 0 additions & 9 deletions src/hooks/pages/useHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,6 @@ export function useHeader(): UseHeaderReturn {
[connect, disconnect, isConnected, router, setkeepAccountAlive],
)

useEffect(() => {
;(async () => {
if (!account && keepAccountAlive) {
enableConnection()
}
})()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [account, keepAccountAlive])

// --------------------

const provider = () => {
Expand Down

0 comments on commit d17e03f

Please sign in to comment.