From 3bc2e80d73a7374019305199ef4b5683a52e7bd0 Mon Sep 17 00:00:00 2001 From: 0xaptosj <129789810+0xaptosj@users.noreply.github.com> Date: Thu, 19 Oct 2023 12:25:49 -0700 Subject: [PATCH] fix gql --- frontend/src/app/home/Connected.tsx | 6 +- frontend/src/app/home/Pet/Collection.tsx | 73 ++++++++++-------------- frontend/src/utils/address.ts | 8 +++ 3 files changed, 41 insertions(+), 46 deletions(-) create mode 100644 frontend/src/utils/address.ts diff --git a/frontend/src/app/home/Connected.tsx b/frontend/src/app/home/Connected.tsx index cc89315f..d9baa53f 100644 --- a/frontend/src/app/home/Connected.tsx +++ b/frontend/src/app/home/Connected.tsx @@ -47,11 +47,11 @@ export function Connected() { arguments: [], }; - const aptogotchiCollectionIDResponse = await provider.view( + const aptogotchiCollectionIDResponse = (await provider.view( getAptogotchiCollectionIDPayload - ); + )) as string[]; - setCollectionID(aptogotchiCollectionIDResponse as unknown as string); + setCollectionID(aptogotchiCollectionIDResponse[0]); }, [account?.address]); useEffect(() => { diff --git a/frontend/src/app/home/Pet/Collection.tsx b/frontend/src/app/home/Pet/Collection.tsx index f46c3fca..2ff85766 100644 --- a/frontend/src/app/home/Pet/Collection.tsx +++ b/frontend/src/app/home/Pet/Collection.tsx @@ -3,6 +3,7 @@ import { useCallback, useState, useEffect } from "react"; import { useWallet } from "@aptos-labs/wallet-adapter-react"; import { Network, Provider } from "aptos"; +import { padAddressIfNeeded } from "@/utils/address"; export const provider = new Provider(Network.TESTNET); @@ -22,6 +23,11 @@ export type CollectionHolder = { owner_address: string; }; +export type CollectionResponse = { + current_collections_v2: Collection[]; + current_collection_ownership_v2_view: CollectionHolder[]; +}; + export function Collection({ collectionID }: CollectionProps) { const { account, network } = useWallet(); const [collection, setCollection] = useState(); @@ -34,6 +40,24 @@ export function Collection({ collectionID }: CollectionProps) { const getCollectionDataGql = { query: ` query MyQuery($collection_id: String) { + current_collections_v2( + where: { collection_id: { _eq: $collection_id } } + ) { + collection_id + collection_name + current_supply + description + creator_address + last_transaction_timestamp + max_supply + last_transaction_version + mutable_description + mutable_uri + token_standard + table_handle_v1 + total_minted_v2 + uri + } current_collection_ownership_v2_view( where: { collection_id: { _eq: $collection_id } } ) { @@ -42,54 +66,17 @@ export function Collection({ collectionID }: CollectionProps) { } `, variables: { - collection_id: collectionID, + collection_id: padAddressIfNeeded(collectionID), }, }; - // const tokenDataResponse = await provider. + const collectionResponse: CollectionResponse = + await provider.indexerClient.queryIndexer(getCollectionDataGql); - const collectionResponse = await provider.indexerClient.queryIndexer( - getCollectionDataGql + setCollection(collectionResponse.current_collections_v2[0]); + setCollectionHolders( + collectionResponse.current_collection_ownership_v2_view ); - - // const getAllCollectionHoldersGql = { - // query: ` - // query MyQuery($collection_id: String) { - // current_collection_ownership_v2_view( - // where: { collection_id: { _eq: $collection_id } } - // ) { - // owner_address - // } - // } - // `, - // variables: { - // collection_id: collectionID, - // }, - // }; - // const collectionHolderResponse = await provider.indexerClient.queryIndexer( - // getAllCollectionHoldersGql - // ); - - console.log(JSON.stringify(collectionResponse, null, 2)); - // console.log( - // JSON.stringify( - // // @ts-ignore - // collectionHolderResponse, - // null, - // 2 - // ) - // ); - - // setCollection(collectionResponse); - - // setCollectionHolders( - // collectionHolderResponse - // // // @ts-ignore - // // collectionHolderResponse.current_collection_ownership_v2_view.map( - // // // @ts-ignore - // // (d) => d.owner_address - // // ) - // ); }, [account?.address]); useEffect(() => { diff --git a/frontend/src/utils/address.ts b/frontend/src/utils/address.ts new file mode 100644 index 00000000..c9fe3d9f --- /dev/null +++ b/frontend/src/utils/address.ts @@ -0,0 +1,8 @@ +// In some case the leading char is 0 after the 0x and it got truncated +// This function will add it back if needed cause indexer doesn't auto pad it +export function padAddressIfNeeded(address: string) { + if (address.length === 67) { + return address; + } + return `0x0${address.slice(2)}`; +}