Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use sdk v2 #28

Merged
merged 6 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
"aptos": "aptos"
},
"dependencies": {
"@aptos-labs/ts-sdk": "^0.0.0",
"@aptos-labs/wallet-adapter-react": "^1.3.2",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@types/node": "20.4.5",
"@types/react": "18.2.17",
"@types/react-dom": "18.2.7",
"aptos": "^1.15.0",
"autoprefixer": "10.4.14",
"clsx": "^2.0.0",
"eslint": "^8.48.0",
Expand Down
39 changes: 32 additions & 7 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 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,11 @@ 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`,
arguments: [account.address],
},
});

const noPet = { name: "", birthday: 0, energyPoints: 0, parts: "0x" };
Expand Down
22 changes: 15 additions & 7 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 {
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,16 @@ 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({
transactionHash: response.hash,
});
} catch (error: any) {
console.error(error);
} finally {
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/app/home/NotConnected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { useTypingEffect } from "@/utils/useTypingEffect";
import { Pet } from "./Pet";
import { PetImage, bodies, ears, faces } from "./Pet/Image";
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
39 changes: 23 additions & 16 deletions frontend/src/app/home/Pet/Actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import { Dispatch, SetStateAction, useState } from "react";
import { useWallet } from "@aptos-labs/wallet-adapter-react";
import { Network, Provider } from "aptos";
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";

const aptosClient = getAptosClient();

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

export interface ActionsProps {
Expand Down Expand Up @@ -42,27 +49,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({ transactionHash: 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 @@ -78,26 +85,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({
transactionHash: response.hash,
});

setPet((pet) => {
if (!pet) return pet;
if (
pet.energy_points <= Number(process.env.NEXT_PUBLIC_ENERGY_DECREASE)
)
if (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 @@ -109,7 +116,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
Loading