From f47f0d45332c695f2a348dbaef838f81a6046621 Mon Sep 17 00:00:00 2001 From: evilpeach Date: Sat, 6 Jul 2024 10:09:39 +0700 Subject: [PATCH 1/2] feat: support both new, old db schema --- CHANGELOG.md | 1 + src/lib/query/collection.ts | 12 +- src/lib/query/collectionOld.ts | 32 +- src/lib/query/nft.ts | 4 +- src/lib/query/nftOld.ts | 24 +- src/lib/services/expression/nftExpression.ts | 106 +++--- .../services/expression/nftExpressionOld.ts | 111 +++--- src/lib/services/nft/collection.ts | 347 ++++++++++++++---- src/lib/services/nft/collectionService.ts | 174 ++------- src/lib/services/nft/nft.ts | 275 ++++++++++---- src/lib/services/nft/nftService.ts | 127 +------ 11 files changed, 661 insertions(+), 552 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fea0a467..909237246 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Features +- [#1008](https://github.com/alleslabs/celatone-frontend/pull/1008) Support both new, old DB schema for NFTs - [#994](https://github.com/alleslabs/celatone-frontend/pull/994) Add Sequencer, Mesa tier and TierSwitcher component ### Improvements diff --git a/src/lib/query/collection.ts b/src/lib/query/collection.ts index 85525d276..e027550be 100644 --- a/src/lib/query/collection.ts +++ b/src/lib/query/collection.ts @@ -3,11 +3,11 @@ import { gql } from "graphql-request"; export const getCollectionsQuery = gql` query getCollectionsQuery( $offset: Int! - $pageSize: Int! + $limit: Int! $expression: collections_bool_exp! ) { collections( - limit: $pageSize + limit: $limit offset: $offset where: $expression order_by: { name: asc } @@ -107,10 +107,10 @@ export const getCollectionActivitiesQuery = gql` query getCollectionActivitiesQuery( $expression: collection_transactions_bool_exp $offset: Int! - $pageSize: Int! + $limit: Int! ) { collection_transactions( - limit: $pageSize + limit: $limit offset: $offset where: $expression order_by: [ @@ -144,10 +144,10 @@ export const getCollectionMutateEventsQuery = gql` query getCollectionMutateEventsQuery( $collectionAddress: String! $offset: Int! - $pageSize: Int! + $limit: Int! ) { collection_mutation_events( - limit: $pageSize + limit: $limit offset: $offset where: { collection_id: { _eq: $collectionAddress } } order_by: { block_height: desc } diff --git a/src/lib/query/collectionOld.ts b/src/lib/query/collectionOld.ts index d2c7dccbf..f19aa18cf 100644 --- a/src/lib/query/collectionOld.ts +++ b/src/lib/query/collectionOld.ts @@ -1,13 +1,13 @@ import { gql } from "graphql-request"; export const getCollectionsQueryOld = gql` - query getCollectionsQuery( + query getCollectionsQueryOld( $offset: Int! - $pageSize: Int! + $limit: Int! $expression: collections_bool_exp! ) { collections( - limit: $pageSize + limit: $limit offset: $offset where: $expression order_by: { name: asc } @@ -31,7 +31,7 @@ export const getCollectionsQueryOld = gql` `; export const getCollectionByCollectionAddressQueryOld = gql` - query getCollectionByCollectionAddressQuery($vmAddress: String!) { + query getCollectionByCollectionAddressQueryOld($vmAddress: String!) { collections(where: { vm_address: { vm_address: { _eq: $vmAddress } } }) { name uri @@ -50,7 +50,7 @@ export const getCollectionByCollectionAddressQueryOld = gql` `; export const getCollectionTotalBurnedCountQueryOld = gql` - query getCollectionTotalBurnedCountQuery($vmAddress: String!) { + query getCollectionTotalBurnedCountQueryOld($vmAddress: String!) { nfts_aggregate( where: { collectionByCollection: { @@ -67,7 +67,7 @@ export const getCollectionTotalBurnedCountQueryOld = gql` `; export const getCollectionCreatorQueryOld = gql` - query getCollectionCreatorQuery($vmAddress: String!) { + query getCollectionCreatorQueryOld($vmAddress: String!) { collections(where: { vm_address: { vm_address: { _eq: $vmAddress } } }) { vmAddressByCreator { vm_address @@ -89,7 +89,7 @@ export const getCollectionCreatorQueryOld = gql` `; export const getCollectionActivitiesCountQueryOld = gql` - query getCollectionActivitiesCountQuery($vmAddress: String!) { + query getCollectionActivitiesCountQueryOld($vmAddress: String!) { collection_transactions_aggregate( where: { collection: { vm_address: { vm_address: { _eq: $vmAddress } } } } ) { @@ -101,7 +101,7 @@ export const getCollectionActivitiesCountQueryOld = gql` `; export const getCollectionMutateEventsCountQueryOld = gql` - query getCollectionMutateEventsCountQuery($vmAddress: String!) { + query getCollectionMutateEventsCountQueryOld($vmAddress: String!) { collection_mutation_events_aggregate( where: { collection: { vm_address: { vm_address: { _eq: $vmAddress } } } } ) { @@ -113,7 +113,7 @@ export const getCollectionMutateEventsCountQueryOld = gql` `; export const getCollectionUniqueHoldersCountQueryOld = gql` - query getCollectionUniqueHoldersCountQuery($vmAddress: String!) { + query getCollectionUniqueHoldersCountQueryOld($vmAddress: String!) { nfts_aggregate( where: { collectionByCollection: { @@ -130,13 +130,13 @@ export const getCollectionUniqueHoldersCountQueryOld = gql` `; export const getCollectionActivitiesQueryOld = gql` - query getCollectionActivitiesQuery( + query getCollectionActivitiesQueryOld( $expression: collection_transactions_bool_exp $offset: Int! - $pageSize: Int! + $limit: Int! ) { collection_transactions( - limit: $pageSize + limit: $limit offset: $offset where: $expression order_by: [ @@ -169,13 +169,13 @@ export const getCollectionActivitiesQueryOld = gql` `; export const getCollectionMutateEventsQueryOld = gql` - query getCollectionMutateEventsQuery( + query getCollectionMutateEventsQueryOld( $collectionAddress: String! $offset: Int! - $pageSize: Int! + $limit: Int! ) { collection_mutation_events( - limit: $pageSize + limit: $limit offset: $offset where: { collection: { vm_address: { vm_address: { _eq: $collectionAddress } } } @@ -194,7 +194,7 @@ export const getCollectionMutateEventsQueryOld = gql` `; export const getCollectionsByAccountQueryOld = gql` - query getCollectionsByAccountQuery($accountAddress: String!) { + query getCollectionsByAccountQueryOld($accountAddress: String!) { collections( where: { nfts: { vmAddressByOwner: { vm_address: { _eq: $accountAddress } } } diff --git a/src/lib/query/nft.ts b/src/lib/query/nft.ts index 9d607537b..7f8fa977c 100644 --- a/src/lib/query/nft.ts +++ b/src/lib/query/nft.ts @@ -141,12 +141,12 @@ export const getNftMutateEventsCountQuery = gql` export const getNftsByAccountQuery = gql` query getNftsByAccountQuery( - $pageSize: Int! + $limit: Int! $offset: Int! $expression: nfts_bool_exp! ) { nfts( - limit: $pageSize + limit: $limit offset: $offset order_by: { token_id: asc } where: $expression diff --git a/src/lib/query/nftOld.ts b/src/lib/query/nftOld.ts index bc54d7b1d..08cef4b08 100644 --- a/src/lib/query/nftOld.ts +++ b/src/lib/query/nftOld.ts @@ -1,7 +1,7 @@ import { gql } from "graphql-request"; export const getNftQueryOld = gql` - query getNftQuery($collectionAddress: String!, $nftAddress: String!) { + query getNftQueryOld($collectionAddress: String!, $nftAddress: String!) { nfts( where: { collectionByCollection: { @@ -28,7 +28,11 @@ export const getNftQueryOld = gql` `; export const getNftsQueryOld = gql` - query getNftsQuery($limit: Int!, $offset: Int!, $expression: nfts_bool_exp!) { + query getNftsQueryOld( + $limit: Int! + $offset: Int! + $expression: nfts_bool_exp! + ) { nfts( limit: $limit offset: $offset @@ -56,7 +60,7 @@ export const getNftsQueryOld = gql` `; export const getNftTransactionsCountQueryOld = gql` - query getNftTransactionsCountQuery($nftAddress: String!) { + query getNftTransactionsCountQueryOld($nftAddress: String!) { nft_transactions_aggregate( where: { nft: { vm_address: { vm_address: { _eq: $nftAddress } } } } ) { @@ -68,7 +72,7 @@ export const getNftTransactionsCountQueryOld = gql` `; export const getNftTransactionsQueryOld = gql` - query getNftTransactionsQuery( + query getNftTransactionsQueryOld( $limit: Int! $offset: Int! $nftAddress: String! @@ -101,7 +105,7 @@ export const getNftTransactionsQueryOld = gql` `; export const getNftMutateEventsQueryOld = gql` - query getNftMutateEventsQuery( + query getNftMutateEventsQueryOld( $limit: Int! $offset: Int! $nftAddress: String! @@ -124,7 +128,7 @@ export const getNftMutateEventsQueryOld = gql` `; export const getNftMutateEventsCountQueryOld = gql` - query getNftMutateEventsCountQuery($nftAddress: String!) { + query getNftMutateEventsCountQueryOld($nftAddress: String!) { nft_mutation_events_aggregate( where: { nft: { vm_address: { vm_address: { _eq: $nftAddress } } } } ) { @@ -136,13 +140,13 @@ export const getNftMutateEventsCountQueryOld = gql` `; export const getNftsByAccountQueryOld = gql` - query getNftsByAccountQuery( - $pageSize: Int! + query getNftsByAccountQueryOld( + $limit: Int! $offset: Int! $expression: nfts_bool_exp! ) { nfts( - limit: $pageSize + limit: $limit offset: $offset order_by: { token_id: asc } where: $expression @@ -172,7 +176,7 @@ export const getNftsByAccountQueryOld = gql` `; export const getNftsCountByAccountQueryOld = gql` - query getNftsCountByAccountQuery($accountAddress: String!) { + query getNftsCountByAccountQueryOld($accountAddress: String!) { nfts_aggregate( where: { vmAddressByOwner: { vm_address: { _eq: $accountAddress } } diff --git a/src/lib/services/expression/nftExpression.ts b/src/lib/services/expression/nftExpression.ts index ad7d770c2..b0b513c7e 100644 --- a/src/lib/services/expression/nftExpression.ts +++ b/src/lib/services/expression/nftExpression.ts @@ -1,21 +1,15 @@ -import { useMemo } from "react"; - import type { HexAddr, HexAddr32 } from "lib/types"; import { isHexModuleAddress, isTxHash } from "lib/utils"; -export const useCollectionsExpression = (search = "") => - useMemo(() => { - if (search.trim().length === 0) return {}; +export const getCollectionsExpression = (search = "") => { + if (search.trim().length === 0) return {}; - return { - _or: [ - { name: { _iregex: search } }, - { id: { _eq: search.toLowerCase() } }, - ], - }; - }, [search]); + return { + _or: [{ name: { _iregex: search } }, { id: { _eq: search.toLowerCase() } }], + }; +}; -export const useCollectionActivitiesExpression = ( +export const getCollectionActivitiesExpression = ( collectionAddress: HexAddr32, search = "" ) => { @@ -35,56 +29,54 @@ export const useCollectionActivitiesExpression = ( const searchOption = isHash ? txHashSearch : tokenIdSearch; - return useMemo( - () => ({ - collection_id: { _eq: collectionAddress }, - ...(search ? { _and: searchOption } : {}), - }), - [collectionAddress, search, searchOption] - ); + return { + collection_id: { _eq: collectionAddress }, + ...(search ? { _and: searchOption } : {}), + }; }; -export const useNftsExpression = (collectionAddress: HexAddr32, search = "") => - useMemo(() => { - const orExpression = { - _or: [ - { token_id: { _iregex: search } }, - ...(isHexModuleAddress(search) - ? [{ id: { _eq: search.toLowerCase() } }] - : []), - ], - }; +export const getNftsExpression = ( + collectionAddress: HexAddr32, + search = "" +) => { + const orExpression = { + _or: [ + { token_id: { _iregex: search } }, + ...(isHexModuleAddress(search) + ? [{ id: { _eq: search.toLowerCase() } }] + : []), + ], + }; - return { - collection: { _eq: collectionAddress }, - is_burned: { _eq: false }, - ...(search.trim().length > 0 ? orExpression : {}), - }; - }, [collectionAddress, search]); + return { + collection: { _eq: collectionAddress }, + is_burned: { _eq: false }, + ...(search.trim().length > 0 ? orExpression : {}), + }; +}; -export const useNftsByAccountExpression = ( +export const getNftsByAccountExpression = ( accountAddress: HexAddr, collectionAddress?: HexAddr32, search = "" -) => - useMemo(() => { - const orExpression = { - _or: [ - { token_id: { _iregex: search } }, - ...(isHexModuleAddress(search) - ? [{ id: { _eq: search.toLowerCase() } }] - : []), - ], - }; +) => { + const orExpression = { + _or: [ + { token_id: { _iregex: search } }, + ...(isHexModuleAddress(search) + ? [{ id: { _eq: search.toLowerCase() } }] + : []), + ], + }; - const collectionExpression = { - collection: { _eq: collectionAddress?.toLowerCase() }, - }; + const collectionExpression = { + collection: { _eq: collectionAddress?.toLowerCase() }, + }; - return { - owner: { _eq: accountAddress }, - is_burned: { _eq: false }, - ...(collectionAddress ? collectionExpression : {}), - ...(search.trim().length > 0 ? orExpression : {}), - }; - }, [accountAddress, collectionAddress, search]); + return { + owner: { _eq: accountAddress }, + is_burned: { _eq: false }, + ...(collectionAddress ? collectionExpression : {}), + ...(search.trim().length > 0 ? orExpression : {}), + }; +}; diff --git a/src/lib/services/expression/nftExpressionOld.ts b/src/lib/services/expression/nftExpressionOld.ts index 237961220..f51e6a757 100644 --- a/src/lib/services/expression/nftExpressionOld.ts +++ b/src/lib/services/expression/nftExpressionOld.ts @@ -1,21 +1,18 @@ -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 {}; +export const getCollectionsExpressionOld = (search = "") => { + if (search.trim().length === 0) return {}; - return { - _or: [ - { name: { _iregex: search } }, - { vm_address: { vm_address: { _eq: search.toLowerCase() } } }, - ], - }; - }, [search]); + return { + _or: [ + { name: { _iregex: search } }, + { vm_address: { vm_address: { _eq: search.toLowerCase() } } }, + ], + }; +}; -export const useCollectionActivitiesExpressionOld = ( +export const getCollectionActivitiesExpressionOld = ( collectionAddress: HexAddr32, search = "" ) => { @@ -37,62 +34,60 @@ export const useCollectionActivitiesExpressionOld = ( const searchOption = isHash ? txHashSearch : tokenIdSearch; - return useMemo( - () => ({ - collection: { vm_address: { vm_address: { _eq: collectionAddress } } }, - ...(search ? { _and: searchOption } : {}), - }), - [collectionAddress, search, searchOption] - ); + return { + collection: { vm_address: { vm_address: { _eq: collectionAddress } } }, + ...(search ? { _and: searchOption } : {}), + }; }; -export const useNftsExpressionOld = ( +export const getNftsExpressionOld = ( collectionAddress: HexAddr32, search = "" -) => - useMemo(() => { - const orExpression = { - _or: [ - { token_id: { _iregex: search } }, - ...(isHexModuleAddress(search) - ? [{ vm_address: { vm_address: { _eq: search.toLowerCase() } } }] - : []), - ], - }; +) => { + const orExpression = { + _or: [ + { token_id: { _iregex: search } }, + ...(isHexModuleAddress(search) + ? [{ vm_address: { vm_address: { _eq: search.toLowerCase() } } }] + : []), + ], + }; - return { - collectionByCollection: { - vm_address: { vm_address: { _eq: collectionAddress } }, - }, - is_burned: { _eq: false }, - ...(search.trim().length > 0 ? orExpression : {}), - }; - }, [collectionAddress, search]); + return { + collectionByCollection: { + vm_address: { vm_address: { _eq: collectionAddress } }, + }, + is_burned: { _eq: false }, + ...(search.trim().length > 0 ? orExpression : {}), + }; +}; -export const useNftsByAccountExpressionOld = ( +export const getNftsByAccountExpressionOld = ( accountAddress: HexAddr, collectionAddress?: HexAddr32, search = "" -) => - useMemo(() => { - const orExpression = { - _or: [{ token_id: { _iregex: search } }], - }; +) => { + const orExpression = { + _or: [ + { token_id: { _iregex: search } }, + ...(isHexModuleAddress(search) + ? [{ vm_address: { vm_address: { _eq: search.toLowerCase() } } }] + : []), + ], + }; - const collectionExpression = collectionAddress - ? [ - { + return { + vmAddressByOwner: { vm_address: { _eq: accountAddress } }, + is_burned: { _eq: false }, + ...(collectionAddress + ? { + collectionByCollection: { vm_address: { vm_address: { _eq: collectionAddress.toLowerCase() }, }, }, - ] - : []; - - return { - vmAddressByOwner: { vm_address: { _eq: accountAddress } }, - is_burned: { _eq: false }, - ...(collectionAddress ? collectionExpression : {}), - ...(search.trim().length > 0 ? orExpression : {}), - }; - }, [accountAddress, collectionAddress, search]); + } + : {}), + ...(search.trim().length > 0 ? orExpression : {}), + }; +}; diff --git a/src/lib/services/nft/collection.ts b/src/lib/services/nft/collection.ts index cb519fc25..630810f8a 100644 --- a/src/lib/services/nft/collection.ts +++ b/src/lib/services/nft/collection.ts @@ -1,16 +1,31 @@ import axios from "axios"; import { z } from "zod"; +import { + getCollectionActivitiesExpression, + getCollectionActivitiesExpressionOld, + getCollectionsExpression, + getCollectionsExpressionOld, +} from "../expression"; import { getCollectionActivitiesCountQuery, + getCollectionActivitiesCountQueryOld, getCollectionActivitiesQuery, + getCollectionActivitiesQueryOld, getCollectionByCollectionAddressQuery, + getCollectionByCollectionAddressQueryOld, getCollectionCreatorQuery, + getCollectionCreatorQueryOld, getCollectionMutateEventsCountQuery, + getCollectionMutateEventsCountQueryOld, getCollectionMutateEventsQuery, + getCollectionMutateEventsQueryOld, getCollectionsByAccountQuery, + getCollectionsByAccountQueryOld, getCollectionsQuery, + getCollectionsQueryOld, getCollectionUniqueHoldersCountQuery, + getCollectionUniqueHoldersCountQueryOld, } from "lib/query"; import type { HexAddr, HexAddr32, MutateEvent } from "lib/types"; import { zHexAddr, zHexAddr32, zRemark, zUtcDate } from "lib/types"; @@ -33,9 +48,25 @@ const zCollection = z })); export type Collection = z.infer; +const zCollectionOld = 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: z.union([zCollection, zCollectionOld]).array(), collections_aggregate: z.object({ aggregate: z.object({ count: z.number(), @@ -50,16 +81,27 @@ export type CollectionsResponse = z.infer; export const getCollections = async ( indexer: string, + limit: number, offset: number, - pageSize: number, - expression: object -) => - axios - .post(indexer, { + search?: string +) => { + const expressionNew = getCollectionsExpression(search); + const expressionOld = getCollectionsExpressionOld(search); + + try { + const res = await axios.post(indexer, { query: getCollectionsQuery, - variables: { offset, pageSize, expression }, - }) - .then(({ data: res }) => parseWithError(zCollectionsResponse, res.data)); + variables: { offset, limit, expression: expressionNew }, + }); + return parseWithError(zCollectionsResponse, res.data.data); + } catch { + const res = await axios.post(indexer, { + query: getCollectionsQueryOld, + variables: { offset, limit, expression: expressionOld }, + }); + return parseWithError(zCollectionsResponse, res.data.data); + } +}; const zCollectionByCollectionAddressResponse = z.object({ data: z @@ -96,17 +138,25 @@ export type CollectionByCollectionAddressResponse = z.infer< export const getCollectionByCollectionAddress = async ( indexer: string, collectionAddress: HexAddr32 -) => - axios - .post(indexer, { +) => { + try { + const res = await axios.post(indexer, { query: getCollectionByCollectionAddressQuery, variables: { vmAddress: collectionAddress }, - }) - .then(({ data: res }) => - parseWithError(zCollectionByCollectionAddressResponse, { - data: res.data.collections[0], - }) - ); + }); + return parseWithError(zCollectionByCollectionAddressResponse, { + data: res.data.data.collections[0], + }); + } catch { + const res = await axios.post(indexer, { + query: getCollectionByCollectionAddressQueryOld, + variables: { vmAddress: collectionAddress }, + }); + return parseWithError(zCollectionByCollectionAddressResponse, { + data: res.data.data.collections[0], + }); + } +}; const zCollectionCreatorResponse = z .object({ @@ -128,18 +178,53 @@ export type CollectionCreatorResponse = z.infer< typeof zCollectionCreatorResponse >; +const zCollectionCreatorResponseOld = 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 getCollectionCreator = async ( indexer: string, collectionAddress: HexAddr32 -) => - axios - .post(indexer, { +) => { + try { + const res = await axios.post(indexer, { query: getCollectionCreatorQuery, variables: { vmAddress: collectionAddress }, - }) - .then(({ data: res }) => - parseWithError(zCollectionCreatorResponse, res.data) - ); + }); + return parseWithError(zCollectionCreatorResponse, res.data.data); + } catch { + const res = await axios.post(indexer, { + query: getCollectionCreatorQueryOld, + variables: { vmAddress: collectionAddress }, + }); + return parseWithError(zCollectionCreatorResponseOld, res.data.data); + } +}; const zActivity = z .object({ @@ -171,26 +256,81 @@ const zActivity = z })); export type Activity = z.infer; +const zActivityOld = 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 getCollectionActivities = async ( indexer: string, - pageSize: number, + collectionAddress: HexAddr32, + limit: number, offset: number, - expression?: object + search?: string ) => { - return axios - .post(indexer, { + const expressionNew = getCollectionActivitiesExpression( + collectionAddress, + search + ); + const expressionOld = getCollectionActivitiesExpressionOld( + collectionAddress, + search + ); + + try { + const res = await axios.post(indexer, { query: getCollectionActivitiesQuery, variables: { - pageSize, + limit, offset, - expression, + expression: expressionNew, }, - }) - .then(({ data: res }) => - parseWithError(zActivity.array(), res.data.collection_transactions) + }); + return parseWithError( + zActivity.array(), + res.data.data.collection_transactions ); + } catch { + const res = await axios.post(indexer, { + query: getCollectionActivitiesQueryOld, + variables: { + limit, + offset, + expression: expressionOld, + }, + }); + return parseWithError( + zActivityOld.array(), + res.data.data.collection_transactions + ); + } }; +// TODO: here const zActivitiesCountResponse = z .object({ collection_transactions_aggregate: z.object({ @@ -204,13 +344,21 @@ const zActivitiesCountResponse = z export const getCollectionActivitiesCount = async ( indexer: string, collectionAddress: HexAddr32 -) => - axios - .post(indexer, { +) => { + try { + const res = await axios.post(indexer, { query: getCollectionActivitiesCountQuery, variables: { vmAddress: collectionAddress }, - }) - .then(({ data }) => parseWithError(zActivitiesCountResponse, data.data)); + }); + return parseWithError(zActivitiesCountResponse, res.data.data); + } catch { + const res = await axios.post(indexer, { + query: getCollectionActivitiesCountQueryOld, + variables: { vmAddress: collectionAddress }, + }); + return parseWithError(zActivitiesCountResponse, res.data.data); + } +}; const zCollectionMutateEventsResponse = z .object({ @@ -231,24 +379,29 @@ const zCollectionMutateEventsResponse = z export const getCollectionMutateEvents = async ( indexer: string, collectionAddress: HexAddr32, - pageSize: number, + limit: number, offset: number -) => - axios - .post(indexer, { +) => { + try { + const res = await axios.post(indexer, { query: getCollectionMutateEventsQuery, - variables: { - collectionAddress, - pageSize, - offset, - }, - }) - .then(({ data: res }) => - parseWithError( - zCollectionMutateEventsResponse.array(), - res.data.collection_mutation_events - ) + variables: { collectionAddress, limit, offset }, + }); + return parseWithError( + zCollectionMutateEventsResponse.array(), + res.data.data.collection_mutation_events + ); + } catch { + const res = await axios.post(indexer, { + query: getCollectionMutateEventsQueryOld, + variables: { collectionAddress, limit, offset }, + }); + return parseWithError( + zCollectionMutateEventsResponse.array(), + res.data.data.collection_mutation_events ); + } +}; const zMutationEventsCountResponseItem = z .object({ @@ -263,15 +416,21 @@ const zMutationEventsCountResponseItem = z export const getCollectionMutateEventsCount = async ( indexer: string, collectionAddress: HexAddr32 -) => - axios - .post(indexer, { +) => { + try { + const res = await axios.post(indexer, { query: getCollectionMutateEventsCountQuery, variables: { vmAddress: collectionAddress }, - }) - .then(({ data }) => - parseWithError(zMutationEventsCountResponseItem, data.data) - ); + }); + return parseWithError(zMutationEventsCountResponseItem, res.data.data); + } catch { + const res = await axios.post(indexer, { + query: getCollectionMutateEventsCountQueryOld, + variables: { vmAddress: collectionAddress }, + }); + return parseWithError(zMutationEventsCountResponseItem, res.data.data); + } +}; const zUniqueHoldersCountResponseItem = z .object({ @@ -284,15 +443,21 @@ const zUniqueHoldersCountResponseItem = z export const getCollectionUniqueHoldersCount = async ( indexer: string, collectionAddress: HexAddr32 -) => - axios - .post(indexer, { +) => { + try { + const res = await axios.post(indexer, { query: getCollectionUniqueHoldersCountQuery, variables: { vmAddress: collectionAddress }, - }) - .then(({ data }) => - parseWithError(zUniqueHoldersCountResponseItem, data.data) - ); + }); + return parseWithError(zUniqueHoldersCountResponseItem, res.data.data); + } catch { + const res = await axios.post(indexer, { + query: getCollectionUniqueHoldersCountQueryOld, + variables: { vmAddress: collectionAddress }, + }); + return parseWithError(zUniqueHoldersCountResponseItem, res.data.data); + } +}; const zCollectionsByAccountResponse = z .object({ @@ -306,24 +471,46 @@ const zCollectionsByAccountResponse = z collectionAddress: val.id, uri: val.uri, hold: val.nfts_aggregate.aggregate.count, - })) - .array(); + })); export type CollectionsByAccountResponse = z.infer< typeof zCollectionsByAccountResponse >; +const zCollectionsByAccountResponseOld = 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, + })); + export const getCollectionsByAccount = async ( indexer: string, accountAddress: HexAddr -) => - axios - .post(indexer, { +) => { + try { + const res = await axios.post(indexer, { query: getCollectionsByAccountQuery, variables: { accountAddress }, - }) - .then(({ data: res }) => - parseWithError( - zCollectionsByAccountResponse, - res.data.collections - ).filter((collection) => collection.hold > 0) - ); + }); + return parseWithError( + zCollectionsByAccountResponse.array(), + res.data.data.collections + ).filter((collection) => collection.hold > 0); + } catch { + const res = await axios.post(indexer, { + query: getCollectionsByAccountQueryOld, + variables: { accountAddress }, + }); + return parseWithError( + zCollectionsByAccountResponseOld.array(), + res.data.data.collections + ).filter((collection) => collection.hold > 0); + } +}; diff --git a/src/lib/services/nft/collectionService.ts b/src/lib/services/nft/collectionService.ts index 5dd3f8bf9..d3596c959 100644 --- a/src/lib/services/nft/collectionService.ts +++ b/src/lib/services/nft/collectionService.ts @@ -1,17 +1,7 @@ import type { UseQueryOptions } from "@tanstack/react-query"; import { useQuery } from "@tanstack/react-query"; -import { - useCollectionActivitiesExpression, - useCollectionActivitiesExpressionOld, - useCollectionsExpression, - useCollectionsExpressionOld, -} from "../expression"; -import { - CELATONE_QUERY_KEYS, - useCelatoneApp, - useCurrentChain, -} from "lib/app-provider"; +import { CELATONE_QUERY_KEYS, useCelatoneApp } from "lib/app-provider"; import type { HexAddr, HexAddr32, MutateEvent } from "lib/types"; import type { @@ -32,47 +22,23 @@ 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, + limit: number, offset: number, search?: string, options?: Pick, "onSuccess"> ) => { const { chainConfig } = useCelatoneApp(); - const { - chain: { chain_id: chainId }, - } = useCurrentChain(); - const expressionNew = useCollectionsExpression(search); - const expressionOld = useCollectionsExpressionOld(search); - const expression = - chainId === INITIATION_CHAIN_ID ? expressionNew : expressionOld; - return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTIONS, chainConfig.indexer, - pageSize, + limit, offset, - expression, + search, ], - async () => - chainId === INITIATION_CHAIN_ID - ? getCollections(chainConfig.indexer, offset, pageSize, expression) - : getCollectionsOld(chainConfig.indexer, offset, pageSize, expression), + async () => getCollections(chainConfig.indexer, limit, offset, search), { retry: 1, refetchOnWindowFocus: false, @@ -85,9 +51,6 @@ export const useCollectionByCollectionAddress = ( collectionAddress: HexAddr32 ) => { const { chainConfig } = useCelatoneApp(); - const { - chain: { chain_id: chainId }, - } = useCurrentChain(); return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTION_BY_COLLECTION_ADDRESS, @@ -95,15 +58,7 @@ export const useCollectionByCollectionAddress = ( collectionAddress, ], async () => - chainId === INITIATION_CHAIN_ID - ? getCollectionByCollectionAddress( - chainConfig.indexer, - collectionAddress - ) - : getCollectionByCollectionAddressOld( - chainConfig.indexer, - collectionAddress - ), + getCollectionByCollectionAddress(chainConfig.indexer, collectionAddress), { retry: 1, refetchOnWindowFocus: false, @@ -113,19 +68,13 @@ 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 () => - chainId === INITIATION_CHAIN_ID - ? getCollectionCreator(chainConfig.indexer, collectionAddress) - : getCollectionCreatorOld(chainConfig.indexer, collectionAddress), + async () => getCollectionCreator(chainConfig.indexer, collectionAddress), { retry: 1, refetchOnWindowFocus: false, @@ -135,48 +84,28 @@ export const useCollectionCreator = (collectionAddress: HexAddr32) => { export const useCollectionActivities = ( collectionAddress: HexAddr32, - pageSize: number, + limit: number, offset: number, search?: string ) => { const { chainConfig } = useCelatoneApp(); - const { - chain: { chain_id: chainId }, - } = useCurrentChain(); - const expressionNew = useCollectionActivitiesExpression( - collectionAddress, - search - ); - const expressionOld = useCollectionActivitiesExpressionOld( - collectionAddress, - search - ); - const expression = - chainId === INITIATION_CHAIN_ID ? expressionNew : expressionOld; - return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTION_ACTIVITIES, chainConfig.indexer, collectionAddress, + limit, offset, - pageSize, - expression, + search, ], async () => - chainId === INITIATION_CHAIN_ID - ? getCollectionActivities( - chainConfig.indexer, - pageSize, - offset, - expression - ) - : getCollectionActivitiesOld( - chainConfig.indexer, - pageSize, - offset, - expression - ), + getCollectionActivities( + chainConfig.indexer, + collectionAddress, + limit, + offset, + search + ), { retry: 1, refetchOnWindowFocus: false, @@ -186,9 +115,6 @@ 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, @@ -196,12 +122,7 @@ export const useCollectionActivitiesCount = (collectionAddress: HexAddr32) => { collectionAddress, ], async () => - chainId === INITIATION_CHAIN_ID - ? getCollectionActivitiesCount(chainConfig.indexer, collectionAddress) - : getCollectionActivitiesCountOld( - chainConfig.indexer, - collectionAddress - ), + getCollectionActivitiesCount(chainConfig.indexer, collectionAddress), { retry: 1, refetchOnWindowFocus: false, @@ -211,35 +132,25 @@ export const useCollectionActivitiesCount = (collectionAddress: HexAddr32) => { export const useCollectionMutateEvents = ( collectionAddress: HexAddr32, - pageSize: number, + limit: number, offset: number ) => { const { chainConfig } = useCelatoneApp(); - const { - chain: { chain_id: chainId }, - } = useCurrentChain(); return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTION_MUTATE_EVENTS, chainConfig.indexer, collectionAddress, + limit, offset, - pageSize, ], async () => - chainId === INITIATION_CHAIN_ID - ? getCollectionMutateEvents( - chainConfig.indexer, - collectionAddress, - pageSize, - offset - ) - : getCollectionMutateEventsOld( - chainConfig.indexer, - collectionAddress, - pageSize, - offset - ), + getCollectionMutateEvents( + chainConfig.indexer, + collectionAddress, + limit, + offset + ), { retry: 1, refetchOnWindowFocus: false, @@ -251,9 +162,6 @@ export const useCollectionMutateEventsCount = ( collectionAddress: HexAddr32 ) => { const { chainConfig } = useCelatoneApp(); - const { - chain: { chain_id: chainId }, - } = useCurrentChain(); return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTION_MUTATE_EVENTS_COUNT, @@ -261,12 +169,7 @@ export const useCollectionMutateEventsCount = ( collectionAddress, ], async () => - chainId === INITIATION_CHAIN_ID - ? getCollectionMutateEventsCount(chainConfig.indexer, collectionAddress) - : getCollectionMutateEventsCountOld( - chainConfig.indexer, - collectionAddress - ), + getCollectionMutateEventsCount(chainConfig.indexer, collectionAddress), { retry: 1, refetchOnWindowFocus: false, @@ -278,9 +181,6 @@ export const useCollectionUniqueHoldersCount = ( collectionAddress: HexAddr32 ) => { const { chainConfig } = useCelatoneApp(); - const { - chain: { chain_id: chainId }, - } = useCurrentChain(); return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTION_UNIQUE_HOLDERS_COUNT, @@ -288,15 +188,7 @@ export const useCollectionUniqueHoldersCount = ( collectionAddress, ], async () => - chainId === INITIATION_CHAIN_ID - ? getCollectionUniqueHoldersCount( - chainConfig.indexer, - collectionAddress - ) - : getCollectionUniqueHoldersCountOld( - chainConfig.indexer, - collectionAddress - ), + getCollectionUniqueHoldersCount(chainConfig.indexer, collectionAddress), { retry: 1, refetchOnWindowFocus: false, @@ -306,19 +198,13 @@ export const useCollectionUniqueHoldersCount = ( export const useCollectionsByAccount = (accountAddress: HexAddr) => { const { chainConfig } = useCelatoneApp(); - const { - chain: { chain_id: chainId }, - } = useCurrentChain(); - return useQuery( + return useQuery( [ CELATONE_QUERY_KEYS.NFT_COLLECTIONS_BY_ACCOUNT, chainConfig.indexer, accountAddress, ], - async () => - chainId === INITIATION_CHAIN_ID - ? getCollectionsByAccount(chainConfig.indexer, accountAddress) - : getCollectionsByAccountOld(chainConfig.indexer, accountAddress), + async () => getCollectionsByAccount(chainConfig.indexer, accountAddress), { retry: 1, refetchOnWindowFocus: false, diff --git a/src/lib/services/nft/nft.ts b/src/lib/services/nft/nft.ts index 62fca62cd..75fd955f1 100644 --- a/src/lib/services/nft/nft.ts +++ b/src/lib/services/nft/nft.ts @@ -1,16 +1,30 @@ import axios from "axios"; import { z } from "zod"; +import { + getNftsByAccountExpression, + getNftsByAccountExpressionOld, + getNftsExpression, + getNftsExpressionOld, +} from "../expression"; import { getNftMintInfoQuery, getNftMutateEventsCountQuery, + getNftMutateEventsCountQueryOld, getNftMutateEventsQuery, + getNftMutateEventsQueryOld, getNftQuery, + getNftQueryOld, getNftsByAccountQuery, + getNftsByAccountQueryOld, getNftsCountByAccountQuery, + getNftsCountByAccountQueryOld, getNftsQuery, + getNftsQueryOld, getNftTransactionsCountQuery, + getNftTransactionsCountQueryOld, getNftTransactionsQuery, + getNftTransactionsQueryOld, } from "lib/query"; import { zBechAddr, zHexAddr, zHexAddr32, zRemark, zUtcDate } from "lib/types"; import type { HexAddr, HexAddr32, MutateEvent } from "lib/types"; @@ -41,36 +55,65 @@ const zNft = z })); export type Nft = z.infer; +const zNftOld = 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 getNfts = async ( indexer: string, - pageSize: number, - offset: number, - expression: object -) => - axios - .post(indexer, { + collectionAddress: HexAddr32, + search: string, + limit: number, + offset: number +) => { + const expressionNew = getNftsExpression(collectionAddress, search); + const expressionOld = getNftsExpressionOld(collectionAddress, search); + + try { + const res = await axios.post(indexer, { query: getNftsQuery, variables: { - limit: pageSize, + limit, offset, - expression, + expression: expressionNew, }, - }) - .then(({ data: res }) => parseWithError(zNft.array(), res.data.nfts)); - -export const getNftsCountByAccount = async ( - indexer: string, - accountAddress: HexAddr -) => - axios - .post(indexer, { - query: getNftsCountByAccountQuery, - variables: { accountAddress }, - }) - .then(({ data: res }) => res.data.nfts_aggregate.aggregate.count); + }); + return parseWithError(zNft.array(), res.data.data.nfts); + } catch { + const res = await axios.post(indexer, { + query: getNftsQueryOld, + variables: { + limit, + offset, + expression: expressionOld, + }, + }); + return parseWithError(zNftOld.array(), res.data.data.nfts); + } +}; const zNftByNftAddressResponse = z.object({ - data: zNft.optional(), + data: z.union([zNft, zNftOld]).optional(), }); export type NftByNftAddressResponse = z.infer; @@ -78,15 +121,44 @@ export const getNftByNftAddress = async ( indexer: string, collectionAddress: HexAddr32, nftAddress: HexAddr32 -) => - axios - .post(indexer, { +) => { + try { + const res = await axios.post(indexer, { query: getNftQuery, variables: { collectionAddress, nftAddress }, - }) - .then(({ data }) => - parseWithError(zNftByNftAddressResponse, { data: data.data.nfts[0] }) - ); + }); + return parseWithError(zNftByNftAddressResponse, { + data: res.data.data.nfts[0], + }); + } catch { + const res = await axios.post(indexer, { + query: getNftQueryOld, + variables: { collectionAddress, nftAddress }, + }); + return parseWithError(zNftByNftAddressResponse, { + data: res.data.data.nfts[0], + }); + } +}; + +export const getNftsCountByAccount = async ( + indexer: string, + accountAddress: HexAddr +) => { + try { + const res = await axios.post(indexer, { + query: getNftsCountByAccountQuery, + variables: { accountAddress }, + }); + return z.number().parse(res.data.data.nfts_aggregate.aggregate.count); + } catch { + const res = await axios.post(indexer, { + query: getNftsCountByAccountQueryOld, + variables: { accountAddress }, + }); + return z.number().parse(res.data.data.nfts_aggregate.aggregate.count); + } +}; const zNftMintInfoResponse = z .object({ @@ -164,33 +236,52 @@ export type NftTransactions = z.infer; export const getNftTransactions = async ( indexer: string, nftAddress: HexAddr32, - offset: number, - limit: number -) => - axios - .post(indexer, { + limit: number, + offset: number +) => { + try { + const res = await axios.post(indexer, { query: getNftTransactionsQuery, variables: { limit, offset, nftAddress }, - }) - .then(({ data: res }) => - parseWithError( - zNftTransactionsResponse.array(), - res.data.nft_transactions - ) + }); + return parseWithError( + zNftTransactionsResponse.array(), + res.data.data.nft_transactions ); + } catch (err) { + const res = await axios.post(indexer, { + query: getNftTransactionsQueryOld, + variables: { limit, offset, nftAddress }, + }); + return parseWithError( + zNftTransactionsResponse.array(), + res.data.data.nft_transactions + ); + } +}; export const getNftTransactionsCount = async ( indexer: string, nftAddress: HexAddr32 -) => - axios - .post(indexer, { +) => { + try { + const res = await axios.post(indexer, { query: getNftTransactionsCountQuery, variables: { nftAddress }, - }) - .then( - ({ data: res }) => res.data.nft_transactions_aggregate.aggregate.count - ); + }); + return z + .number() + .parse(res.data.data.nft_transactions_aggregate.aggregate.count); + } catch { + const res = await axios.post(indexer, { + query: getNftTransactionsCountQueryOld, + variables: { nftAddress }, + }); + return z + .number() + .parse(res.data.data.nft_transactions_aggregate.aggregate.count); + } +}; const zNftMutateEventsResponseItem = z .object({ @@ -213,35 +304,54 @@ export const getNftMutateEvents = async ( nftAddress: HexAddr32, offset: number, limit: number -) => - axios - .post(indexer, { +) => { + try { + const res = await axios.post(indexer, { query: getNftMutateEventsQuery, variables: { limit, offset, nftAddress }, - }) - .then(({ data: res }) => - parseWithError( - zNftMutateEventsResponseItem.array(), - res.data.nft_mutation_events - ) + }); + return parseWithError( + zNftMutateEventsResponseItem.array(), + res.data.data.nft_mutation_events + ); + } catch { + const res = await axios.post(indexer, { + query: getNftMutateEventsQueryOld, + variables: { limit, offset, nftAddress }, + }); + return parseWithError( + zNftMutateEventsResponseItem.array(), + res.data.data.nft_mutation_events ); + } +}; export const getNftMutateEventsCount = async ( indexer: string, nftAddress: HexAddr32 -) => - axios - .post(indexer, { +) => { + try { + const rest = await axios.post(indexer, { query: getNftMutateEventsCountQuery, variables: { nftAddress }, - }) - .then( - ({ data }) => data.data.nft_mutation_events_aggregate.aggregate.count - ); + }); + return z + .number() + .parse(rest.data.data.nft_mutation_events_aggregate.aggregate.count); + } catch { + const res = await axios.post(indexer, { + query: getNftMutateEventsCountQueryOld, + variables: { nftAddress }, + }); + return z + .number() + .parse(res.data.data.nft_mutation_events_aggregate.aggregate.count); + } +}; const zNftsByAccountResponse = z .object({ - nfts: zNft.array(), + nfts: z.union([zNft, zNftOld]).array(), nfts_aggregate: z.object({ aggregate: z.object({ count: z.number(), @@ -256,13 +366,34 @@ export type NftsByAccountResponse = z.infer; export const getNftsByAccount = async ( indexer: string, - pageSize: number, + accountAddress: HexAddr, + limit: number, offset: number, - expression: object -) => - axios - .post(indexer, { + collectionAddress?: HexAddr32, + search?: string +) => { + const expressionNew = getNftsByAccountExpression( + accountAddress, + collectionAddress, + search + ); + const expressionOld = getNftsByAccountExpressionOld( + accountAddress, + collectionAddress, + search + ); + + try { + const res = await axios.post(indexer, { query: getNftsByAccountQuery, - variables: { pageSize, offset, expression }, - }) - .then(({ data: res }) => parseWithError(zNftsByAccountResponse, res.data)); + variables: { limit, offset, expression: expressionNew }, + }); + return parseWithError(zNftsByAccountResponse, res.data.data); + } catch { + const res = await axios.post(indexer, { + query: getNftsByAccountQueryOld, + variables: { limit, offset, expression: expressionOld }, + }); + return parseWithError(zNftsByAccountResponse, res.data.data); + } +}; diff --git a/src/lib/services/nft/nftService.ts b/src/lib/services/nft/nftService.ts index e7d6b376a..fdfe9e0ac 100644 --- a/src/lib/services/nft/nftService.ts +++ b/src/lib/services/nft/nftService.ts @@ -1,16 +1,9 @@ import type { UseQueryOptions } from "@tanstack/react-query"; import { useQuery } from "@tanstack/react-query"; -import { - useNftsByAccountExpression, - useNftsByAccountExpressionOld, - useNftsExpression, - useNftsExpressionOld, -} from "../expression"; import { CELATONE_QUERY_KEYS, useCelatoneApp, - useCurrentChain, useNftConfig, } from "lib/app-provider"; import type { HexAddr, HexAddr32, MutateEvent } from "lib/types"; @@ -35,48 +28,26 @@ 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, - pageSize: number, + limit: number, offset: number, search = "" ) => { const { chainConfig } = useCelatoneApp(); - const { - chain: { chain_id: chainId }, - } = useCurrentChain(); - const expressionNew = useNftsExpression(collectionAddress, search); - const expressionOld = useNftsExpressionOld(collectionAddress, search); - const expression = - chainId === INITIATION_CHAIN_ID ? expressionNew : expressionOld; return useQuery( [ CELATONE_QUERY_KEYS.NFTS, chainConfig.indexer, collectionAddress, + limit, offset, - pageSize, search, - expression, ], async () => - chainId === INITIATION_CHAIN_ID - ? getNfts(chainConfig.indexer, pageSize, offset, expression) - : getNftsOld(chainConfig.indexer, pageSize, offset, expression), + getNfts(chainConfig.indexer, collectionAddress, search, limit, offset), { retry: 1, refetchOnWindowFocus: false, @@ -89,9 +60,6 @@ export const useNftByNftAddress = ( nftAddress: HexAddr32 ) => { const { chainConfig } = useCelatoneApp(); - const { - chain: { chain_id: chainId }, - } = useCurrentChain(); return useQuery( [ @@ -101,13 +69,7 @@ export const useNftByNftAddress = ( nftAddress, ], async () => - chainId === INITIATION_CHAIN_ID - ? getNftByNftAddress(chainConfig.indexer, collectionAddress, nftAddress) - : getNftByNftAddressOld( - chainConfig.indexer, - collectionAddress, - nftAddress - ), + getNftByNftAddress(chainConfig.indexer, collectionAddress, nftAddress), { retry: 1, refetchOnWindowFocus: false, @@ -145,10 +107,6 @@ export const useNftTransactions = ( nftAddress: HexAddr32 ) => { const { chainConfig } = useCelatoneApp(); - const { - chain: { chain_id: chainId }, - } = useCurrentChain(); - return useQuery( [ CELATONE_QUERY_KEYS.NFT_TRANSACTIONS, @@ -158,9 +116,7 @@ export const useNftTransactions = ( offset, ], async () => - chainId === INITIATION_CHAIN_ID - ? getNftTransactions(chainConfig.indexer, nftAddress, offset, limit) - : getNftTransactionsOld(chainConfig.indexer, nftAddress, offset, limit), + getNftTransactions(chainConfig.indexer, nftAddress, limit, offset), { retry: 1, refetchOnWindowFocus: false, @@ -170,20 +126,13 @@ 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 () => - chainId === INITIATION_CHAIN_ID - ? getNftTransactionsCount(chainConfig.indexer, nftAddress) - : getNftTransactionsCountOld(chainConfig.indexer, nftAddress), + async () => getNftTransactionsCount(chainConfig.indexer, nftAddress), { retry: 1, refetchOnWindowFocus: false, @@ -197,10 +146,6 @@ export const useNftMutateEvents = ( nftAddress: HexAddr32 ) => { const { chainConfig } = useCelatoneApp(); - const { - chain: { chain_id: chainId }, - } = useCurrentChain(); - return useQuery( [ CELATONE_QUERY_KEYS.NFT_MUTATE_EVENTS, @@ -210,9 +155,7 @@ export const useNftMutateEvents = ( offset, ], async () => - chainId === INITIATION_CHAIN_ID - ? getNftMutateEvents(chainConfig.indexer, nftAddress, offset, limit) - : getNftMutateEventsOld(chainConfig.indexer, nftAddress, offset, limit), + getNftMutateEvents(chainConfig.indexer, nftAddress, offset, limit), { retry: 1, refetchOnWindowFocus: false, @@ -222,20 +165,13 @@ 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 () => - chainId === INITIATION_CHAIN_ID - ? getNftMutateEventsCount(chainConfig.indexer, nftAddress) - : getNftMutateEventsCountOld(chainConfig.indexer, nftAddress), + async () => getNftMutateEventsCount(chainConfig.indexer, nftAddress), { retry: 1, refetchOnWindowFocus: false, @@ -246,20 +182,13 @@ 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( [ CELATONE_QUERY_KEYS.NFTS_COUNT_BY_ACCOUNT, chainConfig.indexer, accountAddress, ], - async () => - chainId === INITIATION_CHAIN_ID - ? getNftsCountByAccount(chainConfig.indexer, accountAddress) - : getNftsCountByAccountOld(chainConfig.indexer, accountAddress), + async () => getNftsCountByAccount(chainConfig.indexer, accountAddress), { retry: 1, refetchOnWindowFocus: false, @@ -270,48 +199,32 @@ export const useNftsCountByAccount = (accountAddress: HexAddr) => { export const useNftsByAccountByCollection = ( accountAddress: HexAddr, - pageSize: number, + limit: number, offset: number, search = "", collectionAddress?: HexAddr32, options: Pick, "onSuccess"> = {} ) => { const { chainConfig } = useCelatoneApp(); - const { - chain: { chain_id: chainId }, - } = useCurrentChain(); - const expressionNew = useNftsByAccountExpression( - accountAddress, - collectionAddress, - search - ); - const expressionOld = useNftsByAccountExpressionOld( - accountAddress, - collectionAddress, - search - ); - const expression = - chainId === INITIATION_CHAIN_ID ? expressionNew : expressionOld; - return useQuery( [ CELATONE_QUERY_KEYS.NFTS_BY_ACCOUNT_BY_COLLECTION, chainConfig.indexer, accountAddress, - pageSize, + limit, offset, collectionAddress, - expression, + search, ], async () => - chainId === INITIATION_CHAIN_ID - ? getNftsByAccount(chainConfig.indexer, pageSize, offset, expression) - : getNftsByAccountOld( - chainConfig.indexer, - pageSize, - offset, - expression - ), + getNftsByAccount( + chainConfig.indexer, + accountAddress, + limit, + offset, + collectionAddress, + search + ), { retry: 1, refetchOnWindowFocus: false, From 648f062119e9356d5f2ff736dba35dae6c3b5633 Mon Sep 17 00:00:00 2001 From: evilpeach Date: Mon, 8 Jul 2024 13:47:13 +0700 Subject: [PATCH 2/2] fix: remove redundant --- src/lib/services/nft/collection.ts | 34 +++++++++++++------------- src/lib/services/nft/nft.ts | 38 ++++++++++++++++-------------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/lib/services/nft/collection.ts b/src/lib/services/nft/collection.ts index 630810f8a..9996acbe0 100644 --- a/src/lib/services/nft/collection.ts +++ b/src/lib/services/nft/collection.ts @@ -85,19 +85,24 @@ export const getCollections = async ( offset: number, search?: string ) => { - const expressionNew = getCollectionsExpression(search); - const expressionOld = getCollectionsExpressionOld(search); - try { const res = await axios.post(indexer, { query: getCollectionsQuery, - variables: { offset, limit, expression: expressionNew }, + variables: { + offset, + limit, + expression: getCollectionsExpression(search), + }, }); return parseWithError(zCollectionsResponse, res.data.data); } catch { const res = await axios.post(indexer, { query: getCollectionsQueryOld, - variables: { offset, limit, expression: expressionOld }, + variables: { + offset, + limit, + expression: getCollectionsExpressionOld(search), + }, }); return parseWithError(zCollectionsResponse, res.data.data); } @@ -292,22 +297,16 @@ export const getCollectionActivities = async ( offset: number, search?: string ) => { - const expressionNew = getCollectionActivitiesExpression( - collectionAddress, - search - ); - const expressionOld = getCollectionActivitiesExpressionOld( - collectionAddress, - search - ); - try { const res = await axios.post(indexer, { query: getCollectionActivitiesQuery, variables: { limit, offset, - expression: expressionNew, + expression: getCollectionActivitiesExpression( + collectionAddress, + search + ), }, }); return parseWithError( @@ -320,7 +319,10 @@ export const getCollectionActivities = async ( variables: { limit, offset, - expression: expressionOld, + expression: getCollectionActivitiesExpressionOld( + collectionAddress, + search + ), }, }); return parseWithError( diff --git a/src/lib/services/nft/nft.ts b/src/lib/services/nft/nft.ts index 75fd955f1..2106b4214 100644 --- a/src/lib/services/nft/nft.ts +++ b/src/lib/services/nft/nft.ts @@ -86,16 +86,13 @@ export const getNfts = async ( limit: number, offset: number ) => { - const expressionNew = getNftsExpression(collectionAddress, search); - const expressionOld = getNftsExpressionOld(collectionAddress, search); - try { const res = await axios.post(indexer, { query: getNftsQuery, variables: { limit, offset, - expression: expressionNew, + expression: getNftsExpression(collectionAddress, search), }, }); return parseWithError(zNft.array(), res.data.data.nfts); @@ -105,7 +102,7 @@ export const getNfts = async ( variables: { limit, offset, - expression: expressionOld, + expression: getNftsExpressionOld(collectionAddress, search), }, }); return parseWithError(zNftOld.array(), res.data.data.nfts); @@ -372,27 +369,32 @@ export const getNftsByAccount = async ( collectionAddress?: HexAddr32, search?: string ) => { - const expressionNew = getNftsByAccountExpression( - accountAddress, - collectionAddress, - search - ); - const expressionOld = getNftsByAccountExpressionOld( - accountAddress, - collectionAddress, - search - ); - try { const res = await axios.post(indexer, { query: getNftsByAccountQuery, - variables: { limit, offset, expression: expressionNew }, + variables: { + limit, + offset, + expression: getNftsByAccountExpression( + accountAddress, + collectionAddress, + search + ), + }, }); return parseWithError(zNftsByAccountResponse, res.data.data); } catch { const res = await axios.post(indexer, { query: getNftsByAccountQueryOld, - variables: { limit, offset, expression: expressionOld }, + variables: { + limit, + offset, + expression: getNftsByAccountExpressionOld( + accountAddress, + collectionAddress, + search + ), + }, }); return parseWithError(zNftsByAccountResponse, res.data.data); }