Skip to content

Commit

Permalink
fix: poll wallet balance (#3011)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmanu authored Dec 14, 2023
1 parent e4d19d2 commit f82d825
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
48 changes: 48 additions & 0 deletions src/hooks/wallets/__tests__/useWalletBalance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { act, renderHook, waitFor } from '@testing-library/react'
import useWalletBalance from '../useWalletBalance'
import * as web3 from '@/hooks/wallets/web3'
import * as useWallet from '../useWallet'

import { BigNumber } from 'ethers'
import type { JsonRpcProvider } from '@ethersproject/providers'
import { connectedWalletBuilder } from '@/tests/builders/wallet'
import { POLLING_INTERVAL } from '@/config/constants'

describe('useWalletBalance', () => {
beforeAll(() => {
jest.useFakeTimers()
})

afterAll(() => {
jest.useRealTimers()
})
it('should poll balance after interval passed', async () => {
jest.spyOn(web3, 'useWeb3ReadOnly').mockReturnValue({
getBalance: () => Promise.resolve(BigNumber.from(420)),
} as unknown as JsonRpcProvider)
jest.spyOn(useWallet, 'default').mockReturnValue(connectedWalletBuilder().build())

const { result } = renderHook(useWalletBalance)

await waitFor(() => {
expect(result.current[0]).toEqual(BigNumber.from(420))
})

jest.spyOn(web3, 'useWeb3ReadOnly').mockReturnValue({
getBalance: () => Promise.resolve(BigNumber.from(69)),
} as unknown as JsonRpcProvider)

// We only poll after the interval passed
await waitFor(() => {
expect(result.current[0]).toEqual(BigNumber.from(420))
})

act(() => {
jest.advanceTimersByTime(POLLING_INTERVAL)
})

await waitFor(() => {
expect(result.current[0]).toEqual(BigNumber.from(69))
})
})
})
26 changes: 20 additions & 6 deletions src/hooks/wallets/useWalletBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@ import useAsync, { type AsyncResult } from '../useAsync'
import useWallet from './useWallet'
import { useWeb3ReadOnly } from '@/hooks/wallets/web3'
import { type BigNumber } from 'ethers'
import useIntervalCounter from '../useIntervalCounter'
import { POLLING_INTERVAL } from '@/config/constants'
import { useEffect } from 'react'

const useWalletBalance = (): AsyncResult<BigNumber | undefined> => {
const web3ReadOnly = useWeb3ReadOnly()
const wallet = useWallet()
const [pollingInterval, resetPolling] = useIntervalCounter(POLLING_INTERVAL)

return useAsync<BigNumber | undefined>(() => {
if (!wallet || !web3ReadOnly) {
return undefined
}
// Reset polling if new wallet connects
useEffect(() => {
resetPolling()
}, [resetPolling, wallet, web3ReadOnly])

return web3ReadOnly.getBalance(wallet.address, 'latest')
}, [wallet, web3ReadOnly])
return useAsync<BigNumber | undefined>(
() => {
if (!wallet || !web3ReadOnly) {
return undefined
}

return web3ReadOnly.getBalance(wallet.address, 'latest')
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[wallet, web3ReadOnly, pollingInterval],
false,
)
}

export default useWalletBalance

0 comments on commit f82d825

Please sign in to comment.