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: add latest height query #926

Merged
merged 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- [#926](https://github.com/alleslabs/celatone-frontend/pull/926) Add latest height query
- [#919](https://github.com/alleslabs/celatone-frontend/pull/919) Remove singleStakingDenom config and use from lcd instead
- [#918](https://github.com/alleslabs/celatone-frontend/pull/918) Support lite version for delegation informations
- [#916](https://github.com/alleslabs/celatone-frontend/pull/916) Support lite version balances
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 @@ -28,6 +28,7 @@ export enum CELATONE_QUERY_KEYS {
// BLOCK
BLOCKS = "CELATONE_QUERY_BLOCKS",
BLOCK_DATA = "CELATONE_QUERY_BLOCK_DATA",
BLOCK_LATEST_HEIGHT = "CELATONE_QUERY_BLOCK_LATEST_HEIGHT",
evilpeach marked this conversation as resolved.
Show resolved Hide resolved
// CODE GQL
CODES = "CELATONE_QUERY_CODES",
CODES_LCD = "CELATONE_QUERY_CODES_LCD",
Expand Down
14 changes: 11 additions & 3 deletions src/lib/layout/InformationFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { Flex, Skeleton, Text } from "@chakra-ui/react";
import Link from "next/link";

import { AmpEvent, trackWebsite } from "lib/amplitude";
import { useTierConfig } from "lib/app-provider";
import type { IconKeys } from "lib/components/icon";
import { CustomIcon } from "lib/components/icon";
import { USER_GUIDE_DOCS_LINK } from "lib/data";
import { useLatestBlock } from "lib/services/block";
import { useOverviewsStats } from "lib/services/overviewService";

const FOOTER_BUTTONS = [
Expand All @@ -25,7 +27,13 @@ const FOOTER_BUTTONS = [
];

export const InformationFooter = () => {
const { data: overviewsStats, isLoading } = useOverviewsStats();
const isFullTier = useTierConfig() === "full";
const { data: overviewsStats, isLoading: isLoadingFull } =
useOverviewsStats(isFullTier);
const { data: latestHeight, isLoading: isLoadingLite } = useLatestBlock();

const latest = isFullTier ? overviewsStats?.latestBlock : latestHeight;
const isLoading = isFullTier ? isLoadingFull : isLoadingLite;

return (
<Flex
Expand All @@ -47,9 +55,9 @@ export const InformationFooter = () => {
<Flex gap={1} py={1} px={2} align="center">
<CustomIcon name="block" color="gray.600" boxSize={3} />
<Text variant="body3" color="text.dark">
evilpeach marked this conversation as resolved.
Show resolved Hide resolved
{overviewsStats?.latestBlock ?? "N/A"}
{latest ?? "N/A"}
</Text>
{overviewsStats && (
{latest && (
<Flex
w={2}
h={2}
Expand Down
10 changes: 4 additions & 6 deletions src/lib/pages/home/components/QuickMenuLite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const highlightCardProps = {
interface ShortcutMetadata {
title: string;
subtitle: string;
slug?: string;
slug: string;
icon: IconKeys;
isHighlight: boolean;
isDocument: boolean;
Expand Down Expand Up @@ -161,6 +161,7 @@ export const QuickMenuLite = () => {
{
title: "User Guide",
subtitle: "View Celatone documents",
slug: "wasm-user-guide",
icon: "document" as const,
isHighlight: false,
isDocument: true,
Expand Down Expand Up @@ -197,6 +198,7 @@ export const QuickMenuLite = () => {
title: "User Guide",
subtitle: "View Celatone documents",
icon: "document" as const,
slug: "move-user-guide",
isHighlight: false,
isDocument: true,
}
Expand Down Expand Up @@ -229,11 +231,7 @@ export const QuickMenuLite = () => {
<ContentCard item={item} isDocument={item.isDocument} />
</Link>
) : (
<AppLink
href={`/${item.slug}`}
key={item.slug}
style={{ height: "100%" }}
>
<AppLink href={`/${item.slug}`} style={{ height: "100%" }}>
<ContentCard item={item} isDocument={item.isDocument} />
</AppLink>
)}
Expand Down
9 changes: 4 additions & 5 deletions src/lib/pages/home/lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Flex, Heading, Spinner, Text } from "@chakra-ui/react";
import { useCelatoneApp, useMobile } from "lib/app-provider";
import { ConnectWalletAlert } from "lib/components/ConnectWalletAlert";
import PageContainer from "lib/components/PageContainer";
import { useOverviewsStats } from "lib/services/overviewService";
import { useLatestBlock } from "lib/services/block";

import { QuickMenuLite, QuickMenuMobileLite } from "./components";

Expand All @@ -14,8 +14,7 @@ export const HomeLite = () => {
theme,
} = useCelatoneApp();

// TODO: replace with latest block height data from lcd
const { data: overviewsStats, isLoading } = useOverviewsStats();
const { data: latestHeight, isLoading } = useLatestBlock();

return (
<PageContainer display="flex" alignItems="center">
Expand Down Expand Up @@ -55,7 +54,7 @@ export const HomeLite = () => {
<Spinner size="md" />
) : (
<Flex>
{overviewsStats && (
{latestHeight && (
<Flex
w={2}
h={2}
Expand All @@ -71,7 +70,7 @@ export const HomeLite = () => {
/>
)}
<Heading as="h5" variant="h5">
{overviewsStats?.latestBlock.toString() ?? "N/A"}
{latestHeight?.toString() ?? "N/A"}
</Heading>
</Flex>
)}
Expand Down
23 changes: 22 additions & 1 deletion src/lib/services/block/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { useQuery } from "@tanstack/react-query";
import type { UseQueryOptions } from "@tanstack/react-query";

import { CELATONE_QUERY_KEYS, useBaseApiRoute } from "lib/app-provider";
import {
CELATONE_QUERY_KEYS,
useBaseApiRoute,
useCelatoneApp,
useLcdEndpoint,
} from "lib/app-provider";
import type { BlocksResponse } from "lib/services/types";
import type { BlockData } from "lib/types";

import { getBlockData, getBlocks } from "./api";
import { getLatestBlockLcd } from "./lcd";

export const useBlocks = (
limit: number,
Expand Down Expand Up @@ -33,3 +39,18 @@ export const useBlockData = (height: number, enabled = true) => {
}
);
};

export const useLatestBlock = () => {
evilpeach marked this conversation as resolved.
Show resolved Hide resolved
const { currentChainId } = useCelatoneApp();
evilpeach marked this conversation as resolved.
Show resolved Hide resolved
const endpoint = useLcdEndpoint();

return useQuery(
[CELATONE_QUERY_KEYS.BLOCK_LATEST_HEIGHT, endpoint, currentChainId],
async () => getLatestBlockLcd(endpoint),
{
retry: false,
refetchOnWindowFocus: false,
cacheTime: 0,
}
);
};
10 changes: 10 additions & 0 deletions src/lib/services/block/lcd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import axios from "axios";

import { zBlockLcd } from "../types";
import { parseWithError } from "lib/utils";

export const getLatestBlockLcd = async (endpoint: string) => {
return axios
evilpeach marked this conversation as resolved.
Show resolved Hide resolved
.get(`${endpoint}/cosmos/base/tendermint/v1beta1/blocks/latest`)
.then(({ data }) => parseWithError(zBlockLcd, data).block.header.height);
};
5 changes: 4 additions & 1 deletion src/lib/services/overviewService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import { CELATONE_QUERY_KEYS, useBaseApiRoute } from "lib/app-provider";
import { getOverviewsStats } from "./overview";
import type { OverviewsStats } from "./overview";

export const useOverviewsStats = (): UseQueryResult<OverviewsStats> => {
export const useOverviewsStats = (
enabled: boolean = true
): UseQueryResult<OverviewsStats> => {
const endpoint = useBaseApiRoute("overviews");

return useQuery(
[CELATONE_QUERY_KEYS.OVERVIEWS_STATS, endpoint],
async () => getOverviewsStats(endpoint),
{
refetchOnWindowFocus: false,
enabled,
}
);
};
14 changes: 13 additions & 1 deletion src/lib/services/types/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from "zod";

import type { Block, BlockData, Validator } from "lib/types";
import { zUtcDate, zValidatorAddr } from "lib/types";
import { parseTxHash } from "lib/utils";
import { parseTxHash, snakeToCamel } from "lib/utils";

const zNullableValidator = z.nullable(
z
Expand Down Expand Up @@ -58,3 +58,15 @@ export const zBlockDataResponse = z
gasUsed: val.gas_used,
gasLimit: val.gas_limit,
}));

export const zBlockLcd = z
.object({
block: z.object({
header: z.object({
chain_id: z.string(),
height: z.coerce.number(),
// TODO: Fill in the rest of the block fields
}),
}),
})
.transform(snakeToCamel);
Loading