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

feat: module info lcd #1000

Merged
merged 2 commits into from
Jul 2, 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 @@ -80,6 +80,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Improvements

- [#1000](https://github.com/alleslabs/celatone-frontend/pull/1000) Query module info from lcd directly
- [#998](https://github.com/alleslabs/celatone-frontend/pull/998) Use expression on Nft query
- [#996](https://github.com/alleslabs/celatone-frontend/pull/996) Allow disable voting period tally config
- [#960](https://github.com/alleslabs/celatone-frontend/pull/960) Adjust UI in contract and account detail page
Expand Down
19 changes: 11 additions & 8 deletions src/lib/pages/module-details/components/ModuleInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ import { Flex, Heading, Text } from "@chakra-ui/react";
import { CustomIcon } from "lib/components/icon";
import { ModuleSourceCode } from "lib/components/module";
import type { ModuleVerificationInternal } from "lib/services/types";
import type { ModuleData, Nullable, Option } from "lib/types";
import type {
IndexedModule,
ModulePublishInfo,
Nullish,
Option,
} from "lib/types";

import { ModuleInfoBody } from "./ModuleInfoBody";

export interface ModuleInfoProps {
verificationData: Option<Nullable<ModuleVerificationInternal>>;
moduleData: Partial<ModuleData>;
indexedModule: IndexedModule;
modulePublishInfo: Option<ModulePublishInfo>;
verificationData: Nullish<ModuleVerificationInternal>;
}

export const ModuleInfo = ({
verificationData,
moduleData,
}: ModuleInfoProps) => (
export const ModuleInfo = ({ verificationData, ...props }: ModuleInfoProps) => (
<Flex flexDirection="column" gap={4}>
<Flex justifyContent="space-between" alignItems="center" w="full">
<Heading as="h6" variant="h6" fontWeight={600}>
Expand All @@ -31,7 +34,7 @@ export const ModuleInfo = ({
</Flex>
)}
</Flex>
<ModuleInfoBody moduleData={moduleData} />
<ModuleInfoBody {...props} />
<ModuleSourceCode sourceCode={verificationData?.source} />
</Flex>
);
51 changes: 25 additions & 26 deletions src/lib/pages/module-details/components/ModuleInfoBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,38 @@ import { Grid } from "@chakra-ui/react";
import { useTierConfig } from "lib/app-provider";
import { ExplorerLink } from "lib/components/ExplorerLink";
import { LabelText } from "lib/components/LabelText";
import type { ModuleData } from "lib/types";
import { dateFromNow, formatUTC } from "lib/utils";

import type { ModuleInfoProps } from "./ModuleInfo";

const ModuleInfoBodyPublishedAndRepublished = ({
moduleData,
}: {
moduleData: Partial<ModuleData>;
}) => {
const { isRepublished, recentPublishTransaction, recentPublishProposal } =
moduleData;
const labelPrefix = isRepublished ? "Latest Republished" : "Published";
modulePublishInfo,
}: Pick<ModuleInfoProps, "modulePublishInfo">) => {
const labelPrefix = modulePublishInfo?.isRepublished
? "Latest Republished"
: "Published";

if (recentPublishTransaction) {
if (modulePublishInfo?.recentPublishTransaction) {
return (
<LabelText label={`${labelPrefix} Transaction`}>
<ExplorerLink
type="tx_hash"
value={recentPublishTransaction}
value={modulePublishInfo.recentPublishTransaction}
showCopyOnHover
/>
</LabelText>
);
}

if (recentPublishProposal) {
if (modulePublishInfo?.recentPublishProposal) {
return (
<LabelText
label={`${labelPrefix} Proposal ID`}
helperText1={recentPublishProposal.title}
helperText1={modulePublishInfo.recentPublishProposal.title}
>
<ExplorerLink
type="proposal_id"
value={recentPublishProposal.id.toString()}
value={modulePublishInfo.recentPublishProposal.id.toString()}
showCopyOnHover
/>
</LabelText>
Expand All @@ -48,15 +45,11 @@ const ModuleInfoBodyPublishedAndRepublished = ({
};

export const ModuleInfoBody = ({
moduleData,
indexedModule,
modulePublishInfo,
}: Omit<ModuleInfoProps, "verificationData">) => {
const isFullTier = useTierConfig() === "full";
const {
address,
upgradePolicy,
recentPublishBlockHeight,
recentPublishBlockTimestamp,
} = moduleData;
const { address, upgradePolicy } = indexedModule;

return (
<Grid
Expand All @@ -77,24 +70,30 @@ export const ModuleInfoBody = ({
</LabelText>
{isFullTier && (
<>
{recentPublishBlockTimestamp && (
{modulePublishInfo?.recentPublishBlockTimestamp && (
<LabelText
label="Published Block Height"
helperText1={formatUTC(recentPublishBlockTimestamp)}
helperText2={dateFromNow(recentPublishBlockTimestamp)}
helperText1={formatUTC(
modulePublishInfo.recentPublishBlockTimestamp
)}
helperText2={dateFromNow(
modulePublishInfo.recentPublishBlockTimestamp
)}
>
{recentPublishBlockHeight ? (
{modulePublishInfo?.recentPublishBlockHeight ? (
<ExplorerLink
type="block_height"
value={recentPublishBlockHeight.toString()}
value={modulePublishInfo.recentPublishBlockHeight.toString()}
showCopyOnHover
/>
) : (
"N/A"
)}
</LabelText>
)}
<ModuleInfoBodyPublishedAndRepublished moduleData={moduleData} />
<ModuleInfoBodyPublishedAndRepublished
modulePublishInfo={modulePublishInfo}
/>
</>
)}
</Grid>
Expand Down
15 changes: 8 additions & 7 deletions src/lib/pages/module-details/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { UserDocsLink } from "lib/components/UserDocsLink";
import { useFormatAddresses } from "lib/hooks/useFormatAddresses";
import {
useModuleByAddressLcd,
useModuleData,
useModulePublishInfo,
useModuleTableCounts,
useVerifyModule,
} from "lib/services/move/module";
Expand Down Expand Up @@ -50,13 +50,13 @@ const ModuleDetailsBody = ({
const isFullTier = useTierConfig() === "full";
const currentTab =
!isFullTier && tab === TabIndex.TxsHistories ? TabIndex.Overview : tab;
const fullData = useModuleData(vmAddress, moduleName, isFullTier);
const liteData = useModuleByAddressLcd({

const { data, isLoading: isModuleLoading } = useModuleByAddressLcd({
address: vmAddress,
moduleName,
options: { enabled: !isFullTier },
});
const { data, isLoading } = isFullTier ? fullData : liteData;
const { data: modulePublishInfo, isFetching: isPublishInfoLoading } =
useModulePublishInfo(vmAddress, moduleName, isFullTier);

const { data: moduleTableCounts } = useModuleTableCounts(
vmAddress,
Expand Down Expand Up @@ -115,7 +115,7 @@ const ModuleDetailsBody = ({
? Object.values(TabIndex)
: Object.values(TabIndex).filter((t) => t !== TabIndex.TxsHistories);

if (isLoading) return <Loading />;
if (isModuleLoading || isPublishInfoLoading) return <Loading />;
if (!data) return <ErrorFetching dataName="module information" />;

return (
Expand Down Expand Up @@ -187,8 +187,9 @@ const ModuleDetailsBody = ({
}}
/>
<ModuleInfo
indexedModule={data}
modulePublishInfo={modulePublishInfo}
verificationData={verificationData}
moduleData={data}
/>
{isFullTier && (
<ModuleTables
Expand Down
23 changes: 13 additions & 10 deletions src/lib/services/move/module/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import type {
} from "lib/services/types";
import {
zAccountModulesResponse,
zModuleDataResponse,
zModuleHistoriesResponse,
zModulePublishInfoResponse,
zModuleRelatedProposalsResponse,
zModulesResponse,
zModuleTableCountsResponse,
Expand All @@ -22,7 +22,7 @@ import type {
ExposedFunction,
HexAddr,
ModuleAbi,
ModuleData,
ModulePublishInfo,
Nullable,
} from "lib/types";
import {
Expand Down Expand Up @@ -98,19 +98,22 @@ export const getModules = async (
})
.then(({ data }) => parseWithError(zModulesResponse, data));

export const getModuleData = async (
export const getModulePublishInfo = async (
endpoint: string,
vmAddress: HexAddr,
moduleName: string,
isGov: boolean
): Promise<ModuleData> =>
): Promise<ModulePublishInfo> =>
axios
.get(`${endpoint}/${encodeURI(vmAddress)}/${encodeURI(moduleName)}/info`, {
params: {
is_gov: isGov,
},
})
.then(({ data }) => parseWithError(zModuleDataResponse, data));
.get(
`${endpoint}/${encodeURI(vmAddress)}/${encodeURI(moduleName)}/publish_info`,
{
params: {
is_gov: isGov,
},
}
)
.then(({ data }) => parseWithError(zModulePublishInfoResponse, data));

export const getModuleTableCounts = async (
endpoint: string,
Expand Down
10 changes: 5 additions & 5 deletions src/lib/services/move/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import type {
ExposedFunction,
HexAddr,
IndexedModule,
ModuleData,
ModulePublishInfo,
Nullable,
Option,
RpcQueryError,
Expand All @@ -42,8 +42,8 @@ import {
decodeModule,
decodeScript,
getFunctionView,
getModuleData,
getModuleHistories,
getModulePublishInfo,
getModuleRelatedProposals,
getModules,
getModulesByAddress,
Expand Down Expand Up @@ -251,15 +251,15 @@ export const useModules = (
);
};

export const useModuleData = (
export const useModulePublishInfo = (
vmAddress: HexAddr,
moduleName: string,
enabled = true
) => {
const endpoint = useBaseApiRoute("modules");
const govConfig = useGovConfig({ shouldRedirect: false });

return useQuery<ModuleData>(
return useQuery<ModulePublishInfo>(
[
CELATONE_QUERY_KEYS.MODULE_DATA,
endpoint,
Expand All @@ -268,7 +268,7 @@ export const useModuleData = (
govConfig.enabled,
],
async () =>
getModuleData(endpoint, vmAddress, moduleName, govConfig.enabled),
getModulePublishInfo(endpoint, vmAddress, moduleName, govConfig.enabled),
{
retry: 1,
refetchOnWindowFocus: false,
Expand Down
14 changes: 6 additions & 8 deletions src/lib/services/types/move/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type {
HexAddr,
IndexedModule,
ModuleAbi,
ModuleData,
ModuleInfo,
ModulePublishInfo,
Option,
} from "lib/types";
import {
Expand Down Expand Up @@ -84,20 +84,18 @@ export const zModulesResponse = z.object({
});
export type ModulesResponse = z.infer<typeof zModulesResponse>;

export const zModuleDataResponse = zBaseModuleLcd
.extend({
export const zModulePublishInfoResponse = z
.object({
recent_publish_transaction: z.string().nullable(),
recent_publish_proposal: zProposal
.pick({ id: true, title: true })
.nullish()
.default(null),
.nullish(),
recent_publish_block_height: z.number().nonnegative(),
recent_publish_block_timestamp: zUtcDate,
is_republished: z.boolean(),
})
.transform<ModuleData>((val) => ({
.transform<ModulePublishInfo>((val) => ({
...snakeToCamel(val),
...indexModuleAbi(val.abi),
recentPublishTransaction: val.recent_publish_transaction
? parseTxHash(val.recent_publish_transaction)
: null,
Expand All @@ -106,7 +104,7 @@ export const zModuleDataResponse = zBaseModuleLcd
export const zModuleTableCountsResponse = z.object({
txs: z.number().nonnegative().nullable(),
histories: z.number().nonnegative().nullable(),
proposals: z.number().nonnegative().nullish().default(null),
proposals: z.number().nonnegative().nullish(),
});
export type ModuleTableCountsResponse = z.infer<
typeof zModuleTableCountsResponse
Expand Down
4 changes: 2 additions & 2 deletions src/lib/types/move/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export interface ModuleInfo
isVerified?: boolean;
}

export interface ModuleData extends IndexedModule {
export interface ModulePublishInfo {
recentPublishTransaction: Nullable<string>;
recentPublishProposal: Nullable<Pick<Proposal, "id" | "title">>;
recentPublishProposal?: Nullable<Pick<Proposal, "id" | "title">>;
recentPublishBlockHeight: number;
recentPublishBlockTimestamp: Date;
isRepublished: boolean;
Expand Down
Loading