Skip to content

Commit

Permalink
refactor(balance): move formatWeiNumber to util-functions and update …
Browse files Browse the repository at this point in the history
…imports
  • Loading branch information
mohandast52 committed Nov 8, 2024
1 parent d4cfb71 commit 8b095ae
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 32 deletions.
21 changes: 0 additions & 21 deletions apps/govern/common-util/functions/balance.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
import isNil from 'lodash/isNil';

/**
* Return formatted number with appropriate suffix
*/
export const formatWeiNumber = ({
value,
minimumFractionDigits = 0,
maximumFractionDigits = 2,
}: {
value: number | string | undefined;
minimumFractionDigits?: number;
maximumFractionDigits?: number;
}) => {
if (isNil(value) || Number(value) === 0) return '0';

return new Intl.NumberFormat('en', {
notation: 'compact',
minimumFractionDigits,
maximumFractionDigits,
}).format(Number(value));
};

/**
* Converts a number to a comma separated format
* eg: 1000000 => 1,000,000, 12345.67 => 12,345.67
Expand Down
2 changes: 1 addition & 1 deletion apps/govern/components/Contracts/ContractsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Allocation, StakingContract } from 'types';
import { useAccount } from 'wagmi';

import { CHAIN_NAMES } from 'libs/util-constants/src';
import { formatWeiNumber } from 'libs/util-functions/src';

import { formatWeiNumber } from 'common-util/functions/balance';
import { NextWeekTooltip } from 'components/NextWeekTooltip';
import { useVotingPower } from 'hooks/useVotingPower';
import { useAppSelector } from 'store/index';
Expand Down
2 changes: 1 addition & 1 deletion apps/govern/components/Layout/Balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { useAccount } from 'wagmi';

import { COLOR } from 'libs/ui-theme/src/lib/ui-theme';
import { UNICODE_SYMBOLS } from 'libs/util-constants/src';
import { formatWeiNumber } from 'libs/util-functions/src';

import { formatWeiNumber } from 'common-util/functions';
import { useVotingPower } from 'hooks/index';

const { Text, Paragraph } = Typography;
Expand Down
2 changes: 1 addition & 1 deletion apps/govern/components/Proposals/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { ethers } from 'ethers';
import { Address } from 'viem';

import { areAddressesEqual } from 'libs/util-functions/src';
import { formatWeiNumber } from 'libs/util-functions/src';

import { formatWeiNumber } from 'common-util/functions';
import { Proposal } from 'common-util/graphql/types';

export enum VoteSupport {
Expand Down
3 changes: 2 additions & 1 deletion apps/govern/components/VeOlas/VeOlasComponents.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { formatWeiNumber } from 'libs/util-functions/src';

import {
formatWeiNumber,
getCommaSeparatedNumber,
getFormattedDate,
getFullFormattedDate,
Expand Down
2 changes: 1 addition & 1 deletion apps/operate/components/Contracts/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export const useStakingContractsList = () => {
const rewardsPerSecond = rewardsPerSecondList[index] as bigint;
const minStakingDeposit = minStakingDepositList[index] as bigint;
const numAgentInstances = numAgentInstancesList[index] as bigint;
const availableRewards = Number(formatUnits(availableRewardsInWei, 18)).toFixed(2);
const availableRewards = formatUnits(availableRewardsInWei, 18);

const apy = getApy(rewardsPerSecond, minStakingDeposit, numAgentInstances);
const stakeRequired = getStakeRequired(minStakingDeposit, numAgentInstances);
Expand Down
3 changes: 2 additions & 1 deletion apps/operate/components/Contracts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { StakingContract } from 'types';
import { Caption, TextWithTooltip } from 'libs/ui-components/src';
import { BREAK_POINT } from 'libs/ui-theme/src';
import { CHAIN_NAMES, GOVERN_URL, NA, UNICODE_SYMBOLS } from 'libs/util-constants/src';
import { formatWeiNumber } from 'libs/util-functions/src';

import { RunAgentButton } from 'components/RunAgentButton';

Expand Down Expand Up @@ -54,7 +55,7 @@ const columns: ColumnsType<StakingContract> = [
title: () => 'Available Rewards (OLAS)',
dataIndex: 'availableRewards',
key: 'availableRewards',
render: (availableRewards) => <Text>{availableRewards}</Text>,
render: (availableRewards) => <Text>{formatWeiNumber({ value: availableRewards })}</Text>,
className: 'text-end',
},
{
Expand Down
1 change: 1 addition & 0 deletions libs/util-functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './lib/sendTransaction/helpers';
export * from './lib/notifications';
export * from './lib/requests';
export * from './lib/ethers';
export * from './lib/numbers';
6 changes: 3 additions & 3 deletions libs/util-functions/src/lib/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { notification } from 'antd';
export const notifySuccess = (
message: ReactNode = 'Successful',
description: ReactNode = '',
key?: string
key?: string,
) => notification.success({ message, description, key });

export const notifyError = (
message: ReactNode = 'Some error occurred',
description: ReactNode = '',
key?: string
key?: string,
) => {
notification.error({ message, description, key });
};

export const notifyWarning = (
message: ReactNode = 'Some error occurred',
description: ReactNode = '',
key?: string
key?: string,
) => notification.warning({ message, description, key });
22 changes: 22 additions & 0 deletions libs/util-functions/src/lib/numbers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { isNil } from 'lodash';

/**
* Return formatted number with appropriate suffix
*/
export const formatWeiNumber = ({
value,
minimumFractionDigits = 0,
maximumFractionDigits = 2,
}: {
value: number | string | undefined;
minimumFractionDigits?: number;
maximumFractionDigits?: number;
}) => {
if (isNil(value) || Number(value) === 0) return '0';

return new Intl.NumberFormat('en', {
notation: 'compact',
minimumFractionDigits,
maximumFractionDigits,
}).format(Number(value));
};
3 changes: 1 addition & 2 deletions libs/util-functions/src/lib/requests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Contract } from 'ethers';


const ESTIMATED_GAS_LIMIT = 500_000;

/**
Expand All @@ -23,4 +22,4 @@ export const getEstimatedGasLimit = async (
}

return ESTIMATED_GAS_LIMIT;
};
};

0 comments on commit 8b095ae

Please sign in to comment.