Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
0xaptosj committed Oct 19, 2023
1 parent f3a4786 commit 3f321fc
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 71 deletions.
2 changes: 1 addition & 1 deletion frontend/.env
Original file line number Diff line number Diff line change
@@ -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
Expand Down
43 changes: 31 additions & 12 deletions frontend/src/app/home/Connected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const provider = new Provider(Network.TESTNET);

export function Connected() {
const [pet, setPet] = useState<Pet>();
const [collectionID, setCollectionID] = useState<string>();
const { account, network } = useWallet();

const fetchPet = useCallback(async () => {
Expand All @@ -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"];

Expand All @@ -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 (
<div className="flex flex-col gap-3 p-3">
{pet ? <Pet pet={pet} setPet={setPet} /> : <Mint fetchPet={fetchPet} />}
{collectionID ? (
pet ? (
<Pet pet={pet} setPet={setPet} collectionID={collectionID} />
) : (
<Mint fetchPet={fetchPet} />
)
) : null}
</div>
);
}
95 changes: 55 additions & 40 deletions frontend/src/app/home/Pet/Collection.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"use client";

import { Pet } from ".";
import { useCallback, useState, useEffect } from "react";
import { useWallet } from "@aptos-labs/wallet-adapter-react";
import { Network, Provider } from "aptos";

export const provider = new Provider(Network.TESTNET);

export interface CollectionProps {
pet: Pet;
collectionID: string;
}

export type Collection = {
Expand All @@ -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<Collection>();
const [collectionHolders, setCollectionHolders] = useState<string[]>();
const [collectionHolders, setCollectionHolders] =
useState<CollectionHolder[]>();

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(
Expand All @@ -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(() => {
Expand All @@ -94,7 +107,9 @@ export function Collection({ pet }: CollectionProps) {
<ul className="nes-list is-disc">
<label>
{JSON.stringify(
collectionHolders?.map((holder) => holder.substring(0, 5) + "...")
collectionHolders?.map(
(holder) => holder.owner_address.substring(0, 5) + "..."
)
)}
</label>
</ul>
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/app/home/Pet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export interface Pet {
name: string;
energy_points: number;
parts: number[];
address: string;
}

interface PetProps {
pet: Pet;
setPet: Dispatch<SetStateAction<Pet | undefined>>;
collectionID: string;
}

export function Pet({ pet, setPet }: PetProps) {
export function Pet({ pet, setPet, collectionID }: PetProps) {
const [selectedAction, setSelectedAction] = useState<PetAction>("feed");

return (
Expand All @@ -36,7 +36,7 @@ export function Pet({ pet, setPet }: PetProps) {
avatarStyle
/>
<PetDetails pet={pet} setPet={setPet} />
<Collection pet={pet} />
<Collection collectionID={collectionID} />
</div>
<div className="flex flex-col gap-8 w-[680px] h-full">
<Actions
Expand Down
2 changes: 1 addition & 1 deletion move/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ upgrade_policy = "compatible"
[addresses]
aptogotchi = '_'

# used for testing so you don't need to manually pass in the address
# used for testing so you don't need to manually pass in the address in cli
[dev-addresses]
aptogotchi = "0x100"

Expand Down
2 changes: 1 addition & 1 deletion move/sh_scripts/move_publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e

echo "##### Publishing module #####"

PROFILE=testnet-1
PROFILE=testnet-2
ADDR=0x$(aptos config show-profiles --profile=$PROFILE | grep 'account' | sed -n 's/.*"account": \"\(.*\)\".*/\1/p')

aptos move publish \
Expand Down
34 changes: 21 additions & 13 deletions move/sources/aptogotchi.move
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,9 @@ module aptogotchi::main {
}

// Get reference to Aptogotchi token object (CAN'T modify the reference)
#[view]
public fun get_aptogotchi_address(creator_addr: address): (address) acquires CollectionCapability {
fun get_aptogotchi_address(creator_addr: &address): (address) acquires CollectionCapability {
let collection = string::utf8(APTOGOTCHI_COLLECTION_NAME);
let token_name = to_string(&creator_addr);
let token_name = to_string(creator_addr);
let creator = &get_token_signer();
let token_address = token::create_token_address(
&signer::address_of(creator),
Expand All @@ -153,10 +152,19 @@ module aptogotchi::main {
token_address
}

// Get collection ID of aptogotchi collection
#[view]
public fun get_aptogotchi_collection_id(): (address) acquires CollectionCapability {
let collection_name = string::utf8(APTOGOTCHI_COLLECTION_NAME);
let creator = &get_token_signer();
let creator_addr = signer::address_of(creator);
collection::create_collection_address(&creator_addr, &collection_name)
}

// Returns true if this address owns an Aptogotchi
#[view]
public fun has_aptogotchi(owner_addr: address): (bool) acquires CollectionCapability {
let token_address = get_aptogotchi_address(owner_addr);
let token_address = get_aptogotchi_address(&owner_addr);
let has_gotchi = exists<AptoGotchi>(token_address);

has_gotchi
Expand All @@ -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<AptoGotchi>(token_address);

// view function can only return primitive types.
Expand All @@ -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<AptoGotchi>(token_address);

gotchi.name
Expand All @@ -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<AptoGotchi>(token_address);
gotchi.name = name;

Expand All @@ -204,15 +212,15 @@ 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<AptoGotchi>(token_address);

gotchi.energy_points
}

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<AptoGotchi>(token_address);

gotchi.energy_points = if (gotchi.energy_points + points > ENERGY_UPPER_BOUND) {
Expand All @@ -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<AptoGotchi>(token_address);

gotchi.energy_points = if (gotchi.energy_points < points) {
Expand All @@ -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<AptoGotchi>(token_address);

gotchi.parts
Expand All @@ -254,7 +262,7 @@ module aptogotchi::main {
// Sets Aptogotchi's body parts
public entry fun set_parts(owner: &signer, parts: vector<u8>) 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<AptoGotchi>(token_address);
gotchi.parts = parts;

Expand Down Expand Up @@ -325,4 +333,4 @@ module aptogotchi::main {
feed(creator, 3);
assert!(get_energy_points(signer::address_of(creator)) == ENERGY_UPPER_BOUND - 2, 1);
}
}
}

0 comments on commit 3f321fc

Please sign in to comment.