From 62cab6db8bb8ef08279bc259a2d22acafaa9d1ed Mon Sep 17 00:00:00 2001 From: Edd Date: Thu, 7 Dec 2023 15:16:56 +0000 Subject: [PATCH] fix(governance): address feedback --- .../consensus-validators-table.tsx | 14 ++++++++------ .../standby-pending-validators-table.tsx | 14 ++++++++------ apps/governance/src/routes/staking/shared.ts | 2 +- libs/utils/src/lib/format/number.spec.ts | 8 ++++++++ libs/utils/src/lib/format/number.ts | 9 ++++++++- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/apps/governance/src/routes/staking/home/validator-tables/consensus-validators-table.tsx b/apps/governance/src/routes/staking/home/validator-tables/consensus-validators-table.tsx index 2cb2e3ded..63272683f 100644 --- a/apps/governance/src/routes/staking/home/validator-tables/consensus-validators-table.tsx +++ b/apps/governance/src/routes/staking/home/validator-tables/consensus-validators-table.tsx @@ -221,12 +221,14 @@ export const ConsensusValidatorsTable = ({ calculatesPerformancePenalty(performanceScore), 2 ), - [ValidatorFields.OVERSTAKING_PENALTY]: overstakingPenalty - ? formatNumberPercentage(overstakingPenalty, 2) - : '-', - [ValidatorFields.TOTAL_PENALTIES]: totalPenalty - ? formatNumberPercentage(totalPenalty, 2) - : '-', + [ValidatorFields.OVERSTAKING_PENALTY]: formatNumberPercentage( + overstakingPenalty, + 2 + ), + [ValidatorFields.TOTAL_PENALTIES]: formatNumberPercentage( + totalPenalty, + 2 + ), [ValidatorFields.PENDING_STAKE]: pendingStake, [ValidatorFields.STAKED_BY_USER]: stakedByUser ? formatNumber(toBigNum(stakedByUser, decimals), 2) diff --git a/apps/governance/src/routes/staking/home/validator-tables/standby-pending-validators-table.tsx b/apps/governance/src/routes/staking/home/validator-tables/standby-pending-validators-table.tsx index 730135451..8c06ed633 100644 --- a/apps/governance/src/routes/staking/home/validator-tables/standby-pending-validators-table.tsx +++ b/apps/governance/src/routes/staking/home/validator-tables/standby-pending-validators-table.tsx @@ -162,12 +162,14 @@ export const StandbyPendingValidatorsTable = ({ calculatesPerformancePenalty(performanceScore), 2 ), - [ValidatorFields.OVERSTAKING_PENALTY]: overstakingPenalty - ? formatNumberPercentage(overstakingPenalty, 2) - : '-', - [ValidatorFields.TOTAL_PENALTIES]: totalPenalty - ? formatNumberPercentage(totalPenalty, 2) - : '-', + [ValidatorFields.OVERSTAKING_PENALTY]: formatNumberPercentage( + overstakingPenalty, + 2 + ), + [ValidatorFields.TOTAL_PENALTIES]: formatNumberPercentage( + totalPenalty, + 2 + ), [ValidatorFields.PENDING_STAKE]: pendingStake, [ValidatorFields.STAKED_BY_USER]: stakedByUser ? formatNumber(toBigNum(stakedByUser, decimals), 2) diff --git a/apps/governance/src/routes/staking/shared.ts b/apps/governance/src/routes/staking/shared.ts index dff96816a..69ca4ecf0 100644 --- a/apps/governance/src/routes/staking/shared.ts +++ b/apps/governance/src/routes/staking/shared.ts @@ -41,7 +41,7 @@ const calculateTheoreticalStakeScore = ( }; /** - * Calculates overall penalty for a given nodsadase + * Calculates overall penalty for a given node * @param nodeId Id of a node for which a penalty is calculated * @param nodes A collection of all nodes - needed to calculate theoretical stake score * @returns % diff --git a/libs/utils/src/lib/format/number.spec.ts b/libs/utils/src/lib/format/number.spec.ts index ca2530460..acbc71a7d 100644 --- a/libs/utils/src/lib/format/number.spec.ts +++ b/libs/utils/src/lib/format/number.spec.ts @@ -84,6 +84,14 @@ describe('number utils', () => { expect(formatNumberPercentage(v, d)).toStrictEqual(o); }); + it('formatNumberPercentage returns "-" when value is null', () => { + expect(formatNumberPercentage(null)).toStrictEqual('-'); + }); + + it('formatNumberPercentage returns "-" when value is undefined', () => { + expect(formatNumberPercentage(undefined)).toStrictEqual('-'); + }); + describe('toNumberParts', () => { it.each([ { v: null, d: 3, o: ['0', '000', '.'] }, diff --git a/libs/utils/src/lib/format/number.ts b/libs/utils/src/lib/format/number.ts index 6f721ccbd..67305f66e 100644 --- a/libs/utils/src/lib/format/number.ts +++ b/libs/utils/src/lib/format/number.ts @@ -156,7 +156,14 @@ export const addDecimalsFixedFormatNumber = ( return formatNumberFixed(x, formatDecimals); }; -export const formatNumberPercentage = (value: BigNumber, decimals?: number) => { +export const formatNumberPercentage = ( + value: BigNumber | null | undefined, + decimals?: number +) => { + if (!value) { + return '-'; + } + const decimalPlaces = typeof decimals === 'undefined' ? value.dp() || 0 : decimals; return `${formatNumber(value, decimalPlaces)}%`;