From 5502b8bbaf1efe0debdce42a0b4f859834a08531 Mon Sep 17 00:00:00 2001 From: Dexter Date: Tue, 5 Mar 2024 20:14:32 +0300 Subject: [PATCH] fix: handle case where decimals are 0 --- .../format-asset-amount.spec.tsx | 14 ++++++++++++++ .../format-asset-amount/format-asset-amount.tsx | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/frontend/hooks/format-asset-amount/format-asset-amount.spec.tsx b/frontend/hooks/format-asset-amount/format-asset-amount.spec.tsx index 4f4d51670..7e965b1b7 100644 --- a/frontend/hooks/format-asset-amount/format-asset-amount.spec.tsx +++ b/frontend/hooks/format-asset-amount/format-asset-amount.spec.tsx @@ -66,4 +66,18 @@ describe('useFormatAssetAmount', () => { expect(result.current?.formattedAmount).toBe('0.000000000123') expect(result.current?.symbol).toBe('SYMBOL') }) + + it('handles decimals as 0', () => { + mockStore(useAssetsStore, { + getAssetById: () => ({ + details: { + symbol: 'SYMBOL', + decimals: 0 + } + }) + }) + const { result } = renderHook(() => useFormatAssetAmount('foo', '123')) + expect(result.current?.formattedAmount).toBe('123') + expect(result.current?.symbol).toBe('SYMBOL') + }) }) diff --git a/frontend/hooks/format-asset-amount/format-asset-amount.tsx b/frontend/hooks/format-asset-amount/format-asset-amount.tsx index ee90a7dc1..1e634e4c6 100644 --- a/frontend/hooks/format-asset-amount/format-asset-amount.tsx +++ b/frontend/hooks/format-asset-amount/format-asset-amount.tsx @@ -12,7 +12,8 @@ export const useFormatAssetAmount = (assetId?: string, amount?: string) => { const assetInfo = getAssetById(assetId) const decimals = Number(get(assetInfo, 'details.decimals')) const symbol = get(assetInfo, 'details.symbol') - if (!symbol || !decimals) + const noDecimals = !decimals && decimals !== 0 + if (!symbol || noDecimals) throw new Error(`Could not find amount, decimals or symbol when trying to render transaction for asset ${assetId}`) const formattedAmount = formatNumber(toBigNum(amount, decimals), decimals) return { formattedAmount, symbol }