fix: Handle scientific notation for NFT Details pricing (#6707)

* fix: Handle scientific notation for NFT Details pricing

* add test case

---------

Co-authored-by: Charles Bachmeier <charlie@genie.xyz>
This commit is contained in:
Charles Bachmeier 2023-06-09 09:10:40 -07:00 committed by GitHub
parent a4d61d8eaa
commit cb362f1b2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

@ -0,0 +1,38 @@
import { renderHook } from 'test-utils/render'
import { useNftAssetDetails } from './Details'
describe('useNftAssetDetails', () => {
it('should handle listing.price.value of 1e-18 without crashing', () => {
// Mock the useDetailsQuery hook
const mockUseDetailsQuery = jest.fn(() => ({
data: {
nftAssets: {
edges: [
{
node: {
listings: {
edges: [
{
node: {
price: {
value: 1e-18,
},
},
},
],
},
},
},
],
},
},
loading: false,
}))
jest.mock('../__generated__/types-and-hooks', () => ({
useDetailsQuery: mockUseDetailsQuery,
}))
const { result } = renderHook(() => useNftAssetDetails('address', 'tokenId'))
expect(result.current.data[0].priceInfo.ETHPrice).toBe('0')
})
})

@ -1,6 +1,7 @@
import { parseEther } from '@ethersproject/units'
import gql from 'graphql-tag'
import { CollectionInfoForAsset, GenieAsset, Markets, SellOrder } from 'nft/types'
import { wrapScientificNotation } from 'nft/utils'
import { useMemo } from 'react'
import { NftAsset, useDetailsQuery } from '../__generated__/types-and-hooks'
@ -105,7 +106,7 @@ export function useNftAssetDetails(
const asset = queryData?.nftAssets?.edges[0]?.node as NonNullable<NftAsset> | undefined
const collection = asset?.collection
const listing = asset?.listings?.edges[0]?.node
const ethPrice = parseEther(listing?.price?.value?.toString() ?? '0').toString()
const ethPrice = parseEther(wrapScientificNotation(listing?.price?.value?.toString() ?? '0')).toString()
return useMemo(
() => ({