Skip to content

Commit

Permalink
Move getCollectiblePrices to kit package
Browse files Browse the repository at this point in the history
  • Loading branch information
corbanbrook committed Apr 30, 2024
1 parent 0ef9f91 commit 6eaeecc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 62 deletions.
30 changes: 20 additions & 10 deletions packages/kit/src/hooks/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,6 @@ export const useExchangeRate = (toCurrency: string) => {
})
}

// const getCoinPrices = async (apiClient: SequenceAPIClient, tokens: Token[]) => {
// if (args.tokens.length === 0) {
// return []
// }

// const res = await apiClient.getCoinPrices(args)

// return res?.tokenPrices || []
// }

export const useCoinPrices = (tokens: Token[]) => {
const apiClient = useAPIClient()

Expand All @@ -175,6 +165,26 @@ export const useCoinPrices = (tokens: Token[]) => {
})
}

export const useCollectiblePrices = (tokens: Token[]) => {
const apiClient = useAPIClient()

return useQuery({
queryKey: ['useCollectiblePrices', tokens],
queryFn: async () => {
if (tokens.length === 0) {
return []
}

const res = await apiClient.getCollectiblePrices({ tokens })

return res?.tokenPrices || []
},
retry: true,
staleTime: time.oneMinute,
enabled: tokens.length > 0
})
}

export const useTokenMetadata = (chainId: number, contractAddress: string, tokenIds: string[]) => {
const metadataClient = useMetadataClient()

Expand Down
22 changes: 0 additions & 22 deletions packages/wallet/src/api/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,28 +385,6 @@ export const getCollectibleBalance = async ({
return tokenBalance
}

export interface GetCollectiblePricesArgs {
tokens: Token[]
}

export const getCollectiblePrices = async ({ tokens }: GetCollectiblePricesArgs) => {
try {
if (tokens.length === 0) return []
const chainId = tokens[0].chainId

const { apiClient } = await getNetworkConfigAndClients(chainId)

const res = await apiClient.getCollectiblePrices({
tokens
})

return res?.tokenPrices || []
} catch (e) {
console.error(e)
return
}
}

export interface GetTransactionHistoryArgs {
chainId: number
accountAddress: string
Expand Down
15 changes: 1 addition & 14 deletions packages/wallet/src/hooks/data.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { ethers } from 'ethers'
import { useQuery, useInfiniteQuery } from '@tanstack/react-query'
import { useQuery } from '@tanstack/react-query'
import {
GetTokenBalancesArgs,
fetchBalancesAssetsSummary,
getNativeToken,
getTokenBalances,
getCollectibleBalance,
GetCollectibleBalanceArgs,
getCollectiblePrices,
GetCollectiblePricesArgs,
getTransactionHistory,
GetTransactionHistoryArgs,
GetTokenBalancesOptions,
FetchBalancesAssetsArgs,
getTransactionHistorySummary,
Expand Down Expand Up @@ -64,15 +60,6 @@ export const useCollectibleBalance = (args: GetCollectibleBalanceArgs) =>
enabled: !!args.chainId && !!args.accountAddress && !!args.collectionAddress && !!args.tokenId
})

export const useCollectiblePrices = (args: GetCollectiblePricesArgs) =>
useQuery({
queryKey: ['useCollectiblePrices', args],
queryFn: () => getCollectiblePrices(args),
retry: true,
staleTime: time.oneMinute,
enabled: args.tokens.length > 0
})

export const useTransactionHistorySummary = (args: GetTransactionHistorySummaryArgs) =>
useQuery({
queryKey: ['transactionHistorySummary', args],
Expand Down
20 changes: 9 additions & 11 deletions packages/wallet/src/views/CollectibleDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React from 'react'
import { ethers } from 'ethers'
import { useAccount, useConfig } from 'wagmi'
import { Box, Button, Image, SendIcon, Text } from '@0xsequence/design-system'
import { getNativeTokenInfoByChainId, useExchangeRate, useTransactionHistory } from '@0xsequence/kit'
import { getNativeTokenInfoByChainId, useExchangeRate, useTransactionHistory, useCollectiblePrices } from '@0xsequence/kit'

import { CollectibleDetailsSkeleton } from './Skeleton'

import { computeBalanceFiat, formatDisplay, flattenPaginatedTransactionHistory } from '../../utils'
import { useCollectiblePrices, useCollectibleBalance, useSettings, useNavigation } from '../../hooks'
import { useCollectibleBalance, useSettings, useNavigation } from '../../hooks'
import { InfiniteScroll } from '../../shared/InfiniteScroll'
import { TransactionHistoryList } from '../../shared/TransactionHistoryList'
import { CollectibleTileImage } from '../../shared/CollectibleTileImage'
Expand Down Expand Up @@ -50,15 +50,13 @@ export const CollectibleDetails = ({ contractAddress, chainId, tokenId }: Collec
verifiedOnly: false
})

const { data: dataCollectiblePrices, isPending: isPendingCollectiblePrices } = useCollectiblePrices({
tokens: [
{
chainId,
contractAddress,
tokenId
}
]
})
const { data: dataCollectiblePrices, isPending: isPendingCollectiblePrices } = useCollectiblePrices([
{
chainId,
contractAddress,
tokenId
}
])

const { data: conversionRate = 1, isPending: isPendingConversionRate } = useExchangeRate(fiatCurrency.symbol)

Expand Down
8 changes: 3 additions & 5 deletions packages/wallet/src/views/TransactionDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ethers } from 'ethers'
import { Token } from '@0xsequence/api'
import { Transaction, TxnTransfer } from '@0xsequence/indexer'
import { ArrowRightIcon, Box, Button, Divider, GradientAvatar, Image, LinkIcon, Text } from '@0xsequence/design-system'
import { getNativeTokenInfoByChainId, useExchangeRate, useCoinPrices } from '@0xsequence/kit'
import { getNativeTokenInfoByChainId, useExchangeRate, useCoinPrices, useCollectiblePrices } from '@0xsequence/kit'
import dayjs from 'dayjs'
import { useConfig } from 'wagmi'

Expand All @@ -12,7 +12,7 @@ import { CopyButton } from '../../shared/CopyButton'
import { NetworkBadge } from '../../shared/NetworkBadge'
import { Skeleton } from '../../shared/Skeleton'
import { compareAddress, formatDisplay } from '../../utils'
import { useCollectiblePrices, useSettings } from '../../hooks'
import { useSettings } from '../../hooks'

interface TransactionDetailProps {
transaction: Transaction
Expand Down Expand Up @@ -57,9 +57,7 @@ export const TransactionDetails = ({ transaction }: TransactionDetailProps) => {

const { data: coinPricesData, isPending: isPendingCoinPrices } = useCoinPrices(coins)

const { data: collectiblePricesData, isPending: isPendingCollectiblePrices } = useCollectiblePrices({
tokens: collectibles
})
const { data: collectiblePricesData, isPending: isPendingCollectiblePrices } = useCollectiblePrices(collectibles)

const { data: conversionRate = 1, isPending: isPendingConversionRate } = useExchangeRate(fiatCurrency.symbol)

Expand Down

0 comments on commit 6eaeecc

Please sign in to comment.