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

fix: support save accounts in lite #976

Merged
merged 3 commits into from
Jun 19, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Bug fixes

- [#976](https://github.com/alleslabs/celatone-frontend/pull/976) Support save accounts in lite version
- [#944](https://github.com/alleslabs/celatone-frontend/pull/944) Fix asset input selector
- [#979](https://github.com/alleslabs/celatone-frontend/pull/979) Fix txs detail receiver overflow screen
- [#980](https://github.com/alleslabs/celatone-frontend/pull/980) Pass through block in search in lite
Expand Down
1 change: 1 addition & 0 deletions src/lib/app-provider/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum CELATONE_QUERY_KEYS {
// ACCOUNT
ACCOUNT_DATA = "CELATONE_QUERY_ACCOUNT_DATA",
ACCOUNT_TYPE = "CELATONE_QUERY_ACCOUNT_TYPE",
ACCOUNT_TYPE_LCD = "CELATONE_QUERY_ACCOUNT_TYPE_LCD",
// ASSET
ASSET_INFOS = "CELATONE_QUERY_ASSET_INFOS",
ASSET_INFO_LIST = "CELATONE_QUERY_ASSET_INFO_LIST",
Expand Down
40 changes: 27 additions & 13 deletions src/lib/components/modal/account/SaveNewAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
useCelatoneApp,
useExampleAddresses,
useMoveConfig,
useTierConfig,
useValidateAddress,
useWasmConfig,
} from "lib/app-provider";
Expand All @@ -17,7 +18,7 @@ import { ControllerInput, ControllerTextarea } from "lib/components/forms";
import { useGetMaxLengthError, useHandleAccountSave } from "lib/hooks";
import { useFormatAddresses } from "lib/hooks/useFormatAddresses";
import { useAccountStore } from "lib/providers/store";
import { useAccountType } from "lib/services/account";
import { useAccountType, useAccountTypeLcd } from "lib/services/account";
import type { BechAddr } from "lib/types";
import { AccountType } from "lib/types";

Expand Down Expand Up @@ -48,6 +49,7 @@ export function SaveNewAccountModal({
publicDescription,
}: SaveNewAccountModalProps) {
const { constants } = useCelatoneApp();
const isFullTier = useTierConfig() === "full";
const { user: exampleUserAddress } = useExampleAddresses();
const { isSomeValidAddress } = useValidateAddress();
const formatAddresses = useFormatAddresses();
Expand Down Expand Up @@ -94,21 +96,33 @@ export function SaveNewAccountModal({
setStatus({ state: "error", message });
};

const { refetch } = useAccountType(addressState, {
const onSuccess = (type: AccountType) => {
if (type !== AccountType.ContractAccount) setStatus(statusSuccess);
else {
setErrorStatus("You need to save contract through Contract page.");
setIsContract(true);
}
};

const onError = (err: Error) => {
resetForm(false);
setErrorStatus(err.message);
};

const { refetch: refetchLite } = useAccountTypeLcd(addressState, {
enabled: false,
onSuccess: (type) => {
if (type !== AccountType.ContractAccount) setStatus(statusSuccess);
else {
setErrorStatus("You need to save contract through Contract page.");
setIsContract(true);
}
},
onError: (err) => {
resetForm(false);
setErrorStatus(err.message);
},
onSuccess,
onError,
});

const { refetch: refetchFull } = useAccountType(addressState, {
enabled: false,
onSuccess,
onError,
});

const refetch = isFullTier ? refetchFull : refetchLite;

useEffect(() => {
if (addressState.trim().length === 0) {
setStatus({
Expand Down
2 changes: 1 addition & 1 deletion src/lib/services/account/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const useAccountType = (
const queryFn = useCallback(async () => {
if (!address)
throw new Error(
"Error fetching account type: failed to retrieve address."
"Fetching account type: failed to retrieve address. (useAccountType)"
);
return indexerGraphClient
.request(getAccountTypeByAddressQueryDocument, {
Expand Down
34 changes: 32 additions & 2 deletions src/lib/services/account/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { UseQueryOptions, UseQueryResult } from "@tanstack/react-query";
import { useQuery } from "@tanstack/react-query";
import { useCallback } from "react";

import type { AccountData, AccountTableCounts } from "../types";
import {
Expand All @@ -10,10 +11,10 @@ import {
useTierConfig,
useWasmConfig,
} from "lib/app-provider";
import type { BechAddr } from "lib/types";
import type { AccountType, BechAddr, Option } from "lib/types";

import { getAccountData, getAccountTableCounts } from "./api";
import { getAccountDataLcd } from "./lcd";
import { getAccountDataLcd, getAccountTypeLcd } from "./lcd";

export const useAccountData = (
address: BechAddr
Expand Down Expand Up @@ -54,4 +55,33 @@ export const useAccountTableCounts = (
);
};

export const useAccountTypeLcd = (
address: Option<BechAddr>,
options: Pick<
UseQueryOptions<AccountType, Error>,
"enabled" | "onSuccess" | "onError"
> = {}
): UseQueryResult<AccountType> => {
const lcdEndpoint = useLcdEndpoint();

const queryFn = useCallback(async () => {
if (!address)
throw new Error(
"Fetching account type: failed to retrieve address. (useAccountTypeLcd)"
);

return getAccountTypeLcd(lcdEndpoint, address);
}, [lcdEndpoint, address]);

return useQuery(
[CELATONE_QUERY_KEYS.ACCOUNT_TYPE_LCD, lcdEndpoint, address],
queryFn,
{
...options,
retry: 1,
refetchOnWindowFocus: false,
}
);
};

export * from "./gql";
20 changes: 20 additions & 0 deletions src/lib/services/account/lcd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
import axios from "axios";

import { getIcnsNamesByAddressLcd } from "../name/lcd";
import { zAccountTypeLcd } from "../types";
import type { AccountData } from "../types";
import { AccountType } from "lib/types";
import type { BechAddr } from "lib/types";
import { parseWithError } from "lib/utils";

export const getAccountDataLcd = async (
endpoint: string,
Expand All @@ -18,3 +21,20 @@ export const getAccountDataLcd = async (
icns,
};
};

export const getAccountTypeLcd = async (
endpoint: string,
address: BechAddr
) => {
const [accountTypeLcd, isContract] = await Promise.all([
axios
.get(`${endpoint}/cosmos/auth/v1beta1/accounts/${encodeURI(address)}`)
.then(({ data }) => parseWithError(zAccountTypeLcd, data)),
axios
.get(`${endpoint}/cosmwasm/wasm/v1/contract/${encodeURI(address)}`)
.then(() => true)
.catch(() => false),
]);

return isContract ? AccountType.ContractAccount : accountTypeLcd;
};
10 changes: 9 additions & 1 deletion src/lib/services/types/account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "zod";

import { zProjectInfo, zPublicAccountInfo } from "lib/types";
import { AccountTypeLcd, zProjectInfo, zPublicAccountInfo } from "lib/types";
import { snakeToCamel } from "lib/utils";

const zIcns = z.object({
Expand Down Expand Up @@ -29,3 +29,11 @@ export const zAccountTableCounts = z
.transform(snakeToCamel);

export type AccountTableCounts = z.infer<typeof zAccountTableCounts>;

export const zAccountTypeLcd = z
.object({
account: z.object({
"@type": z.nativeEnum(AccountTypeLcd),
}),
})
.transform((data) => data.account["@type"]);
5 changes: 5 additions & 0 deletions src/lib/types/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export enum AccountType {
BaseVestingAccount = "BaseVestingAccount",
}

export enum AccountTypeLcd {
BaseAccount = "/cosmos.auth.v1beta1.BaseAccount",
ModuleAccount = "/cosmos.auth.v1beta1.ModuleAccount",
}

export const zPubkey = z.object({
"@type": z.string(),
key: z.string(),
Expand Down
Loading