Skip to content

Commit

Permalink
add abort signal
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhoaidanh committed Aug 10, 2023
1 parent b394ec6 commit b80109f
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/hooks/bridge/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChainId, Currency, CurrencyAmount } from '@kyberswap/ks-sdk-core'
import JSBI from 'jsbi'
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'

import ERC20_INTERFACE from 'constants/abis/erc20'
import { NativeCurrencies } from 'constants/tokens'
Expand All @@ -17,14 +17,22 @@ export const useEthBalanceOfAnotherChain = (chainId: ChainId | undefined) => {
const { readProvider } = useKyberSwapConfig(chainId)
const { account } = useActiveWeb3React()
const [balance, setBalance] = useState<CurrencyAmount<Currency>>()
const controller = useRef(new AbortController())

useEffect(() => {
controller.current.abort()
controller.current = new AbortController()
const signal = controller.current.signal
async function getBalance() {
try {
if (!readProvider || !account || !chainId) {
setBalance(undefined)
return
}
const balance = await readProvider.getBalance(account)
if (signal.aborted) {
return
}
setBalance(CurrencyAmount.fromRawAmount(NativeCurrencies[chainId], JSBI.BigInt(balance)))
} catch (error) {
setBalance(undefined)
Expand Down

0 comments on commit b80109f

Please sign in to comment.