Skip to content

Commit

Permalink
fix gql
Browse files Browse the repository at this point in the history
  • Loading branch information
0xaptosj committed Oct 19, 2023
1 parent f1b8e1f commit 0aa8380
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 46 deletions.
6 changes: 3 additions & 3 deletions frontend/src/app/home/Connected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
73 changes: 30 additions & 43 deletions frontend/src/app/home/Pet/Collection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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<Collection>();
Expand All @@ -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 } }
) {
Expand All @@ -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(() => {
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/utils/address.ts
Original file line number Diff line number Diff line change
@@ -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)}`;
}

0 comments on commit 0aa8380

Please sign in to comment.