diff --git a/app/components/UI/Ramp/hooks/useBalance.ts b/app/components/UI/Ramp/hooks/useBalance.ts index f0e49c63e3a..de0ed400742 100644 --- a/app/components/UI/Ramp/hooks/useBalance.ts +++ b/app/components/UI/Ramp/hooks/useBalance.ts @@ -1,5 +1,5 @@ -import { hexToBN } from '@metamask/controller-utils'; import { useSelector } from 'react-redux'; +import { hexToBN } from '@metamask/controller-utils'; import { NATIVE_ADDRESS } from '../../../../constants/on-ramp'; import { selectAccountsByChainId } from '../../../../selectors/accountTrackerController'; import { @@ -19,13 +19,18 @@ import { weiToFiat, } from '../../../../util/number'; +const defaultReturn = { + balance: null, + balanceFiat: null, + balanceBN: null, +}; + interface Asset { address: string; decimals: number; } export default function useBalance(asset?: Asset) { - const assetAddress = safeToChecksumAddress(asset?.address); const accountsByChainId = useSelector(selectAccountsByChainId); const chainId = useSelector(selectChainId); const selectedAddress = useSelector( @@ -36,8 +41,14 @@ export default function useBalance(asset?: Asset) { const tokenExchangeRates = useSelector(selectContractExchangeRates); const balances = useSelector(selectContractBalances); - if (!asset || !assetAddress) { - return { balance: null, balanceFiat: null, balanceBN: null }; + if (!asset) { + return defaultReturn; + } + + const assetAddress = safeToChecksumAddress(asset.address); + + if (!assetAddress) { + return defaultReturn; } let balance, balanceFiat, balanceBN; @@ -66,7 +77,9 @@ export default function useBalance(asset?: Asset) { currentCurrency, ); balanceBN = - assetAddress && assetAddress in balances ? balances[assetAddress] : null; + assetAddress && assetAddress in balances + ? hexToBN(balances[assetAddress]) + : null; } return { balance, balanceFiat, balanceBN }; diff --git a/app/util/address/index.ts b/app/util/address/index.ts index 010dcebbfe9..3f4d39e509f 100644 --- a/app/util/address/index.ts +++ b/app/util/address/index.ts @@ -317,7 +317,7 @@ export function resemblesAddress(address: string) { export function safeToChecksumAddress(address: string) { if (!address) return undefined; - return toChecksumAddress(address); + return toChecksumAddress(address) as Hex; } /**