diff --git a/frontend/.env b/frontend/.env index d69292c8..25888508 100644 --- a/frontend/.env +++ b/frontend/.env @@ -1,4 +1,4 @@ -NEXT_PUBLIC_CONTRACT_ADDRESS="0x327d9ba5d1bdc6c3e6e607add36f51c5136de9e7f2e47da497f758a1c0db0520" +NEXT_PUBLIC_CONTRACT_ADDRESS="0xe7f3b4e9c597522d45cc26893cfe35f27a20e01c50c2e91ab6ed0e4831a578f1" NEXT_PUBLIC_BODY_OPTIONS=5 NEXT_PUBLIC_EAR_OPTIONS=6 NEXT_PUBLIC_FACE_OPTIONS=4 diff --git a/frontend/src/app/home/Connected.tsx b/frontend/src/app/home/Connected.tsx index 780dd63e..cc89315f 100644 --- a/frontend/src/app/home/Connected.tsx +++ b/frontend/src/app/home/Connected.tsx @@ -10,6 +10,7 @@ export const provider = new Provider(Network.TESTNET); export function Connected() { const [pet, setPet] = useState(); + const [collectionID, setCollectionID] = useState(); const { account, network } = useWallet(); const fetchPet = useCallback(async () => { @@ -21,16 +22,7 @@ export function Connected() { arguments: [account.address], }; - const getAptogotchiAddressPayload = { - function: `${process.env.NEXT_PUBLIC_CONTRACT_ADDRESS}::main::get_aptogotchi_address`, - type_arguments: [], - arguments: [account.address], - }; - - const [aptogotchiResponse, aptogotchiAddressResponse] = await Promise.all([ - provider.view(getAptogotchiPayload), - provider.view(getAptogotchiAddressPayload), - ]); + const aptogotchiResponse = await provider.view(getAptogotchiPayload); const noPet = ["", "0", "0", "0x"]; @@ -42,20 +34,47 @@ export function Connected() { .split("0") .slice(2) .map(Number), - address: aptogotchiAddressResponse[0] as unknown as string, }); } }, [account?.address]); + const fetchCollectionID = useCallback(async () => { + if (!account?.address) return; + + const getAptogotchiCollectionIDPayload = { + function: `${process.env.NEXT_PUBLIC_CONTRACT_ADDRESS}::main::get_aptogotchi_collection_id`, + type_arguments: [], + arguments: [], + }; + + const aptogotchiCollectionIDResponse = await provider.view( + getAptogotchiCollectionIDPayload + ); + + setCollectionID(aptogotchiCollectionIDResponse as unknown as string); + }, [account?.address]); + useEffect(() => { if (!account?.address || !network) return; fetchPet(); }, [account?.address, fetchPet, network]); + useEffect(() => { + if (!account?.address || !network) return; + + fetchCollectionID(); + }, [fetchCollectionID, network]); + return (
- {pet ? : } + {collectionID ? ( + pet ? ( + + ) : ( + + ) + ) : null}
); } diff --git a/frontend/src/app/home/Pet/Collection.tsx b/frontend/src/app/home/Pet/Collection.tsx index 4adbcb36..f46c3fca 100644 --- a/frontend/src/app/home/Pet/Collection.tsx +++ b/frontend/src/app/home/Pet/Collection.tsx @@ -1,6 +1,5 @@ "use client"; -import { Pet } from "."; import { useCallback, useState, useEffect } from "react"; import { useWallet } from "@aptos-labs/wallet-adapter-react"; import { Network, Provider } from "aptos"; @@ -8,7 +7,7 @@ import { Network, Provider } from "aptos"; export const provider = new Provider(Network.TESTNET); export interface CollectionProps { - pet: Pet; + collectionID: string; } export type Collection = { @@ -19,22 +18,20 @@ export type Collection = { current_supply: any; }; -export function Collection({ pet }: CollectionProps) { +export type CollectionHolder = { + owner_address: string; +}; + +export function Collection({ collectionID }: CollectionProps) { const { account, network } = useWallet(); const [collection, setCollection] = useState(); - const [collectionHolders, setCollectionHolders] = useState(); + const [collectionHolders, setCollectionHolders] = + useState(); const fetchCollection = useCallback(async () => { if (!account?.address) return; - const tokenDataResponse = await provider.getTokenData(pet.address, { - tokenStandard: "v2", - }); - - const collectionResponse = - tokenDataResponse?.current_token_datas_v2[0].current_collection!; - - const getAllCollectionHoldersGql = { + const getCollectionDataGql = { query: ` query MyQuery($collection_id: String) { current_collection_ownership_v2_view( @@ -45,38 +42,54 @@ export function Collection({ pet }: CollectionProps) { } `, variables: { - collection_id: collectionResponse.collection_id, + collection_id: collectionID, }, }; - const collectionHolderResponse = await provider.indexerClient.queryIndexer( - getAllCollectionHoldersGql - ); - console.log(JSON.stringify(tokenDataResponse, null, 2)); - console.log( - JSON.stringify( - // @ts-ignore - collectionHolderResponse.current_collection_ownership_v2_view, - null, - 2 - ) - ); + // const tokenDataResponse = await provider. - setCollection({ - collection_id: collectionResponse.collection_id, - collection_name: collectionResponse.collection_name, - creator_address: collectionResponse.creator_address, - uri: collectionResponse.uri, - current_supply: collectionResponse.current_supply, - }); - - setCollectionHolders( - // @ts-ignore - collectionHolderResponse.current_collection_ownership_v2_view.map( - // @ts-ignore - (d) => d.owner_address - ) + const collectionResponse = await provider.indexerClient.queryIndexer( + getCollectionDataGql ); + + // 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(() => { @@ -94,7 +107,9 @@ export function Collection({ pet }: CollectionProps) {
diff --git a/frontend/src/app/home/Pet/index.tsx b/frontend/src/app/home/Pet/index.tsx index 0eaef4af..25632123 100644 --- a/frontend/src/app/home/Pet/index.tsx +++ b/frontend/src/app/home/Pet/index.tsx @@ -11,15 +11,15 @@ export interface Pet { name: string; energy_points: number; parts: number[]; - address: string; } interface PetProps { pet: Pet; setPet: Dispatch>; + collectionID: string; } -export function Pet({ pet, setPet }: PetProps) { +export function Pet({ pet, setPet, collectionID }: PetProps) { const [selectedAction, setSelectedAction] = useState("feed"); return ( @@ -36,7 +36,7 @@ export function Pet({ pet, setPet }: PetProps) { avatarStyle /> - +
(token_address); has_gotchi @@ -172,7 +180,7 @@ module aptogotchi::main { assert!(false, error::unavailable(ENOT_AVAILABLE)); }; - let token_address = get_aptogotchi_address(owner_addr); + let token_address = get_aptogotchi_address(&owner_addr); let gotchi = borrow_global_mut(token_address); // view function can only return primitive types. @@ -182,7 +190,7 @@ module aptogotchi::main { // Returns Aptogotchi's name #[view] public fun get_name(owner_addr: address): String acquires AptoGotchi, CollectionCapability { - let token_address = get_aptogotchi_address(owner_addr); + let token_address = get_aptogotchi_address(&owner_addr); let gotchi = borrow_global(token_address); gotchi.name @@ -191,7 +199,7 @@ module aptogotchi::main { // Sets Aptogotchi's name public entry fun set_name(owner: signer, name: String) acquires AptoGotchi, CollectionCapability { let owner_addr = signer::address_of(&owner); - let token_address = get_aptogotchi_address(owner_addr); + let token_address = get_aptogotchi_address(&owner_addr); let gotchi = borrow_global_mut(token_address); gotchi.name = name; @@ -204,7 +212,7 @@ module aptogotchi::main { assert!(false, error::unavailable(ENOT_AVAILABLE)); }; - let token_address = get_aptogotchi_address(owner_addr); + let token_address = get_aptogotchi_address(&owner_addr); let gotchi = borrow_global(token_address); gotchi.energy_points @@ -212,7 +220,7 @@ module aptogotchi::main { public entry fun feed(owner: &signer, points: u64) acquires AptoGotchi, CollectionCapability { let owner_addr = signer::address_of(owner); - let token_address = get_aptogotchi_address(owner_addr); + let token_address = get_aptogotchi_address(&owner_addr); let gotchi = borrow_global_mut(token_address); gotchi.energy_points = if (gotchi.energy_points + points > ENERGY_UPPER_BOUND) { @@ -226,7 +234,7 @@ module aptogotchi::main { public entry fun play(owner: &signer, points: u64) acquires AptoGotchi, CollectionCapability { let owner_addr = signer::address_of(owner); - let token_address = get_aptogotchi_address(owner_addr); + let token_address = get_aptogotchi_address(&owner_addr); let gotchi = borrow_global_mut(token_address); gotchi.energy_points = if (gotchi.energy_points < points) { @@ -245,7 +253,7 @@ module aptogotchi::main { assert!(false, error::unavailable(ENOT_AVAILABLE)); }; - let token_address = get_aptogotchi_address(owner_addr); + let token_address = get_aptogotchi_address(&owner_addr); let gotchi = borrow_global(token_address); gotchi.parts @@ -254,7 +262,7 @@ module aptogotchi::main { // Sets Aptogotchi's body parts public entry fun set_parts(owner: &signer, parts: vector) acquires AptoGotchi, CollectionCapability { let owner_addr = signer::address_of(owner); - let token_address = get_aptogotchi_address(owner_addr); + let token_address = get_aptogotchi_address(&owner_addr); let gotchi = borrow_global_mut(token_address); gotchi.parts = parts; @@ -325,4 +333,4 @@ module aptogotchi::main { feed(creator, 3); assert!(get_energy_points(signer::address_of(creator)) == ENERGY_UPPER_BOUND - 2, 1); } -} +} \ No newline at end of file