Skip to content

Commit

Permalink
fixed sdk v2
Browse files Browse the repository at this point in the history
  • Loading branch information
0xaptosj committed Oct 20, 2023
1 parent 792137c commit 4b5ba3e
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 111 deletions.
15 changes: 9 additions & 6 deletions frontend/src/app/home/Connected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import { useState, useEffect, useCallback } from "react";
import { Pet } from "./Pet";
import { useWallet } from "@aptos-labs/wallet-adapter-react";
import { Network, Provider } from "aptos";
import { Mint } from "./Mint";
import { NEXT_PUBLIC_CONTRACT_ADDRESS } from "../../utils/env";
import { getAptosClient } from "../../utils/aptosClient";

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

export function Connected() {
const [pet, setPet] = useState<Pet>();
Expand All @@ -15,10 +16,12 @@ export function Connected() {
const fetchPet = useCallback(async () => {
if (!account?.address) return;

const [name, _, energyPoints, parts] = await provider.view({
function: `${process.env.NEXT_PUBLIC_CONTRACT_ADDRESS}::main::get_aptogotchi`,
type_arguments: [],
arguments: [account.address],
const [name, _, energyPoints, parts] = await aptosClient.view({
payload: {
function: `${NEXT_PUBLIC_CONTRACT_ADDRESS}::main::get_aptogotchi`,
typeArguments: [],
arguments: [account.address],
},
});

const noPet = { name: "", birthday: 0, energyPoints: 0, parts: "0x" };
Expand Down
22 changes: 14 additions & 8 deletions frontend/src/app/home/Mint/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { useState } from "react";
import { useWallet } from "@aptos-labs/wallet-adapter-react";
import { Network, Provider } from "aptos";
import { PetImage, bodies, ears, faces } from "../Pet/Image";
import { Pet } from "../Pet";
import { ShuffleButton } from "@/components/ShuffleButton";
import { ShuffleButton } from "../../../components/ShuffleButton";
import {
NEXT_PUBLIC_BODY_OPTIONS,
NEXT_PUBLIC_CONTRACT_ADDRESS,
NEXT_PUBLIC_EAR_OPTIONS,
NEXT_PUBLIC_FACE_OPTIONS,
} from "../../../utils/env";
import { getAptosClient } from "../../../utils/aptosClient";

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

export interface MintProps {
fetchPet: () => Promise<void>;
Expand All @@ -28,9 +34,9 @@ export function Mint({ fetchPet }: MintProps) {

const handleShuffle = () => {
const randomParts = [
Math.floor(Math.random() * Number(process.env.NEXT_PUBLIC_BODY_OPTIONS)),
Math.floor(Math.random() * Number(process.env.NEXT_PUBLIC_EAR_OPTIONS)),
Math.floor(Math.random() * Number(process.env.NEXT_PUBLIC_FACE_OPTIONS)),
Math.floor(Math.random() * Number(NEXT_PUBLIC_BODY_OPTIONS)),
Math.floor(Math.random() * Number(NEXT_PUBLIC_EAR_OPTIONS)),
Math.floor(Math.random() * Number(NEXT_PUBLIC_FACE_OPTIONS)),
];
setParts(randomParts);

Expand All @@ -48,14 +54,14 @@ export function Mint({ fetchPet }: MintProps) {
console.log("MINT PET: ", newName, parts);
const payload = {
type: "entry_function_payload",
function: `${process.env.NEXT_PUBLIC_CONTRACT_ADDRESS}::main::create_aptogotchi`,
function: `${NEXT_PUBLIC_CONTRACT_ADDRESS}::main::create_aptogotchi`,
type_arguments: [],
arguments: [newName, parts],
};

try {
const response = await signAndSubmitTransaction(payload);
await provider.waitForTransaction(response.hash);
await aptosClient.waitForTransaction(response.hash);
} catch (error: any) {
console.error(error);
} finally {
Expand Down
15 changes: 10 additions & 5 deletions frontend/src/app/home/NotConnected.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
"use client";

import React, { useState } from "react";
import { useTypingEffect } from "@/utils/useTypingEffect";
import { useTypingEffect } from "../../utils/useTypingEffect";
import { Pet } from "./Pet";
import { PetImage, bodies, ears, faces } from "./Pet/Image";
import { ShuffleButton } from "@/components/ShuffleButton";
import { ShuffleButton } from "../../components/ShuffleButton";
import {
NEXT_PUBLIC_BODY_OPTIONS,
NEXT_PUBLIC_EAR_OPTIONS,
NEXT_PUBLIC_FACE_OPTIONS,
} from "../../utils/env";

const defaultPet: Pet = {
name: "Unknown",
Expand All @@ -22,9 +27,9 @@ export function NotConnected() {

const handleShuffle = () => {
const randomPet = [
Math.floor(Math.random() * Number(process.env.NEXT_PUBLIC_BODY_OPTIONS)),
Math.floor(Math.random() * Number(process.env.NEXT_PUBLIC_EAR_OPTIONS)),
Math.floor(Math.random() * Number(process.env.NEXT_PUBLIC_FACE_OPTIONS)),
Math.floor(Math.random() * Number(NEXT_PUBLIC_BODY_OPTIONS)),
Math.floor(Math.random() * Number(NEXT_PUBLIC_EAR_OPTIONS)),
Math.floor(Math.random() * Number(NEXT_PUBLIC_FACE_OPTIONS)),
];
setActivePet(randomPet);

Expand Down
34 changes: 15 additions & 19 deletions frontend/src/app/home/Pet/Actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

import { Dispatch, SetStateAction, useState } from "react";
import { useWallet } from "@aptos-labs/wallet-adapter-react";
// import { Aptos } from "@aptos-labs/ts-sdk";
import { Pet } from ".";
import { getAptosClient } from "../../../utils/aptosClient";
import { NEXT_PUBLIC_CONTRACT_ADDRESS, NEXT_PUBLIC_ENERGY_CAP, NEXT_PUBLIC_ENERGY_DECREASE, NEXT_PUBLIC_ENERGY_INCREASE } from "../../../utils/env";

import { Account, Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: "devnet" });
// const aptos = new Aptos(config);
const aptosClient = getAptosClient();

// const aptos = new Aptos();

export const provider = new Provider(Network.TESTNET);
export type PetAction = "feed" | "play";

export interface ActionsProps {
Expand Down Expand Up @@ -48,27 +44,27 @@ export function Actions({
setTransactionInProgress(true);
const payload = {
type: "entry_function_payload",
function: `${process.env.NEXT_PUBLIC_CONTRACT_ADDRESS}::main::feed`,
function: `${NEXT_PUBLIC_CONTRACT_ADDRESS}::main::feed`,
type_arguments: [],
arguments: [process.env.NEXT_PUBLIC_ENERGY_INCREASE],
arguments: [NEXT_PUBLIC_ENERGY_INCREASE],
};

try {
const response = await signAndSubmitTransaction(payload);
await provider.waitForTransaction(response.hash);
await aptosClient.waitForTransaction(response.hash);

setPet((pet) => {
if (!pet) return pet;
if (
pet.energy_points + Number(process.env.NEXT_PUBLIC_ENERGY_INCREASE) >
Number(process.env.NEXT_PUBLIC_ENERGY_CAP)
pet.energy_points + Number(NEXT_PUBLIC_ENERGY_INCREASE) >
Number(NEXT_PUBLIC_ENERGY_CAP)
)
return pet;

return {
...pet,
energy_points:
pet.energy_points + Number(process.env.NEXT_PUBLIC_ENERGY_INCREASE),
pet.energy_points + Number(NEXT_PUBLIC_ENERGY_INCREASE),
};
});
} catch (error: any) {
Expand All @@ -84,26 +80,26 @@ export function Actions({
setTransactionInProgress(true);
const payload = {
type: "entry_function_payload",
function: `${process.env.NEXT_PUBLIC_CONTRACT_ADDRESS}::main::play`,
function: `${NEXT_PUBLIC_CONTRACT_ADDRESS}::main::play`,
type_arguments: [],
arguments: [process.env.NEXT_PUBLIC_ENERGY_DECREASE],
arguments: [NEXT_PUBLIC_ENERGY_DECREASE],
};

try {
const response = await signAndSubmitTransaction(payload);
await provider.waitForTransaction(response.hash);
await aptosClient.waitForTransaction(response.hash);

setPet((pet) => {
if (!pet) return pet;
if (
pet.energy_points <= Number(process.env.NEXT_PUBLIC_ENERGY_DECREASE)
pet.energy_points <= Number(NEXT_PUBLIC_ENERGY_DECREASE)
)
return pet;

return {
...pet,
energy_points:
pet.energy_points - Number(process.env.NEXT_PUBLIC_ENERGY_DECREASE),
pet.energy_points - Number(NEXT_PUBLIC_ENERGY_DECREASE),
};
});
} catch (error: any) {
Expand All @@ -115,7 +111,7 @@ export function Actions({

const feedDisabled =
selectedAction === "feed" &&
pet.energy_points === Number(process.env.NEXT_PUBLIC_ENERGY_CAP);
pet.energy_points === Number(NEXT_PUBLIC_ENERGY_CAP);
const playDisabled =
selectedAction === "play" && pet.energy_points === Number(0);

Expand Down
115 changes: 60 additions & 55 deletions frontend/src/app/home/Pet/Collection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import { useCallback, useState, useEffect } from "react";
import { useWallet } from "@aptos-labs/wallet-adapter-react";
import { Network, Provider } from "aptos";
import { padAddressIfNeeded } from "@/utils/address";
import { padAddressIfNeeded } from "../../../utils/address";
import { getAptosClient } from "../../../utils/aptosClient";
import { NEXT_PUBLIC_CONTRACT_ADDRESS } from "../../../utils/env";

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

export type Collection = {
collection_id: string;
Expand Down Expand Up @@ -33,63 +34,67 @@ export function Collection() {
const fetchCollection = 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
)) as [`0x${string}`];

const collectionID = aptogotchiCollectionIDResponse[0];

const getCollectionDataGql = {
query: `
query MyQuery($collection_id: String) {
current_collections_v2(
limit: 3
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
}
}
`,
variables: {
collection_id: padAddressIfNeeded(collectionID),
const aptogotchiCollectionIDResponse = (await aptosClient.view({
payload: {
function: `${NEXT_PUBLIC_CONTRACT_ADDRESS}::main::get_aptogotchi_collection_id`,
typeArguments: [],
arguments: [],
},
};
})) as [`0x${string}`];

const collectionResponse: CollectionResponse =
await provider.indexerClient.queryIndexer(getCollectionDataGql);
await aptosClient.queryIndexer({
query: {
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 } }
) {
owner_address
}
}
`,
variables: {
collection_id: padAddressIfNeeded(
aptogotchiCollectionIDResponse[0]
),
},
},
});

console.log(collectionResponse);

const firstFewAptogotchi = await Promise.all(
collectionResponse.current_collection_ownership_v2_view.map((holder) =>
provider.view({
function: `${process.env.NEXT_PUBLIC_CONTRACT_ADDRESS}::main::get_aptogotchi`,
type_arguments: [],
arguments: [holder.owner_address],
})
)
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`,
typeArguments: [],
arguments: [holder.owner_address],
},
})
)
);

setCollection(collectionResponse.current_collections_v2[0]);
Expand Down
11 changes: 6 additions & 5 deletions frontend/src/app/home/Pet/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import { AiFillSave } from "react-icons/ai";
import { FaCopy } from "react-icons/fa";
import { HealthBar } from "@/components/HealthBar";
import { HealthBar } from "../../../components/HealthBar";
import { Pet } from ".";
import { Dispatch, SetStateAction, useState } from "react";
import { useWallet } from "@aptos-labs/wallet-adapter-react";
import { Network, Provider } from "aptos";
import { NEXT_PUBLIC_CONTRACT_ADDRESS } from "../../../utils/env";
import { getAptosClient } from "../../../utils/aptosClient";

export interface PetDetailsProps {
pet: Pet;
setPet: Dispatch<SetStateAction<Pet | undefined>>;
}

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

export function PetDetails({ pet, setPet }: PetDetailsProps) {
const [newName, setNewName] = useState(pet.name);
Expand All @@ -29,14 +30,14 @@ export function PetDetails({ pet, setPet }: PetDetailsProps) {

const payload = {
type: "entry_function_payload",
function: `${process.env.NEXT_PUBLIC_CONTRACT_ADDRESS}::main::set_name`,
function: `${NEXT_PUBLIC_CONTRACT_ADDRESS}::main::set_name`,
type_arguments: [],
arguments: [newName],
};

try {
const response = await signAndSubmitTransaction(payload);
await provider.waitForTransaction(response.hash);
await aptosClient.waitForTransaction(response.hash);

setPet((pet) => {
if (!pet) return pet;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/home/Pet/Summary.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { useTypingEffect } from "@/utils/useTypingEffect";
import { useTypingEffect } from "../../../utils/useTypingEffect";
import { Pet } from ".";

export interface SummaryProps {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WalletProvider } from "@/context/WalletProvider";
import { WalletProvider } from "../context/WalletProvider";
import type { Metadata } from "next";
import localFont from "next/font/local";
import { PropsWithChildren } from "react";
Expand Down
Loading

0 comments on commit 4b5ba3e

Please sign in to comment.