Skip to content

Commit 9176c93

Browse files
authored
Merge pull request #1524 from oasisprotocol/mz/cumulativeVoting
Provide data to render cumulative voting
2 parents 9ac3f49 + 79befc4 commit 9176c93

8 files changed

Lines changed: 70 additions & 54 deletions

File tree

.changelog/1524.trivial.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Provide data to render cumulative voting
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { FC } from 'react'
2+
import { useTranslation } from 'react-i18next'
3+
4+
type PercentageValueProps = {
5+
adaptMaximumFractionDigits?: boolean
6+
total: number | undefined
7+
value: number | undefined
8+
}
9+
10+
export const PercentageValue: FC<PercentageValueProps> = ({ adaptMaximumFractionDigits, value, total }) => {
11+
const { t } = useTranslation()
12+
13+
if (typeof value !== 'number' || typeof total !== 'number' || total <= 0) {
14+
return null
15+
}
16+
17+
const percentageValue = value / total
18+
19+
return (
20+
<>
21+
{t('common.valuePair', {
22+
value: percentageValue,
23+
formatParams: {
24+
value: {
25+
style: 'percent',
26+
maximumFractionDigits: adaptMaximumFractionDigits && percentageValue < 0.001 ? 3 : 2,
27+
} satisfies Intl.NumberFormatOptions,
28+
},
29+
})}
30+
</>
31+
)
32+
}
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
11
import { FC } from 'react'
2-
import { useTranslation } from 'react-i18next'
2+
import { PercentageValue } from '../PercentageValue'
33

44
type ValidatorCommissionProps = {
55
commission: number
66
}
77

88
export const ValidatorCommission: FC<ValidatorCommissionProps> = ({ commission }) => {
9-
const { t } = useTranslation()
10-
11-
return (
12-
<>
13-
{t('common.valuePair', {
14-
value: commission / 100000,
15-
formatParams: {
16-
value: {
17-
style: 'percent',
18-
maximumFractionDigits: 2,
19-
} satisfies Intl.NumberFormatOptions,
20-
},
21-
})}
22-
</>
23-
)
9+
return <PercentageValue value={commission} total={100000} />
2410
}

src/app/components/Validators/ValidatorCumulativeVoting.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { FC } from 'react'
22
import Box from '@mui/material/Box'
33
import Typography from '@mui/material/Typography'
44
import { styled } from '@mui/material/styles'
5+
import { PercentageValue } from '../PercentageValue'
56

