Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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/2399.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prepare Explorer for incoming breaking changes in Nexus API
11 changes: 7 additions & 4 deletions src/app/components/LabeledProgress/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { FC, ReactNode } from 'react'
import { Progress } from '@oasisprotocol/ui-library/src/components/progress'
import { calculatePercentage } from '../../utils/number-utils'

type LabeledProgresssProps = {
value?: number
max?: number
value?: number | string
max?: number | string
label: ReactNode
}

export const LabeledProgress: FC<LabeledProgresssProps> = ({ value, max, label }) => {
if (!value || !max) {
const percentage = calculatePercentage(value, max)

if (percentage === null) {
return null
}

return (
<div className="w-full relative">
<Progress value={(value / max) * 100} className="h-8 bg-muted-foreground rounded-[8px]" />
<Progress value={percentage} className="h-8 bg-muted-foreground rounded-[8px]" />
<span className="absolute inset-y-0 left-3 flex items-center text-white text-xs font-normal">
{label}
</span>
Expand Down
11 changes: 6 additions & 5 deletions src/app/components/PercentageValue/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { FC } from 'react'
import { useTranslation } from 'react-i18next'
import { calculatePercentage } from '../../utils/number-utils'

type PercentageValueProps = {
adaptMaximumFractionDigits?: boolean
total?: number
value: number | undefined
total?: number | string
value: number | string | undefined
maximumFractionDigits?: number
}

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

if (typeof value !== 'number' || typeof total !== 'number' || total <= 0) {
const percentageValue = calculatePercentage(value, total, false)

if (percentageValue === null) {
return null
}

const percentageValue = value / total

return (
<>
{t('common.valuePair', {
Expand Down
11 changes: 6 additions & 5 deletions src/app/components/Validators/ValidatorCumulativeVoting.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { FC } from 'react'
import { PercentageValue } from '../PercentageValue'
import { calculatePercentage } from '../../utils/number-utils'

type ValidatorCumulativeVotingProps = {
containerMarginThemeSpacing: number
value: number | undefined
total: number | undefined
value: number | string | undefined
total: number | string | undefined
}

export const ValidatorCumulativeVoting: FC<ValidatorCumulativeVotingProps> = ({
containerMarginThemeSpacing,
value,
total,
}) => {
if (typeof value !== 'number' || typeof total !== 'number' || total <= 0) {
const percentage = calculatePercentage(value, total)

if (percentage === null) {
return null
}

const percentage = (value / total) * 100

return (
<div className="flex-1 relative text-center">
<div
Expand Down
7 changes: 4 additions & 3 deletions src/app/pages/ValidatorDetailsPage/VotingPowerCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FC } from 'react'
import { useTranslation } from 'react-i18next'
import { Typography } from '@oasisprotocol/ui-library/src/components/typography'
import BigNumber from 'bignumber.js'
import { Validator, ValidatorAggStats } from '../../../oasis-nexus/api'
import { SnapshotTextCard } from '../../components/Snapshots/SnapshotCard'
import { LabeledProgress } from 'app/components/LabeledProgress'
Expand All @@ -18,13 +19,13 @@ export const VotingPowerCard: FC<VotingPowerCardProps> = ({ validator, stats })
<SnapshotTextCard
title={t('validator.votingPower')}
label={
typeof validator?.voting_power === 'number' && (
<Typography>({validator?.voting_power.toLocaleString()})</Typography>
validator?.voting_power !== undefined && (
<Typography>({new BigNumber(validator?.voting_power).toFormat()})</Typography>
)
}
withContentPadding={false}
>
{typeof validator?.voting_power === 'number' && stats?.total_voting_power && (
{validator?.voting_power !== undefined && stats?.total_voting_power && (
<>
<Typography className="font-normal text-xs text-muted-foreground text-left pb-2">
{t('validator.votingPowerOverall')}
Expand Down
7 changes: 4 additions & 3 deletions src/app/pages/ValidatorDetailsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FC } from 'react'
import { useTranslation } from 'react-i18next'
import { useHref, useLoaderData } from 'react-router-dom'
import { Card, CardContent } from '@oasisprotocol/ui-library/src/components/cards'
import BigNumber from 'bignumber.js'
import {
Validator,
ValidatorAggStats,
Expand Down Expand Up @@ -229,17 +230,17 @@ export const ValidatorDetailsView: FC<{
<dd>{formattedTime}</dd>
</>
)}
{typeof validator.voting_power === 'number' && (
{validator.voting_power !== undefined && (
<>
<dt>{t('validator.votingPower')}</dt>
<dd>
{stats?.total_voting_power ? (
<>
<PercentageValue value={validator.voting_power} total={stats.total_voting_power} />
&nbsp; ({validator.voting_power.toLocaleString()})
&nbsp; ({new BigNumber(validator.voting_power).toFormat()})
</>
) : (
validator.voting_power.toLocaleString()
new BigNumber(validator.voting_power).toFormat()
)}
</dd>
</>
Expand Down
20 changes: 20 additions & 0 deletions src/app/utils/number-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@ export const getGasPrice = ({ fee = '', gasUsed = '' }): string | null => {
}

export const convertToNano = (value: string): string => fromBaseUnits(value, -9)

export const calculatePercentage = (
value: number | string | undefined,
total: number | string | undefined,
asPercentage: boolean = true,
): number | null => {
if (value === undefined || value === null || total === undefined || total === null) {
return null
}

const valueBN = new BigNumber(value)
const totalBN = new BigNumber(total)

if (!valueBN || !totalBN || totalBN.lte(0)) {
return null
}

const result = valueBN.div(totalBN)
return asPercentage ? result.multipliedBy(100).toNumber() : result.toNumber()
}
6 changes: 3 additions & 3 deletions src/oasis-nexus/generated/api.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading