From 7af66875b9a263363fdcba39b13f56d6b163ad9e Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 24 Jul 2020 19:38:14 +0200 Subject: [PATCH] Proper angle/percentage calc from BN (#3261) --- .../src/shared/InputMegaGas.tsx | 1 - packages/page-poll/src/index.tsx | 7 +-- packages/page-staking/src/Targets/Summary.tsx | 57 +++++++++++-------- packages/page-staking/src/index.tsx | 3 +- packages/react-components/src/Progress.tsx | 27 ++++----- packages/react-query/src/FormatBalance.tsx | 11 ++-- 6 files changed, 51 insertions(+), 55 deletions(-) diff --git a/packages/page-contracts/src/shared/InputMegaGas.tsx b/packages/page-contracts/src/shared/InputMegaGas.tsx index 9ad774b70f..509fa42953 100644 --- a/packages/page-contracts/src/shared/InputMegaGas.tsx +++ b/packages/page-contracts/src/shared/InputMegaGas.tsx @@ -40,7 +40,6 @@ function InputMegaGas ({ className, executionTime, help, isValid, label, megaGas diff --git a/packages/page-poll/src/index.tsx b/packages/page-poll/src/index.tsx index 73b513490a..84cb3c741f 100644 --- a/packages/page-poll/src/index.tsx +++ b/packages/page-poll/src/index.tsx @@ -135,11 +135,8 @@ function PollApp ({ className }: Props): React.ReactElement { ) diff --git a/packages/page-staking/src/Targets/Summary.tsx b/packages/page-staking/src/Targets/Summary.tsx index ad5a1ef744..a98e4fbdca 100644 --- a/packages/page-staking/src/Targets/Summary.tsx +++ b/packages/page-staking/src/Targets/Summary.tsx @@ -19,46 +19,53 @@ interface Props { totalStaked?: BN; } +interface Progress { + hideValue: true; + total: BN; + value: BN; +} + function Summary ({ lastReward, numNominators, numValidators, totalStaked }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const totalIssuance = useCall(api.query.balances?.totalIssuance, []); - const [percentage, setPercentage] = useState(); + const [progressStake, setProgressStake] = useState(); useEffect((): void => { - totalIssuance && totalStaked && totalStaked.gtn(0) && setPercentage( - `${(totalStaked.muln(10000).div(totalIssuance).toNumber() / 100).toFixed(2)}%` - ); + totalIssuance && totalStaked && totalStaked.gtn(0) && setProgressStake({ + hideValue: true, + total: totalIssuance, + value: totalStaked + }); }, [totalIssuance, totalStaked]); return (
- {totalStaked && ( - ('total staked')}> - - - )} - {totalStaked && totalIssuance && ( - / - )} {totalIssuance && ( - ('total issuance')}> - + ('total staked')}/` : ''}${t('total issuance')}`} + progress={progressStake} + > +
+ {totalStaked?.gtn(0) && ( + <> + + / + + )} + +
)}
- {percentage && ( - ('staked')}> - {percentage} - - )} {numValidators && numNominators && ( ('validators/nominators')}> {numValidators}/{numNominators} diff --git a/packages/page-staking/src/index.tsx b/packages/page-staking/src/index.tsx index d7f5a44c97..3e88a1299e 100644 --- a/packages/page-staking/src/index.tsx +++ b/packages/page-staking/src/index.tsx @@ -46,8 +46,7 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement status.isOpen }); const hasQueries = useMemo( - (): boolean => - hasAccounts && !!(api.query.imOnline?.authoredBlocks) && !!(api.query.staking.activeEra), + () => hasAccounts && !!(api.query.imOnline?.authoredBlocks) && !!(api.query.staking.activeEra), [api, hasAccounts] ); const items = useMemo(() => [ diff --git a/packages/react-components/src/Progress.tsx b/packages/react-components/src/Progress.tsx index 8fdf5e00dc..c8dc753ba0 100644 --- a/packages/react-components/src/Progress.tsx +++ b/packages/react-components/src/Progress.tsx @@ -6,15 +6,13 @@ import BN from 'bn.js'; import React from 'react'; import styled from 'styled-components'; import { UInt } from '@polkadot/types'; -import { bnToBn, isBn, isUndefined } from '@polkadot/util'; +import { bnToBn } from '@polkadot/util'; interface Props { className?: string; isDisabled?: boolean; - percent?: BN | number; - total?: UInt | BN | number; - value?: UInt | BN | number; - withSummary?: boolean; + total?: UInt | BN | number | null; + value?: UInt | BN | number | null; } interface RotateProps { @@ -35,21 +33,16 @@ function DivClip ({ angle, type }: RotateProps): React.ReactElement const Clip = React.memo(DivClip); -function Progress ({ className = '', isDisabled, percent, total, value, withSummary = true }: Props): React.ReactElement | null { - const _total = bnToBn(total); - const _value = bnToBn(value); - const width = _total.gtn(0) - ? (100.0 * _value.toNumber() / _total.toNumber()) - : isBn(percent) - ? percent.toNumber() - : percent; +function Progress ({ className = '', isDisabled, total, value }: Props): React.ReactElement | null { + const _total = bnToBn(total || 0); + const angle = _total.gtn(0) + ? (bnToBn(value || 0).muln(36000).div(_total).toNumber() / 100) + : 0; - if (isUndefined(width) || width < 0) { + if (angle < 0) { return null; } - const angle = width * 360 / 100; - return (
@@ -70,7 +63,7 @@ function Progress ({ className = '', isDisabled, percent, total, value, withSumm type='second' />
- {withSummary &&
{width.toFixed(1)}%
} +
{(angle * 100 / 360).toFixed(1)}%
); diff --git a/packages/react-query/src/FormatBalance.tsx b/packages/react-query/src/FormatBalance.tsx index 5fb153f2a1..075c59eb8d 100644 --- a/packages/react-query/src/FormatBalance.tsx +++ b/packages/react-query/src/FormatBalance.tsx @@ -17,6 +17,7 @@ interface Props { label?: React.ReactNode; labelPost?: string; value?: Compact | BN | string | null | 'all'; + withCurrency?: boolean; withSi?: boolean; } @@ -24,21 +25,21 @@ interface Props { const M_LENGTH = 6 + 1; const K_LENGTH = 3 + 1; -function format (value: Compact | BN | string, currency: string, withSi?: boolean, _isShort?: boolean, labelPost?: string): React.ReactNode { +function format (value: Compact | BN | string, currency: string | null, withSi?: boolean, _isShort?: boolean, labelPost?: string): React.ReactNode { const [prefix, postfix] = formatBalance(value, { forceUnit: '-', withSi: false }).split('.'); const isShort = _isShort || (withSi && prefix.length >= K_LENGTH); if (prefix.length > M_LENGTH) { // TODO Format with balance-postfix - return `${formatBalance(value)}${labelPost || ''}`; + return `${formatBalance(value, { withUnit: !!currency })}${labelPost || ''}`; } - return <>{`${prefix}${isShort ? '' : '.'}`}{!isShort && (<>{`000${postfix || ''}`.slice(-3)})} {`${currency}${labelPost || ''}`}; + return <>{`${prefix}${isShort ? '' : '.'}`}{!isShort && (<>{`000${postfix || ''}`.slice(-3)})}{`${currency ? ` ${currency}` : ''}${labelPost || ''}`}; } -function FormatBalance ({ children, className = '', isShort, label, labelPost, value, withSi }: Props): React.ReactElement { +function FormatBalance ({ children, className = '', isShort, label, labelPost, value, withCurrency = true, withSi }: Props): React.ReactElement { const { t } = useTranslation(); - const [currency] = useState(formatBalance.getDefaults().unit); + const [currency] = useState(withCurrency ? formatBalance.getDefaults().unit : null); // labelPost here looks messy, however we ensure we have one less text node return (