Skip to content

Commit

Permalink
simplified args in indexer call
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlee348 committed Nov 17, 2024
1 parent 6639d04 commit 958ac1e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 48 deletions.
47 changes: 7 additions & 40 deletions packages/kit/src/hooks/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,17 @@ export const getTokenBalances = async (indexerClient: SequenceIndexer, args: Get
}

export const getTokenBalancesSummary = async (indexerClient: SequenceIndexer, args: GetTokenBalancesSummaryArgs) => {
const res = await indexerClient.getTokenBalancesSummary({
filter: args.filter,
omitMetadata: args.omitMetadata,
page: args.page
})

const res = await indexerClient.getTokenBalancesSummary(args)
return res?.balances || []
}

export const getTokenBalancesDetails = async (indexerClient: SequenceIndexer, args: GetTokenBalancesDetailsArgs) => {
const res = await indexerClient.getTokenBalancesDetails({
filter: args.filter,
omitMetadata: args.omitMetadata,
page: args.page
})

const res = await indexerClient.getTokenBalancesDetails(args)
return res?.balances || []
}

export const getTokenBalancesByContract = async (indexerClient: SequenceIndexer, args: GetTokenBalancesByContractArgs) => {
const res = await indexerClient.getTokenBalancesByContract({
filter: args.filter,
omitMetadata: args.omitMetadata,
page: args.page
})

const res = await indexerClient.getTokenBalancesByContract(args)
return res?.balances || []
}

Expand All @@ -105,14 +90,7 @@ export const getBalances = async (indexerClient: SequenceIndexer, chainId: numbe
const balances = (
await Promise.allSettled([
getNativeTokenBalance(indexerClient, chainId, args.filter.accountAddresses[0]),
getTokenBalancesSummary(indexerClient, {
filter: {
accountAddresses: args.filter.accountAddresses,
contractStatus: args.filter.contractStatus,
contractWhitelist: args.filter.contractWhitelist,
contractBlacklist: args.filter.contractBlacklist
}
})
getTokenBalancesSummary(indexerClient, args)
])
)
.map(res => (res.status === 'fulfilled' ? res.value : []))
Expand Down Expand Up @@ -196,24 +174,13 @@ export const useCollectibleBalance = (args: UseCollectibleBalanceArgs) => {
}

export const getCollectionBalance = async (indexerClient: SequenceIndexer, args: UseCollectionBalanceArgs) => {
const res = await indexerClient.getTokenBalancesDetails({
filter: {
accountAddresses: [args.accountAddress],
contractStatus: args.verifiedOnly ? ContractVerificationStatus.VERIFIED : ContractVerificationStatus.ALL,
contractWhitelist: [args.contractAddress],
contractBlacklist: []
}
})
const res = await indexerClient.getTokenBalancesDetails(args)

return res?.balances || []
}

interface UseCollectionBalanceArgs {
interface UseCollectionBalanceArgs extends GetTokenBalancesDetailsArgs {
chainId: number
accountAddress: string
contractAddress: string
includeMetadata?: boolean
verifiedOnly?: boolean
}

export const useCollectionBalance = (args: UseCollectionBalanceArgs) => {
Expand All @@ -224,7 +191,7 @@ export const useCollectionBalance = (args: UseCollectionBalanceArgs) => {
queryFn: () => getCollectionBalance(indexerClient, args),
retry: true,
staleTime: time.oneSecond * 30,
enabled: !!args.chainId && !!args.accountAddress && !!args.contractAddress
enabled: !!args.chainId && !!args.filter.accountAddresses[0] && !!args.filter.contractWhitelist[0]
})
}

Expand Down
10 changes: 7 additions & 3 deletions packages/wallet/src/hooks/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,14 @@ export const getBalancesAssetsSummary = async (
}

const balance = await getCollectionBalance(indexerClient, {
accountAddress,
filter: {
accountAddresses: [accountAddress],
contractStatus: ContractVerificationStatus.ALL,
contractWhitelist: [collectionBalance.contractAddress],
contractBlacklist: []
},
chainId: collectionBalance.chainId,
contractAddress: collectionBalance.contractAddress,
includeMetadata: false
omitMetadata: false
})

return balance
Expand Down
1 change: 0 additions & 1 deletion packages/wallet/src/views/CollectibleDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ContractVerificationStatus
} from '@0xsequence/kit'
import { ethers } from 'ethers'
import React from 'react'
import { useAccount } from 'wagmi'

import { HEADER_HEIGHT } from '../../constants'
Expand Down
11 changes: 7 additions & 4 deletions packages/wallet/src/views/CollectionDetails/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Box, Image, Text, TokenImage, vars } from '@0xsequence/design-system'
import { TokenBalance } from '@0xsequence/indexer'
import { useCollectionBalance } from '@0xsequence/kit'
import { useCollectionBalance, ContractVerificationStatus } from '@0xsequence/kit'
import { ethers } from 'ethers'
import React from 'react'
import { useAccount } from 'wagmi'

import { useNavigation } from '../../hooks'
Expand All @@ -21,8 +20,12 @@ export const CollectionDetails = ({ chainId, contractAddress }: CollectionDetail
const { address: accountAddress } = useAccount()
const { data: collectionBalanceData, isPending: isPendingCollectionBalance } = useCollectionBalance({
chainId,
accountAddress: accountAddress || '',
contractAddress
filter: {
accountAddresses: accountAddress ? [accountAddress] : [],
contractStatus: ContractVerificationStatus.ALL,
contractWhitelist: [contractAddress],
contractBlacklist: []
}
})

const contractInfo = collectionBalanceData?.[0]?.contractInfo
Expand Down

0 comments on commit 958ac1e

Please sign in to comment.