From 84e1e8598695ac80d2ff1c45bdb6fb32f5bea90e Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Fri, 15 Nov 2024 17:07:39 -0500 Subject: [PATCH 1/6] added comments where we need to switch out old api --- .../next/src/app/components/Connected.tsx | 2 + examples/react/src/components/Connected.tsx | 2 + packages/kit/src/hooks/data.ts | 122 ++++++++++-------- packages/wallet/src/hooks/data.ts | 2 + 4 files changed, 71 insertions(+), 57 deletions(-) diff --git a/examples/next/src/app/components/Connected.tsx b/examples/next/src/app/components/Connected.tsx index f9cd8a2e..7973de8a 100644 --- a/examples/next/src/app/components/Connected.tsx +++ b/examples/next/src/app/components/Connected.tsx @@ -85,8 +85,10 @@ export const Connected = () => { const checkTokenBalancesForFeeOptions = async () => { if (pendingFeeOptionConfirmation) { const [account] = await walletClient!.getAddresses() + // ATTENTION: switch to new api const nativeTokenBalance = await indexerClient.getEtherBalance({ accountAddress: account }) + // ATTENTION: switch to new api const tokenBalances = await indexerClient.getTokenBalances({ accountAddress: account }) diff --git a/examples/react/src/components/Connected.tsx b/examples/react/src/components/Connected.tsx index 89903ae3..748c6c2a 100644 --- a/examples/react/src/components/Connected.tsx +++ b/examples/react/src/components/Connected.tsx @@ -132,8 +132,10 @@ export const Connected = () => { const checkTokenBalancesForFeeOptions = async () => { if (pendingFeeOptionConfirmation && walletClient) { const [account] = await walletClient.getAddresses() + // ATTENTION: switch to new api const nativeTokenBalance = await indexerClient.getEtherBalance({ accountAddress: account }) + // ATTENTION: switch to new api const tokenBalances = await indexerClient.getTokenBalances({ accountAddress: account }) diff --git a/packages/kit/src/hooks/data.ts b/packages/kit/src/hooks/data.ts index 20b11b72..7bdb8a42 100644 --- a/packages/kit/src/hooks/data.ts +++ b/packages/kit/src/hooks/data.ts @@ -20,6 +20,7 @@ export const time = { } export const getNativeTokenBalance = async (indexerClient: SequenceIndexer, chainId: number, accountAddress: string) => { + // ATTENTION: switch to new api const res = await indexerClient.getEtherBalance({ accountAddress }) const tokenBalance: TokenBalance = { @@ -47,6 +48,7 @@ interface GetTokenBalancesArgs { } export const getTokenBalances = async (indexerClient: SequenceIndexer, args: GetTokenBalancesArgs) => { + // ATTENTION: switch to new api const res = await indexerClient.getTokenBalances({ accountAddress: args.accountAddress, includeMetadata: args.includeMetadata ?? true, @@ -60,6 +62,7 @@ export const getTokenBalances = async (indexerClient: SequenceIndexer, args: Get } export const getBalances = async (indexerClient: SequenceIndexer, chainId: number, args: GetTokenBalancesArgs) => { + // ATTENTION: switch to new api if (!args.accountAddress) { return [] } @@ -106,6 +109,7 @@ interface UseCoinBalanceArgs extends GetTokenBalancesArgs { } export const useCoinBalance = (args: UseCoinBalanceArgs) => { + // ATTENTION: switch to new api const indexerClient = useIndexerClient(args.chainId) return useQuery({ @@ -134,6 +138,7 @@ interface UseCollectibleBalanceArgs { } export const useCollectibleBalance = (args: UseCollectibleBalanceArgs) => { + // ATTENTION: switch to new api const indexerClient = useIndexerClient(args.chainId) return useQuery({ @@ -158,6 +163,7 @@ export const useCollectibleBalance = (args: UseCollectibleBalanceArgs) => { } export const getCollectionBalance = async (indexerClient: SequenceIndexer, args: UseCollectionBalanceArgs) => { + // ATTENTION: switch to new api const res = await indexerClient.getTokenBalances({ accountAddress: args.accountAddress, contractAddress: args.contractAddress, @@ -388,7 +394,7 @@ const getSwapPrices = async ( const { withContractInfo, ...swapPricesArgs } = args const res = await apiClient.getSwapPrices({ - ...swapPricesArgs, + ...swapPricesArgs }) if (res.swapPrices === null) { @@ -399,36 +405,34 @@ const getSwapPrices = async ( if (withContractInfo) { res?.swapPrices.forEach(price => { const { currencyAddress: rawCurrencyAddress } = price - const currencyAddress = compareAddress(rawCurrencyAddress, NATIVE_TOKEN_ADDRESS_0X) - ? zeroAddress - : rawCurrencyAddress + const currencyAddress = compareAddress(rawCurrencyAddress, NATIVE_TOKEN_ADDRESS_0X) ? zeroAddress : rawCurrencyAddress const isNativeToken = compareAddress(currencyAddress, zeroAddress) if (currencyAddress && !currencyInfoMap.has(currencyAddress)) { - const getNativeTokenInfo = () =>new Promise((resolve, reject) => { - resolve({ - ...network?.nativeToken, - logoURI: network?.logoURI || '', - address: zeroAddress - } as ContractInfo) - }) + const getNativeTokenInfo = () => + new Promise((resolve, reject) => { + resolve({ + ...network?.nativeToken, + logoURI: network?.logoURI || '', + address: zeroAddress + } as ContractInfo) + }) currencyInfoMap.set( currencyAddress, - isNativeToken ? - getNativeTokenInfo().then(data => { - return data - }) - : - metadataClient - .getContractInfo({ - chainID: String(args.chainId), - contractAddress: currencyAddress - }) - .then(data => { - return ({ - ...data.contractInfo, + isNativeToken + ? getNativeTokenInfo().then(data => { + return data }) - }) + : metadataClient + .getContractInfo({ + chainID: String(args.chainId), + contractAddress: currencyAddress + }) + .then(data => { + return { + ...data.contractInfo + } + }) ) } }) @@ -437,34 +441,35 @@ const getSwapPrices = async ( const currencyBalanceInfoMap = new Map>() res?.swapPrices.forEach(price => { const { currencyAddress: rawCurrencyAddress } = price - const currencyAddress = compareAddress(rawCurrencyAddress, NATIVE_TOKEN_ADDRESS_0X) - ? zeroAddress - : rawCurrencyAddress + const currencyAddress = compareAddress(rawCurrencyAddress, NATIVE_TOKEN_ADDRESS_0X) ? zeroAddress : rawCurrencyAddress const isNativeToken = compareAddress(currencyAddress, zeroAddress) + + // ATTENTION: switch to new api if (currencyAddress && !currencyBalanceInfoMap.has(currencyAddress)) { currencyBalanceInfoMap.set( currencyAddress, - isNativeToken ? - indexerClient.getEtherBalance({ - accountAddress: args.userAddress - }).then(res => ({ - balance: res.balance.balanceWei - })) - : - indexerClient - .getTokenBalances({ - accountAddress: args.userAddress, - contractAddress: currencyAddress, - includeMetadata: false, - metadataOptions: { - verifiedOnly: true - } - }) - .then(balances => { - return ({ - balance: balances.balances?.[0].balance || '0' - }) - }) + isNativeToken + ? indexerClient + .getEtherBalance({ + accountAddress: args.userAddress + }) + .then(res => ({ + balance: res.balance.balanceWei + })) + : indexerClient + .getTokenBalances({ + accountAddress: args.userAddress, + contractAddress: currencyAddress, + includeMetadata: false, + metadataOptions: { + verifiedOnly: true + } + }) + .then(balances => { + return { + balance: balances.balances?.[0].balance || '0' + } + }) ) } }) @@ -472,18 +477,16 @@ const getSwapPrices = async ( return Promise.all( res?.swapPrices.map(async price => { const { currencyAddress: rawCurrencyAddress } = price - const currencyAddress = compareAddress(rawCurrencyAddress, NATIVE_TOKEN_ADDRESS_0X) - ? zeroAddress - : rawCurrencyAddress + const currencyAddress = compareAddress(rawCurrencyAddress, NATIVE_TOKEN_ADDRESS_0X) ? zeroAddress : rawCurrencyAddress - return ({ + return { price: { ...price, currencyAddress }, info: (await currencyInfoMap.get(currencyAddress)) || undefined, balance: (await currencyBalanceInfoMap.get(currencyAddress)) || { balance: '0' } - }) + } }) || [] ) } catch (e) { @@ -510,7 +513,12 @@ export const useSwapPrices = (args: UseSwapPricesArgs, options: SwapPricesOption const indexerClient = useIndexerClient(args.chainId) const enabled = - !!args.chainId && !!args.userAddress && !!args.buyCurrencyAddress && !!args.buyAmount && args.buyAmount !== '0' && !options.disabled + !!args.chainId && + !!args.userAddress && + !!args.buyCurrencyAddress && + !!args.buyAmount && + args.buyAmount !== '0' && + !options.disabled return useQuery({ queryKey: ['swapPrices', args], @@ -527,7 +535,7 @@ interface UseSwapQuoteOptions { disabled?: boolean } -export const useSwapQuote = (args:GetSwapQuoteArgs, options: UseSwapQuoteOptions) => { +export const useSwapQuote = (args: GetSwapQuoteArgs, options: UseSwapQuoteOptions) => { const apiClient = useAPIClient() const { disabled = false } = options @@ -555,4 +563,4 @@ export const useSwapQuote = (args:GetSwapQuoteArgs, options: UseSwapQuoteOptio staleTime: time.oneMinute * 1, enabled: !disabled || !args.userAddress || !args.chainId || !args.buyCurrencyAddress }) -} \ No newline at end of file +} diff --git a/packages/wallet/src/hooks/data.ts b/packages/wallet/src/hooks/data.ts index 40a49440..72362de1 100644 --- a/packages/wallet/src/hooks/data.ts +++ b/packages/wallet/src/hooks/data.ts @@ -93,6 +93,7 @@ export const getBalancesAssetsSummary = async ( return [] } + // ATTENTION: switch to new api return getTokenBalances(indexerClient, { accountAddress, contractAddress: asset.contractAddress, @@ -112,6 +113,7 @@ export const getBalancesAssetsSummary = async ( await Promise.all([ ...indexerClientsArr.map(([chainId, indexerClient]) => getNativeTokenBalance(indexerClient, chainId, accountAddress)), ...indexerClientsArr.map(([_chainId, indexerClient]) => + // ATTENTION: switch to new api getTokenBalances(indexerClient, { accountAddress, hideCollectibles, From 6639d041f380b1f650725377d027e56cca9ca485 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Sun, 17 Nov 2024 17:59:57 -0500 Subject: [PATCH 2/6] changed out old endpoint with new ones with updated arg interfaces --- .../next/src/app/components/Connected.tsx | 23 +-- examples/react/src/components/Connected.tsx | 15 +- .../src/views/CheckoutSelection/index.tsx | 10 +- .../PaymentSelection/PayWithCrypto/index.tsx | 17 ++- .../src/views/PaymentSelection/index.tsx | 15 +- .../src/components/TxnDetails/TxnDetails.tsx | 10 +- packages/kit/src/hooks/data.ts | 135 +++++++++++------- packages/kit/src/index.ts | 6 + packages/wallet/src/hooks/data.ts | 46 +++--- .../wallet/src/views/CoinDetails/index.tsx | 14 +- .../src/views/CollectibleDetails/index.tsx | 19 ++- .../Home/components/AssetSummary/index.tsx | 1 - .../wallet/src/views/Search/SearchWallet.tsx | 18 ++- .../src/views/Search/SearchWalletViewAll.tsx | 16 ++- .../views/Search/components/BalanceItem.tsx | 4 +- packages/wallet/src/views/SendCoin.tsx | 13 +- packages/wallet/src/views/SendCollectible.tsx | 21 ++- 17 files changed, 247 insertions(+), 136 deletions(-) diff --git a/examples/next/src/app/components/Connected.tsx b/examples/next/src/app/components/Connected.tsx index 7973de8a..7aca0b38 100644 --- a/examples/next/src/app/components/Connected.tsx +++ b/examples/next/src/app/components/Connected.tsx @@ -14,6 +14,8 @@ import { isDebugMode, sponsoredContractAddresses } from '../../config' import { messageToSign } from '@/constants' import { abi } from '@/constants/nft-abi' +import { ContractVerificationStatus } from '@0xsequence/kit' + export const Connected = () => { const { address } = useAccount() const { setOpenWalletModal } = useOpenWalletModal() @@ -86,23 +88,24 @@ export const Connected = () => { if (pendingFeeOptionConfirmation) { const [account] = await walletClient!.getAddresses() // ATTENTION: switch to new api - const nativeTokenBalance = await indexerClient.getEtherBalance({ accountAddress: account }) - - // ATTENTION: switch to new api - const tokenBalances = await indexerClient.getTokenBalances({ - accountAddress: account + const nativeTokenBalance = await indexerClient.getNativeTokenBalance({ accountAddress: account }) + + //ATTENTION: switch to new api + const tokenBalances = await indexerClient.getTokenBalancesSummary({ + filter: { + accountAddresses: [account], + contractStatus: ContractVerificationStatus.ALL, + contractWhitelist: [], + contractBlacklist: [] + } }) - console.log('feeOptions', pendingFeeOptionConfirmation.options) - console.log('nativeTokenBalance', nativeTokenBalance) - console.log('tokenBalances', tokenBalances) - const balances = pendingFeeOptionConfirmation.options.map(option => { if (option.token.contractAddress === null) { return { tokenName: option.token.name, decimals: option.token.decimals || 0, - balance: nativeTokenBalance.balance.balanceWei + balance: nativeTokenBalance.balance.balance } } else { return { diff --git a/examples/react/src/components/Connected.tsx b/examples/react/src/components/Connected.tsx index 748c6c2a..041a85a2 100644 --- a/examples/react/src/components/Connected.tsx +++ b/examples/react/src/components/Connected.tsx @@ -31,6 +31,8 @@ import { messageToSign } from '../constants' import { abi } from '../constants/nft-abi' import { delay, getCheckoutSettings, getOrderbookCalldata } from '../utils' +import { ContractVerificationStatus } from '@0xsequence/kit' + // append ?debug to url to enable debug mode const searchParams = new URLSearchParams(location.search) const isDebugMode = searchParams.has('debug') @@ -133,11 +135,16 @@ export const Connected = () => { if (pendingFeeOptionConfirmation && walletClient) { const [account] = await walletClient.getAddresses() // ATTENTION: switch to new api - const nativeTokenBalance = await indexerClient.getEtherBalance({ accountAddress: account }) + const nativeTokenBalance = await indexerClient.getNativeTokenBalance({ accountAddress: account }) // ATTENTION: switch to new api - const tokenBalances = await indexerClient.getTokenBalances({ - accountAddress: account + const tokenBalances = await indexerClient.getTokenBalancesSummary({ + filter: { + accountAddresses: [account], + contractStatus: ContractVerificationStatus.ALL, + contractWhitelist: [], + contractBlacklist: [] + } }) const balances = pendingFeeOptionConfirmation.options.map(option => { @@ -145,7 +152,7 @@ export const Connected = () => { return { tokenName: option.token.name, decimals: option.token.decimals || 0, - balance: nativeTokenBalance.balance.balanceWei + balance: nativeTokenBalance.balance.balance } } else { return { diff --git a/packages/checkout/src/views/CheckoutSelection/index.tsx b/packages/checkout/src/views/CheckoutSelection/index.tsx index 796acded..b80aa5d6 100644 --- a/packages/checkout/src/views/CheckoutSelection/index.tsx +++ b/packages/checkout/src/views/CheckoutSelection/index.tsx @@ -11,9 +11,8 @@ import { Skeleton, TokenImage } from '@0xsequence/design-system' -import { getNativeTokenInfoByChainId, useBalances, useContractInfo } from '@0xsequence/kit' +import { ContractVerificationStatus, getNativeTokenInfoByChainId, useBalances, useContractInfo } from '@0xsequence/kit' import { ethers } from 'ethers' -import React from 'react' import { useAccount, useConfig } from 'wagmi' import { HEADER_HEIGHT } from '../../constants' @@ -40,7 +39,12 @@ export const CheckoutSelection = () => { const { data: balancesData, isPending: isPendingBalances } = useBalances({ chainIds: [cryptoCheckoutSettings?.chainId || 1], - accountAddress: accountAddress || '' + filter: { + accountAddresses: accountAddress ? [accountAddress] : [], + contractStatus: ContractVerificationStatus.ALL, + contractWhitelist: [], + contractBlacklist: [] + } }) const isPending = (isPendingContractInfo || isPendingBalances) && cryptoCheckoutSettings diff --git a/packages/checkout/src/views/PaymentSelection/PayWithCrypto/index.tsx b/packages/checkout/src/views/PaymentSelection/PayWithCrypto/index.tsx index 162edbd0..d4e13703 100644 --- a/packages/checkout/src/views/PaymentSelection/PayWithCrypto/index.tsx +++ b/packages/checkout/src/views/PaymentSelection/PayWithCrypto/index.tsx @@ -1,8 +1,8 @@ import { Box, Text, Scroll, Spinner } from '@0xsequence/design-system' -import { useBalances, useContractInfo, useSwapPrices, compareAddress } from '@0xsequence/kit' +import { useBalances, useContractInfo, useSwapPrices, compareAddress, ContractVerificationStatus } from '@0xsequence/kit' import { findSupportedNetwork } from '@0xsequence/network' -import { useState, useEffect, Fragment, SetStateAction } from 'react' -import { formatUnits, zeroAddress } from 'viem' +import { useEffect, Fragment, SetStateAction } from 'react' +import { formatUnits } from 'viem' import { useAccount } from 'wagmi' import { SelectPaymentSettings } from '../../../contexts' @@ -35,10 +35,13 @@ export const PayWithCrypto = ({ const { data: currencyBalanceData, isLoading: currencyBalanceIsLoading } = useBalances({ chainIds: [chainId], - contractAddress: currencyAddress, - accountAddress: userAddress || '', - // includeMetadata must be false to work around a bug - includeMetadata: false + filter: { + accountAddresses: userAddress ? [userAddress] : [], + contractStatus: ContractVerificationStatus.ALL, + contractWhitelist: [currencyAddress], + contractBlacklist: [] + }, + omitMetadata: true }) const { data: currencyInfoData, isLoading: isLoadingCurrencyInfo } = useContractInfo(chainId, currencyAddress) diff --git a/packages/checkout/src/views/PaymentSelection/index.tsx b/packages/checkout/src/views/PaymentSelection/index.tsx index 1bdd9524..3702f3be 100644 --- a/packages/checkout/src/views/PaymentSelection/index.tsx +++ b/packages/checkout/src/views/PaymentSelection/index.tsx @@ -7,7 +7,8 @@ import { compareAddress, TRANSACTION_CONFIRMATIONS_DEFAULT, sendTransactions, - SwapPricesWithCurrencyInfo + SwapPricesWithCurrencyInfo, + ContractVerificationStatus } from '@0xsequence/kit' import { findSupportedNetwork } from '@0xsequence/network' import { useState, useEffect } from 'react' @@ -92,10 +93,14 @@ export const PaymentSelectionContent = () => { const { data: currencyBalanceData, isLoading: currencyBalanceIsLoading } = useBalances({ chainIds: [chainId], - contractAddress: currencyAddress, - accountAddress: userAddress || '', - // includeMetadata must be false to work around a bug - includeMetadata: false + filter: { + accountAddresses: userAddress ? [userAddress] : [], + contractStatus: ContractVerificationStatus.ALL, + contractWhitelist: [currencyAddress], + contractBlacklist: [] + }, + // omitMetadata must be true to avoid a bug + omitMetadata: true }) const { data: currencyInfoData, isLoading: isLoadingCurrencyInfo } = useContractInfo(chainId, currencyAddress) diff --git a/packages/kit/src/components/TxnDetails/TxnDetails.tsx b/packages/kit/src/components/TxnDetails/TxnDetails.tsx index 25111a7a..f15d428e 100644 --- a/packages/kit/src/components/TxnDetails/TxnDetails.tsx +++ b/packages/kit/src/components/TxnDetails/TxnDetails.tsx @@ -1,6 +1,6 @@ import { commons } from '@0xsequence/core' import { Box, Card, GradientAvatar, Skeleton, Text, TokenImage } from '@0xsequence/design-system' -import { ContractType } from '@0xsequence/indexer' +import { ContractType, ContractVerificationStatus } from '@0xsequence/indexer' import { ethers } from 'ethers' import React, { useEffect, useState } from 'react' import { useConfig } from 'wagmi' @@ -96,8 +96,12 @@ const TransferItemInfo = ({ address, transferProps, chainId }: TransferItemInfoP const { data: balances = [] } = useBalances({ chainIds: [chainId], - accountAddress: address, - contractAddress + filter: { + accountAddresses: [address], + contractStatus: ContractVerificationStatus.ALL, + contractWhitelist: [contractAddress], + contractBlacklist: [] + } }) const { data: tokenMetadata } = useTokenMetadata(chainId, contractAddress, transferProps.tokenIds ?? []) diff --git a/packages/kit/src/hooks/data.ts b/packages/kit/src/hooks/data.ts index 7bdb8a42..c012215b 100644 --- a/packages/kit/src/hooks/data.ts +++ b/packages/kit/src/hooks/data.ts @@ -3,7 +3,7 @@ import { ContractType, Page, SequenceIndexer, TokenBalance } from '@0xsequence/i import { ContractInfo, SequenceMetadata } from '@0xsequence/metadata' import { findSupportedNetwork } from '@0xsequence/network' import { useInfiniteQuery, useQuery } from '@tanstack/react-query' -import { blobsToProofsErrorType, zeroAddress } from 'viem' +import { zeroAddress } from 'viem' import { compareAddress } from '../utils/helpers' import { NATIVE_TOKEN_ADDRESS_0X } from '../constants' @@ -11,7 +11,13 @@ import { NATIVE_TOKEN_ADDRESS_0X } from '../constants' import { useAPIClient } from './useAPIClient' import { useIndexerClient, useIndexerClients } from './useIndexerClient' import { useMetadataClient } from './useMetadataClient' -import { indexer } from '0xsequence/dist/declarations/src/sequence' + +import { + ContractVerificationStatus, + GetTokenBalancesSummaryArgs, + GetTokenBalancesDetailsArgs, + GetTokenBalancesByContractArgs +} from '@0xsequence/indexer' export const time = { oneSecond: 1 * 1000, @@ -21,13 +27,13 @@ export const time = { export const getNativeTokenBalance = async (indexerClient: SequenceIndexer, chainId: number, accountAddress: string) => { // ATTENTION: switch to new api - const res = await indexerClient.getEtherBalance({ accountAddress }) + const res = await indexerClient.getNativeTokenBalance({ accountAddress }) const tokenBalance: TokenBalance = { chainId, contractAddress: zeroAddress, accountAddress, - balance: res?.balance.balanceWei || '0', + balance: res?.balance.balance || '0', contractType: ContractType.UNKNOWN, blockHash: '', blockNumber: 0, @@ -48,7 +54,6 @@ interface GetTokenBalancesArgs { } export const getTokenBalances = async (indexerClient: SequenceIndexer, args: GetTokenBalancesArgs) => { - // ATTENTION: switch to new api const res = await indexerClient.getTokenBalances({ accountAddress: args.accountAddress, includeMetadata: args.includeMetadata ?? true, @@ -61,16 +66,53 @@ export const getTokenBalances = async (indexerClient: SequenceIndexer, args: Get return res?.balances || [] } -export const getBalances = async (indexerClient: SequenceIndexer, chainId: number, args: GetTokenBalancesArgs) => { +export const getTokenBalancesSummary = async (indexerClient: SequenceIndexer, args: GetTokenBalancesSummaryArgs) => { + const res = await indexerClient.getTokenBalancesSummary({ + filter: args.filter, + omitMetadata: args.omitMetadata, + page: args.page + }) + + 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 + }) + + 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 + }) + + return res?.balances || [] +} + +export const getBalances = async (indexerClient: SequenceIndexer, chainId: number, args: GetTokenBalancesSummaryArgs) => { // ATTENTION: switch to new api - if (!args.accountAddress) { + if (!args.filter.accountAddresses[0]) { return [] } const balances = ( await Promise.allSettled([ - getNativeTokenBalance(indexerClient, chainId, args.accountAddress), - getTokenBalances(indexerClient, args) + 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 + } + }) ]) ) .map(res => (res.status === 'fulfilled' ? res.value : [])) @@ -79,7 +121,7 @@ export const getBalances = async (indexerClient: SequenceIndexer, chainId: numbe return balances } -interface UseBalancesArgs extends GetTokenBalancesArgs { +interface UseBalancesArgs extends GetTokenBalancesSummaryArgs { chainIds: number[] } @@ -100,11 +142,11 @@ export const useBalances = ({ chainIds, ...args }: UseBalancesArgs) => { }, retry: true, staleTime: time.oneSecond * 30, - enabled: chainIds.length > 0 && !!args.accountAddress + enabled: chainIds.length > 0 && !!args.filter.accountAddresses[0] }) } -interface UseCoinBalanceArgs extends GetTokenBalancesArgs { +interface UseCoinBalanceArgs extends GetTokenBalancesSummaryArgs { chainId: number } @@ -115,26 +157,23 @@ export const useCoinBalance = (args: UseCoinBalanceArgs) => { return useQuery({ queryKey: ['coinBalance', args], queryFn: async () => { - if (compareAddress(args?.contractAddress || '', zeroAddress)) { - const res = await getNativeTokenBalance(indexerClient, args.chainId, args.accountAddress) + if (compareAddress(args?.filter.accountAddresses[0] || '', zeroAddress)) { + const res = await getNativeTokenBalance(indexerClient, args.chainId, args.filter.accountAddresses[0] || '') return res } else { - const res = await getTokenBalances(indexerClient, args) + const res = await getTokenBalancesSummary(indexerClient, args) return res[0] } }, retry: true, staleTime: time.oneSecond * 30, - enabled: !!args.chainId && !!args.accountAddress + enabled: !!args.chainId && !!args.filter.accountAddresses[0] }) } -interface UseCollectibleBalanceArgs { - accountAddress: string +interface UseCollectibleBalanceArgs extends GetTokenBalancesDetailsArgs { chainId: number - contractAddress: string tokenId: string - verifiedOnly?: boolean } export const useCollectibleBalance = (args: UseCollectibleBalanceArgs) => { @@ -144,32 +183,25 @@ export const useCollectibleBalance = (args: UseCollectibleBalanceArgs) => { return useQuery({ queryKey: ['collectibleBalance', args], queryFn: async () => { - const res = await indexerClient.getTokenBalances({ - accountAddress: args.accountAddress, - contractAddress: args.contractAddress, - tokenID: args.tokenId, - includeMetadata: true, - metadataOptions: { - verifiedOnly: args.verifiedOnly ?? true - } - }) + const res = await indexerClient.getTokenBalancesDetails(args) - return res.balances[0] + const balance = res.balances.find(balance => balance.tokenID === args.tokenId) + + return balance }, retry: true, staleTime: time.oneSecond * 30, - enabled: !!args.chainId && !!args.accountAddress && !!args.contractAddress && !!args.tokenId + enabled: !!args.chainId && !!args.filter.accountAddresses[0] && !!args.filter.contractWhitelist[0] && !!args.tokenId }) } export const getCollectionBalance = async (indexerClient: SequenceIndexer, args: UseCollectionBalanceArgs) => { - // ATTENTION: switch to new api - const res = await indexerClient.getTokenBalances({ - accountAddress: args.accountAddress, - contractAddress: args.contractAddress, - includeMetadata: args.includeMetadata ?? true, - metadataOptions: { - verifiedOnly: args.verifiedOnly ?? true + const res = await indexerClient.getTokenBalancesDetails({ + filter: { + accountAddresses: [args.accountAddress], + contractStatus: args.verifiedOnly ? ContractVerificationStatus.VERIFIED : ContractVerificationStatus.ALL, + contractWhitelist: [args.contractAddress], + contractBlacklist: [] } }) @@ -450,26 +482,25 @@ const getSwapPrices = async ( currencyAddress, isNativeToken ? indexerClient - .getEtherBalance({ + .getNativeTokenBalance({ accountAddress: args.userAddress }) .then(res => ({ - balance: res.balance.balanceWei + balance: res.balance.balance })) : indexerClient - .getTokenBalances({ - accountAddress: args.userAddress, - contractAddress: currencyAddress, - includeMetadata: false, - metadataOptions: { - verifiedOnly: true - } - }) - .then(balances => { - return { - balance: balances.balances?.[0].balance || '0' - } + .getTokenBalancesSummary({ + filter: { + accountAddresses: [args.userAddress], + contractStatus: ContractVerificationStatus.VERIFIED, + contractWhitelist: [currencyAddress], + contractBlacklist: [] + }, + omitMetadata: true }) + .then(res => ({ + balance: res.balances?.[0].balance || '0' + })) ) } }) diff --git a/packages/kit/src/index.ts b/packages/kit/src/index.ts index 57342645..43f41a7f 100644 --- a/packages/kit/src/index.ts +++ b/packages/kit/src/index.ts @@ -100,6 +100,9 @@ export { useBalances, useExchangeRate, getTokenBalances, + getTokenBalancesSummary, + getTokenBalancesDetails, + getTokenBalancesByContract, useCoinBalance, useCoinPrices, useCollectionBalance, @@ -115,3 +118,6 @@ export { // Components export { NetworkBadge } from './components/NetworkBadge' export { CollectibleTileImage } from './components/CollectibleTileImage' + +// Indexer +export { ContractVerificationStatus } from '@0xsequence/indexer' diff --git a/packages/wallet/src/hooks/data.ts b/packages/wallet/src/hooks/data.ts index 72362de1..dab8e00b 100644 --- a/packages/wallet/src/hooks/data.ts +++ b/packages/wallet/src/hooks/data.ts @@ -6,16 +6,18 @@ import { useIndexerClients, DisplayedAsset, getNativeTokenBalance, - getTokenBalances, getCoinPrices, getCollectionBalance, - useMetadataClient + useMetadataClient, + getTokenBalancesSummary, + getTokenBalancesDetails, + ContractVerificationStatus } from '@0xsequence/kit' import { GetContractInfoBatchReturn, SequenceMetadata } from '@0xsequence/metadata' import { useQuery } from '@tanstack/react-query' import { ethers } from 'ethers' -import { compareAddress, sampleSize, sortBalancesByType, isTruthy } from '../utils' +import { compareAddress, sortBalancesByType, isTruthy } from '../utils' export const time = { oneSecond: 1 * 1000, @@ -39,8 +41,6 @@ export const getBalancesAssetsSummary = async ( ) => { const indexerClientsArr = Array.from(indexerClients.entries()) - const MAX_COLLECTIBLES_AMOUNTS = 10 - let tokenBalances: TokenBalance[] = [] const customDisplayAssets = displayAssets.length > 0 @@ -70,7 +70,6 @@ export const getBalancesAssetsSummary = async ( } otherAssetsByChainId[asset.chainId].push(asset) }) - tokenBalances = ( await Promise.all([ ...Object.keys(nativeTokensByChainId).map(chainId => { @@ -94,12 +93,13 @@ export const getBalancesAssetsSummary = async ( } // ATTENTION: switch to new api - return getTokenBalances(indexerClient, { - accountAddress, - contractAddress: asset.contractAddress, - includeMetadata: false, - hideCollectibles, - verifiedOnly + return getTokenBalancesDetails(indexerClient, { + filter: { + accountAddresses: [accountAddress], + contractStatus: verifiedOnly ? ContractVerificationStatus.VERIFIED : ContractVerificationStatus.ALL, + contractWhitelist: [asset.contractAddress], + contractBlacklist: [] + } }) }) ) @@ -114,11 +114,13 @@ export const getBalancesAssetsSummary = async ( ...indexerClientsArr.map(([chainId, indexerClient]) => getNativeTokenBalance(indexerClient, chainId, accountAddress)), ...indexerClientsArr.map(([_chainId, indexerClient]) => // ATTENTION: switch to new api - getTokenBalances(indexerClient, { - accountAddress, - hideCollectibles, - includeMetadata: false, - verifiedOnly + getTokenBalancesSummary(indexerClient, { + filter: { + accountAddresses: [accountAddress], + contractStatus: verifiedOnly ? ContractVerificationStatus.VERIFIED : ContractVerificationStatus.ALL, + contractWhitelist: [], + contractBlacklist: [] + } }) ) ]) @@ -220,23 +222,23 @@ export const getBalancesAssetsSummary = async ( return bValue - aValue }) - const collectibles: TokenBalance[] = sampleSize(collectionCollectibles.flat(), MAX_COLLECTIBLES_AMOUNTS).sort((a, b) => { + const collectibles: TokenBalance[] = collectionCollectibles.flat().sort((a, b) => { return a.contractAddress.localeCompare(b.contractAddress) }) if (hideCollectibles) { const summaryBalances: TokenBalance[] = [ - ...(nativeTokens.length > 0 ? [nativeTokens[0]] : []), + ...(nativeTokens.length > 0 ? [...nativeTokens] : []), // the spots normally occupied by collectibles will be filled by erc20 tokens - ...(erc20HighestValue.length > 0 ? erc20HighestValue.slice(0, MAX_COLLECTIBLES_AMOUNTS + 1) : []) + ...(erc20HighestValue.length > 0 ? erc20HighestValue : []) ] return summaryBalances } const summaryBalances: TokenBalance[] = [ - ...(nativeTokens.length > 0 ? [nativeTokens[0]] : []), - ...(erc20HighestValue.length > 0 ? [erc20HighestValue[0]] : []), + ...(nativeTokens.length > 0 ? [...nativeTokens] : []), + ...(erc20HighestValue.length > 0 ? [...erc20HighestValue] : []), ...(collectibles.length > 0 ? [...collectibles] : []) ] diff --git a/packages/wallet/src/views/CoinDetails/index.tsx b/packages/wallet/src/views/CoinDetails/index.tsx index a3ec417c..d9e8ddb1 100644 --- a/packages/wallet/src/views/CoinDetails/index.tsx +++ b/packages/wallet/src/views/CoinDetails/index.tsx @@ -4,7 +4,8 @@ import { useExchangeRate, useCoinPrices, useTransactionHistory, - useCoinBalance + useCoinBalance, + ContractVerificationStatus } from '@0xsequence/kit' import { ethers } from 'ethers' import React from 'react' @@ -45,10 +46,13 @@ export const CoinDetails = ({ contractAddress, chainId }: CoinDetailsProps) => { const transactionHistory = flattenPaginatedTransactionHistory(dataTransactionHistory) const { data: dataCoinBalance, isPending: isPendingCoinBalance } = useCoinBalance({ - accountAddress: accountAddress || '', - contractAddress, - chainId, - verifiedOnly: hideUnlistedTokens + filter: { + accountAddresses: [accountAddress || ''], + contractStatus: hideUnlistedTokens ? ContractVerificationStatus.VERIFIED : ContractVerificationStatus.ALL, + contractWhitelist: [contractAddress], + contractBlacklist: [] + }, + chainId }) const { data: dataCoinPrices, isPending: isPendingCoinPrices } = useCoinPrices([ diff --git a/packages/wallet/src/views/CollectibleDetails/index.tsx b/packages/wallet/src/views/CollectibleDetails/index.tsx index a814be50..8893d990 100644 --- a/packages/wallet/src/views/CollectibleDetails/index.tsx +++ b/packages/wallet/src/views/CollectibleDetails/index.tsx @@ -1,5 +1,11 @@ import { Box, Button, Image, NetworkImage, SendIcon, Text } from '@0xsequence/design-system' -import { useExchangeRate, useTransactionHistory, useCollectiblePrices, useCollectibleBalance } from '@0xsequence/kit' +import { + useExchangeRate, + useTransactionHistory, + useCollectiblePrices, + useCollectibleBalance, + ContractVerificationStatus +} from '@0xsequence/kit' import { ethers } from 'ethers' import React from 'react' import { useAccount } from 'wagmi' @@ -40,11 +46,14 @@ export const CollectibleDetails = ({ contractAddress, chainId, tokenId }: Collec const transactionHistory = flattenPaginatedTransactionHistory(dataTransactionHistory) const { data: dataCollectibleBalance, isPending: isPendingCollectibleBalance } = useCollectibleBalance({ - accountAddress: accountAddress || '', - contractAddress, + filter: { + accountAddresses: accountAddress ? [accountAddress] : [], + contractStatus: ContractVerificationStatus.ALL, + contractWhitelist: [contractAddress], + contractBlacklist: [] + }, chainId, - tokenId, - verifiedOnly: false + tokenId }) const { data: dataCollectiblePrices, isPending: isPendingCollectiblePrices } = useCollectiblePrices([ diff --git a/packages/wallet/src/views/Home/components/AssetSummary/index.tsx b/packages/wallet/src/views/Home/components/AssetSummary/index.tsx index 831bc04d..59d763b9 100644 --- a/packages/wallet/src/views/Home/components/AssetSummary/index.tsx +++ b/packages/wallet/src/views/Home/components/AssetSummary/index.tsx @@ -1,7 +1,6 @@ import { Box, vars } from '@0xsequence/design-system' import { TokenBalance } from '@0xsequence/indexer' import { useWalletSettings } from '@0xsequence/kit' -import React from 'react' import { useAccount } from 'wagmi' import { useBalancesAssetsSummary, useNavigation, useSettings } from '../../../../hooks' diff --git a/packages/wallet/src/views/Search/SearchWallet.tsx b/packages/wallet/src/views/Search/SearchWallet.tsx index 12f82f40..296b8609 100644 --- a/packages/wallet/src/views/Search/SearchWallet.tsx +++ b/packages/wallet/src/views/Search/SearchWallet.tsx @@ -1,8 +1,14 @@ import { Box, SearchIcon, Skeleton, Text, TextInput } from '@0xsequence/design-system' -import { getNativeTokenInfoByChainId, useExchangeRate, useCoinPrices, useBalances } from '@0xsequence/kit' +import { + getNativeTokenInfoByChainId, + useExchangeRate, + useCoinPrices, + useBalances, + ContractVerificationStatus +} from '@0xsequence/kit' import { ethers } from 'ethers' import Fuse from 'fuse.js' -import React, { useState } from 'react' +import { useState } from 'react' import { useAccount, useConfig } from 'wagmi' import { useSettings } from '../../hooks' @@ -19,8 +25,12 @@ export const SearchWallet = () => { const { data: tokenBalancesData, isPending: isPendingTokenBalances } = useBalances({ chainIds: selectedNetworks, - accountAddress: accountAddress || '', - verifiedOnly: hideUnlistedTokens + filter: { + accountAddresses: accountAddress ? [accountAddress] : [], + contractStatus: hideUnlistedTokens ? ContractVerificationStatus.VERIFIED : ContractVerificationStatus.ALL, + contractWhitelist: [], + contractBlacklist: [] + } }) const coinBalancesUnordered = diff --git a/packages/wallet/src/views/Search/SearchWalletViewAll.tsx b/packages/wallet/src/views/Search/SearchWalletViewAll.tsx index 16fe92b1..1f940367 100644 --- a/packages/wallet/src/views/Search/SearchWalletViewAll.tsx +++ b/packages/wallet/src/views/Search/SearchWalletViewAll.tsx @@ -1,5 +1,11 @@ import { Box, SearchIcon, Skeleton, TabsContent, TabsHeader, TabsRoot, Text, TextInput } from '@0xsequence/design-system' -import { getNativeTokenInfoByChainId, useExchangeRate, useCoinPrices, useBalances } from '@0xsequence/kit' +import { + getNativeTokenInfoByChainId, + useExchangeRate, + useCoinPrices, + useBalances, + ContractVerificationStatus +} from '@0xsequence/kit' import { ethers } from 'ethers' import Fuse from 'fuse.js' import React, { useState, useEffect } from 'react' @@ -28,8 +34,12 @@ export const SearchWalletViewAll = ({ defaultTab }: SearchWalletViewAllProps) => const { data: tokenBalancesData, isPending: isPendingTokenBalances } = useBalances({ chainIds: selectedNetworks, - accountAddress: accountAddress || '', - verifiedOnly: hideUnlistedTokens + filter: { + accountAddresses: accountAddress ? [accountAddress] : [], + contractStatus: hideUnlistedTokens ? ContractVerificationStatus.VERIFIED : ContractVerificationStatus.ALL, + contractWhitelist: [], + contractBlacklist: [] + } }) const coinBalancesUnordered = diff --git a/packages/wallet/src/views/Search/components/BalanceItem.tsx b/packages/wallet/src/views/Search/components/BalanceItem.tsx index 16d2224c..681f2a29 100644 --- a/packages/wallet/src/views/Search/components/BalanceItem.tsx +++ b/packages/wallet/src/views/Search/components/BalanceItem.tsx @@ -72,8 +72,8 @@ export const BalanceItem = ({ balance }: BalanceItemProps) => { {tokenName} - - + + {getQuantity()} diff --git a/packages/wallet/src/views/SendCoin.tsx b/packages/wallet/src/views/SendCoin.tsx index 2050e804..ab34b613 100644 --- a/packages/wallet/src/views/SendCoin.tsx +++ b/packages/wallet/src/views/SendCoin.tsx @@ -19,10 +19,11 @@ import { ExtendedConnector, useExchangeRate, useCoinPrices, - useBalances + useBalances, + ContractVerificationStatus } from '@0xsequence/kit' import { ethers } from 'ethers' -import React, { useState, ChangeEvent, useRef } from 'react' +import { useState, ChangeEvent, useRef } from 'react' import { useAccount, useChainId, useSwitchChain, useConfig, useSendTransaction } from 'wagmi' import { ERC_20_ABI, HEADER_HEIGHT } from '../constants' @@ -53,8 +54,12 @@ export const SendCoin = ({ chainId, contractAddress }: SendCoinProps) => { const [isSendTxnPending, setIsSendTxnPending] = useState(false) const { data: balances = [], isPending: isPendingBalances } = useBalances({ chainIds: [chainId], - accountAddress: accountAddress, - contractAddress + filter: { + accountAddresses: [accountAddress], + contractStatus: ContractVerificationStatus.ALL, + contractWhitelist: [contractAddress], + contractBlacklist: [] + } }) const nativeTokenInfo = getNativeTokenInfoByChainId(chainId, chains) const tokenBalance = (balances as TokenBalance[]).find(b => b.contractAddress === contractAddress) diff --git a/packages/wallet/src/views/SendCollectible.tsx b/packages/wallet/src/views/SendCollectible.tsx index 2eee3c84..8b2bb153 100644 --- a/packages/wallet/src/views/SendCollectible.tsx +++ b/packages/wallet/src/views/SendCollectible.tsx @@ -15,9 +15,15 @@ import { Card } from '@0xsequence/design-system' import { TokenBalance } from '@0xsequence/indexer' -import { getNativeTokenInfoByChainId, useAnalyticsContext, ExtendedConnector, useCollectibleBalance } from '@0xsequence/kit' +import { + getNativeTokenInfoByChainId, + useAnalyticsContext, + ExtendedConnector, + useCollectibleBalance, + ContractVerificationStatus +} from '@0xsequence/kit' import { ethers } from 'ethers' -import React, { useRef, useState, ChangeEvent, useEffect } from 'react' +import { useRef, useState, ChangeEvent, useEffect } from 'react' import { useAccount, useChainId, useSwitchChain, useConfig, useSendTransaction } from 'wagmi' import { ERC_1155_ABI, ERC_721_ABI, HEADER_HEIGHT } from '../constants' @@ -48,11 +54,14 @@ export const SendCollectible = ({ chainId, contractAddress, tokenId }: SendColle const { sendTransaction } = useSendTransaction() const [isSendTxnPending, setIsSendTxnPending] = useState(false) const { data: tokenBalance, isPending: isPendingBalances } = useCollectibleBalance({ - accountAddress, + filter: { + accountAddresses: [accountAddress], + contractStatus: ContractVerificationStatus.ALL, + contractWhitelist: [contractAddress], + contractBlacklist: [] + }, chainId, - contractAddress, - tokenId, - verifiedOnly: false + tokenId }) const { contractType } = tokenBalance as TokenBalance From 958ac1e33056f4e407271acc463cf3f099a8824d Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Sun, 17 Nov 2024 18:22:53 -0500 Subject: [PATCH 3/6] simplified args in indexer call --- packages/kit/src/hooks/data.ts | 47 +++---------------- packages/wallet/src/hooks/data.ts | 10 ++-- .../src/views/CollectibleDetails/index.tsx | 1 - .../src/views/CollectionDetails/index.tsx | 11 +++-- 4 files changed, 21 insertions(+), 48 deletions(-) diff --git a/packages/kit/src/hooks/data.ts b/packages/kit/src/hooks/data.ts index c012215b..efdba2bd 100644 --- a/packages/kit/src/hooks/data.ts +++ b/packages/kit/src/hooks/data.ts @@ -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 || [] } @@ -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 : [])) @@ -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) => { @@ -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] }) } diff --git a/packages/wallet/src/hooks/data.ts b/packages/wallet/src/hooks/data.ts index dab8e00b..899e6dd1 100644 --- a/packages/wallet/src/hooks/data.ts +++ b/packages/wallet/src/hooks/data.ts @@ -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 diff --git a/packages/wallet/src/views/CollectibleDetails/index.tsx b/packages/wallet/src/views/CollectibleDetails/index.tsx index 8893d990..422e818b 100644 --- a/packages/wallet/src/views/CollectibleDetails/index.tsx +++ b/packages/wallet/src/views/CollectibleDetails/index.tsx @@ -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' diff --git a/packages/wallet/src/views/CollectionDetails/index.tsx b/packages/wallet/src/views/CollectionDetails/index.tsx index 511d942c..cc2650fc 100644 --- a/packages/wallet/src/views/CollectionDetails/index.tsx +++ b/packages/wallet/src/views/CollectionDetails/index.tsx @@ -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' @@ -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 From f237cb1d67ce4c2d21e373e2a35277edcb36e05a Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Mon, 25 Nov 2024 11:29:53 -0500 Subject: [PATCH 4/6] fixed erc1155 balance in SearchWallet --- packages/wallet/src/views/Search/components/BalanceItem.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/wallet/src/views/Search/components/BalanceItem.tsx b/packages/wallet/src/views/Search/components/BalanceItem.tsx index 681f2a29..ef74e1d5 100644 --- a/packages/wallet/src/views/Search/components/BalanceItem.tsx +++ b/packages/wallet/src/views/Search/components/BalanceItem.tsx @@ -23,7 +23,7 @@ export const BalanceItem = ({ balance }: BalanceItemProps) => { const getQuantity = () => { if (balance.contractType === 'ERC721' || balance.contractType === 'ERC1155') { - return balance.balance + return balance.uniqueCollectibles } const decimals = isNativeToken ? nativeTokenInfo.decimals : balance?.contractInfo?.decimals const bal = ethers.formatUnits(balance.balance, decimals || 0) From de95c4516e71da8c844fc4c0d7c144aef02961bf Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Mon, 25 Nov 2024 11:52:35 -0500 Subject: [PATCH 5/6] fixed native token logic for useCoinBalance --- packages/kit/src/hooks/data.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/hooks/data.ts b/packages/kit/src/hooks/data.ts index efdba2bd..41f45dab 100644 --- a/packages/kit/src/hooks/data.ts +++ b/packages/kit/src/hooks/data.ts @@ -135,7 +135,7 @@ export const useCoinBalance = (args: UseCoinBalanceArgs) => { return useQuery({ queryKey: ['coinBalance', args], queryFn: async () => { - if (compareAddress(args?.filter.accountAddresses[0] || '', zeroAddress)) { + if (compareAddress(args?.filter.contractWhitelist[0] || '', zeroAddress)) { const res = await getNativeTokenBalance(indexerClient, args.chainId, args.filter.accountAddresses[0] || '') return res } else { From 57b86fba76410dd99f6dd908e7d207e0dc378366 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Tue, 26 Nov 2024 10:05:18 -0500 Subject: [PATCH 6/6] removed comments --- examples/next/src/app/components/Connected.tsx | 2 -- examples/react/src/components/Connected.tsx | 2 -- packages/kit/src/hooks/data.ts | 5 ----- packages/wallet/src/hooks/data.ts | 2 -- 4 files changed, 11 deletions(-) diff --git a/examples/next/src/app/components/Connected.tsx b/examples/next/src/app/components/Connected.tsx index 7aca0b38..da9eed39 100644 --- a/examples/next/src/app/components/Connected.tsx +++ b/examples/next/src/app/components/Connected.tsx @@ -87,10 +87,8 @@ export const Connected = () => { const checkTokenBalancesForFeeOptions = async () => { if (pendingFeeOptionConfirmation) { const [account] = await walletClient!.getAddresses() - // ATTENTION: switch to new api const nativeTokenBalance = await indexerClient.getNativeTokenBalance({ accountAddress: account }) - //ATTENTION: switch to new api const tokenBalances = await indexerClient.getTokenBalancesSummary({ filter: { accountAddresses: [account], diff --git a/examples/react/src/components/Connected.tsx b/examples/react/src/components/Connected.tsx index 041a85a2..249b6426 100644 --- a/examples/react/src/components/Connected.tsx +++ b/examples/react/src/components/Connected.tsx @@ -134,10 +134,8 @@ export const Connected = () => { const checkTokenBalancesForFeeOptions = async () => { if (pendingFeeOptionConfirmation && walletClient) { const [account] = await walletClient.getAddresses() - // ATTENTION: switch to new api const nativeTokenBalance = await indexerClient.getNativeTokenBalance({ accountAddress: account }) - // ATTENTION: switch to new api const tokenBalances = await indexerClient.getTokenBalancesSummary({ filter: { accountAddresses: [account], diff --git a/packages/kit/src/hooks/data.ts b/packages/kit/src/hooks/data.ts index 41f45dab..2b25f5e6 100644 --- a/packages/kit/src/hooks/data.ts +++ b/packages/kit/src/hooks/data.ts @@ -26,7 +26,6 @@ export const time = { } export const getNativeTokenBalance = async (indexerClient: SequenceIndexer, chainId: number, accountAddress: string) => { - // ATTENTION: switch to new api const res = await indexerClient.getNativeTokenBalance({ accountAddress }) const tokenBalance: TokenBalance = { @@ -82,7 +81,6 @@ export const getTokenBalancesByContract = async (indexerClient: SequenceIndexer, } export const getBalances = async (indexerClient: SequenceIndexer, chainId: number, args: GetTokenBalancesSummaryArgs) => { - // ATTENTION: switch to new api if (!args.filter.accountAddresses[0]) { return [] } @@ -129,7 +127,6 @@ interface UseCoinBalanceArgs extends GetTokenBalancesSummaryArgs { } export const useCoinBalance = (args: UseCoinBalanceArgs) => { - // ATTENTION: switch to new api const indexerClient = useIndexerClient(args.chainId) return useQuery({ @@ -155,7 +152,6 @@ interface UseCollectibleBalanceArgs extends GetTokenBalancesDetailsArgs { } export const useCollectibleBalance = (args: UseCollectibleBalanceArgs) => { - // ATTENTION: switch to new api const indexerClient = useIndexerClient(args.chainId) return useQuery({ @@ -443,7 +439,6 @@ const getSwapPrices = async ( const currencyAddress = compareAddress(rawCurrencyAddress, NATIVE_TOKEN_ADDRESS_0X) ? zeroAddress : rawCurrencyAddress const isNativeToken = compareAddress(currencyAddress, zeroAddress) - // ATTENTION: switch to new api if (currencyAddress && !currencyBalanceInfoMap.has(currencyAddress)) { currencyBalanceInfoMap.set( currencyAddress, diff --git a/packages/wallet/src/hooks/data.ts b/packages/wallet/src/hooks/data.ts index 899e6dd1..6289d683 100644 --- a/packages/wallet/src/hooks/data.ts +++ b/packages/wallet/src/hooks/data.ts @@ -92,7 +92,6 @@ export const getBalancesAssetsSummary = async ( return [] } - // ATTENTION: switch to new api return getTokenBalancesDetails(indexerClient, { filter: { accountAddresses: [accountAddress], @@ -113,7 +112,6 @@ export const getBalancesAssetsSummary = async ( await Promise.all([ ...indexerClientsArr.map(([chainId, indexerClient]) => getNativeTokenBalance(indexerClient, chainId, accountAddress)), ...indexerClientsArr.map(([_chainId, indexerClient]) => - // ATTENTION: switch to new api getTokenBalancesSummary(indexerClient, { filter: { accountAddresses: [accountAddress],