Skip to content

Commit

Permalink
update and implement getAccountResource hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
arjanjohan committed Sep 21, 2024
1 parent 4df89ea commit 888c6cb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 37 deletions.
30 changes: 16 additions & 14 deletions packages/nextjs/app/bio/page.tsx
Original file line number Diff line number Diff line change
@@ -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 }) => (
Expand All @@ -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<string>("");
const [inputBio, setInputBio] = useState<string>("");
Expand All @@ -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 <Alert message="Onchain Bio module not found!" />;
Expand All @@ -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();
}
Expand Down
35 changes: 22 additions & 13 deletions packages/nextjs/hooks/scaffold-move/useGetAccountResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -15,33 +16,41 @@ export function getAccountResource(
ledgerVersion?: number;
},
client: Aptos,
): Promise<Types.MoveResource[]> {
const { address, moduleAddress, moduleName, resourceName } = requestParameters;
): Promise<Types.MoveResource> {
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<Types.MoveResource[], ResponseError> {
): UseQueryResult<Types.MoveResource, ResponseError> {
const network = useTargetNetwork();
const aptosClient = useAptosClient(network.targetNetwork.id);
const moduleAddress = useGetModule(moduleName)?.abi.address ?? "";
const { account } = useWallet();

const test = useQuery<Array<Types.MoveResource>, 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<Types.MoveResource, ResponseError>({
queryKey: ["accountResource", { address: resourceAddress, moduleAddress, moduleName, resourceName }],
queryFn: () =>
getAccountResource({ address: resourceAddress, moduleAddress, moduleName, resourceName }, aptosClient),
retry: options?.retry ?? false,
});
return test;
}
20 changes: 14 additions & 6 deletions packages/nextjs/hooks/scaffold-move/useGetAccountResources.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<Types.MoveResource[], ResponseError> {
const network = useTargetNetwork();
const aptosClient = useAptosClient(network.targetNetwork.id);
const { account } = useWallet();

const test = useQuery<Array<Types.MoveResource>, 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<Array<Types.MoveResource>, ResponseError>({
queryKey: ["accountResources", { address: resourceAddress }],
queryFn: () => getAccountResources({ address: resourceAddress }, aptosClient),
retry: options?.retry ?? false,
});
return test;
}
5 changes: 1 addition & 4 deletions packages/nextjs/utils/scaffold-move/module.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down

0 comments on commit 888c6cb

Please sign in to comment.