From 16ef9ac89e021189fb891e0e1c34e2b0bc1342ed Mon Sep 17 00:00:00 2001 From: toniocodo Date: Thu, 1 Aug 2024 10:30:22 +0200 Subject: [PATCH] feat: null check formatNumber --- .../src/overview/components/MyVotingPowerCard.tsx | 2 +- .../src/overview/components/ProposalsCountCard.tsx | 4 ++-- .../ogn/src/staking/components/ExtendAddLockupModal.tsx | 6 +++--- libs/defi/ogn/src/staking/components/LockupsTable.tsx | 9 ++++++--- .../defi/ogn/src/staking/components/StakeRewardModal.tsx | 9 ++++++--- libs/defi/ogn/src/staking/components/StakingForm.tsx | 2 +- .../ogn/src/staking/components/UnstakeLockupModal.tsx | 4 ++-- libs/defi/ogn/src/staking/components/VotingPowerCard.tsx | 2 +- libs/defi/ogv/src/migration/components/ConvertModal.tsx | 6 ++++-- libs/defi/ogv/src/migration/components/MigrationForm.tsx | 6 ++++-- libs/defi/shared/src/components/TokenInput/index.tsx | 2 +- libs/shared/providers/src/intl/hooks.ts | 8 ++++---- .../providers/src/wagmi/components/BalanceList.tsx | 2 +- 13 files changed, 36 insertions(+), 26 deletions(-) diff --git a/libs/defi/governance/src/overview/components/MyVotingPowerCard.tsx b/libs/defi/governance/src/overview/components/MyVotingPowerCard.tsx index 25e6b241c..2bdb668b4 100644 --- a/libs/defi/governance/src/overview/components/MyVotingPowerCard.tsx +++ b/libs/defi/governance/src/overview/components/MyVotingPowerCard.tsx @@ -59,7 +59,7 @@ export const MyVotingPowerCard = (props: CardProps) => { const votingPowerPercent = toNumber( div( [BigInt(info?.votingPower ?? 0), tokens.mainnet.xOGN.decimals], - [xOgnTotalSupply, tokens.mainnet.xOGN.decimals], + [xOgnTotalSupply ?? 1n, tokens.mainnet.xOGN.decimals], ), ); diff --git a/libs/defi/governance/src/overview/components/ProposalsCountCard.tsx b/libs/defi/governance/src/overview/components/ProposalsCountCard.tsx index 66003a961..69495ee52 100644 --- a/libs/defi/governance/src/overview/components/ProposalsCountCard.tsx +++ b/libs/defi/governance/src/overview/components/ProposalsCountCard.tsx @@ -18,11 +18,11 @@ export const ProposalsCountCard = (props: CardProps) => { token: tokens.mainnet.xOGN.address, chainId: tokens.mainnet.xOGN.chainId, }, - { select: (data) => data?.erc20HoldersConnection?.totalCount }, + { select: (data) => data?.erc20HoldersConnection?.totalCount ?? 0 }, ); const active = - proposals?.filter((p) => + proposals?.filter?.((p) => [OgvProposalState.Active.toLowerCase()].includes(p.status.toLowerCase()), )?.length ?? 0; diff --git a/libs/defi/ogn/src/staking/components/ExtendAddLockupModal.tsx b/libs/defi/ogn/src/staking/components/ExtendAddLockupModal.tsx index 1badcd5da..b8ac467d0 100644 --- a/libs/defi/ogn/src/staking/components/ExtendAddLockupModal.tsx +++ b/libs/defi/ogn/src/staking/components/ExtendAddLockupModal.tsx @@ -313,7 +313,7 @@ export const ExtendAddLockupModal = ({ {intl.formatNumber( +formatUnits( - BigInt(lockup.amount), + BigInt(lockup?.amount ?? 0), tokens.mainnet.OGN.decimals, ), { @@ -326,7 +326,7 @@ export const ExtendAddLockupModal = ({ {intl.formatNumber( +formatUnits( - BigInt(lockup.points) ?? 0n, + BigInt(lockup?.points ?? 0), tokens.mainnet.xOGN.decimals, ) / +formatUnits( @@ -523,7 +523,7 @@ export const ExtendAddLockupModal = ({ votingPowerPercent <= 1e-6 && votingPowerPercent > 0 ? `~ ` : '', - value: intl.formatNumber(votingPowerPercent, { + value: intl.formatNumber(votingPowerPercent ?? 0, { style: 'percent', minimumFractionDigits: 2, maximumFractionDigits: 5, diff --git a/libs/defi/ogn/src/staking/components/LockupsTable.tsx b/libs/defi/ogn/src/staking/components/LockupsTable.tsx index d302e7129..4c99fca1b 100644 --- a/libs/defi/ogn/src/staking/components/LockupsTable.tsx +++ b/libs/defi/ogn/src/staking/components/LockupsTable.tsx @@ -61,7 +61,10 @@ export const LockupsTable = () => { header: intl.formatMessage({ defaultMessage: 'OGN' }), cell: (info) => intl.formatNumber( - +formatUnits(BigInt(info.getValue()), tokens.mainnet.OGN.decimals), + +formatUnits( + BigInt(info.getValue() ?? 0), + tokens.mainnet.OGN.decimals, + ), { minimumFractionDigits: 0, maximumFractionDigits: 0, @@ -95,7 +98,7 @@ export const LockupsTable = () => { cell: (info) => intl.formatNumber( +formatUnits( - BigInt(info.getValue()), + BigInt(info.getValue() ?? 0), tokens.mainnet.xOGN.decimals, ), { @@ -116,7 +119,7 @@ export const LockupsTable = () => { > {intl.formatNumber( +formatUnits( - BigInt(info.getValue()) ?? 0n, + BigInt(info.getValue() ?? 0), tokens.mainnet.xOGN.decimals, ) / +formatUnits( diff --git a/libs/defi/ogn/src/staking/components/StakeRewardModal.tsx b/libs/defi/ogn/src/staking/components/StakeRewardModal.tsx index f30294fec..eab9acaa8 100644 --- a/libs/defi/ogn/src/staking/components/StakeRewardModal.tsx +++ b/libs/defi/ogn/src/staking/components/StakeRewardModal.tsx @@ -553,7 +553,7 @@ export const StakeRewardModal = (props: DialogProps) => { : '', value: amount > 0n - ? intl.formatNumber(votingPowerPercent, { + ? intl.formatNumber(votingPowerPercent ?? 0, { style: 'percent', minimumFractionDigits: 2, maximumFractionDigits: 5, @@ -621,7 +621,10 @@ function LockupSelect({ header: intl.formatMessage({ defaultMessage: 'OGN' }), cell: (info) => intl.formatNumber( - +formatUnits(BigInt(info.getValue()), tokens.mainnet.OGN.decimals), + +formatUnits( + BigInt(info.getValue() ?? 0), + tokens.mainnet.OGN.decimals, + ), { minimumFractionDigits: 0, maximumFractionDigits: 0, @@ -643,7 +646,7 @@ function LockupSelect({ cell: (info) => intl.formatNumber( +formatUnits( - BigInt(info.getValue()) ?? 0n, + BigInt(info.getValue() ?? 0), tokens.mainnet.xOGN.decimals, ) / +formatUnits( diff --git a/libs/defi/ogn/src/staking/components/StakingForm.tsx b/libs/defi/ogn/src/staking/components/StakingForm.tsx index 75a89c979..629c3cc6d 100644 --- a/libs/defi/ogn/src/staking/components/StakingForm.tsx +++ b/libs/defi/ogn/src/staking/components/StakingForm.tsx @@ -412,7 +412,7 @@ export const StakingForm = () => { votingPowerPercent <= 1e-6 && votingPowerPercent > 0 ? `~ ` : '', - value: intl.formatNumber(votingPowerPercent, { + value: intl.formatNumber(votingPowerPercent ?? 0, { style: 'percent', minimumFractionDigits: 2, maximumFractionDigits: 5, diff --git a/libs/defi/ogn/src/staking/components/UnstakeLockupModal.tsx b/libs/defi/ogn/src/staking/components/UnstakeLockupModal.tsx index 25d8c6bbb..736952e49 100644 --- a/libs/defi/ogn/src/staking/components/UnstakeLockupModal.tsx +++ b/libs/defi/ogn/src/staking/components/UnstakeLockupModal.tsx @@ -149,7 +149,7 @@ export const UnstakeLockupModal = ({ {intl.formatNumber( +formatUnits( - BigInt(lockup.amount), + BigInt(lockup.amount ?? 0), tokens.mainnet.OGN.decimals, ), { @@ -160,7 +160,7 @@ export const UnstakeLockupModal = ({ {formatTimeRemaining(lockup.end)} - {intl.formatNumber(penaltyPercent, { + {intl.formatNumber(penaltyPercent ?? 0, { style: 'percent', minimumFractionDigits: 2, maximumFractionDigits: 2, diff --git a/libs/defi/ogn/src/staking/components/VotingPowerCard.tsx b/libs/defi/ogn/src/staking/components/VotingPowerCard.tsx index d477cf836..7044f77d6 100644 --- a/libs/defi/ogn/src/staking/components/VotingPowerCard.tsx +++ b/libs/defi/ogn/src/staking/components/VotingPowerCard.tsx @@ -123,7 +123,7 @@ export const VotingPowerCard = (props: CardProps) => { defaultMessage: '{tilt}{value}', }, { - value: intl.formatNumber(percent, { + value: intl.formatNumber(percent ?? 0, { style: 'percent', minimumFractionDigits: 2, maximumFractionDigits: 5, diff --git a/libs/defi/ogv/src/migration/components/ConvertModal.tsx b/libs/defi/ogv/src/migration/components/ConvertModal.tsx index 5407b15a8..f4a8505df 100644 --- a/libs/defi/ogv/src/migration/components/ConvertModal.tsx +++ b/libs/defi/ogv/src/migration/components/ConvertModal.tsx @@ -250,7 +250,7 @@ export const ConvertModal = ({ > - {intl.formatNumber(stakingRatio / 100, { + {intl.formatNumber((stakingRatio ?? 0) / 100, { style: 'percent', })} @@ -267,7 +267,9 @@ export const ConvertModal = ({ fontWeight="bold" color="primary.main" > - {intl.formatNumber(stakingRatio / 100, { style: 'percent' })} + {intl.formatNumber((stakingRatio ?? 0) / 100, { + style: 'percent', + })} @@ -379,7 +379,9 @@ function SummaryCard({ ogv, convertProps, ...rest }: SummaryCardProps) { spacing={1} > - {intl.formatNumber(+formatUnits(ogv, tokens.mainnet.OGV.decimals))} + {intl.formatNumber( + +formatUnits(ogv ?? 0n, tokens.mainnet.OGV.decimals), + )} ( flexGrow={1} color={amount === 0n ? 'text.secondary' : 'text.primary'} > - {intl.formatNumber(+formatUnits(amount, decimals), { + {intl.formatNumber(+formatUnits(amount ?? 0n, decimals), { roundingMode: 'floor', minimumFractionDigits: 0, maximumFractionDigits: 8, diff --git a/libs/shared/providers/src/intl/hooks.ts b/libs/shared/providers/src/intl/hooks.ts index b1646fad7..3e9f2f08e 100644 --- a/libs/shared/providers/src/intl/hooks.ts +++ b/libs/shared/providers/src/intl/hooks.ts @@ -28,7 +28,7 @@ export const useFormat = () => { zeroPlaceholder = '0.00', options?: FormatNumberOptions, ) => { - if (!amount || amount === 0n) return zeroPlaceholder; + if (!amount || amount === 0n || amount === 0) return zeroPlaceholder; const amt = typeof amount === 'bigint' ? +formatUnits(amount, decimals) : amount; @@ -55,7 +55,7 @@ export const useFormat = () => { zeroPlaceholder = '0.00', options?: FormatNumberOptions, ) => { - if (!amount || amount === 0n) return zeroPlaceholder; + if (!amount || amount === 0n || amount === 0) return zeroPlaceholder; const amt = typeof amount === 'bigint' ? +formatUnits(amount, decimals) : amount; @@ -76,7 +76,7 @@ export const useFormat = () => { zeroPlaceholder = '$0.00', options?: FormatNumberOptions, ) => { - if (!amount || amount === 0n) return zeroPlaceholder; + if (!amount || amount === 0n || amount === 0) return zeroPlaceholder; const amt = typeof amount === 'bigint' ? +formatUnits(amount, decimals) : amount; @@ -100,7 +100,7 @@ export const useFormat = () => { zeroPlaceholder = '0.00', options?: FormatNumberOptions, ) => { - if (!amount || amount === 0n) return zeroPlaceholder; + if (!amount || amount === 0n || amount === 0) return zeroPlaceholder; const amt = typeof amount === 'bigint' ? +formatUnits(amount, decimals) : amount; diff --git a/libs/shared/providers/src/wagmi/components/BalanceList.tsx b/libs/shared/providers/src/wagmi/components/BalanceList.tsx index cd7eab9b4..ad6552825 100644 --- a/libs/shared/providers/src/wagmi/components/BalanceList.tsx +++ b/libs/shared/providers/src/wagmi/components/BalanceList.tsx @@ -51,7 +51,7 @@ function BalanceRow({ {isBalanceLoading ? ( ) : ( - intl.formatNumber(balance, { + intl.formatNumber(balance ?? 0, { minimumFractionDigits: 4, maximumFractionDigits: 4, })