synapsecns/sanguine

View on GitHub
packages/widget/src/hooks/useCurrentTokenBalance.ts

Summary

Maintainability
A
1 hr
Test Coverage
import { useContext, useMemo } from 'react'
import { Web3Context } from 'providers/Web3Provider'

import { useBridgeState } from '@/state/slices/bridge/hooks'
import { TokenBalance } from '@/utils/actions/fetchTokenBalances'
import { useWalletState } from '@/state/slices/wallet/hooks'

export const useCurrentTokenBalance = () => {
  const web3Context = useContext(Web3Context)
  const { connectedAddress } = web3Context.web3Provider
  const { originChainId, originToken } = useBridgeState()
  const { balances } = useWalletState()

  return useMemo(() => {
    if (!Array.isArray(balances) || !originToken || !originChainId) {
      return {
        rawBalance: null,
        parsedBalance: null,
        decimals: null,
      }
    } else {
      const matchedTokenBalance = balances?.find(
        (token: TokenBalance) =>
          token?.token?.addresses[originChainId] ===
          originToken?.addresses[originChainId]
      )
      const decimals: number =
        typeof matchedTokenBalance?.token?.decimals === 'number'
          ? matchedTokenBalance?.token?.decimals
          : matchedTokenBalance?.token?.decimals[originChainId]

      return {
        rawBalance: matchedTokenBalance?.balance,
        parsedBalance: matchedTokenBalance?.parsedBalance,
        decimals,
      }
    }
  }, [balances, originToken, originChainId, connectedAddress])
}