67
const StyledBox = styled(Box, {
78
shouldForwardProp: prop => prop !== 'value' && prop !== 'containerMarginThemeSpacing',
8-
})<ValidatorCumulativeVotingProps>(({ containerMarginThemeSpacing, value, theme }) => ({
9+
})<Omit<ValidatorCumulativeVotingProps, 'total'>>(({ containerMarginThemeSpacing, value, theme }) => ({
910
position: 'relative',
1011
textAlign: 'center',
1112
'&::before': {
@@ -22,18 +23,24 @@ const StyledBox = styled(Box, {
2223

2324
type ValidatorCumulativeVotingProps = {
2425
containerMarginThemeSpacing: number
25-
value: number
26+
value: number | undefined
27+
total: number | undefined
2628
}
2729

2830
export const ValidatorCumulativeVoting: FC<ValidatorCumulativeVotingProps> = ({
2931
containerMarginThemeSpacing,
3032
value,
33+
total,
3134
}) => {
35+
if (typeof value !== 'number' || typeof total !== 'number' || total <= 0) {
36+
return null
37+
}
38+
3239
return (
3340
<Box sx={{ flex: 1 }}>
34-
<StyledBox value={value} containerMarginThemeSpacing={containerMarginThemeSpacing}>
41+
<StyledBox value={(value / total) * 100} containerMarginThemeSpacing={containerMarginThemeSpacing}>
3542
<Typography component="span" sx={{ position: 'relative', zIndex: 2 }}>
36-
{value ? value : '-'}
43+
<PercentageValue value={value} total={total} />
3744
</Typography>
3845
</StyledBox>
3946
</Box>

src/app/components/Validators/index.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { ValidatorCumulativeVoting } from './ValidatorCumulativeVoting'
1212
import { ValidatorLink } from './ValidatorLink'
1313
import { useRequiredScopeParam } from '../../hooks/useScopeParam'
1414
import { BalancesDiff } from '../BalancesDiff'
15+
import { PercentageValue } from '../PercentageValue'
1516

1617
type ValidatorsProps = {
1718
validators?: Validator[]
@@ -64,26 +65,23 @@ export const Validators: FC<ValidatorsProps> = ({ isLoading, limit, pagination,
6465
},
6566
{
6667
align: TableCellAlign.Right,
67-
// TODO: provide cumulative voting when it is implemented in the API
68-
content: <ValidatorCumulativeVoting containerMarginThemeSpacing={5} value={0} />,
68+
content: (
69+
<ValidatorCumulativeVoting
70+
containerMarginThemeSpacing={5}
71+
value={validator.voting_power_cumulative}
72+
total={stats?.total_voting_power}
73+
/>
74+
),
6975
key: 'cumulativeVoting',
7076
},
7177
{
7278
align: TableCellAlign.Right,
7379
content: (
74-
<>
75-
{typeof validator?.voting_power === 'number' && stats?.total_voting_power
76-
? t('common.valuePair', {
77-
value: validator.voting_power / stats.total_voting_power,
78-
formatParams: {
79-
value: {
80-
style: 'percent',
81-
maximumFractionDigits: 2,
82-
} satisfies Intl.NumberFormatOptions,
83-
},
84-
})
85-
: t('common.missing')}
86-
</>
80+
<PercentageValue
81+
value={validator.voting_power}
82+
total={stats?.total_voting_power}
83+
adaptMaximumFractionDigits
84+
/>
8785
),
8886

8987
key: 'voting',

src/app/pages/ValidatorDetailsPage/VotingPowerCard.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Validator, ValidatorAggStats } from '../../../oasis-nexus/api'
66
import { SnapshotTextCard } from '../../components/Snapshots/SnapshotCard'
77
import { COLORS } from 'styles/theme/colors'
88
import { VerticalProgressBar } from 'app/components/ProgressBar'
9+
import { PercentageValue } from '../../components/PercentageValue'
910

1011
type VotingPowerCardProps = {
1112
validator: Validator | undefined
@@ -33,15 +34,7 @@ export const VotingPowerCard: FC<VotingPowerCardProps> = ({ validator, stats })
3334
<Typography sx={{ fontSize: 12, color: COLORS.grayMedium, textAlign: 'left', paddingBottom: 3 }}>
3435
{t('validator.votingPowerOverall')}
3536
</Typography>
36-
{t('common.valuePair', {
37-
value: validator.voting_power / stats.total_voting_power,
38-
formatParams: {
39-
value: {
40-
style: 'percent',
41-
maximumFractionDigits: 2,
42-
} satisfies Intl.NumberFormatOptions,
43-
},
44-
})}
37+
<PercentageValue value={validator.voting_power} total={stats.total_voting_power} />
4538
</Box>
4639
<Box sx={{ paddingTop: 4 }}>
4740
<VerticalProgressBar

src/app/pages/ValidatorDetailsPage/index.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { ValidatorDetailsContext } from './hooks'
2828
import { debondingContainerId, delegatorsContainerId } from './tabAnchors'
2929
import { ValidatorStatusBadge } from './ValidatorStatusBadge'
3030
import { eventsContainerId } from './../../pages/ConsensusAccountDetailsPage/ConsensusAccountEventsCard'
31+
import { PercentageValue } from '../../components/PercentageValue'
3132

3233
export const StyledGrid = styled(Grid)(({ theme }) => ({
3334
[theme.breakpoints.up('sm')]: {
@@ -147,15 +148,7 @@ export const ValidatorDetailsView: FC<{
147148
<>
148149
<dt>{t('validator.totalShare')}</dt>
149150
<dd>
150-
{t('common.valuePair', {
151-
value: validator.voting_power / stats.total_voting_power,
152-
formatParams: {
153-
value: {
154-
style: 'percent',
155-
maximumFractionDigits: 2,
156-
} satisfies Intl.NumberFormatOptions,
157-
},
158-
})}
151+
<PercentageValue value={validator.voting_power} total={stats.total_voting_power} />
159152
</dd>
160153
</>
161154
)}
@@ -194,7 +187,11 @@ export const ValidatorDetailsView: FC<{
194187
</dd>
195188
<dt>{t('validator.cumulativeVoting')}</dt>
196189
<dd>
197-
<ValidatorCumulativeVoting containerMarginThemeSpacing={4} value={0} />
190+
<ValidatorCumulativeVoting
191+
containerMarginThemeSpacing={5}
192+
value={validator.voting_power_cumulative}
193+
total={stats?.total_voting_power}
194+
/>
198195
</dd>
199196
<dt>{t('validator.voting')}</dt>
200197
<dd>-</dd>

src/oasis-nexus/generated/api.ts

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)