Skip to content

Commit b342df6

Browse files
authored
Merge pull request #2399 from oasisprotocol/mz/syncWithNexus
Prepare Explorer for incoming breaking changes in Nexus API
2 parents bdbc35b + ff45769 commit b342df6

File tree

8 files changed

+51
-23
lines changed

8 files changed

+51
-23
lines changed

.changelog/2399.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prepare Explorer for incoming breaking changes in Nexus API

src/app/components/LabeledProgress/index.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import { FC, ReactNode } from 'react'
22
import { Progress } from '@oasisprotocol/ui-library/src/components/progress'
3+
import { calculatePercentage } from '../../utils/number-utils'
34

45
type LabeledProgresssProps = {
5-
value?: number
6-
max?: number
6+
value?: number | string
7+
max?: number | string
78
label: ReactNode
89
}
910

1011
export const LabeledProgress: FC<LabeledProgresssProps> = ({ value, max, label }) => {
11-
if (!value || !max) {
12+
const percentage = calculatePercentage(value, max)
13+
14+
if (percentage === null) {
1215
return null
1316
}
1417

1518
return (
1619
<div className="w-full relative">
17-
<Progress value={(value / max) * 100} className="h-8 bg-muted-foreground rounded-[8px]" />
20+
<Progress value={percentage} className="h-8 bg-muted-foreground rounded-[8px]" />
1821
<span className="absolute inset-y-0 left-3 flex items-center text-white text-xs font-normal">
1922
{label}
2023
</span>

src/app/components/PercentageValue/index.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { FC } from 'react'
22
import { useTranslation } from 'react-i18next'
3+
import { calculatePercentage } from '../../utils/number-utils'
34

45
type PercentageValueProps = {
56
adaptMaximumFractionDigits?: boolean
6-
total?: number
7-
value: number | undefined
7+
total?: number | string
8+
value: number | string | undefined
89
maximumFractionDigits?: number
910
}
1011

@@ -16,12 +17,12 @@ export const PercentageValue: FC<PercentageValueProps> = ({
1617
}) => {
1718
const { t } = useTranslation()
1819

19-
if (typeof value !== 'number' || typeof total !== 'number' || total <= 0) {
20+
const percentageValue = calculatePercentage(value, total, false)
21+
22+
if (percentageValue === null) {
2023
return null
2124
}
2225

23-
const percentageValue = value / total
24-
2526
return (
2627
<>
2728
{t('common.valuePair', {

src/app/components/Validators/ValidatorCumulativeVoting.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import { FC } from 'react'
22
import { PercentageValue } from '../PercentageValue'
3+
import { calculatePercentage } from '../../utils/number-utils'
34

45
type ValidatorCumulativeVotingProps = {
56
containerMarginThemeSpacing: number
6-
value: number | undefined
7-
total: number | undefined
7+
value: number | string | undefined
8+
total: number | string | undefined
89
}
910

1011
export const ValidatorCumulativeVoting: FC<ValidatorCumulativeVotingProps> = ({
1112
containerMarginThemeSpacing,
1213
value,
1314
total,
1415
}) => {
15-
if (typeof value !== 'number' || typeof total !== 'number' || total <= 0) {
16+
const percentage = calculatePercentage(value, total)
17+
18+
if (percentage === null) {
1619
return null
1720
}
1821

19-
const percentage = (value / total) * 100
20-
2122
return (
2223
<div className="flex-1 relative text-center">
2324
<div

src/app/pages/ValidatorDetailsPage/VotingPowerCard.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { FC } from 'react'
22
import { useTranslation } from 'react-i18next'
33
import { Typography } from '@oasisprotocol/ui-library/src/components/typography'
4+
import BigNumber from 'bignumber.js'
45
import { Validator, ValidatorAggStats } from '../../../oasis-nexus/api'
56
import { SnapshotTextCard } from '../../components/Snapshots/SnapshotCard'
67
import { LabeledProgress } from 'app/components/LabeledProgress'
@@ -18,13 +19,13 @@ export const VotingPowerCard: FC<VotingPowerCardProps> = ({ validator, stats })
1819
<SnapshotTextCard
1920
title={t('validator.votingPower')}
2021
label={
21-
typeof validator?.voting_power === 'number' && (
22-
<Typography>({validator?.voting_power.toLocaleString()})</Typography>
22+
validator?.voting_power !== undefined && (
23+
<Typography>({new BigNumber(validator?.voting_power).toFormat()})</Typography>
2324
)
2425
}
2526
withContentPadding={false}
2627
>
27-
{typeof validator?.voting_power === 'number' && stats?.total_voting_power && (
28+
{validator?.voting_power !== undefined && stats?.total_voting_power && (
2829
<>
2930
<Typography className="font-normal text-xs text-muted-foreground text-left pb-2">
3031
{t('validator.votingPowerOverall')}

src/app/pages/ValidatorDetailsPage/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { FC } from 'react'
22
import { useTranslation } from 'react-i18next'
33
import { useHref, useLoaderData } from 'react-router-dom'
44
import { Card, CardContent } from '@oasisprotocol/ui-library/src/components/cards'
5+
import BigNumber from 'bignumber.js'
56
import {
67
Validator,
78
ValidatorAggStats,
@@ -229,17 +230,17 @@ export const ValidatorDetailsView: FC<{
229230
<dd>{formattedTime}</dd>
230231
</>
231232
)}
232-
{typeof validator.voting_power === 'number' && (
233+
{validator.voting_power !== undefined && (
233234
<>
234235
<dt>{t('validator.votingPower')}</dt>
235236
<dd>
236237
{stats?.total_voting_power ? (
237238
<>
238239
<PercentageValue value={validator.voting_power} total={stats.total_voting_power} />
239-
&nbsp; ({validator.voting_power.toLocaleString()})
240+
&nbsp; ({new BigNumber(validator.voting_power).toFormat()})
240241
</>
241242
) : (
242-
validator.voting_power.toLocaleString()
243+
new BigNumber(validator.voting_power).toFormat()
243244
)}
244245
</dd>
245246
</>

src/app/utils/number-utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,23 @@ export const getGasPrice = ({ fee = '', gasUsed = '' }): string | null => {
1717
}
1818

1919
export const convertToNano = (value: string): string => fromBaseUnits(value, -9)
20+
21+
export const calculatePercentage = (
22+
value: number | string | undefined,
23+
total: number | string | undefined,
24+
asPercentage: boolean = true,
25+
): number | null => {
26+
if (value === undefined || value === null || total === undefined || total === null) {
27+
return null
28+
}
29+
30+
const valueBN = new BigNumber(value)
31+
const totalBN = new BigNumber(total)
32+
33+
if (!valueBN || !totalBN || totalBN.lte(0)) {
34+
return null
35+
}
36+
37+
const result = valueBN.div(totalBN)
38+
return asPercentage ? result.multipliedBy(100).toNumber() : result.toNumber()
39+
}

src/oasis-nexus/generated/api.ts

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

0 commit comments

Comments
 (0)