From 1c20d5ac5c7e62f2bc569da258612245e7054537 Mon Sep 17 00:00:00 2001 From: songwongtp <16089160+songwongtp@users.noreply.github.com> Date: Mon, 1 Jul 2024 16:32:54 +0700 Subject: [PATCH 1/7] feat: module info lcd --- CHANGELOG.md | 1 + .../module-details/components/ModuleInfo.tsx | 19 ++++--- .../components/ModuleInfoBody.tsx | 51 +++++++++---------- src/lib/pages/module-details/index.tsx | 15 +++--- src/lib/services/move/module/api.ts | 10 ++-- src/lib/services/move/module/index.ts | 10 ++-- src/lib/services/types/move/module.ts | 9 ++-- src/lib/types/move/module.ts | 2 +- 8 files changed, 60 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d5313781..26225ea1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Improvements +- [#1000](https://github.com/alleslabs/celatone-frontend/pull/1000) Query module info from lcd directly - [#998](https://github.com/alleslabs/celatone-frontend/pull/998) Use expression on Nft query - [#996](https://github.com/alleslabs/celatone-frontend/pull/996) Allow disable voting period tally config - [#960](https://github.com/alleslabs/celatone-frontend/pull/960) Adjust UI in contract and account detail page diff --git a/src/lib/pages/module-details/components/ModuleInfo.tsx b/src/lib/pages/module-details/components/ModuleInfo.tsx index 8e4ed539f..e11638866 100644 --- a/src/lib/pages/module-details/components/ModuleInfo.tsx +++ b/src/lib/pages/module-details/components/ModuleInfo.tsx @@ -3,19 +3,22 @@ import { Flex, Heading, Text } from "@chakra-ui/react"; import { CustomIcon } from "lib/components/icon"; import { ModuleSourceCode } from "lib/components/module"; import type { ModuleVerificationInternal } from "lib/services/types"; -import type { ModuleData, Nullable, Option } from "lib/types"; +import type { + IndexedModule, + ModulePublishInfo, + Nullish, + Option, +} from "lib/types"; import { ModuleInfoBody } from "./ModuleInfoBody"; export interface ModuleInfoProps { - verificationData: Option>; - moduleData: Partial; + indexedModule: IndexedModule; + modulePublishInfo: Option; + verificationData: Nullish; } -export const ModuleInfo = ({ - verificationData, - moduleData, -}: ModuleInfoProps) => ( +export const ModuleInfo = ({ verificationData, ...props }: ModuleInfoProps) => ( @@ -31,7 +34,7 @@ export const ModuleInfo = ({ )} - + ); diff --git a/src/lib/pages/module-details/components/ModuleInfoBody.tsx b/src/lib/pages/module-details/components/ModuleInfoBody.tsx index 0277e4185..242e1d3ae 100644 --- a/src/lib/pages/module-details/components/ModuleInfoBody.tsx +++ b/src/lib/pages/module-details/components/ModuleInfoBody.tsx @@ -3,41 +3,38 @@ import { Grid } from "@chakra-ui/react"; import { useTierConfig } from "lib/app-provider"; import { ExplorerLink } from "lib/components/ExplorerLink"; import { LabelText } from "lib/components/LabelText"; -import type { ModuleData } from "lib/types"; import { dateFromNow, formatUTC } from "lib/utils"; import type { ModuleInfoProps } from "./ModuleInfo"; const ModuleInfoBodyPublishedAndRepublished = ({ - moduleData, -}: { - moduleData: Partial; -}) => { - const { isRepublished, recentPublishTransaction, recentPublishProposal } = - moduleData; - const labelPrefix = isRepublished ? "Latest Republished" : "Published"; + modulePublishInfo, +}: Pick) => { + const labelPrefix = modulePublishInfo?.isRepublished + ? "Latest Republished" + : "Published"; - if (recentPublishTransaction) { + if (modulePublishInfo?.recentPublishTransaction) { return ( ); } - if (recentPublishProposal) { + if (modulePublishInfo?.recentPublishProposal) { return ( @@ -48,15 +45,11 @@ const ModuleInfoBodyPublishedAndRepublished = ({ }; export const ModuleInfoBody = ({ - moduleData, + indexedModule, + modulePublishInfo, }: Omit) => { const isFullTier = useTierConfig() === "full"; - const { - address, - upgradePolicy, - recentPublishBlockHeight, - recentPublishBlockTimestamp, - } = moduleData; + const { address, upgradePolicy } = indexedModule; return ( {isFullTier && ( <> - {recentPublishBlockTimestamp && ( + {modulePublishInfo?.recentPublishBlockTimestamp && ( - {recentPublishBlockHeight ? ( + {modulePublishInfo?.recentPublishBlockHeight ? ( ) : ( @@ -94,7 +91,9 @@ export const ModuleInfoBody = ({ )} )} - + )} diff --git a/src/lib/pages/module-details/index.tsx b/src/lib/pages/module-details/index.tsx index 8e8d735f2..99c7b78ff 100644 --- a/src/lib/pages/module-details/index.tsx +++ b/src/lib/pages/module-details/index.tsx @@ -14,7 +14,7 @@ import { UserDocsLink } from "lib/components/UserDocsLink"; import { useFormatAddresses } from "lib/hooks/useFormatAddresses"; import { useModuleByAddressLcd, - useModuleData, + useModulePublishInfo, useModuleTableCounts, useVerifyModule, } from "lib/services/move/module"; @@ -50,13 +50,13 @@ const ModuleDetailsBody = ({ const isFullTier = useTierConfig() === "full"; const currentTab = !isFullTier && tab === TabIndex.TxsHistories ? TabIndex.Overview : tab; - const fullData = useModuleData(vmAddress, moduleName, isFullTier); - const liteData = useModuleByAddressLcd({ + + const { data, isLoading: isModuleLoading } = useModuleByAddressLcd({ address: vmAddress, moduleName, - options: { enabled: !isFullTier }, }); - const { data, isLoading } = isFullTier ? fullData : liteData; + const { data: modulePublishInfo, isFetching: isPublishInfoLoading } = + useModulePublishInfo(vmAddress, moduleName, isFullTier); const { data: moduleTableCounts } = useModuleTableCounts( vmAddress, @@ -115,7 +115,7 @@ const ModuleDetailsBody = ({ ? Object.values(TabIndex) : Object.values(TabIndex).filter((t) => t !== TabIndex.TxsHistories); - if (isLoading) return ; + if (isModuleLoading || isPublishInfoLoading) return ; if (!data) return ; return ( @@ -187,8 +187,9 @@ const ModuleDetailsBody = ({ }} /> {isFullTier && ( parseWithError(zModulesResponse, data)); -export const getModuleData = async ( +export const getModulePublishInfo = async ( endpoint: string, vmAddress: HexAddr, moduleName: string, isGov: boolean -): Promise => +): Promise => axios .get(`${endpoint}/${encodeURI(vmAddress)}/${encodeURI(moduleName)}/info`, { params: { is_gov: isGov, }, }) - .then(({ data }) => parseWithError(zModuleDataResponse, data)); + .then(({ data }) => parseWithError(zModulePublishInfoResponse, data)); export const getModuleTableCounts = async ( endpoint: string, diff --git a/src/lib/services/move/module/index.ts b/src/lib/services/move/module/index.ts index b3d5fba1e..c056b5a41 100644 --- a/src/lib/services/move/module/index.ts +++ b/src/lib/services/move/module/index.ts @@ -31,7 +31,7 @@ import type { ExposedFunction, HexAddr, IndexedModule, - ModuleData, + ModulePublishInfo, Nullable, Option, RpcQueryError, @@ -42,8 +42,8 @@ import { decodeModule, decodeScript, getFunctionView, - getModuleData, getModuleHistories, + getModulePublishInfo, getModuleRelatedProposals, getModules, getModulesByAddress, @@ -251,7 +251,7 @@ export const useModules = ( ); }; -export const useModuleData = ( +export const useModulePublishInfo = ( vmAddress: HexAddr, moduleName: string, enabled = true @@ -259,7 +259,7 @@ export const useModuleData = ( const endpoint = useBaseApiRoute("modules"); const govConfig = useGovConfig({ shouldRedirect: false }); - return useQuery( + return useQuery( [ CELATONE_QUERY_KEYS.MODULE_DATA, endpoint, @@ -268,7 +268,7 @@ export const useModuleData = ( govConfig.enabled, ], async () => - getModuleData(endpoint, vmAddress, moduleName, govConfig.enabled), + getModulePublishInfo(endpoint, vmAddress, moduleName, govConfig.enabled), { retry: 1, refetchOnWindowFocus: false, diff --git a/src/lib/services/types/move/module.ts b/src/lib/services/types/move/module.ts index 2193f4b8c..2f408e0e3 100644 --- a/src/lib/services/types/move/module.ts +++ b/src/lib/services/types/move/module.ts @@ -6,8 +6,8 @@ import type { HexAddr, IndexedModule, ModuleAbi, - ModuleData, ModuleInfo, + ModulePublishInfo, Option, } from "lib/types"; import { @@ -84,8 +84,8 @@ export const zModulesResponse = z.object({ }); export type ModulesResponse = z.infer; -export const zModuleDataResponse = zBaseModuleLcd - .extend({ +export const zModulePublishInfoResponse = z + .object({ recent_publish_transaction: z.string().nullable(), recent_publish_proposal: zProposal .pick({ id: true, title: true }) @@ -95,9 +95,8 @@ export const zModuleDataResponse = zBaseModuleLcd recent_publish_block_timestamp: zUtcDate, is_republished: z.boolean(), }) - .transform((val) => ({ + .transform((val) => ({ ...snakeToCamel(val), - ...indexModuleAbi(val.abi), recentPublishTransaction: val.recent_publish_transaction ? parseTxHash(val.recent_publish_transaction) : null, diff --git a/src/lib/types/move/module.ts b/src/lib/types/move/module.ts index b1a9c2667..0459b5aaf 100644 --- a/src/lib/types/move/module.ts +++ b/src/lib/types/move/module.ts @@ -31,7 +31,7 @@ export interface ModuleInfo isVerified?: boolean; } -export interface ModuleData extends IndexedModule { +export interface ModulePublishInfo { recentPublishTransaction: Nullable; recentPublishProposal: Nullable>; recentPublishBlockHeight: number; From 883716cffd1adbcd5c7e3610dbd74bdeefc9fcdc Mon Sep 17 00:00:00 2001 From: songwongtp <16089160+songwongtp@users.noreply.github.com> Date: Mon, 1 Jul 2024 16:44:12 +0700 Subject: [PATCH 2/7] fix: link path --- src/lib/services/move/module/api.ts | 13 ++++++++----- src/lib/services/types/move/module.ts | 5 ++--- src/lib/types/move/module.ts | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/lib/services/move/module/api.ts b/src/lib/services/move/module/api.ts index 9dca6bda5..acb10c1df 100644 --- a/src/lib/services/move/module/api.ts +++ b/src/lib/services/move/module/api.ts @@ -105,11 +105,14 @@ export const getModulePublishInfo = async ( isGov: boolean ): Promise => axios - .get(`${endpoint}/${encodeURI(vmAddress)}/${encodeURI(moduleName)}/info`, { - params: { - is_gov: isGov, - }, - }) + .get( + `${endpoint}/${encodeURI(vmAddress)}/${encodeURI(moduleName)}/publish_info`, + { + params: { + is_gov: isGov, + }, + } + ) .then(({ data }) => parseWithError(zModulePublishInfoResponse, data)); export const getModuleTableCounts = async ( diff --git a/src/lib/services/types/move/module.ts b/src/lib/services/types/move/module.ts index 2f408e0e3..e06bf8f85 100644 --- a/src/lib/services/types/move/module.ts +++ b/src/lib/services/types/move/module.ts @@ -89,8 +89,7 @@ export const zModulePublishInfoResponse = z recent_publish_transaction: z.string().nullable(), recent_publish_proposal: zProposal .pick({ id: true, title: true }) - .nullish() - .default(null), + .nullish(), recent_publish_block_height: z.number().nonnegative(), recent_publish_block_timestamp: zUtcDate, is_republished: z.boolean(), @@ -105,7 +104,7 @@ export const zModulePublishInfoResponse = z export const zModuleTableCountsResponse = z.object({ txs: z.number().nonnegative().nullable(), histories: z.number().nonnegative().nullable(), - proposals: z.number().nonnegative().nullish().default(null), + proposals: z.number().nonnegative().nullish(), }); export type ModuleTableCountsResponse = z.infer< typeof zModuleTableCountsResponse diff --git a/src/lib/types/move/module.ts b/src/lib/types/move/module.ts index 0459b5aaf..0e423ea27 100644 --- a/src/lib/types/move/module.ts +++ b/src/lib/types/move/module.ts @@ -33,7 +33,7 @@ export interface ModuleInfo export interface ModulePublishInfo { recentPublishTransaction: Nullable; - recentPublishProposal: Nullable>; + recentPublishProposal?: Nullable>; recentPublishBlockHeight: number; recentPublishBlockTimestamp: Date; isRepublished: boolean; From cbf7541ee070d2280cccdc36f545823fa59238a9 Mon Sep 17 00:00:00 2001 From: evilpeach Date: Mon, 1 Jul 2024 18:16:47 +0700 Subject: [PATCH 3/7] fix: handle pre and post db schema on nft, collections --- CHANGELOG.md | 1 + src/lib/query/collectionOld.ts | 222 +++++++++++++ src/lib/query/index.ts | 2 + src/lib/query/nftOld.ts | 215 ++++++++++++ src/lib/services/expression/nftExpression.ts | 31 ++ src/lib/services/nft/collectionOld.ts | 331 +++++++++++++++++++ src/lib/services/nft/collectionService.ts | 137 ++++++-- src/lib/services/nft/nftOld.ts | 214 ++++++++++++ src/lib/services/nft/nftService.ts | 94 +++++- 9 files changed, 1219 insertions(+), 28 deletions(-) create mode 100644 src/lib/query/collectionOld.ts create mode 100644 src/lib/query/nftOld.ts create mode 100644 src/lib/services/nft/collectionOld.ts create mode 100644 src/lib/services/nft/nftOld.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d5313781..78b2dd04a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Improvements +- [#1001](https://github.com/alleslabs/celatone-frontend/pull/1001) Support pre and post db schema on nft & collection queries - [#998](https://github.com/alleslabs/celatone-frontend/pull/998) Use expression on Nft query - [#996](https://github.com/alleslabs/celatone-frontend/pull/996) Allow disable voting period tally config - [#960](https://github.com/alleslabs/celatone-frontend/pull/960) Adjust UI in contract and account detail page diff --git a/src/lib/query/collectionOld.ts b/src/lib/query/collectionOld.ts new file mode 100644 index 000000000..3c8c986de --- /dev/null +++ b/src/lib/query/collectionOld.ts @@ -0,0 +1,222 @@ +import { gql } from "graphql-request"; + +export const getCollectionsQueryOld = gql` + query getCollectionsQuery($offset: Int!, $pageSize: Int!, $search: String) { + collections( + limit: $pageSize + offset: $offset + where: { + _or: [ + { name: { _iregex: $search } } + { vm_address: { vm_address: { _eq: $search } } } + ] + } + order_by: { name: asc } + ) { + name + uri + description + vm_address { + vm_address + } + vmAddressByCreator { + vm_address + } + } + collections_aggregate(where: { name: { _iregex: $search } }) { + aggregate { + count + } + } + } +`; + +export const getCollectionByCollectionAddressQueryOld = gql` + query getCollectionByCollectionAddressQuery($vmAddress: String!) { + collections(where: { vm_address: { vm_address: { _eq: $vmAddress } } }) { + name + uri + description + vmAddressByCreator { + collectionsByCreator { + block_height + name + vmAddressByCreator { + vm_address + } + } + } + } + } +`; + +export const getCollectionTotalBurnedCountQueryOld = gql` + query getCollectionTotalBurnedCountQuery($vmAddress: String!) { + nfts_aggregate( + where: { + collectionByCollection: { + vm_address: { vm_address: { _eq: $vmAddress } } + } + is_burned: { _eq: true } + } + ) { + aggregate { + count + } + } + } +`; + +export const getCollectionCreatorQueryOld = gql` + query getCollectionCreatorQuery($vmAddress: String!) { + collections(where: { vm_address: { vm_address: { _eq: $vmAddress } } }) { + vmAddressByCreator { + vm_address + } + collection_transactions( + order_by: { block_height: asc } + where: { is_collection_create: { _eq: true } } + ) { + transaction { + hash + block { + height + timestamp + } + } + } + } + } +`; + +export const getCollectionActivitiesCountQueryOld = gql` + query getCollectionActivitiesCountQuery($vmAddress: String!) { + collection_transactions_aggregate( + where: { collection: { vm_address: { vm_address: { _eq: $vmAddress } } } } + ) { + aggregate { + count + } + } + } +`; + +export const getCollectionMutateEventsCountQueryOld = gql` + query getCollectionMutateEventsCountQuery($vmAddress: String!) { + collection_mutation_events_aggregate( + where: { collection: { vm_address: { vm_address: { _eq: $vmAddress } } } } + ) { + aggregate { + count + } + } + } +`; + +export const getCollectionUniqueHoldersCountQueryOld = gql` + query getCollectionUniqueHoldersCountQuery($vmAddress: String!) { + nfts_aggregate( + where: { + collectionByCollection: { + vm_address: { vm_address: { _eq: $vmAddress } } + } + } + distinct_on: owner + ) { + aggregate { + count + } + } + } +`; + +export const getCollectionActivitiesQueryOld = gql` + query getCollectionActivitiesQuery( + $expression: collection_transactions_bool_exp + $offset: Int! + $pageSize: Int! + ) { + collection_transactions( + limit: $pageSize + offset: $offset + where: $expression + order_by: [ + { block_height: desc } + { nft: { token_id: desc } } + { is_nft_burn: desc } + { is_nft_transfer: desc } + { is_nft_mint: desc } + { is_collection_create: desc } + ] + ) { + transaction { + hash + block { + timestamp + } + } + is_nft_burn + is_nft_mint + is_nft_transfer + nft { + token_id + vm_address { + vm_address + } + } + is_collection_create + } + } +`; + +export const getCollectionMutateEventsQueryOld = gql` + query getCollectionMutateEventsQuery( + $collectionAddress: String! + $offset: Int! + $pageSize: Int! + ) { + collection_mutation_events( + limit: $pageSize + offset: $offset + where: { + collection: { vm_address: { vm_address: { _eq: $collectionAddress } } } + } + order_by: { block_height: desc } + ) { + mutated_field_name + new_value + old_value + remark + block { + timestamp + } + } + } +`; + +export const getCollectionsByAccountQueryOld = gql` + query getCollectionsByAccountQuery($accountAddress: String!) { + collections( + where: { + nfts: { vmAddressByOwner: { vm_address: { _eq: $accountAddress } } } + } + order_by: { name: asc } + ) { + name + uri + vm_address { + vm_address + } + nfts_aggregate( + where: { + vmAddressByOwner: { vm_address: { _eq: $accountAddress } } + is_burned: { _eq: false } + } + ) { + aggregate { + count + } + } + } + } +`; diff --git a/src/lib/query/index.ts b/src/lib/query/index.ts index df43855ee..0811e0302 100644 --- a/src/lib/query/index.ts +++ b/src/lib/query/index.ts @@ -5,4 +5,6 @@ export * from "./pool"; export * from "./tx"; export * from "./account"; export * from "./collection"; +export * from "./collectionOld"; export * from "./nft"; +export * from "./nftOld"; diff --git a/src/lib/query/nftOld.ts b/src/lib/query/nftOld.ts new file mode 100644 index 000000000..f6cf9407f --- /dev/null +++ b/src/lib/query/nftOld.ts @@ -0,0 +1,215 @@ +import { gql } from "graphql-request"; + +export const getNftQueryOld = gql` + query getNftQuery($collectionAddress: String!, $nftAddress: String!) { + nfts( + where: { + collectionByCollection: { + vm_address: { vm_address: { _eq: $collectionAddress } } + } + vm_address: { vm_address: { _eq: $nftAddress } } + } + ) { + token_id + uri + description + is_burned + vmAddressByOwner { + vm_address + } + collectionByCollection { + vm_address { + vm_address + } + name + } + } + } +`; + +export const getNftsQueryOld = gql` + query getNftsQuery( + $limit: Int! + $offset: Int! + $collectionAddress: String! + $search: String + ) { + nfts( + limit: $limit + offset: $offset + order_by: { token_id: asc } + where: { + collectionByCollection: { + vm_address: { vm_address: { _eq: $collectionAddress } } + } + is_burned: { _eq: false } + _or: [ + { token_id: { _iregex: $search } } + { vm_address: { vm_address: { _eq: $search } } } + ] + } + ) { + token_id + uri + description + is_burned + vmAddressByOwner { + vm_address + } + vm_address { + vm_address + } + collectionByCollection { + vm_address { + vm_address + } + name + } + } + } +`; + +export const getNftTransactionsCountQueryOld = gql` + query getNftTransactionsCountQuery($nftAddress: String!) { + nft_transactions_aggregate( + where: { nft: { vm_address: { vm_address: { _eq: $nftAddress } } } } + ) { + aggregate { + count + } + } + } +`; + +export const getNftTransactionsQueryOld = gql` + query getNftTransactionsQuery( + $limit: Int! + $offset: Int! + $nftAddress: String! + $search: String + ) { + nft_transactions( + offset: $offset + limit: $limit + where: { nft: { vm_address: { vm_address: { _eq: $nftAddress } } } } + order_by: [ + { block_height: desc } + { nft: { token_id: desc } } + { is_nft_burn: desc } + { is_nft_transfer: desc } + { is_nft_mint: desc } + ] + ) { + is_nft_burn + is_nft_mint + is_nft_transfer + transaction { + hash + block { + timestamp + height + } + } + } + } +`; + +export const getNftMutateEventsQueryOld = gql` + query getNftMutateEventsQuery( + $limit: Int! + $offset: Int! + $nftAddress: String! + ) { + nft_mutation_events( + limit: $limit + offset: $offset + where: { nft: { vm_address: { vm_address: { _eq: $nftAddress } } } } + order_by: { block_height: desc } + ) { + old_value + remark + mutated_field_name + new_value + block { + timestamp + } + } + } +`; + +export const getNftMutateEventsCountQueryOld = gql` + query getNftMutateEventsCountQuery($nftAddress: String!) { + nft_mutation_events_aggregate( + where: { nft: { vm_address: { vm_address: { _eq: $nftAddress } } } } + ) { + aggregate { + count + } + } + } +`; + +export const getNftsByAccountQueryOld = gql` + query getNftsByAccountQuery( + $accountAddress: String! + $pageSize: Int! + $offset: Int! + $search: String + ) { + nfts( + limit: $pageSize + offset: $offset + order_by: { token_id: asc } + where: { + vmAddressByOwner: { vm_address: { _eq: $accountAddress } } + is_burned: { _eq: false } + _or: [ + { token_id: { _iregex: $search } } + { vm_address: { vm_address: { _eq: $search } } } + ] + } + ) { + token_id + uri + is_burned + collectionByCollection { + vm_address { + vm_address + } + name + } + vmAddressByOwner { + vm_address + } + vm_address { + vm_address + } + } + nfts_aggregate( + where: { + vmAddressByOwner: { vm_address: { _eq: $accountAddress } } + token_id: { _iregex: $search } + is_burned: { _eq: false } + } + ) { + aggregate { + count + } + } + } +`; + +export const getNftsCountByAccountQueryOld = gql` + query getNftsCountByAccountQuery($accountAddress: String!) { + nfts_aggregate( + where: { + vmAddressByOwner: { vm_address: { _eq: $accountAddress } } + is_burned: { _eq: false } + } + ) { + aggregate { + count + } + } + } +`; diff --git a/src/lib/services/expression/nftExpression.ts b/src/lib/services/expression/nftExpression.ts index a757e1459..ea4e97baa 100644 --- a/src/lib/services/expression/nftExpression.ts +++ b/src/lib/services/expression/nftExpression.ts @@ -44,6 +44,37 @@ export const useCollectionActivitiesExpression = ( ); }; +export const useCollectionActivitiesExpressionOld = ( + collectionAddress: HexAddr32, + search = "" +) => { + const isNftAddress = isHexModuleAddress(search); + const isHash = isTxHash(search); + + const tokenIdSearch = { + nft: isNftAddress + ? { vm_address: { vm_address: { _eq: search.toLowerCase() } } } + : { token_id: { _iregex: search } }, + }; + const txHashSearch = { + transaction: { + hash: { + _eq: `\\x${search.toLowerCase()}`, + }, + }, + }; + + const searchOption = isHash ? txHashSearch : tokenIdSearch; + + return useMemo( + () => ({ + collection: { vm_address: { vm_address: { _eq: collectionAddress } } }, + _and: search ? searchOption : {}, + }), + [collectionAddress, search, searchOption] + ); +}; + /** * * @param collectionAddress diff --git a/src/lib/services/nft/collectionOld.ts b/src/lib/services/nft/collectionOld.ts new file mode 100644 index 000000000..5ef6c4c12 --- /dev/null +++ b/src/lib/services/nft/collectionOld.ts @@ -0,0 +1,331 @@ +import axios from "axios"; +import { z } from "zod"; + +import { + getCollectionActivitiesCountQueryOld, + getCollectionActivitiesQueryOld, + getCollectionByCollectionAddressQueryOld, + getCollectionCreatorQueryOld, + getCollectionMutateEventsCountQueryOld, + getCollectionMutateEventsQueryOld, + getCollectionsByAccountQueryOld, + getCollectionsQueryOld, + getCollectionUniqueHoldersCountQueryOld, +} from "lib/query"; +import type { HexAddr, HexAddr32, MutateEvent } from "lib/types"; +import { zHexAddr, zHexAddr32, zRemark, zUtcDate } from "lib/types"; +import { parseTxHash, parseWithError } from "lib/utils"; + +import type { Collection } from "./collection"; + +const zCollection = z + .object({ + name: z.string(), + vm_address: z.object({ vm_address: zHexAddr32 }), + uri: z.string(), + description: z.string(), + vmAddressByCreator: z.object({ vm_address: zHexAddr32 }), + }) + .transform((val) => ({ + description: val.description, + uri: val.uri, + name: val.name, + collectionAddress: val.vm_address.vm_address, + creator: val.vmAddressByCreator.vm_address, + })); + +const zCollectionsResponse = z + .object({ + collections: zCollection.array(), + collections_aggregate: z.object({ + aggregate: z.object({ + count: z.number(), + }), + }), + }) + .transform((val) => ({ + items: val.collections, + total: val.collections_aggregate.aggregate.count, + })); + +export const getCollectionsOld = async ( + indexer: string, + offset: number, + pageSize: number, + search?: string +) => + axios + .post(indexer, { + query: getCollectionsQueryOld, + variables: { offset, pageSize, search }, + }) + .then(({ data: res }) => parseWithError(zCollectionsResponse, res.data)); + +const zCollectionByCollectionAddressResponse = z.object({ + data: z + .object({ + name: z.string(), + description: z.string(), + uri: z.string(), + vmAddressByCreator: z.object({ + collectionsByCreator: z + .object({ + block_height: z.number(), + name: z.string(), + vmAddressByCreator: z.object({ vm_address: zHexAddr }), + }) + .array(), + }), + }) + .transform((val) => ({ + description: val.description, + name: val.name, + uri: val.uri, + createdHeight: + val.vmAddressByCreator.collectionsByCreator[0].block_height, + creatorAddress: + val.vmAddressByCreator.collectionsByCreator[0].vmAddressByCreator + .vm_address, + })) + .optional(), +}); + +export const getCollectionByCollectionAddressOld = async ( + indexer: string, + collectionAddress: HexAddr32 +) => + axios + .post(indexer, { + query: getCollectionByCollectionAddressQueryOld, + variables: { vmAddress: collectionAddress }, + }) + .then(({ data: res }) => + parseWithError(zCollectionByCollectionAddressResponse, { + data: res.data.collections[0], + }) + ); + +const zCollectionCreatorResponse = z + .object({ + collections: z + .object({ + collection_transactions: z + .object({ + transaction: z.object({ + block: z.object({ height: z.number(), timestamp: zUtcDate }), + hash: z.string(), + }), + }) + .array(), + vmAddressByCreator: z.object({ vm_address: zHexAddr }), + }) + .array(), + }) + .transform((val) => ({ + creatorAddress: val.collections[0].vmAddressByCreator.vm_address, + height: + val.collections[0].collection_transactions[0].transaction.block.height, + timestamp: + val.collections[0].collection_transactions[0].transaction.block.timestamp, + txhash: + val.collections[0].collection_transactions[0].transaction.hash.replace( + "\\x", + "" + ), + })); + +export const getCollectionCreatorOld = async ( + indexer: string, + collectionAddress: HexAddr32 +) => + axios + .post(indexer, { + query: getCollectionCreatorQueryOld, + variables: { vmAddress: collectionAddress }, + }) + .then(({ data: res }) => + parseWithError(zCollectionCreatorResponse, res.data) + ); + +const zActivity = z + .object({ + transaction: z.object({ + block: z.object({ timestamp: zUtcDate }), + hash: z.string().transform(parseTxHash), + }), + is_nft_burn: z.boolean(), + is_nft_mint: z.boolean(), + is_nft_transfer: z.boolean(), + nft: z + .object({ + token_id: z.string(), + vm_address: z.object({ vm_address: zHexAddr }), + }) + .optional() + .nullable(), + is_collection_create: z.boolean(), + }) + .transform((data) => ({ + timestamp: data.transaction.block.timestamp, + txhash: data.transaction.hash, + isNftBurn: data.is_nft_burn, + isNftMint: data.is_nft_mint, + isNftTransfer: data.is_nft_transfer, + tokenId: data.nft?.token_id, + nftAddress: data.nft?.vm_address.vm_address, + isCollectionCreate: data.is_collection_create, + })); + +export const getCollectionActivitiesOld = async ( + indexer: string, + pageSize: number, + offset: number, + expression?: object +) => { + return axios + .post(indexer, { + query: getCollectionActivitiesQueryOld, + variables: { + pageSize, + offset, + expression, + }, + }) + .then(({ data: res }) => + parseWithError(zActivity.array(), res.data.collection_transactions) + ); +}; + +const zActivitiesCountResponse = z + .object({ + collection_transactions_aggregate: z.object({ + aggregate: z.object({ count: z.number() }), + }), + }) + .transform( + (val) => val.collection_transactions_aggregate.aggregate.count + ); + +export const getCollectionActivitiesCountOld = async ( + indexer: string, + collectionAddress: HexAddr32 +) => + axios + .post(indexer, { + query: getCollectionActivitiesCountQueryOld, + variables: { vmAddress: collectionAddress }, + }) + .then(({ data }) => parseWithError(zActivitiesCountResponse, data.data)); + +const zCollectionMutateEventsResponse = z + .object({ + old_value: z.string(), + new_value: z.string(), + mutated_field_name: z.string(), + remark: zRemark, + block: z.object({ timestamp: zUtcDate }), + }) + .transform((val) => ({ + oldValue: val.old_value, + newValue: val.new_value, + mutatedFieldName: val.mutated_field_name, + remark: val.remark, + timestamp: val.block.timestamp, + })); + +export const getCollectionMutateEventsOld = async ( + indexer: string, + collectionAddress: HexAddr32, + pageSize: number, + offset: number +) => + axios + .post(indexer, { + query: getCollectionMutateEventsQueryOld, + variables: { + collectionAddress, + pageSize, + offset, + }, + }) + .then(({ data: res }) => + parseWithError( + zCollectionMutateEventsResponse.array(), + res.data.collection_mutation_events + ) + ); + +const zMutationEventsCountResponseItem = z + .object({ + collection_mutation_events_aggregate: z.object({ + aggregate: z.object({ count: z.number() }), + }), + }) + .transform( + (val) => val.collection_mutation_events_aggregate.aggregate.count + ); + +export const getCollectionMutateEventsCountOld = async ( + indexer: string, + collectionAddress: HexAddr32 +) => + axios + .post(indexer, { + query: getCollectionMutateEventsCountQueryOld, + variables: { vmAddress: collectionAddress }, + }) + .then(({ data }) => + parseWithError(zMutationEventsCountResponseItem, data.data) + ); + +const zUniqueHoldersCountResponseItem = z + .object({ + nfts_aggregate: z.object({ + aggregate: z.object({ count: z.number() }), + }), + }) + .transform((val) => val.nfts_aggregate.aggregate.count); + +export const getCollectionUniqueHoldersCountOld = async ( + indexer: string, + collectionAddress: HexAddr32 +) => + axios + .post(indexer, { + query: getCollectionUniqueHoldersCountQueryOld, + variables: { vmAddress: collectionAddress }, + }) + .then(({ data }) => + parseWithError(zUniqueHoldersCountResponseItem, data.data) + ); + +const zCollectionsByAccountResponse = z + .object({ + name: z.string(), + vm_address: z.object({ vm_address: zHexAddr32 }), + uri: z.string(), + nfts_aggregate: z.object({ aggregate: z.object({ count: z.number() }) }), + }) + .transform((val) => ({ + collectionName: val.name, + collectionAddress: val.vm_address.vm_address, + uri: val.uri, + hold: val.nfts_aggregate.aggregate.count, + })) + .array(); + +export const getCollectionsByAccountOld = async ( + indexer: string, + accountAddress: HexAddr +) => + axios + .post(indexer, { + query: getCollectionsByAccountQueryOld, + variables: { accountAddress }, + }) + .then(({ data: res }) => + parseWithError( + zCollectionsByAccountResponse, + res.data.collections + ).filter((collection) => collection.hold > 0) + ); diff --git a/src/lib/services/nft/collectionService.ts b/src/lib/services/nft/collectionService.ts index 5768640f4..5cc65dee5 100644 --- a/src/lib/services/nft/collectionService.ts +++ b/src/lib/services/nft/collectionService.ts @@ -5,7 +5,11 @@ import { useCollectionActivitiesExpression, useCollectionsExpression, } from "../expression"; -import { CELATONE_QUERY_KEYS, useCelatoneApp } from "lib/app-provider"; +import { + CELATONE_QUERY_KEYS, + useCelatoneApp, + useCurrentChain, +} from "lib/app-provider"; import type { HexAddr, HexAddr32, MutateEvent } from "lib/types"; import type { @@ -26,6 +30,19 @@ import { getCollectionsByAccount, getCollectionUniqueHoldersCount, } from "./collection"; +import { + getCollectionActivitiesCountOld, + getCollectionActivitiesOld, + getCollectionByCollectionAddressOld, + getCollectionCreatorOld, + getCollectionMutateEventsCountOld, + getCollectionMutateEventsOld, + getCollectionsByAccountOld, + getCollectionsOld, + getCollectionUniqueHoldersCountOld, +} from "./collectionOld"; + +const INITIATION_CHAIN_ID = "initiation-1"; export const useCollections = ( pageSize: number, @@ -34,6 +51,9 @@ export const useCollections = ( options?: Pick, "onSuccess"> ) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); const expression = useCollectionsExpression(search); return useQuery( @@ -45,7 +65,9 @@ export const useCollections = ( expression, ], async () => - getCollections(chainConfig.indexer, offset, pageSize, expression), + chainId === INITIATION_CHAIN_ID + ? getCollections(chainConfig.indexer, offset, pageSize, expression) + : getCollectionsOld(chainConfig.indexer, offset, pageSize, search), { retry: 1, refetchOnWindowFocus: false, @@ -58,6 +80,9 @@ export const useCollectionByCollectionAddress = ( collectionAddress: HexAddr32 ) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTION_BY_COLLECTION_ADDRESS, @@ -65,7 +90,15 @@ export const useCollectionByCollectionAddress = ( collectionAddress, ], async () => - getCollectionByCollectionAddress(chainConfig.indexer, collectionAddress), + chainId === INITIATION_CHAIN_ID + ? getCollectionByCollectionAddress( + chainConfig.indexer, + collectionAddress + ) + : getCollectionByCollectionAddressOld( + chainConfig.indexer, + collectionAddress + ), { retry: 1, refetchOnWindowFocus: false, @@ -75,13 +108,19 @@ export const useCollectionByCollectionAddress = ( export const useCollectionCreator = (collectionAddress: HexAddr32) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTION_CREATOR, chainConfig.indexer, collectionAddress, ], - async () => getCollectionCreator(chainConfig.indexer, collectionAddress), + async () => + chainId === INITIATION_CHAIN_ID + ? getCollectionCreator(chainConfig.indexer, collectionAddress) + : getCollectionCreatorOld(chainConfig.indexer, collectionAddress), { retry: 1, refetchOnWindowFocus: false, @@ -96,10 +135,17 @@ export const useCollectionActivities = ( search?: string ) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); const expression = useCollectionActivitiesExpression( collectionAddress, search ); + const expressionOld = useCollectionActivitiesExpression( + collectionAddress, + search + ); return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTION_ACTIVITIES, @@ -108,14 +154,22 @@ export const useCollectionActivities = ( offset, pageSize, expression, + expressionOld, ], async () => - getCollectionActivities( - chainConfig.indexer, - pageSize, - offset, - expression - ), + chainId === INITIATION_CHAIN_ID + ? getCollectionActivities( + chainConfig.indexer, + pageSize, + offset, + expression + ) + : getCollectionActivitiesOld( + chainConfig.indexer, + pageSize, + offset, + expressionOld + ), { retry: 1, refetchOnWindowFocus: false, @@ -125,6 +179,9 @@ export const useCollectionActivities = ( export const useCollectionActivitiesCount = (collectionAddress: HexAddr32) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTION_ACTIVITIES_COUNT, @@ -132,7 +189,12 @@ export const useCollectionActivitiesCount = (collectionAddress: HexAddr32) => { collectionAddress, ], async () => - getCollectionActivitiesCount(chainConfig.indexer, collectionAddress), + chainId === INITIATION_CHAIN_ID + ? getCollectionActivitiesCount(chainConfig.indexer, collectionAddress) + : getCollectionActivitiesCountOld( + chainConfig.indexer, + collectionAddress + ), { retry: 1, refetchOnWindowFocus: false, @@ -146,6 +208,9 @@ export const useCollectionMutateEvents = ( offset: number ) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTION_MUTATE_EVENTS, @@ -155,12 +220,19 @@ export const useCollectionMutateEvents = ( pageSize, ], async () => - getCollectionMutateEvents( - chainConfig.indexer, - collectionAddress, - pageSize, - offset - ), + chainId === INITIATION_CHAIN_ID + ? getCollectionMutateEvents( + chainConfig.indexer, + collectionAddress, + pageSize, + offset + ) + : getCollectionMutateEventsOld( + chainConfig.indexer, + collectionAddress, + pageSize, + offset + ), { retry: 1, refetchOnWindowFocus: false, @@ -172,6 +244,9 @@ export const useCollectionMutateEventsCount = ( collectionAddress: HexAddr32 ) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTION_MUTATE_EVENTS_COUNT, @@ -179,7 +254,12 @@ export const useCollectionMutateEventsCount = ( collectionAddress, ], async () => - getCollectionMutateEventsCount(chainConfig.indexer, collectionAddress), + chainId === INITIATION_CHAIN_ID + ? getCollectionMutateEventsCount(chainConfig.indexer, collectionAddress) + : getCollectionMutateEventsCountOld( + chainConfig.indexer, + collectionAddress + ), { retry: 1, refetchOnWindowFocus: false, @@ -191,6 +271,9 @@ export const useCollectionUniqueHoldersCount = ( collectionAddress: HexAddr32 ) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTION_UNIQUE_HOLDERS_COUNT, @@ -198,7 +281,15 @@ export const useCollectionUniqueHoldersCount = ( collectionAddress, ], async () => - getCollectionUniqueHoldersCount(chainConfig.indexer, collectionAddress), + chainId === INITIATION_CHAIN_ID + ? getCollectionUniqueHoldersCount( + chainConfig.indexer, + collectionAddress + ) + : getCollectionUniqueHoldersCountOld( + chainConfig.indexer, + collectionAddress + ), { retry: 1, refetchOnWindowFocus: false, @@ -208,13 +299,19 @@ export const useCollectionUniqueHoldersCount = ( export const useCollectionsByAccount = (accountAddress: HexAddr) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTIONS_BY_ACCOUNT, chainConfig.indexer, accountAddress, ], - async () => getCollectionsByAccount(chainConfig.indexer, accountAddress), + async () => + chainId === INITIATION_CHAIN_ID + ? getCollectionsByAccount(chainConfig.indexer, accountAddress) + : getCollectionsByAccountOld(chainConfig.indexer, accountAddress), { retry: 1, refetchOnWindowFocus: false, diff --git a/src/lib/services/nft/nftOld.ts b/src/lib/services/nft/nftOld.ts new file mode 100644 index 000000000..db87499c4 --- /dev/null +++ b/src/lib/services/nft/nftOld.ts @@ -0,0 +1,214 @@ +import axios from "axios"; +import { z } from "zod"; + +import { + getNftMutateEventsCountQueryOld, + getNftMutateEventsQueryOld, + getNftQueryOld, + getNftsByAccountQueryOld, + getNftsCountByAccountQueryOld, + getNftsQueryOld, + getNftTransactionsCountQueryOld, + getNftTransactionsQueryOld, +} from "lib/query"; +import { zHexAddr, zHexAddr32, zRemark, zUtcDate } from "lib/types"; +import type { HexAddr, HexAddr32, MutateEvent } from "lib/types"; +import { parseTxHash, parseWithError } from "lib/utils"; + +import type { Nft } from "./nft"; + +const zNft = z + .object({ + uri: z.string(), + token_id: z.string(), + description: z.string().optional(), + is_burned: z.boolean(), + vmAddressByOwner: z.object({ vm_address: zHexAddr }), + vm_address: z.object({ vm_address: zHexAddr32 }).optional(), + collectionByCollection: z.object({ + vm_address: z.object({ vm_address: zHexAddr32 }), + name: z.string(), + }), + }) + .transform((val) => ({ + uri: val.uri, + tokenId: val.token_id, + description: val.description, + isBurned: val.is_burned, + ownerAddress: val.vmAddressByOwner?.vm_address, + nftAddress: val.vm_address?.vm_address || ("" as HexAddr32), + collectionAddress: val.collectionByCollection.vm_address.vm_address, + collectionName: val.collectionByCollection.name, + })); + +export const getNftsOld = async ( + indexer: string, + collectionAddress: HexAddr32, + pageSize: number, + offset: number, + search: string +) => + axios + .post(indexer, { + query: getNftsQueryOld, + variables: { + collectionAddress, + limit: pageSize, + offset, + search, + }, + }) + .then(({ data: res }) => parseWithError(zNft.array(), res.data.nfts)); + +export const getNftsCountByAccountOld = async ( + indexer: string, + accountAddress: HexAddr +) => + axios + .post(indexer, { + query: getNftsCountByAccountQueryOld, + variables: { accountAddress }, + }) + .then(({ data: res }) => res.data.nfts_aggregate.aggregate.count); + +const zNftByNftAddressResponse = z.object({ + data: zNft.optional(), +}); + +export const getNftByNftAddressOld = async ( + indexer: string, + collectionAddress: HexAddr32, + nftAddress: HexAddr32 +) => + axios + .post(indexer, { + query: getNftQueryOld, + variables: { collectionAddress, nftAddress }, + }) + .then(({ data }) => + parseWithError(zNftByNftAddressResponse, { data: data.data.nfts[0] }) + ); + +const zNftTransactionsResponse = z + .object({ + transaction: z.object({ + hash: z.string().transform(parseTxHash), + block: z.object({ timestamp: zUtcDate, height: z.number() }), + }), + is_nft_burn: z.boolean(), + is_nft_mint: z.boolean(), + is_nft_transfer: z.boolean(), + }) + .transform((val) => ({ + txhash: val.transaction.hash, + timestamp: val.transaction.block.timestamp, + isNftBurn: val.is_nft_burn, + isNftMint: val.is_nft_mint, + isNftTransfer: val.is_nft_transfer, + })); + +export const getNftTransactionsOld = async ( + indexer: string, + nftAddress: HexAddr32, + offset: number, + limit: number +) => + axios + .post(indexer, { + query: getNftTransactionsQueryOld, + variables: { limit, offset, nftAddress }, + }) + .then(({ data: res }) => + parseWithError( + zNftTransactionsResponse.array(), + res.data.nft_transactions + ) + ); + +export const getNftTransactionsCountOld = async ( + indexer: string, + nftAddress: HexAddr32 +) => + axios + .post(indexer, { + query: getNftTransactionsCountQueryOld, + variables: { nftAddress }, + }) + .then( + ({ data: res }) => res.data.nft_transactions_aggregate.aggregate.count + ); + +const zNftMutateEventsResponseItem = z + .object({ + old_value: z.string(), + new_value: z.string(), + mutated_field_name: z.string(), + remark: zRemark, + block: z.object({ timestamp: zUtcDate }), + }) + .transform((val) => ({ + oldValue: val.old_value, + newValue: val.new_value, + mutatedFieldName: val.mutated_field_name, + remark: val.remark, + timestamp: val.block.timestamp, + })); + +export const getNftMutateEventsOld = async ( + indexer: string, + nftAddress: HexAddr32, + offset: number, + limit: number +) => + axios + .post(indexer, { + query: getNftMutateEventsQueryOld, + variables: { limit, offset, nftAddress }, + }) + .then(({ data: res }) => + parseWithError( + zNftMutateEventsResponseItem.array(), + res.data.nft_mutation_events + ) + ); + +export const getNftMutateEventsCountOld = async ( + indexer: string, + nftAddress: HexAddr32 +) => + axios + .post(indexer, { + query: getNftMutateEventsCountQueryOld, + variables: { nftAddress }, + }) + .then( + ({ data }) => data.data.nft_mutation_events_aggregate.aggregate.count + ); + +const zNftsByAccountResponse = z + .object({ + nfts: zNft.array(), + nfts_aggregate: z.object({ + aggregate: z.object({ + count: z.number(), + }), + }), + }) + .transform((val) => ({ + nfts: val.nfts, + total: val.nfts_aggregate.aggregate.count, + })); + +export const getNftsByAccountOld = async ( + indexer: string, + accountAddress: HexAddr, + pageSize: number, + offset: number, + search: string +) => + axios + .post(indexer, { + query: getNftsByAccountQueryOld, + variables: { accountAddress, pageSize, offset, search }, + }) + .then(({ data: res }) => parseWithError(zNftsByAccountResponse, res.data)); diff --git a/src/lib/services/nft/nftService.ts b/src/lib/services/nft/nftService.ts index 9617ddf21..8124dac0c 100644 --- a/src/lib/services/nft/nftService.ts +++ b/src/lib/services/nft/nftService.ts @@ -5,6 +5,7 @@ import { useNftsByAccountExpression, useNftsExpression } from "../expression"; import { CELATONE_QUERY_KEYS, useCelatoneApp, + useCurrentChain, useNftConfig, } from "lib/app-provider"; import type { HexAddr, HexAddr32, MutateEvent } from "lib/types"; @@ -29,6 +30,18 @@ import { getNftTransactions, getNftTransactionsCount, } from "./nft"; +import { + getNftByNftAddressOld, + getNftMutateEventsCountOld, + getNftMutateEventsOld, + getNftsByAccountOld, + getNftsCountByAccountOld, + getNftsOld, + getNftTransactionsCountOld, + getNftTransactionsOld, +} from "./nftOld"; + +const INITIATION_CHAIN_ID = "initiation-1"; export const useNfts = ( collectionAddress: HexAddr32, @@ -37,6 +50,9 @@ export const useNfts = ( search = "" ) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); const expression = useNftsExpression(collectionAddress, search); return useQuery( @@ -49,7 +65,16 @@ export const useNfts = ( search, expression, ], - async () => getNfts(chainConfig.indexer, pageSize, offset, expression), + async () => + chainId === INITIATION_CHAIN_ID + ? getNfts(chainConfig.indexer, pageSize, offset, expression) + : getNftsOld( + chainConfig.indexer, + collectionAddress, + pageSize, + offset, + search + ), { retry: 1, refetchOnWindowFocus: false, @@ -62,6 +87,10 @@ export const useNftByNftAddress = ( nftAddress: HexAddr32 ) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); + return useQuery( [ CELATONE_QUERY_KEYS.NFT_BY_NFT_ADDRESS, @@ -70,7 +99,13 @@ export const useNftByNftAddress = ( nftAddress, ], async () => - getNftByNftAddress(chainConfig.indexer, collectionAddress, nftAddress), + chainId === INITIATION_CHAIN_ID + ? getNftByNftAddress(chainConfig.indexer, collectionAddress, nftAddress) + : getNftByNftAddressOld( + chainConfig.indexer, + collectionAddress, + nftAddress + ), { retry: 1, refetchOnWindowFocus: false, @@ -108,6 +143,10 @@ export const useNftTransactions = ( nftAddress: HexAddr32 ) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); + return useQuery( [ CELATONE_QUERY_KEYS.NFT_TRANSACTIONS, @@ -117,7 +156,9 @@ export const useNftTransactions = ( offset, ], async () => - getNftTransactions(chainConfig.indexer, nftAddress, offset, limit), + chainId === INITIATION_CHAIN_ID + ? getNftTransactions(chainConfig.indexer, nftAddress, offset, limit) + : getNftTransactionsOld(chainConfig.indexer, nftAddress, offset, limit), { retry: 1, refetchOnWindowFocus: false, @@ -127,13 +168,20 @@ export const useNftTransactions = ( export const useNftTransactionsCount = (nftAddress: HexAddr32) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); + return useQuery( [ CELATONE_QUERY_KEYS.NFT_TRANSACTIONS_COUNT, chainConfig.indexer, nftAddress, ], - async () => getNftTransactionsCount(chainConfig.indexer, nftAddress), + async () => + chainId === INITIATION_CHAIN_ID + ? getNftTransactionsCount(chainConfig.indexer, nftAddress) + : getNftTransactionsCountOld(chainConfig.indexer, nftAddress), { retry: 1, refetchOnWindowFocus: false, @@ -147,6 +195,10 @@ export const useNftMutateEvents = ( nftAddress: HexAddr32 ) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); + return useQuery( [ CELATONE_QUERY_KEYS.NFT_MUTATE_EVENTS, @@ -156,7 +208,9 @@ export const useNftMutateEvents = ( offset, ], async () => - getNftMutateEvents(chainConfig.indexer, nftAddress, offset, limit), + chainId === INITIATION_CHAIN_ID + ? getNftMutateEvents(chainConfig.indexer, nftAddress, offset, limit) + : getNftMutateEventsOld(chainConfig.indexer, nftAddress, offset, limit), { retry: 1, refetchOnWindowFocus: false, @@ -166,13 +220,20 @@ export const useNftMutateEvents = ( export const useNftMutateEventsCount = (nftAddress: HexAddr32) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); + return useQuery( [ CELATONE_QUERY_KEYS.NFT_MUTATE_EVENTS_COUNT, chainConfig.indexer, nftAddress, ], - async () => getNftMutateEventsCount(chainConfig.indexer, nftAddress), + async () => + chainId === INITIATION_CHAIN_ID + ? getNftMutateEventsCount(chainConfig.indexer, nftAddress) + : getNftMutateEventsCountOld(chainConfig.indexer, nftAddress), { retry: 1, refetchOnWindowFocus: false, @@ -183,6 +244,9 @@ export const useNftMutateEventsCount = (nftAddress: HexAddr32) => { export const useNftsCountByAccount = (accountAddress: HexAddr) => { const { chainConfig } = useCelatoneApp(); const { enabled } = useNftConfig({ shouldRedirect: false }); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); return useQuery( [ @@ -190,7 +254,10 @@ export const useNftsCountByAccount = (accountAddress: HexAddr) => { chainConfig.indexer, accountAddress, ], - async () => getNftsCountByAccount(chainConfig.indexer, accountAddress), + async () => + chainId === INITIATION_CHAIN_ID + ? getNftsCountByAccount(chainConfig.indexer, accountAddress) + : getNftsCountByAccountOld(chainConfig.indexer, accountAddress), { retry: 1, refetchOnWindowFocus: false, @@ -208,6 +275,9 @@ export const useNftsByAccountByCollection = ( options: Pick, "onSuccess"> = {} ) => { const { chainConfig } = useCelatoneApp(); + const { + chain: { chain_id: chainId }, + } = useCurrentChain(); const expression = useNftsByAccountExpression( accountAddress, collectionAddress, @@ -225,7 +295,15 @@ export const useNftsByAccountByCollection = ( collectionAddress, ], async () => - getNftsByAccount(chainConfig.indexer, pageSize, offset, expression), + chainId === INITIATION_CHAIN_ID + ? getNftsByAccount(chainConfig.indexer, pageSize, offset, expression) + : getNftsByAccountOld( + chainConfig.indexer, + accountAddress, + pageSize, + offset, + search + ), { retry: 1, refetchOnWindowFocus: false, From ae88e8c3549466bc0f2ba6ab8dbf37cbcee93532 Mon Sep 17 00:00:00 2001 From: evilpeach Date: Mon, 1 Jul 2024 18:49:10 +0700 Subject: [PATCH 4/7] fix: wrong expression --- src/lib/services/nft/collectionService.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/services/nft/collectionService.ts b/src/lib/services/nft/collectionService.ts index 5cc65dee5..2b0030196 100644 --- a/src/lib/services/nft/collectionService.ts +++ b/src/lib/services/nft/collectionService.ts @@ -3,6 +3,7 @@ import { useQuery } from "@tanstack/react-query"; import { useCollectionActivitiesExpression, + useCollectionActivitiesExpressionOld, useCollectionsExpression, } from "../expression"; import { @@ -142,7 +143,7 @@ export const useCollectionActivities = ( collectionAddress, search ); - const expressionOld = useCollectionActivitiesExpression( + const expressionOld = useCollectionActivitiesExpressionOld( collectionAddress, search ); From d126214f80768e643de25699a1150fdad5cb534c Mon Sep 17 00:00:00 2001 From: evilpeach Date: Tue, 2 Jul 2024 12:00:59 +0700 Subject: [PATCH 5/7] fix: improve queries --- src/lib/query/collectionOld.ts | 15 ++- src/lib/query/nftOld.ts | 19 +--- src/lib/services/expression/index.ts | 1 + src/lib/services/expression/nftExpression.ts | 37 -------- .../services/expression/nftExpressionOld.ts | 94 +++++++++++++++++++ src/lib/services/nft/collectionOld.ts | 4 +- src/lib/services/nft/collectionService.ts | 16 +++- src/lib/services/nft/nftOld.ts | 4 +- src/lib/services/nft/nftService.ts | 25 ++++- 9 files changed, 140 insertions(+), 75 deletions(-) create mode 100644 src/lib/services/expression/nftExpressionOld.ts diff --git a/src/lib/query/collectionOld.ts b/src/lib/query/collectionOld.ts index 3c8c986de..d2c7dccbf 100644 --- a/src/lib/query/collectionOld.ts +++ b/src/lib/query/collectionOld.ts @@ -1,16 +1,15 @@ import { gql } from "graphql-request"; export const getCollectionsQueryOld = gql` - query getCollectionsQuery($offset: Int!, $pageSize: Int!, $search: String) { + query getCollectionsQuery( + $offset: Int! + $pageSize: Int! + $expression: collections_bool_exp! + ) { collections( limit: $pageSize offset: $offset - where: { - _or: [ - { name: { _iregex: $search } } - { vm_address: { vm_address: { _eq: $search } } } - ] - } + where: $expression order_by: { name: asc } ) { name @@ -23,7 +22,7 @@ export const getCollectionsQueryOld = gql` vm_address } } - collections_aggregate(where: { name: { _iregex: $search } }) { + collections_aggregate(where: $expression) { aggregate { count } diff --git a/src/lib/query/nftOld.ts b/src/lib/query/nftOld.ts index f6cf9407f..2d153b4f0 100644 --- a/src/lib/query/nftOld.ts +++ b/src/lib/query/nftOld.ts @@ -154,20 +154,13 @@ export const getNftsByAccountQueryOld = gql` $accountAddress: String! $pageSize: Int! $offset: Int! - $search: String + $expression: nfts_bool_exp! ) { nfts( limit: $pageSize offset: $offset order_by: { token_id: asc } - where: { - vmAddressByOwner: { vm_address: { _eq: $accountAddress } } - is_burned: { _eq: false } - _or: [ - { token_id: { _iregex: $search } } - { vm_address: { vm_address: { _eq: $search } } } - ] - } + where: $expression ) { token_id uri @@ -185,13 +178,7 @@ export const getNftsByAccountQueryOld = gql` vm_address } } - nfts_aggregate( - where: { - vmAddressByOwner: { vm_address: { _eq: $accountAddress } } - token_id: { _iregex: $search } - is_burned: { _eq: false } - } - ) { + nfts_aggregate(where: $expression) { aggregate { count } diff --git a/src/lib/services/expression/index.ts b/src/lib/services/expression/index.ts index d77544e68..99b61a2ef 100644 --- a/src/lib/services/expression/index.ts +++ b/src/lib/services/expression/index.ts @@ -1,3 +1,4 @@ export * from "./nftExpression"; +export * from "./nftExpressionOld"; export * from "./poolExpression"; export * from "./txExpression"; diff --git a/src/lib/services/expression/nftExpression.ts b/src/lib/services/expression/nftExpression.ts index ea4e97baa..ad7d770c2 100644 --- a/src/lib/services/expression/nftExpression.ts +++ b/src/lib/services/expression/nftExpression.ts @@ -44,43 +44,6 @@ export const useCollectionActivitiesExpression = ( ); }; -export const useCollectionActivitiesExpressionOld = ( - collectionAddress: HexAddr32, - search = "" -) => { - const isNftAddress = isHexModuleAddress(search); - const isHash = isTxHash(search); - - const tokenIdSearch = { - nft: isNftAddress - ? { vm_address: { vm_address: { _eq: search.toLowerCase() } } } - : { token_id: { _iregex: search } }, - }; - const txHashSearch = { - transaction: { - hash: { - _eq: `\\x${search.toLowerCase()}`, - }, - }, - }; - - const searchOption = isHash ? txHashSearch : tokenIdSearch; - - return useMemo( - () => ({ - collection: { vm_address: { vm_address: { _eq: collectionAddress } } }, - _and: search ? searchOption : {}, - }), - [collectionAddress, search, searchOption] - ); -}; - -/** - * - * @param collectionAddress - * @param search - NFT address or token ID - * @returns - */ export const useNftsExpression = (collectionAddress: HexAddr32, search = "") => useMemo(() => { const orExpression = { diff --git a/src/lib/services/expression/nftExpressionOld.ts b/src/lib/services/expression/nftExpressionOld.ts new file mode 100644 index 000000000..adcd3afb9 --- /dev/null +++ b/src/lib/services/expression/nftExpressionOld.ts @@ -0,0 +1,94 @@ +import { useMemo } from "react"; + +import type { HexAddr, HexAddr32 } from "lib/types"; +import { isHexModuleAddress, isTxHash } from "lib/utils"; + +export const useCollectionsExpressionOld = (search = "") => + useMemo(() => { + if (search.trim().length === 0) return {}; + + return { + _or: [ + { name: { _iregex: search } }, + { vm_address: { vm_address: { _eq: search.toLowerCase() } } }, + ], + }; + }, [search]); + +export const useCollectionActivitiesExpressionOld = ( + collectionAddress: HexAddr32, + search = "" +) => { + const isNftAddress = isHexModuleAddress(search); + const isHash = isTxHash(search); + + const tokenIdSearch = { + nft: isNftAddress + ? { vm_address: { vm_address: { _eq: search.toLowerCase() } } } + : { token_id: { _iregex: search } }, + }; + const txHashSearch = { + transaction: { + hash: { + _eq: `\\x${search.toLowerCase()}`, + }, + }, + }; + + const searchOption = isHash ? txHashSearch : tokenIdSearch; + + return useMemo( + () => ({ + collection: { vm_address: { vm_address: { _eq: collectionAddress } } }, + ...(search ? { _and: searchOption } : {}), + }), + [collectionAddress, search, searchOption] + ); +}; + +export const useNftsExpressionOld = ( + collectionAddress: HexAddr32, + search = "" +) => + useMemo(() => { + const orExpression = { + _or: [ + { token_id: { _iregex: search } }, + ...(isHexModuleAddress(search) + ? [{ vm_address: { vm_address: { _eq: search.toLowerCase() } } }] + : []), + ], + }; + + return { + collection: { _eq: collectionAddress }, + is_burned: { _eq: false }, + ...(search.trim().length > 0 ? orExpression : {}), + }; + }, [collectionAddress, search]); + +export const useNftsByAccountExpressionOld = ( + accountAddress: HexAddr, + collectionAddress?: HexAddr32, + search = "" +) => + useMemo(() => { + const collectionExpression = collectionAddress + ? [ + { + vm_address: { + vm_address: { _eq: collectionAddress.toLowerCase() }, + }, + }, + ] + : []; + const orExpression = { + _or: [{ token_id: { _iregex: search } }, ...collectionExpression], + }; + + return { + vmAddressByOwner: { vm_address: { _eq: accountAddress } }, + is_burned: { _eq: false }, + ...(search.trim().length > 0 ? orExpression : {}), + }; + }, [accountAddress, collectionAddress, search]); diff --git a/src/lib/services/nft/collectionOld.ts b/src/lib/services/nft/collectionOld.ts index 5ef6c4c12..23d04d2bd 100644 --- a/src/lib/services/nft/collectionOld.ts +++ b/src/lib/services/nft/collectionOld.ts @@ -52,12 +52,12 @@ export const getCollectionsOld = async ( indexer: string, offset: number, pageSize: number, - search?: string + expression: object ) => axios .post(indexer, { query: getCollectionsQueryOld, - variables: { offset, pageSize, search }, + variables: { offset, pageSize, expression }, }) .then(({ data: res }) => parseWithError(zCollectionsResponse, res.data)); diff --git a/src/lib/services/nft/collectionService.ts b/src/lib/services/nft/collectionService.ts index 2b0030196..5dd3f8bf9 100644 --- a/src/lib/services/nft/collectionService.ts +++ b/src/lib/services/nft/collectionService.ts @@ -5,6 +5,7 @@ import { useCollectionActivitiesExpression, useCollectionActivitiesExpressionOld, useCollectionsExpression, + useCollectionsExpressionOld, } from "../expression"; import { CELATONE_QUERY_KEYS, @@ -55,7 +56,10 @@ export const useCollections = ( const { chain: { chain_id: chainId }, } = useCurrentChain(); - const expression = useCollectionsExpression(search); + const expressionNew = useCollectionsExpression(search); + const expressionOld = useCollectionsExpressionOld(search); + const expression = + chainId === INITIATION_CHAIN_ID ? expressionNew : expressionOld; return useQuery( [ @@ -68,7 +72,7 @@ export const useCollections = ( async () => chainId === INITIATION_CHAIN_ID ? getCollections(chainConfig.indexer, offset, pageSize, expression) - : getCollectionsOld(chainConfig.indexer, offset, pageSize, search), + : getCollectionsOld(chainConfig.indexer, offset, pageSize, expression), { retry: 1, refetchOnWindowFocus: false, @@ -139,7 +143,7 @@ export const useCollectionActivities = ( const { chain: { chain_id: chainId }, } = useCurrentChain(); - const expression = useCollectionActivitiesExpression( + const expressionNew = useCollectionActivitiesExpression( collectionAddress, search ); @@ -147,6 +151,9 @@ export const useCollectionActivities = ( collectionAddress, search ); + const expression = + chainId === INITIATION_CHAIN_ID ? expressionNew : expressionOld; + return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTION_ACTIVITIES, @@ -155,7 +162,6 @@ export const useCollectionActivities = ( offset, pageSize, expression, - expressionOld, ], async () => chainId === INITIATION_CHAIN_ID @@ -169,7 +175,7 @@ export const useCollectionActivities = ( chainConfig.indexer, pageSize, offset, - expressionOld + expression ), { retry: 1, diff --git a/src/lib/services/nft/nftOld.ts b/src/lib/services/nft/nftOld.ts index db87499c4..7f444f013 100644 --- a/src/lib/services/nft/nftOld.ts +++ b/src/lib/services/nft/nftOld.ts @@ -204,11 +204,11 @@ export const getNftsByAccountOld = async ( accountAddress: HexAddr, pageSize: number, offset: number, - search: string + expression: object ) => axios .post(indexer, { query: getNftsByAccountQueryOld, - variables: { accountAddress, pageSize, offset, search }, + variables: { accountAddress, pageSize, offset, expression }, }) .then(({ data: res }) => parseWithError(zNftsByAccountResponse, res.data)); diff --git a/src/lib/services/nft/nftService.ts b/src/lib/services/nft/nftService.ts index 8124dac0c..36e84c5fa 100644 --- a/src/lib/services/nft/nftService.ts +++ b/src/lib/services/nft/nftService.ts @@ -1,7 +1,12 @@ import type { UseQueryOptions } from "@tanstack/react-query"; import { useQuery } from "@tanstack/react-query"; -import { useNftsByAccountExpression, useNftsExpression } from "../expression"; +import { + useNftsByAccountExpression, + useNftsByAccountExpressionOld, + useNftsExpression, + useNftsExpressionOld, +} from "../expression"; import { CELATONE_QUERY_KEYS, useCelatoneApp, @@ -53,7 +58,10 @@ export const useNfts = ( const { chain: { chain_id: chainId }, } = useCurrentChain(); - const expression = useNftsExpression(collectionAddress, search); + const expressionNew = useNftsExpression(collectionAddress, search); + const expressionOld = useNftsExpressionOld(collectionAddress, search); + const expression = + chainId === INITIATION_CHAIN_ID ? expressionNew : expressionOld; return useQuery( [ @@ -278,11 +286,18 @@ export const useNftsByAccountByCollection = ( const { chain: { chain_id: chainId }, } = useCurrentChain(); - const expression = useNftsByAccountExpression( + const expressionNew = useNftsByAccountExpression( + accountAddress, + collectionAddress, + search + ); + const expressionOld = useNftsByAccountExpressionOld( accountAddress, collectionAddress, search ); + const expression = + chainId === INITIATION_CHAIN_ID ? expressionNew : expressionOld; return useQuery( [ @@ -291,8 +306,8 @@ export const useNftsByAccountByCollection = ( accountAddress, pageSize, offset, - search, collectionAddress, + expression, ], async () => chainId === INITIATION_CHAIN_ID @@ -302,7 +317,7 @@ export const useNftsByAccountByCollection = ( accountAddress, pageSize, offset, - search + expression ), { retry: 1, From 49663cca96f4898ba8cbe9ef68b28b662745f92e Mon Sep 17 00:00:00 2001 From: evilpeach Date: Tue, 2 Jul 2024 12:05:37 +0700 Subject: [PATCH 6/7] fix: remove unused --- src/lib/query/nftOld.ts | 1 - src/lib/services/nft/nftOld.ts | 3 +-- src/lib/services/nft/nftService.ts | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/lib/query/nftOld.ts b/src/lib/query/nftOld.ts index 2d153b4f0..ac91de560 100644 --- a/src/lib/query/nftOld.ts +++ b/src/lib/query/nftOld.ts @@ -151,7 +151,6 @@ export const getNftMutateEventsCountQueryOld = gql` export const getNftsByAccountQueryOld = gql` query getNftsByAccountQuery( - $accountAddress: String! $pageSize: Int! $offset: Int! $expression: nfts_bool_exp! diff --git a/src/lib/services/nft/nftOld.ts b/src/lib/services/nft/nftOld.ts index 7f444f013..2f55289b2 100644 --- a/src/lib/services/nft/nftOld.ts +++ b/src/lib/services/nft/nftOld.ts @@ -201,7 +201,6 @@ const zNftsByAccountResponse = z export const getNftsByAccountOld = async ( indexer: string, - accountAddress: HexAddr, pageSize: number, offset: number, expression: object @@ -209,6 +208,6 @@ export const getNftsByAccountOld = async ( axios .post(indexer, { query: getNftsByAccountQueryOld, - variables: { accountAddress, pageSize, offset, expression }, + variables: { pageSize, offset, expression }, }) .then(({ data: res }) => parseWithError(zNftsByAccountResponse, res.data)); diff --git a/src/lib/services/nft/nftService.ts b/src/lib/services/nft/nftService.ts index 36e84c5fa..1944fc8ff 100644 --- a/src/lib/services/nft/nftService.ts +++ b/src/lib/services/nft/nftService.ts @@ -314,7 +314,6 @@ export const useNftsByAccountByCollection = ( ? getNftsByAccount(chainConfig.indexer, pageSize, offset, expression) : getNftsByAccountOld( chainConfig.indexer, - accountAddress, pageSize, offset, expression From c957c4aa4219c1c52f7025617a631ac55614be43 Mon Sep 17 00:00:00 2001 From: evilpeach Date: Tue, 2 Jul 2024 15:46:16 +0700 Subject: [PATCH 7/7] fix: as comments --- src/lib/query/nftOld.ts | 18 ++---------------- .../services/expression/nftExpressionOld.ts | 12 ++++++++---- src/lib/services/nft/nftOld.ts | 6 ++---- src/lib/services/nft/nftService.ts | 8 +------- 4 files changed, 13 insertions(+), 31 deletions(-) diff --git a/src/lib/query/nftOld.ts b/src/lib/query/nftOld.ts index ac91de560..bc54d7b1d 100644 --- a/src/lib/query/nftOld.ts +++ b/src/lib/query/nftOld.ts @@ -28,26 +28,12 @@ export const getNftQueryOld = gql` `; export const getNftsQueryOld = gql` - query getNftsQuery( - $limit: Int! - $offset: Int! - $collectionAddress: String! - $search: String - ) { + query getNftsQuery($limit: Int!, $offset: Int!, $expression: nfts_bool_exp!) { nfts( limit: $limit offset: $offset order_by: { token_id: asc } - where: { - collectionByCollection: { - vm_address: { vm_address: { _eq: $collectionAddress } } - } - is_burned: { _eq: false } - _or: [ - { token_id: { _iregex: $search } } - { vm_address: { vm_address: { _eq: $search } } } - ] - } + where: $expression ) { token_id uri diff --git a/src/lib/services/expression/nftExpressionOld.ts b/src/lib/services/expression/nftExpressionOld.ts index adcd3afb9..237961220 100644 --- a/src/lib/services/expression/nftExpressionOld.ts +++ b/src/lib/services/expression/nftExpressionOld.ts @@ -61,7 +61,9 @@ export const useNftsExpressionOld = ( }; return { - collection: { _eq: collectionAddress }, + collectionByCollection: { + vm_address: { vm_address: { _eq: collectionAddress } }, + }, is_burned: { _eq: false }, ...(search.trim().length > 0 ? orExpression : {}), }; @@ -73,6 +75,10 @@ export const useNftsByAccountExpressionOld = ( search = "" ) => useMemo(() => { + const orExpression = { + _or: [{ token_id: { _iregex: search } }], + }; + const collectionExpression = collectionAddress ? [ { @@ -82,13 +88,11 @@ export const useNftsByAccountExpressionOld = ( }, ] : []; - const orExpression = { - _or: [{ token_id: { _iregex: search } }, ...collectionExpression], - }; return { vmAddressByOwner: { vm_address: { _eq: accountAddress } }, is_burned: { _eq: false }, + ...(collectionAddress ? collectionExpression : {}), ...(search.trim().length > 0 ? orExpression : {}), }; }, [accountAddress, collectionAddress, search]); diff --git a/src/lib/services/nft/nftOld.ts b/src/lib/services/nft/nftOld.ts index 2f55289b2..7e133e4ae 100644 --- a/src/lib/services/nft/nftOld.ts +++ b/src/lib/services/nft/nftOld.ts @@ -43,19 +43,17 @@ const zNft = z export const getNftsOld = async ( indexer: string, - collectionAddress: HexAddr32, pageSize: number, offset: number, - search: string + expression: object ) => axios .post(indexer, { query: getNftsQueryOld, variables: { - collectionAddress, limit: pageSize, offset, - search, + expression, }, }) .then(({ data: res }) => parseWithError(zNft.array(), res.data.nfts)); diff --git a/src/lib/services/nft/nftService.ts b/src/lib/services/nft/nftService.ts index 1944fc8ff..e7d6b376a 100644 --- a/src/lib/services/nft/nftService.ts +++ b/src/lib/services/nft/nftService.ts @@ -76,13 +76,7 @@ export const useNfts = ( async () => chainId === INITIATION_CHAIN_ID ? getNfts(chainConfig.indexer, pageSize, offset, expression) - : getNftsOld( - chainConfig.indexer, - collectionAddress, - pageSize, - offset, - search - ), + : getNftsOld(chainConfig.indexer, pageSize, offset, expression), { retry: 1, refetchOnWindowFocus: false,