Skip to content

Commit

Permalink
fix: handle case where decimals are 0
Browse files Browse the repository at this point in the history
  • Loading branch information
dexturr committed Mar 6, 2024
1 parent 05bc3c9 commit 5502b8b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 14 additions & 0 deletions frontend/hooks/format-asset-amount/format-asset-amount.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
3 changes: 2 additions & 1 deletion frontend/hooks/format-asset-amount/format-asset-amount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 5502b8b

Please sign in to comment.