Skip to content

Commit

Permalink
feat: support lite for move
Browse files Browse the repository at this point in the history
  • Loading branch information
evilpeach committed Jun 13, 2024
1 parent 0ccdf30 commit a66da5f
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/lib/pages/account-details/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { UserDocsLink } from "lib/components/UserDocsLink";
import { useFormatAddresses } from "lib/hooks/useFormatAddresses";
import { useAccountData } from "lib/services/account";
import { useModulesByAddress } from "lib/services/move/module";
import { useResourcesByAddress } from "lib/services/move/resourceService";
import { useResourcesByAddressLcd } from "lib/services/move/resource";
import { useNftsCountByAccount } from "lib/services/nft";
import type { Addr, BechAddr, HexAddr, Option } from "lib/types";
import { truncate } from "lib/utils";
Expand Down Expand Up @@ -94,7 +94,7 @@ const AccountDetailsBody = ({
const { data: modulesData, isFetching: isModulesLoading } =
useModulesByAddress({ address: accountAddress });
const { data: resourcesData, isFetching: isResourceLoading } =
useResourcesByAddress(accountAddress);
useResourcesByAddressLcd(accountAddress);
// nft
const { data: nftsCount, isFetching: isNftsCountLoading } =
useNftsCountByAccount(hexAddress);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/pages/nft-collection-details/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useFormatAddresses } from "lib/hooks/useFormatAddresses";
import { useResourcesByAddress } from "lib/services/move/resourceService";
import { useResourcesByAddressLcd } from "lib/services/move/resource";
import type { HexAddr, HexAddr32, Option } from "lib/types";

interface SupplyData {
Expand All @@ -24,7 +24,7 @@ export const useCollectionInfos = (
): { collectionInfos: Option<CollectionInfos>; isLoading: boolean } => {
const formatAddress = useFormatAddresses();
const { address } = formatAddress(collectionAddress);
const { data: resourcesData, isFetching } = useResourcesByAddress(address);
const { data: resourcesData, isFetching } = useResourcesByAddressLcd(address);

if (!resourcesData)
return { collectionInfos: undefined, isLoading: isFetching };
Expand Down
2 changes: 1 addition & 1 deletion src/lib/services/move/module/lcd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const getModulesByAddressLcd = async (
`${endpoint}/initia/move/v1/accounts/${encodeURI(address)}/modules`,
{
params: {
"pagination.key": paginationKey ?? "",
"pagination.key": paginationKey,
},
}
)
Expand Down
29 changes: 0 additions & 29 deletions src/lib/services/move/resource.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useQuery } from "@tanstack/react-query";

import {
CELATONE_QUERY_KEYS,
useBaseApiRoute,
useLcdEndpoint,
useMoveConfig,
} from "lib/app-provider";
import type {
Expand All @@ -14,22 +14,22 @@ import type {
} from "lib/types";
import { truncate } from "lib/utils";

import { getAccountResources } from "./resource";
import { getAccountResourcesLcd } from "./lcd";

export interface ResourcesByAddressReturn {
groupedByOwner: ResourceGroupByAccount[];
groupedByName: ResourceGroup[];
totalCount: number;
}

export const useResourcesByAddress = (
export const useResourcesByAddressLcd = (
address: Addr
): UseQueryResult<ResourcesByAddressReturn> => {
const endpoint = useBaseApiRoute("accounts");
const endpoint = useLcdEndpoint();
const { enabled } = useMoveConfig({ shouldRedirect: false });

const queryFn: QueryFunction<ResourcesByAddressReturn> = () =>
getAccountResources(endpoint, address).then((resources) => {
getAccountResourcesLcd(endpoint, address).then((resources) => {
const groupedByOwner = resources.items.reduce<
Record<string, ResourceGroupByAccount>
>((acc, resource) => {
Expand Down
38 changes: 38 additions & 0 deletions src/lib/services/move/resource/lcd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import axios from "axios";

import { zResourcesResponseLcd } from "lib/services/types";
import type {
ResourceResponse,
ResourceResponseItem,
} from "lib/services/types";
import type { Addr, Nullable } from "lib/types";
import { parseWithError } from "lib/utils";

export const getAccountResourcesLcd = async (
endpoint: string,
address: Addr
): Promise<ResourceResponse> => {
const result: ResourceResponseItem[] = [];

const fetchFn = async (paginationKey: Nullable<string>) => {
const res = await axios
.get(
`${endpoint}/initia/move/v1/accounts/${encodeURI(address)}/resources`,
{
params: {
"pagination.key": paginationKey,
},
}
)
.then(({ data }) => parseWithError(zResourcesResponseLcd, data));
result.push(...res.resources);
if (res.pagination.nextKey) await fetchFn(res.pagination.nextKey);
};

await fetchFn(null);

return {
items: result.sort((a, b) => a.structTag.localeCompare(b.structTag)),
total: result.length,
};
};
1 change: 1 addition & 0 deletions src/lib/services/types/move/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./module";
export * from "./resource";
25 changes: 25 additions & 0 deletions src/lib/services/types/move/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { z } from "zod";

import { zHexAddr, zPagination } from "lib/types";
import { snakeToCamel } from "lib/utils";

const zResourcesResponseItem = z
.object({
address: zHexAddr,
move_resource: z.string(),
raw_bytes: z.string(),
struct_tag: z.string(),
})
.transform(snakeToCamel);
export type ResourceResponseItem = z.infer<typeof zResourcesResponseItem>;

export const zResourcesResponseLcd = z.object({
resources: z.array(zResourcesResponseItem),
pagination: zPagination,
});

export const zResourcesResponse = z.object({
items: z.array(zResourcesResponseItem),
total: z.number().nonnegative(),
});
export type ResourceResponse = z.infer<typeof zResourcesResponse>;

0 comments on commit a66da5f

Please sign in to comment.