Skip to content

Commit

Permalink
Merge pull request #1518 from oasisprotocol/mz/stats
Browse files Browse the repository at this point in the history
Sync with validators API changes and provide data to snapshot cards
  • Loading branch information
buberdds authored Aug 29, 2024
2 parents 6dd3a44 + ee44ac4 commit ce00286
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 72 deletions.
1 change: 1 addition & 0 deletions .changelog/1518.trivial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Provide missing data to validator snapshot cards
9 changes: 5 additions & 4 deletions src/app/components/Validators/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FC } from 'react'
import { useTranslation } from 'react-i18next'
import Box from '@mui/material/Box'
import { Table, TableCellAlign, TableColProps } from '../../components/Table'
import { Validator } from '../../../oasis-nexus/api'
import { Validator, ValidatorAggStats } from '../../../oasis-nexus/api'
import { TablePaginationProps } from '../Table/TablePagination'
import { StatusIcon } from '../StatusIcon'
import { RoundedBalance } from '../RoundedBalance'
Expand All @@ -18,9 +18,10 @@ type ValidatorsProps = {
isLoading: boolean
limit: number
pagination: false | TablePaginationProps
stats: ValidatorAggStats | undefined
}

export const Validators: FC<ValidatorsProps> = ({ isLoading, limit, pagination, validators }) => {
export const Validators: FC<ValidatorsProps> = ({ isLoading, limit, pagination, validators, stats }) => {
const { t } = useTranslation()
const { network } = useRequiredScopeParam()

Expand Down Expand Up @@ -71,9 +72,9 @@ export const Validators: FC<ValidatorsProps> = ({ isLoading, limit, pagination,
align: TableCellAlign.Right,
content: (
<>
{typeof validator?.voting_power === 'number' && validator?.voting_power_total > 0
{typeof validator?.voting_power === 'number' && stats?.total_voting_power
? t('common.valuePair', {
value: validator.voting_power / validator.voting_power_total,
value: validator.voting_power / stats.total_voting_power,
formatParams: {
value: {
style: 'percent',
Expand Down
11 changes: 8 additions & 3 deletions src/app/pages/ConsensusDashboardPage/ConsensusSnapshot.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import { FC } from 'react'
import { useTranslation } from 'react-i18next'
import { SearchScope } from '../../../types/searchScope'
import { useGetConsensusValidators } from '../../../oasis-nexus/api'
import { Snapshot, StyledGrid } from '../../components/Snapshots/Snapshot'
import { API_MAX_TOTAL_COUNT } from '../../config'
import { SnapshotEpoch } from './SnapshotEpoch'
import { SnapshotDelegators } from './SnapshotDelegators'
import { SnapshotValidators } from './SnapshotValidators'
import { SnapshotStaked } from './SnapshotStaked'

export const ConsensusSnapshot: FC<{ scope: SearchScope }> = ({ scope }) => {
const { t } = useTranslation()
const validatorsQuery = useGetConsensusValidators(scope.network, { limit: API_MAX_TOTAL_COUNT })
const validators = validatorsQuery.data?.data.validators
const stats = validatorsQuery.data?.data.stats

return (
<Snapshot title={t('consensusSnapshot.title')} scope={scope}>
<StyledGrid item xs={22} md={5}>
<SnapshotEpoch scope={scope} />
</StyledGrid>
<StyledGrid item xs={22} md={6}>
<SnapshotValidators scope={scope} />
<SnapshotValidators validators={validators} />
</StyledGrid>
<StyledGrid item xs={22} md={5}>
<SnapshotDelegators />
<SnapshotDelegators totalDelegators={stats?.total_delegators} />
</StyledGrid>
<StyledGrid item xs={22} md={6}>
<SnapshotStaked />
<SnapshotStaked totalStaked={stats?.total_staked_balance} ticker={stats?.ticker} />
</StyledGrid>
</Snapshot>
)
Expand Down
10 changes: 6 additions & 4 deletions src/app/pages/ConsensusDashboardPage/SnapshotDelegators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import Box from '@mui/material/Box'
import { SnapshotTextCard } from '../../components/Snapshots/SnapshotCard'
import { useScreenSize } from '../../hooks/useScreensize'

export const SnapshotDelegators: FC = () => {
type SnapshotDelegatorsProps = {
totalDelegators: number | undefined
}

export const SnapshotDelegators: FC<SnapshotDelegatorsProps> = ({ totalDelegators }) => {
const { t } = useTranslation()
const { isMobile } = useScreenSize()
// TODO: provide delegators number when API is ready
const delegators = undefined

return (
<SnapshotTextCard title={t('validator.delegators')}>
<Box sx={{ pb: isMobile ? 3 : 5 }}>{delegators}</Box>
{totalDelegators && <Box sx={{ pb: isMobile ? 3 : 5 }}>{totalDelegators.toLocaleString()}</Box>}
</SnapshotTextCard>
)
}
15 changes: 10 additions & 5 deletions src/app/pages/ConsensusDashboardPage/SnapshotStaked.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import { Trans, useTranslation } from 'react-i18next'
import Box from '@mui/material/Box'
import Typography from '@mui/material/Typography'
import { COLORS } from '../../../styles/theme/colors'
import { Ticker } from '../../../types/ticker'
import { RoundedBalance } from '../../components/RoundedBalance'
import { SnapshotTextCard } from '../../components/Snapshots/SnapshotCard'

export const SnapshotStaked: FC = () => {
type SnapshotStakedProps = {
ticker: Ticker | undefined
totalStaked: string | undefined
}

export const SnapshotStaked: FC<SnapshotStakedProps> = ({ totalStaked, ticker }) => {
const { t } = useTranslation()
// TODO: provide label and staked values when API is ready, validate percentageValue formatting
const staked = undefined
// TODO: provide totalCirculation
const percentageValue = undefined

return (
Expand Down Expand Up @@ -43,9 +48,9 @@ export const SnapshotStaked: FC = () => {
)
}
>
{staked && (
{totalStaked && (
<Box sx={{ wordBreak: 'break-all', lineHeight: 1 }}>
<RoundedBalance value={staked} />
<RoundedBalance value={totalStaked} ticker={ticker} compactLargeNumbers />
</Box>
)}
</SnapshotTextCard>
Expand Down
14 changes: 6 additions & 8 deletions src/app/pages/ConsensusDashboardPage/SnapshotValidators.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { FC } from 'react'
import { useTranslation } from 'react-i18next'
import { TFunction } from 'i18next'
import { useGetConsensusValidators, Validator } from '../../../oasis-nexus/api'
import { SearchScope } from '../../../types/searchScope'
import { Validator } from '../../../oasis-nexus/api'
import { SnapshotCard } from '../../components/Snapshots/SnapshotCard'
import { PieChart } from '../../components/charts/PieChart'
import { API_MAX_TOTAL_COUNT } from '../../config'

type ValidatorsStats = {
label: string
Expand All @@ -22,12 +20,12 @@ function countValidatorsState(t: TFunction, validators: Validator[] | undefined)
]
}

export const SnapshotValidators: FC<{ scope: SearchScope }> = ({ scope }) => {
const { t } = useTranslation()
const { network } = scope
type SnapshotValidatorsProps = {
validators: Validator[] | undefined
}

const validatorsQuery = useGetConsensusValidators(network, { limit: API_MAX_TOTAL_COUNT })
const validators = validatorsQuery.data?.data.validators
export const SnapshotValidators: FC<SnapshotValidatorsProps> = ({ validators }) => {
const { t } = useTranslation()

return (
<SnapshotCard title={t('validator.listTitle')}>
Expand Down
1 change: 1 addition & 0 deletions src/app/pages/ConsensusDashboardPage/Validators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const ValidatorsCard: FC<{ scope: SearchScope }> = ({ scope }) => {
<CardContent>
<Validators
validators={validatorsQuery.data?.data.validators}
stats={validatorsQuery.data?.data.stats}
isLoading={validatorsQuery.isLoading}
limit={limit}
pagination={false}
Expand Down
3 changes: 2 additions & 1 deletion src/app/pages/ProposalDetailsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const VoteLoadingProblemIndicator: FC = () => {
}

export const ProposalDetailView: FC<{
proposal: Proposal
proposal: Proposal | undefined
highlightedPart?: string
isLoading?: boolean
totalVotesLoading?: boolean
Expand All @@ -91,6 +91,7 @@ export const ProposalDetailView: FC<{
const { t } = useTranslation()
const { isMobile } = useScreenSize()
if (isLoading) return <TextSkeleton numberOfRows={7} />
if (!proposal) return null

const proposalType = getTypeNameForProposal(t, proposal)

Expand Down
3 changes: 1 addition & 2 deletions src/app/pages/ProposalsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { LoadMoreButton } from '../../components/LoadMoreButton'
import { useRequiredScopeParam } from '../../hooks/useScopeParam'
import { NetworkProposalsList } from '../../components/NetworkProposalsList'
import { CardHeaderWithCounter } from '../../components/CardHeaderWithCounter'
import { ValidatorDetailsView } from '../ValidatorDetailsPage'
import { VerticalList } from '../../components/VerticalList'
import { ProposalDetailView } from '../ProposalDetailsPage'

Expand Down Expand Up @@ -81,7 +80,7 @@ export const ProposalsPage: FC = () => {
<VerticalList>
{isLoading &&
[...Array(PAGE_SIZE).keys()].map(key => (
<ValidatorDetailsView key={key} isLoading={true} validator={undefined} standalone />
<ProposalDetailView key={key} isLoading={true} proposal={undefined} standalone />
))}
{!isLoading &&
proposalsData?.proposals.map(proposal => (
Expand Down
9 changes: 5 additions & 4 deletions src/app/pages/ValidatorDetailsPage/ValidatorSnapshot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import { SearchScope } from '../../../types/searchScope'
import { UptimeCard } from './UptimeCard'
import { VotingPowerCard } from './VotingPowerCard'
import { BalanceDistributionCard } from './BalanceDistributionCard'
import { Validator } from '../../../oasis-nexus/api'
import { Validator, ValidatorAggStats } from '../../../oasis-nexus/api'
import { StyledGrid } from '../../components/Snapshots/Snapshot'

type ValidatorSnapshotProps = {
scope: SearchScope
validator?: Validator
validator: Validator | undefined
stats: ValidatorAggStats | undefined
}

export const ValidatorSnapshot: FC<ValidatorSnapshotProps> = ({ scope, validator }) => {
export const ValidatorSnapshot: FC<ValidatorSnapshotProps> = ({ scope, validator, stats }) => {
const { t } = useTranslation()

const theme = useTheme()
Expand All @@ -44,7 +45,7 @@ export const ValidatorSnapshot: FC<ValidatorSnapshotProps> = ({ scope, validator
<BalanceDistributionCard validator={validator} />
</StyledGrid>
<StyledGrid item xs={22} md={6}>
<VotingPowerCard validator={validator} />
<VotingPowerCard validator={validator} stats={stats} />
</StyledGrid>
<StyledGrid item xs={22} md={5}>
<UptimeCard />
Expand Down
13 changes: 7 additions & 6 deletions src/app/pages/ValidatorDetailsPage/VotingPowerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { FC } from 'react'
import { useTranslation } from 'react-i18next'
import Box from '@mui/material/Box'
import Typography from '@mui/material/Typography'
import { Validator } from '../../../oasis-nexus/api'
import { Validator, ValidatorAggStats } from '../../../oasis-nexus/api'
import { SnapshotTextCard } from '../../components/Snapshots/SnapshotCard'
import { COLORS } from 'styles/theme/colors'
import { VerticalProgressBar } from 'app/components/ProgressBar'

type VotingPowerCardProps = {
validator?: Validator
validator: Validator | undefined
stats: ValidatorAggStats | undefined
}

export const VotingPowerCard: FC<VotingPowerCardProps> = ({ validator }) => {
export const VotingPowerCard: FC<VotingPowerCardProps> = ({ validator, stats }) => {
const { t } = useTranslation()

return (
Expand All @@ -26,14 +27,14 @@ export const VotingPowerCard: FC<VotingPowerCardProps> = ({ validator }) => {
}
withContentPadding={false}
>
{typeof validator?.voting_power === 'number' && validator?.voting_power_total > 0 && (
{typeof validator?.voting_power === 'number' && stats?.total_voting_power && (
<Box sx={{ display: 'flex', justifyContent: 'space-between' }}>
<Box sx={{ display: 'flex', flexDirection: 'column' }}>
<Typography sx={{ fontSize: 12, color: COLORS.grayMedium, textAlign: 'left', paddingBottom: 3 }}>
{t('validator.votingPowerOverall')}
</Typography>
{t('common.valuePair', {
value: validator.voting_power / validator.voting_power_total,
value: validator.voting_power / stats.total_voting_power,
formatParams: {
value: {
style: 'percent',
Expand All @@ -46,7 +47,7 @@ export const VotingPowerCard: FC<VotingPowerCardProps> = ({ validator }) => {
<VerticalProgressBar
height={80}
width={50}
value={(100 * validator.voting_power) / validator.voting_power_total}
value={(100 * validator.voting_power) / stats.total_voting_power}
barWithBorder={false}
barBackgroundColor={COLORS.grayMediumLight}
/>
Expand Down
21 changes: 12 additions & 9 deletions src/app/pages/ValidatorDetailsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Card from '@mui/material/Card'
import CardContent from '@mui/material/CardContent'
import Divider from '@mui/material/Divider'
import Grid from '@mui/material/Grid'
import { Validator, useGetConsensusValidatorsAddress } from '../../../oasis-nexus/api'
import { Validator, ValidatorAggStats, useGetConsensusValidatorsAddress } from '../../../oasis-nexus/api'
import { useScreenSize } from '../../hooks/useScreensize'
import { useFormattedTimestampStringWithDistance } from '../../hooks/useFormattedTimestamp'
import { RouterTabs } from '../../components/RouterTabs'
Expand Down Expand Up @@ -42,7 +42,8 @@ export const ValidatorDetailsPage: FC = () => {
const { address } = useLoaderData() as AddressLoaderData
const validatorQuery = useGetConsensusValidatorsAddress(scope.network, address)
const { isLoading, data } = validatorQuery
const validator = data?.data
const validator = data?.data.validators[0]
const stats = data?.data.stats
const transactionsLink = useHref('')
const eventsLink = useHref(`events#${eventsContainerId}`)
const delegatorsLink = useHref(`delegators#${delegatorsContainerId}`)
Expand All @@ -52,9 +53,9 @@ export const ValidatorDetailsPage: FC = () => {
return (
<PageLayout>
<ValidatorTitleCard isLoading={isLoading} network={scope.network} validator={validator} />
<ValidatorSnapshot scope={scope} validator={validator} />
<ValidatorSnapshot scope={scope} validator={validator} stats={stats} />
<Divider variant="layout" sx={{ mt: isMobile ? 4 : 0 }} />
<ValidatorDetailsCard isLoading={isLoading} validator={validator} />
<ValidatorDetailsCard isLoading={isLoading} validator={validator} stats={stats} />
<Grid container spacing={4}>
<StyledGrid item xs={12} md={6}>
<StakingTrend address={address} scope={scope} />
Expand All @@ -80,13 +81,14 @@ export const ValidatorDetailsPage: FC = () => {
type ValidatorDetailsCardProps = {
isLoading: boolean
validator: Validator | undefined
stats: ValidatorAggStats | undefined
}

const ValidatorDetailsCard: FC<ValidatorDetailsCardProps> = ({ isLoading, validator }) => {
const ValidatorDetailsCard: FC<ValidatorDetailsCardProps> = ({ isLoading, validator, stats }) => {
return (
<Card>
<CardContent>
<ValidatorDetailsView detailsPage isLoading={isLoading} validator={validator} />
<ValidatorDetailsView detailsPage isLoading={isLoading} validator={validator} stats={stats} />
</CardContent>
</Card>
)
Expand All @@ -97,7 +99,8 @@ export const ValidatorDetailsView: FC<{
isLoading?: boolean
validator: Validator | undefined
standalone?: boolean
}> = ({ detailsPage, isLoading, validator, standalone = false }) => {
stats: ValidatorAggStats | undefined
}> = ({ detailsPage, isLoading, validator, standalone = false, stats }) => {
const { t } = useTranslation()
const { isMobile } = useScreenSize()
const formattedTime = useFormattedTimestampStringWithDistance(validator?.start_date)
Expand Down Expand Up @@ -140,12 +143,12 @@ export const ValidatorDetailsView: FC<{
<dd>{validator.voting_power.toLocaleString()}</dd>
</>
)}
{typeof validator.voting_power === 'number' && validator.voting_power_total > 0 && (
{typeof validator.voting_power === 'number' && stats?.total_voting_power && (
<>
<dt>{t('validator.totalShare')}</dt>
<dd>
{t('common.valuePair', {
value: validator.voting_power / validator.voting_power_total,
value: validator.voting_power / stats.total_voting_power,
formatParams: {
value: {
style: 'percent',
Expand Down
16 changes: 14 additions & 2 deletions src/app/pages/ValidatorsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const ValidatorsPage: FC = () => {
>
{tableView === TableLayout.Horizontal && (
<Validators
stats={validatorsQuery.data?.data.stats}
validators={validatorsQuery.data?.data.validators}
isLoading={validatorsQuery.isLoading}
limit={PAGE_SIZE}
Expand All @@ -79,11 +80,22 @@ export const ValidatorsPage: FC = () => {
<VerticalList>
{isLoading &&
[...Array(PAGE_SIZE).keys()].map(key => (
<ValidatorDetailsView key={key} isLoading={true} validator={undefined} standalone />
<ValidatorDetailsView
key={key}
isLoading={true}
validator={undefined}
stats={undefined}
standalone
/>
))}
{!isLoading &&
validatorsData?.validators.map(validator => (
<ValidatorDetailsView key={validator.entity_address} validator={validator} standalone />
<ValidatorDetailsView
key={validator.entity_address}
validator={validator}
stats={validatorsQuery.data?.data.stats}
standalone
/>
))}
</VerticalList>
)}
Expand Down
Loading

0 comments on commit ce00286

Please sign in to comment.