Skip to content

Commit

Permalink
Refactored custom wallet hooks for asset and history summaries to use…
Browse files Browse the repository at this point in the history
… shared methods from kit package
  • Loading branch information
corbanbrook committed Apr 30, 2024
1 parent d017e3f commit d1d3855
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 473 deletions.
74 changes: 40 additions & 34 deletions packages/kit/src/hooks/data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Token } from '@0xsequence/api'
import { SequenceAPIClient, Token } from '@0xsequence/api'
import { useInfiniteQuery, useQuery } from '@tanstack/react-query'

import { useMetadataClient } from './useMetadataClient'
Expand All @@ -15,7 +15,7 @@ export const time = {
oneHour: 60 * 60 * 1000
}

const getNativeTokenBalance = async (indexerClient: SequenceIndexer, chainId: number, accountAddress: string) => {
export const getNativeTokenBalance = async (indexerClient: SequenceIndexer, chainId: number, accountAddress: string) => {
const res = await indexerClient.getEtherBalance({ accountAddress })

const tokenBalance: TokenBalance = {
Expand All @@ -40,7 +40,7 @@ interface GetTokenBalancesArgs {
contractAddress?: string
}

const getTokenBalances = async (indexerClient: SequenceIndexer, args: GetTokenBalancesArgs) => {
export const getTokenBalances = async (indexerClient: SequenceIndexer, args: GetTokenBalancesArgs) => {
const res = await indexerClient.getTokenBalances({
accountAddress: args.accountAddress,
includeMetadata: args.includeMetadata ?? true,
Expand All @@ -53,7 +53,7 @@ const getTokenBalances = async (indexerClient: SequenceIndexer, args: GetTokenBa
return res?.balances || []
}

const getBalances = async (indexerClient: SequenceIndexer, chainId: number, args: GetTokenBalancesArgs) => {
export const getBalances = async (indexerClient: SequenceIndexer, chainId: number, args: GetTokenBalancesArgs) => {
if (!args.accountAddress) {
return []
}
Expand Down Expand Up @@ -151,6 +151,19 @@ export const useCollectibleBalance = (args: UseCollectibleBalanceArgs) => {
})
}

export const getCollectionBalance = async (indexerClient: SequenceIndexer, args: UseCollectionBalanceArgs) => {
const res = await indexerClient.getTokenBalances({
accountAddress: args.accountAddress,
contractAddress: args.contractAddress,
includeMetadata: args.includeMetadata ?? true,
metadataOptions: {
verifiedOnly: args.verifiedOnly ?? true
}
})

return res?.balances || []
}

interface UseCollectionBalanceArgs {
chainId: number
accountAddress: string
Expand All @@ -164,18 +177,7 @@ export const useCollectionBalance = (args: UseCollectionBalanceArgs) => {

return useQuery({
queryKey: ['collectionBalance', args],
queryFn: async () => {
const res = await indexerClient.getTokenBalances({
accountAddress: args.accountAddress,
contractAddress: args.contractAddress,
includeMetadata: args.includeMetadata ?? true,
metadataOptions: {
verifiedOnly: args.verifiedOnly ?? true
}
})

return res?.balances || []
},
queryFn: () => getCollectionBalance(indexerClient, args),
retry: true,
staleTime: time.oneSecond * 30,
enabled: !!args.chainId && !!args.accountAddress && !!args.contractAddress
Expand All @@ -202,40 +204,44 @@ export const useExchangeRate = (toCurrency: string) => {
})
}

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

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

return res?.tokenPrices || []
}

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

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

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

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

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

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

return res?.tokenPrices || []
}

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 || []
},
queryFn: () => getCollectiblePrices(apiClient, tokens),
retry: true,
staleTime: time.oneMinute,
enabled: tokens.length > 0
Expand Down
Loading

0 comments on commit d1d3855

Please sign in to comment.