-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
164 additions
and
152 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React, { useEffect } from "react"; | ||
import { useWallet } from "@aptos-labs/wallet-adapter-react"; | ||
import { useGetAptogotchiCollection } from "@/hooks/useGetAptogotchiCollection"; | ||
|
||
export function AptogotchiCollection() { | ||
const { account, network } = useWallet(); | ||
const { collection, firstFewAptogotchiName, loading, fetchCollection } = | ||
useGetAptogotchiCollection(); | ||
|
||
useEffect(() => { | ||
if (!account?.address || !network) return; | ||
fetchCollection(); | ||
}, [account?.address, fetchCollection, network]); | ||
|
||
if (loading || !collection) return null; | ||
|
||
return ( | ||
<div className="nes-container with-title h-[100px]"> | ||
<p>{`There are a total of ${collection.current_supply} Aptogotchis in existence.`}</p> | ||
<p>{`Meet your fellow Aptogotchis: ${firstFewAptogotchiName?.join(", ")}${ | ||
(firstFewAptogotchiName?.length || 0) < collection.current_supply | ||
? "..." | ||
: "" | ||
}`}</p> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export const queryAptogotchiCollection = ` | ||
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 } } | ||
) { | ||
owner_address | ||
} | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { useCallback, useState } from "react"; | ||
import { useWallet } from "@aptos-labs/wallet-adapter-react"; | ||
import { getAptosClient } from "@/utils/aptosClient"; | ||
import { NEXT_PUBLIC_CONTRACT_ADDRESS } from "@/utils/env"; | ||
import { queryAptogotchiCollection } from "@/graphql/queryAptogotchiCollection"; | ||
import { padAddressIfNeeded } from "@/utils/address"; | ||
|
||
const aptosClient = getAptosClient(); | ||
|
||
type Collection = { | ||
collection_id: string; | ||
collection_name: string; | ||
creator_address: string; | ||
uri: string; | ||
current_supply: any; | ||
}; | ||
|
||
type CollectionHolder = { | ||
owner_address: string; | ||
}; | ||
|
||
type CollectionResponse = { | ||
current_collections_v2: Collection[]; | ||
current_collection_ownership_v2_view: CollectionHolder[]; | ||
}; | ||
|
||
export function useGetAptogotchiCollection() { | ||
const { account } = useWallet(); | ||
const [collection, setCollection] = useState<Collection>(); | ||
const [firstFewAptogotchiName, setFirstFewAptogotchiName] = | ||
useState<string[]>(); | ||
const [loading, setLoading] = useState(false); | ||
|
||
const fetchCollection = useCallback(async () => { | ||
if (!account?.address) return; | ||
|
||
try { | ||
setLoading(true); | ||
|
||
const aptogotchiCollectionIDResponse = (await aptosClient.view({ | ||
payload: { | ||
function: `${NEXT_PUBLIC_CONTRACT_ADDRESS}::main::get_aptogotchi_collection_id`, | ||
arguments: [], | ||
}, | ||
})) as [`0x${string}`]; | ||
|
||
const collectionResponse: CollectionResponse = | ||
await aptosClient.queryIndexer({ | ||
query: { | ||
query: queryAptogotchiCollection, | ||
variables: { | ||
collection_id: padAddressIfNeeded( | ||
aptogotchiCollectionIDResponse[0] | ||
), | ||
}, | ||
}, | ||
}); | ||
|
||
const firstFewAptogotchi = await Promise.all( | ||
collectionResponse.current_collection_ownership_v2_view | ||
// TODO: change to limit 3 in gql after indexer fix limit | ||
.slice(0, 3) | ||
.map((holder) => | ||
aptosClient.view({ | ||
payload: { | ||
function: `${NEXT_PUBLIC_CONTRACT_ADDRESS}::main::get_aptogotchi`, | ||
arguments: [holder.owner_address], | ||
}, | ||
}) | ||
) | ||
); | ||
|
||
setCollection(collectionResponse.current_collections_v2[0]); | ||
setFirstFewAptogotchiName(firstFewAptogotchi.map((x) => x[0] as string)); | ||
} catch (error) { | ||
console.error("Error fetching Aptogotchi collection:", error); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}, [account?.address]); | ||
|
||
return { collection, firstFewAptogotchiName, loading, fetchCollection }; | ||
} |