Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Switching network inconsistencies #82

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -189,15 +189,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
Loading