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

[CFE-389]: Feat: add get all codes and contracts #1101

Merged
merged 6 commits into from
Aug 22, 2024
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- [#1101](https://github.com/alleslabs/celatone-frontend/pull/1101) Add get all codes and contracts APIs

### Improvements

- [#1093](https://github.com/alleslabs/celatone-frontend/pull/1093) Remove unused userkeys and adjust flex from string to number
Expand Down
3 changes: 2 additions & 1 deletion src/lib/app-provider/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export enum CELATONE_QUERY_KEYS {
CODES = "CELATONE_QUERY_CODES",
CODES_LCD = "CELATONE_QUERY_CODES_LCD",
CODES_BY_ADDRESS = "CELATONE_QUERY_CODES_BY_ADDRESS",
CODES_BY_WALLET_ADDRESS = "CELATONE_QUERY_CODES_BY_WALLET_ADDRESS",
ALL_CODES_BY_ADDRESS = "CELATONE_QUERY_ALL_CODES_BY_ADDRESS",
CODES_BY_IDS = "CELATONE_QUERY_CODES_BY_IDS",
CODE_DATA = "CELATONE_QUERY_CODE_DATA",
CODE_DATA_LCD = "CELATONE_QUERY_CODE_DATA_LCD",
Expand All @@ -43,6 +43,7 @@ export enum CELATONE_QUERY_KEYS {
CONTRACTS_BY_CODE_ID = "CELATONE_QUERY_CONTRACTS_BY_CODE_ID",
CONTRACTS_BY_CODE_ID_LCD = "CELATONE_QUERY_CONTRACTS_BY_CODE_ID_LCD",
INSTANTIATED_CONTRACTS_BY_ADDRESS = "CELATONE_QUERY_INSTANTIATED_CONTRACTS_BY_ADDRESS",
ALL_INSTANTIATED_CONTRACTS_BY_ADDRESS = "CELATONE_QUERY_ALL_INSTANTIATED_CONTRACTS_BY_ADDRESS",
INSTANTIATED_CONTRACTS_BY_ADDRESS_LCD = "CELATONE_QUERY_INSTANTIATED_CONTRACTS_BY_ADDRESS_LCD",
ADMIN_CONTRACTS_BY_ADDRESS = "CELATONE_QUERY_ADMIN_CONTRACTS_BY_ADDRESS",
ALL_ADMIN_CONTRACTS_BY_ADDRESS = "CELATONE_QUERY_ALL_ADMIN_CONTRACTS_BY_ADDRESS",
Expand Down
11 changes: 6 additions & 5 deletions src/lib/model/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import type { PermissionFilterValue } from "lib/hooks";
import { useCodePermissionFilter, useCodeSearchFilter } from "lib/hooks";
import { useCodeStore } from "lib/providers/store";
import {
useAllCodesByAddress,
useCodeListByCodeIds,
useCodeListByWalletAddress,
} from "lib/services/wasm/code";
import type { CodeInfo } from "lib/types";
import type { BechAddr, CodeInfo } from "lib/types";
import { AccessConfigPermission } from "lib/types";

const useStoredCodes = () => {
const { address } = useCurrentChain();
const { getCodeLocalInfo, isCodeIdSaved, isHydrated } = useCodeStore();

const { data: rawStoredCodes, isLoading } =
useCodeListByWalletAddress(address);
const { data: rawStoredCodes, isLoading } = useAllCodesByAddress(
address as BechAddr
);

const storedCodes =
rawStoredCodes?.map<CodeInfo>((code) => ({
rawStoredCodes?.items.map<CodeInfo>((code) => ({
...code,
name: getCodeLocalInfo(code.id)?.name,
isSaved: isCodeIdSaved(code.id),
Expand Down
4 changes: 2 additions & 2 deletions src/lib/model/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCurrentChain, useTierConfig } from "lib/app-provider";
import { INSTANTIATED_LIST_NAME } from "lib/data";
import { useContractStore } from "lib/providers/store";
import {
useInstantiatedContractsByAddress,
useAllInstantiatedContractsByAddress,
useInstantiatedContractsByAddressLcd,
} from "lib/services/wasm/contract";
import type { ContractListInfo } from "lib/stores/contract";
Expand All @@ -18,7 +18,7 @@ export const useInstantiatedByMe = (enable: boolean): InstantiatedByMeState => {
const { isFullTier } = useTierConfig();

const { data: dataApi, isLoading: isApiLoading } =
useInstantiatedContractsByAddress(address, 500, 0, enable && isFullTier);
useAllInstantiatedContractsByAddress(address, enable && isFullTier);
const { data: dataLcd, isLoading: isLcdLoading } =
useInstantiatedContractsByAddressLcd(address, enable && !isFullTier);

Expand Down
32 changes: 14 additions & 18 deletions src/lib/pages/contract-details/components/ContractTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const ContractTop = ({
gap={{ md: 4 }}
>
<Flex direction="column" gap={{ base: 1, md: 3 }} overflow="hidden">
<Flex gap={1} align="start" minH="36px">
<Flex gap={1} align="center">
<CustomIcon
name="contract-address"
boxSize={5}
Expand All @@ -150,23 +150,19 @@ export const ContractTop = ({
height={7}
/>
)}
<Flex alignItems="center" display="inline">
<Heading
as="h5"
mt={{ base: 1, md: 0 }}
ml={{ base: 1, md: 0 }}
variant={{ base: "h6", md: "h5" }}
wordBreak="break-word"
display="inline"
>
{displayName}
</Heading>
<WasmVerifyBadge
status={getWasmVerifyStatus(wasmVerifyInfo)}
relatedVerifiedCodes={wasmVerifyInfo?.relatedVerifiedCodes}
linkedCodeId={contract.codeId}
/>
</Flex>
<Heading
as="h5"
variant={{ base: "h6", md: "h5" }}
wordBreak="break-word"
display="inline"
>
{displayName}
</Heading>
<WasmVerifyBadge
status={getWasmVerifyStatus(wasmVerifyInfo)}
relatedVerifiedCodes={wasmVerifyInfo?.relatedVerifiedCodes}
linkedCodeId={contract.codeId}
/>
</Flex>
<Flex gap={{ base: 2, md: 1 }} direction="column">
<Flex
Expand Down
10 changes: 9 additions & 1 deletion src/lib/services/wasm/code/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ export const getCodesByAddress = async (
offset,
},
})
.then(({ data }) => zCodesResponse.parse(data));
.then(({ data }) => parseWithError(zCodesResponse, data));

export const getAllCodesByAddress = async (
endpoint: string,
address: BechAddr
): Promise<CodesResponse> =>
axios
.get(`${endpoint}/${encodeURIComponent(address)}/wasm/all-codes`)
.then(({ data }) => parseWithError(zCodesResponse, data));

export const getCodeData = async (
endpoint: string,
Expand Down
47 changes: 1 addition & 46 deletions src/lib/services/wasm/code/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,15 @@ import { useQuery } from "@tanstack/react-query";
import { useCallback } from "react";

import { CELATONE_QUERY_KEYS, useCelatoneApp } from "lib/app-provider";
import {
getCodeListByIDsQueryDocument,
getCodeListByUserQueryDocument,
} from "lib/query";
import { getCodeListByIDsQueryDocument } from "lib/query";
import type {
AccessConfigPermission,
BechAddr,
BechAddr20,
CodeInfo,
Option,
PermissionAddresses,
} from "lib/types";

export const useCodeListByWalletAddress = (
walletAddr: Option<BechAddr20>
): UseQueryResult<CodeInfo[]> => {
const { indexerGraphClient } = useCelatoneApp();
const queryFn = useCallback(async () => {
if (!walletAddr)
throw new Error("Wallet address not found (codeByUserQueryFn)");

return indexerGraphClient
.request(getCodeListByUserQueryDocument, {
walletAddr,
})
.then(({ codes }) =>
codes.map<CodeInfo>((code) => ({
id: code.id,
uploader: code.account.uploader as BechAddr,
contractCount: code.contracts_aggregate.aggregate?.count,
instantiatePermission:
code.access_config_permission as AccessConfigPermission,
permissionAddresses:
code.access_config_addresses as PermissionAddresses,
cw2Contract: code.cw2_contract,
cw2Version: code.cw2_version,
}))
);
}, [walletAddr, indexerGraphClient]);

return useQuery(
[
CELATONE_QUERY_KEYS.CODES_BY_WALLET_ADDRESS,
walletAddr,
indexerGraphClient,
],
queryFn,
{
keepPreviousData: true,
enabled: !!walletAddr,
}
);
};

export const useCodeListByCodeIds = (
ids: Option<number[]>
): UseQueryResult<CodeInfo[]> => {
Expand Down
22 changes: 21 additions & 1 deletion src/lib/services/wasm/code/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {
import type { Code, CodeData, CodesResponse } from "lib/services/types";
import type { BechAddr, BechAddr20, Option } from "lib/types";

import { getCodeData, getCodes, getCodesByAddress } from "./api";
import {
getAllCodesByAddress,
getCodeData,
getCodes,
getCodesByAddress,
} from "./api";
import { getCodeLcd, getCodesLcd, getUploadAccessParamsLcd } from "./lcd";

export const useUploadAccessParamsLcd = () => {
Expand Down Expand Up @@ -97,5 +102,20 @@ export const useCodesByAddress = (
);
};

export const useAllCodesByAddress = (
address: BechAddr
): UseQueryResult<CodesResponse> => {
const endpoint = useBaseApiRoute("accounts");

return useQuery(
[CELATONE_QUERY_KEYS.ALL_CODES_BY_ADDRESS, endpoint, address],
async () => getAllCodesByAddress(endpoint, address),
{
retry: 1,
refetchOnWindowFocus: false,
}
);
};

export * from "./gql";
export * from "./lcd";
8 changes: 8 additions & 0 deletions src/lib/services/wasm/contract/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ export const getInstantiatedContractsByAddress = async (
})
.then(({ data }) => parseWithError(zContractsResponse, data));

export const getAllInstantiatedContractsByAddress = async (
endpoint: string,
address: BechAddr
) =>
axios
.get(`${endpoint}/${encodeURI(address)}/wasm/all-instantiated-contracts`)
.then(({ data }) => parseWithError(zContractsResponse, data));

export const getAdminContractsByAddress = async (
endpoint: string,
address: BechAddr,
Expand Down
29 changes: 29 additions & 0 deletions src/lib/services/wasm/contract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
getAdminContractsByAddress,
getAdminsByContractAddresses,
getAllAdminContractsByAddress,
getAllInstantiatedContractsByAddress,
getContractData,
getContracts,
getContractsByCodeId,
Expand Down Expand Up @@ -150,6 +151,34 @@ export const useInstantiatedContractsByAddress = (
);
};

export const useAllInstantiatedContractsByAddress = (
address: Option<BechAddr>,
enabled = true
): UseQueryResult<ContractsResponse> => {
const endpoint = useBaseApiRoute("accounts");

return useQuery(
[
CELATONE_QUERY_KEYS.ALL_INSTANTIATED_CONTRACTS_BY_ADDRESS,
endpoint,
address,
],
async () => {
if (!address)
throw new Error(
"address not found (getAllInstantiatedContractsByAddress)"
);

return getAllInstantiatedContractsByAddress(endpoint, address);
},
{
enabled: Boolean(address) && enabled,
retry: 1,
refetchOnWindowFocus: false,
}
);
};

export const useAdminContractsByAddress = (
address: BechAddr,
limit: number,
Expand Down
Loading