Skip to content

Commit

Permalink
Use PolkadotAPI to show live chain data and Bug fixes in stats-dapp (#…
Browse files Browse the repository at this point in the history
…1566)

Co-authored-by: Trung-Tin Pham <[email protected]>
  • Loading branch information
devpavan04 and AtelyPham authored Aug 30, 2023
1 parent 702fb33 commit a006862
Show file tree
Hide file tree
Showing 17 changed files with 276 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import {
AuthorityQuery,
AuthorityStats,
DiscreteList,
AuthoritySet,
KeyGenKeyListItem,
useAuthority,
useAuthorityAccount,
Expand Down Expand Up @@ -92,7 +92,7 @@ const columns: ColumnDef<KeyGenKeyListItem, any>[] = [
/>
),
cell: (props) => {
const authorities = props.getValue<DiscreteList>();
const authorities = props.getValue<AuthoritySet>();
return (
<AvatarGroup total={authorities.count}>
{authorities.firstElements.map((au, idx) => (
Expand All @@ -107,7 +107,7 @@ const columns: ColumnDef<KeyGenKeyListItem, any>[] = [
columnHelper.accessor('id', {
header: '',
cell: (props) => {
const id = props.row.original.publicKey;
const id = props.row.original.id;
return (
<Link to={`/keys/drawer/${id}`}>
<Button variant="link" as="span" size="sm">
Expand Down
13 changes: 7 additions & 6 deletions apps/stats-dapp/src/containers/KeyDetail/KeyDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { Link, useNavigate, useParams } from 'react-router-dom';
import { KeyDetailProps, KeyGenAuthoredTableProps } from './types';
import { ECPairFactory } from 'ecpair';
import * as tinysecp from 'tiny-secp256k1';
import { POLKADOT_EXPLORER_URL } from '@webb-tools/webb-ui-components/constants';

export const KeyDetail = forwardRef<HTMLDivElement, KeyDetailProps>(
({ isPage }, ref) => {
Expand Down Expand Up @@ -221,8 +222,8 @@ export const KeyDetail = forwardRef<HTMLDivElement, KeyDetailProps>(
key={`${at.toString()}-${idx}`}
title={status}
time={at}
txHash={hash}
externalUrl="https://webb.tools" // TODO: Determine the external url
blockHash={hash}
externalUrl={POLKADOT_EXPLORER_URL + hash}
/>
);
}
Expand All @@ -233,8 +234,8 @@ export const KeyDetail = forwardRef<HTMLDivElement, KeyDetailProps>(
key={`${at.toString()}-${idx}`}
title={status}
time={at}
txHash={hash}
externalUrl="https://webb.tools" // TODO: Determine the external url
blockHash={hash}
externalUrl={POLKADOT_EXPLORER_URL + hash}
extraContent={
<div className="flex items-center space-x-2">
{/* <KeyValueWithButton
Expand All @@ -253,8 +254,8 @@ export const KeyDetail = forwardRef<HTMLDivElement, KeyDetailProps>(
key={`${at.toString()}-${idx}`}
title={status}
time={at}
txHash={hash}
externalUrl="https://webb.tools"
blockHash={hash}
externalUrl={POLKADOT_EXPLORER_URL + hash}
extraContent={
<div className="flex items-center space-x-4">
<LabelWithValue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,49 @@
import { KeyStatusCard } from '@webb-tools/webb-ui-components/components/KeyStatusCard';
import { KeyStatusCardProps } from '@webb-tools/webb-ui-components/components/KeyStatusCard/types';
import { Spinner } from '@webb-tools/icons';
import React, { FC, forwardRef, useMemo } from 'react';
import { KeyStatusCardContainerProps } from './types';
import { useStatsContext } from '../../provider/stats-provider';
import { PublicKey, useActiveKeys } from '../../provider/hooks';
import { useBlocks } from '../../provider/hooks';
import { useMemo } from 'react';

/**
* The wrapper of UI component. Handle logic and mapping fields between backend API and component API
*/
export const KeyStatusCardContainer = () => {
const { dkgDataFromPolkadotAPI, sessionHeight } = useStatsContext();

const { error, isFailed, isLoading, val: data } = useActiveKeys();

const { currentKey } = useMemo<{
currentKey: PublicKey | null | undefined;
}>(() => {
return {
currentKey: data ? data[0] : null,
};
}, [data]);
const {
dkgDataFromPolkadotAPI: {
currentSessionNumber,
currentKey,
currentSessionTimeFrame: { start: sessionStart, end: sessionEnd },
currentAuthorities,
},
} = useStatsContext();

const { time } = useStatsContext();

const authorities = useMemo(
() =>
(currentKey?.keyGenAuthorities ?? []).reduce((acc, cur) => {
acc.add(cur);
return acc;
}, new Set() as KeyStatusCardProps['authorities']) ?? new Set<string>(),
[data]
);
const { val: blocksData } = useBlocks();

const showDetails = useMemo(() => {
if (blocksData?.finalized && blocksData?.latestIndexedBlock) {
if (blocksData.finalized - blocksData.latestIndexedBlock < 5) {
return true;
}
}

const activeKeyData = useMemo(() => {
return {
key: currentKey?.compressed,
session: Number(currentKey?.session),
};
}, [data, currentKey]);
return false;
}, [blocksData]);

return (
<KeyStatusCard
title="Active Key"
titleInfo="The public key of the DKG protocol that is currently active."
instance={time}
sessionNumber={activeKeyData.session}
sessionNumber={currentSessionNumber}
keyType="current"
keyVal={activeKeyData.key ?? ''}
startTime={currentKey?.start ?? null}
endTime={currentKey?.end ?? null}
authorities={authorities ?? new Set<string>()}
totalAuthorities={authorities.size ?? 0}
fullDetailUrl={data ? `drawer/${currentKey?.id}` : ''}
keyVal={currentKey ?? ''}
startTime={sessionStart}
endTime={sessionEnd}
authorities={new Set<string>(currentAuthorities) ?? new Set<string>()}
totalAuthorities={0}
fullDetailUrl={currentKey ? `drawer/${currentKey}` : ''}
showDetails={showDetails}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,21 @@ import {
BreadcrumbsItem,
Chip,
} from '@webb-tools/webb-ui-components';
import { PublicKey, useActiveKeys, useBlocks } from '../../provider/hooks';
import { useBlocks } from '../../provider/hooks';
import { NavLink, useLocation } from 'react-router-dom';
import { useStatsContext } from '../../provider/stats-provider';

export const NavBoxInfoContainer = () => {
const location = useLocation();
const pathnames = location.pathname.split('/').filter((x) => x);
const currentPage = useMemo(() => pathnames[0], [pathnames]);
const subPage = useMemo(() => pathnames[1], [pathnames]);
const {
dkgDataFromPolkadotAPI: { currentSessionNumber },
} = useStatsContext();

const { val: keyData } = useActiveKeys();
const { val: blocksData } = useBlocks();

const { currentKey } = useMemo<{
currentKey: PublicKey | null | undefined;
}>(() => {
return {
currentKey: keyData ? keyData[0] : null, // current key
};
}, [keyData]);

const { bestBlock, finalizedBlock } = useMemo<{
bestBlock: number | null | undefined;
finalizedBlock: number | null | undefined;
Expand Down Expand Up @@ -124,8 +119,8 @@ export const NavBoxInfoContainer = () => {
</Chip>
<Chip color="blue">
<RefreshIcon size="lg" className="fill-blue-90 dark:fill-blue-30" />{' '}
{currentKey ? (
`Session: ${Number(currentKey?.session).toLocaleString()}`
{currentSessionNumber ? (
`Session: ${currentSessionNumber}`
) : (
<Spinner size="md" />
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const ProposalsTable = () => {
() => ({
offset: pagination.pageIndex * pageSize,
perPage: pagination.pageSize,
orderBy: ProposalBatchesOrderBy.IdDesc,
orderBy: ProposalBatchesOrderBy.BlockNumberDesc,
filter: null,
}),
[
Expand Down
22 changes: 16 additions & 6 deletions apps/stats-dapp/src/gql/metadata.graphql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
query MetaData {
sessions(first: 1, orderBy: [BLOCK_NUMBER_DESC] filter: {
publicKeyId: {
isNull: false
}
}) {
sessions(
first: 1
orderBy: [BLOCK_NUMBER_DESC]
filter: { publicKeyId: { isNull: false } }
) {
nodes {
id
blockNumber
Expand All @@ -16,7 +16,11 @@ query MetaData {
}

query LastBlock {
blocks(first: 1, filter: { timestamp: { isNull: false } }, orderBy: [NUMBER_DESC]) {
blocks(
first: 1
filter: { timestamp: { isNull: false } }
orderBy: [NUMBER_DESC]
) {
nodes {
timestamp
number
Expand All @@ -32,3 +36,9 @@ query countries {
}
}
}

query LatestIndexedBlock {
blocks {
totalCount
}
}
6 changes: 6 additions & 0 deletions apps/stats-dapp/src/gql/sessions.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,9 @@ query LatestThresholds(
}
}
}

query LatestSessionId {
sessions {
totalCount
}
}
Loading

0 comments on commit a006862

Please sign in to comment.