Skip to content

Commit

Permalink
Move useTransactionHistory to kit package
Browse files Browse the repository at this point in the history
  • Loading branch information
corbanbrook committed Apr 30, 2024
1 parent ed7267a commit 0ef9f91
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
62 changes: 60 additions & 2 deletions packages/kit/src/hooks/data.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Token } from '@0xsequence/api'
import { useQuery } from '@tanstack/react-query'
import { useInfiniteQuery, useQuery } from '@tanstack/react-query'

import { useMetadataClient } from './useMetadataClient'
import { useAPIClient } from './useAPIClient'
import { useIndexerClient, useIndexerClients } from './useIndexerClient'
import { ContractType, SequenceIndexer, TokenBalance } from '@0xsequence/indexer'
import { ContractType, Page, SequenceIndexer, TokenBalance } from '@0xsequence/indexer'

import { zeroAddress } from 'viem'

Expand Down Expand Up @@ -213,3 +213,61 @@ export const useContractInfo = (chainId: number, contractAddress: string) => {
enabled: !!chainId && !!contractAddress
})
}

export interface GetTransactionHistoryArgs {
accountAddress: string
contractAddress?: string
tokenId?: string
page?: Page
}

export const getTransactionHistory = async (
indexerClient: SequenceIndexer,
{ contractAddress, accountAddress, tokenId, page }: GetTransactionHistoryArgs
) => {
const res = indexerClient.getTransactionHistory({
includeMetadata: true,
page,
filter: {
accountAddress,
contractAddress,
tokenID: tokenId
}
})

return res
}

interface UseTransactionHistoryArgs {
chainId: number
accountAddress: string
contractAddress?: string
tokenId?: string
disabled?: boolean
}

export const useTransactionHistory = (args: UseTransactionHistoryArgs) => {
const indexerClient = useIndexerClient(args.chainId)

return useInfiniteQuery({
queryKey: ['transactionHistory', args],
queryFn: ({ pageParam }) => {
return getTransactionHistory(indexerClient, {
...args,
page: { page: pageParam }
})
},
getNextPageParam: ({ page }) => {
// Note: must return undefined instead of null to stop the infinite scroll
if (!page.more) {
return undefined
}

return page?.page || 1
},
initialPageParam: 1,
retry: true,
staleTime: time.oneSecond * 30,
enabled: !!args.chainId && !!args.accountAddress
})
}
21 changes: 0 additions & 21 deletions packages/wallet/src/hooks/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,6 @@ export const useCollectiblePrices = (args: GetCollectiblePricesArgs) =>
enabled: args.tokens.length > 0
})

export const useTransactionHistory = (arg: Omit<GetTransactionHistoryArgs, 'page'> & { disabled?: boolean }) =>
useInfiniteQuery({
queryKey: ['transactionHistory', arg],
queryFn: ({ pageParam }) => {
return getTransactionHistory({
...(arg as Omit<GetTransactionHistoryArgs, 'page'>),
page: { page: pageParam }
})
},
getNextPageParam: ({ page }) => {
// Note: must return undefined instead of null to stop the infinite scroll
if (!page.more) return undefined

return page?.page || 1
},
initialPageParam: 1,
retry: true,
staleTime: time.oneSecond * 30,
enabled: !!arg.chainId && !arg.disabled && !!arg.accountAddress
})

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

import { useAccount, useConfig } from 'wagmi'

Expand All @@ -10,7 +10,7 @@ import { CoinDetailsSkeleton } from './Skeleton'
import { InfiniteScroll } from '../../shared/InfiniteScroll'
import { NetworkBadge } from '../../shared/NetworkBadge'
import { TransactionHistoryList } from '../../shared/TransactionHistoryList'
import { useCoinBalance, useSettings, useTransactionHistory, useNavigation } from '../../hooks'
import { useCoinBalance, useSettings, useNavigation } from '../../hooks'
import { HEADER_HEIGHT } from '../../constants'
import { compareAddress, computeBalanceFiat, formatDisplay, flattenPaginatedTransactionHistory } from '../../utils'
import { useScrollbarWidth } from '../../hooks/useScrollbarWidth'
Expand Down
6 changes: 3 additions & 3 deletions packages/wallet/src/views/CollectibleDetails/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react'
import { ethers } from 'ethers'
import { useAccount, useConfig } from 'wagmi'
import { Box, Button, Image, SendIcon, Text, vars } from '@0xsequence/design-system'
import { getNativeTokenInfoByChainId, useExchangeRate } from '@0xsequence/kit'
import { Box, Button, Image, SendIcon, Text } from '@0xsequence/design-system'
import { getNativeTokenInfoByChainId, useExchangeRate, useTransactionHistory } from '@0xsequence/kit'

import { CollectibleDetailsSkeleton } from './Skeleton'

import { computeBalanceFiat, formatDisplay, flattenPaginatedTransactionHistory } from '../../utils'
import { useCollectiblePrices, useCollectibleBalance, useSettings, useTransactionHistory, useNavigation } from '../../hooks'
import { useCollectiblePrices, useCollectibleBalance, useSettings, useNavigation } from '../../hooks'
import { InfiniteScroll } from '../../shared/InfiniteScroll'
import { TransactionHistoryList } from '../../shared/TransactionHistoryList'
import { CollectibleTileImage } from '../../shared/CollectibleTileImage'
Expand Down

0 comments on commit 0ef9f91

Please sign in to comment.