From 888c6cb9eab1d1b160c2e43f8a4e69346990e846 Mon Sep 17 00:00:00 2001 From: arjanjohan Date: Sat, 21 Sep 2024 09:29:35 +0200 Subject: [PATCH] update and implement getAccountResource hooks --- packages/nextjs/app/bio/page.tsx | 30 ++++++++-------- .../scaffold-move/useGetAccountResource.ts | 35 ++++++++++++------- .../scaffold-move/useGetAccountResources.ts | 20 +++++++---- packages/nextjs/utils/scaffold-move/module.ts | 5 +-- 4 files changed, 53 insertions(+), 37 deletions(-) diff --git a/packages/nextjs/app/bio/page.tsx b/packages/nextjs/app/bio/page.tsx index 1722531..b5b9b47 100644 --- a/packages/nextjs/app/bio/page.tsx +++ b/packages/nextjs/app/bio/page.tsx @@ -1,13 +1,19 @@ "use client"; import { useState } from "react"; -import { InputTransactionData, useWallet } from "@aptos-labs/wallet-adapter-react"; +import { InputTransactionData, Types, useWallet } from "@aptos-labs/wallet-adapter-react"; import type { NextPage } from "next"; import { InputBase } from "~~/components/scaffold-move"; -import { useAptosClient } from "~~/hooks/scaffold-move"; +import { useGetAccountResource } from "~~/hooks/scaffold-move"; import { useGetModule } from "~~/hooks/scaffold-move/useGetModule"; import useSubmitTransaction from "~~/hooks/scaffold-move/useSubmitTransaction"; -import { useTargetNetwork } from "~~/hooks/scaffold-move/useTargetNetwork"; + +// TODO: Fix this workaround +// Add this interface near the top of the file +interface BioResource extends Types.MoveResource { + name: string; + bio: string; +} // Alert Component for showing error messages or warnings const Alert = ({ message }: { message: string }) => ( @@ -18,9 +24,6 @@ const Alert = ({ message }: { message: string }) => ( const OnchainBio: NextPage = () => { const { account } = useWallet(); - const network = useTargetNetwork(); - const chainId = network.targetNetwork.id; - const aptos = useAptosClient(chainId); const [inputName, setInputName] = useState(""); const [inputBio, setInputBio] = useState(""); @@ -34,6 +37,8 @@ const OnchainBio: NextPage = () => { const moveModule = useGetModule("onchain_bio"); const bioAbi = moveModule?.abi; + const { data: bioResource, refetch: refetchBio } = useGetAccountResource("onchain_bio", "Bio"); + // If the bioModule or ABI is not found, show an alert message and return early if (!bioAbi) { return ; @@ -44,17 +49,14 @@ const OnchainBio: NextPage = () => { if (!account) return; try { - const resourceName = "Bio"; - - const bioResource = await aptos.getAccountResource({ - accountAddress: account.address, - resourceType: `${bioAbi.address}::${bioAbi.name}::${resourceName}`, - }); + await refetchBio(); console; if (bioResource) { setAccountHasBio(true); - setCurrentName(bioResource.name); - setCurrentBio(bioResource.bio); + + // TODO: Fix this workaround + setCurrentName((bioResource as BioResource).name); + setCurrentBio((bioResource as BioResource).bio); } else { clearBio(); } diff --git a/packages/nextjs/hooks/scaffold-move/useGetAccountResource.ts b/packages/nextjs/hooks/scaffold-move/useGetAccountResource.ts index 4c82e03..d5bc5d2 100644 --- a/packages/nextjs/hooks/scaffold-move/useGetAccountResource.ts +++ b/packages/nextjs/hooks/scaffold-move/useGetAccountResource.ts @@ -2,6 +2,7 @@ import { ResponseError, withResponseError } from "../client"; import { useGetModule } from "./useGetModule"; import { useTargetNetwork } from "./useTargetNetwork"; import { Aptos } from "@aptos-labs/ts-sdk"; +import { useWallet } from "@aptos-labs/wallet-adapter-react"; import { UseQueryResult, useQuery } from "@tanstack/react-query"; import { Types } from "aptos"; import { useAptosClient } from "~~/hooks/scaffold-move"; @@ -15,33 +16,41 @@ export function getAccountResource( ledgerVersion?: number; }, client: Aptos, -): Promise { - const { address, moduleAddress, moduleName, resourceName } = requestParameters; +): Promise { + const { address, moduleAddress, moduleName, resourceName, ledgerVersion } = requestParameters; const resourceType: `${string}::${string}::${string}` = `${moduleAddress}::${moduleName}::${resourceName}`; - // let ledgerVersionBig; - // if (ledgerVersion !== undefined) { - // ledgerVersionBig = BigInt(ledgerVersion); - // } - return withResponseError(client.getAccountResource({ accountAddress: address, resourceType })); + let ledgerVersionBig; + if (ledgerVersion !== undefined) { + ledgerVersionBig = BigInt(ledgerVersion); + } + return withResponseError( + client.getAccountResource({ accountAddress: address, resourceType, options: { ledgerVersion: ledgerVersionBig } }), + ); } export function useGetAccountResource( - address: string, moduleName: string, resourceName: string, + address?: string, options?: { retry?: number | boolean; }, -): UseQueryResult { +): UseQueryResult { const network = useTargetNetwork(); const aptosClient = useAptosClient(network.targetNetwork.id); const moduleAddress = useGetModule(moduleName)?.abi.address ?? ""; + const { account } = useWallet(); - const test = useQuery, ResponseError>({ - queryKey: ["accountResource", { address, moduleAddress, moduleName, resourceName }], - queryFn: () => getAccountResource({ address, moduleAddress, moduleName, resourceName }, aptosClient), + // If address is not provided, use the wallet address + // Default to empty string if account is not connected + // Empty string will lead to ResponseError + const resourceAddress = address || account?.address || ""; + + return useQuery({ + queryKey: ["accountResource", { address: resourceAddress, moduleAddress, moduleName, resourceName }], + queryFn: () => + getAccountResource({ address: resourceAddress, moduleAddress, moduleName, resourceName }, aptosClient), retry: options?.retry ?? false, }); - return test; } diff --git a/packages/nextjs/hooks/scaffold-move/useGetAccountResources.ts b/packages/nextjs/hooks/scaffold-move/useGetAccountResources.ts index 65d8fc7..5983c3a 100644 --- a/packages/nextjs/hooks/scaffold-move/useGetAccountResources.ts +++ b/packages/nextjs/hooks/scaffold-move/useGetAccountResources.ts @@ -1,6 +1,7 @@ import { ResponseError, withResponseError } from "../client"; import { useTargetNetwork } from "./useTargetNetwork"; import { Aptos } from "@aptos-labs/ts-sdk"; +import { useWallet } from "@aptos-labs/wallet-adapter-react"; import { UseQueryResult, useQuery } from "@tanstack/react-query"; import { Types } from "aptos"; import { useAptosClient } from "~~/hooks/scaffold-move"; @@ -14,22 +15,29 @@ export function getAccountResources( if (ledgerVersion !== undefined) { ledgerVersionBig = BigInt(ledgerVersion); } - return withResponseError(client.getAccountResources({ accountAddress: address })); + return withResponseError( + client.getAccountResources({ accountAddress: address, options: { ledgerVersion: ledgerVersionBig } }), + ); } export function useGetAccountResources( - address: string, + address?: string, options?: { retry?: number | boolean; }, ): UseQueryResult { const network = useTargetNetwork(); const aptosClient = useAptosClient(network.targetNetwork.id); + const { account } = useWallet(); - const test = useQuery, ResponseError>({ - queryKey: ["accountResources", { address }], - queryFn: () => getAccountResources({ address }, aptosClient), + // If address is not provided, use the wallet address + // Default to empty string if account is not connected + // Empty string will lead to ResponseError + const resourceAddress = address || account?.address || ""; + + return useQuery, ResponseError>({ + queryKey: ["accountResources", { address: resourceAddress }], + queryFn: () => getAccountResources({ address: resourceAddress }, aptosClient), retry: options?.retry ?? false, }); - return test; } diff --git a/packages/nextjs/utils/scaffold-move/module.ts b/packages/nextjs/utils/scaffold-move/module.ts index 1fa22f9..b106807 100644 --- a/packages/nextjs/utils/scaffold-move/module.ts +++ b/packages/nextjs/utils/scaffold-move/module.ts @@ -1,7 +1,4 @@ - -import { - AbiParameter, -} from "abitype"; +import { AbiParameter } from "abitype"; import { Types } from "aptos"; import type { MergeDeepRecord } from "type-fest/source/merge-deep"; import deployedModulesData from "~~/modules/deployedModules";