Skip to content

Commit

Permalink
(operate) feat: Add available rewards on staking contracts website fo…
Browse files Browse the repository at this point in the history
…r each reward (#133)

* feat(operate): add available rewards display in staking contracts

* refactor(balance): move formatWeiNumber to util-functions and update imports

* fix(contracts): update available rewards title format in staking contracts

* fix(contracts): adjust column widths for available rewards and stake required in contracts table
  • Loading branch information
mohandast52 authored Nov 8, 2024
1 parent 8f6d182 commit 91ec7be
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 35 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
9 changes: 6 additions & 3 deletions apps/operate/components/Contracts/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMemo } from 'react';
import { StakingContract } from 'types';
import { Abi, Address, formatEther } from 'viem';
import { Abi, Address, formatEther, formatUnits } from 'viem';
import { useReadContracts } from 'wagmi';

import { useNominees, useNomineesMetadata } from 'libs/common-contract-functions/src';
Expand Down Expand Up @@ -203,11 +203,13 @@ export const useStakingContractsList = () => {
return nominees.map((item, index) => {
const maxSlots = Number(maxNumServicesList[index]);
const servicesLength = ((serviceIdsList[index] as string[]) || []).length;
const availableRewards = availableRewardsList[index] as bigint;
const availableSlots = availableRewards > 0 && maxSlots > 0 ? maxSlots - servicesLength : 0;
const availableRewardsInWei = availableRewardsList[index] as bigint;
const availableSlots =
availableRewardsInWei > 0 && maxSlots > 0 ? maxSlots - servicesLength : 0;
const rewardsPerSecond = rewardsPerSecondList[index] as bigint;
const minStakingDeposit = minStakingDepositList[index] as bigint;
const numAgentInstances = numAgentInstancesList[index] as bigint;
const availableRewards = formatUnits(availableRewardsInWei, 18);

const apy = getApy(rewardsPerSecond, minStakingDeposit, numAgentInstances);
const stakeRequired = getStakeRequired(minStakingDeposit, numAgentInstances);
Expand All @@ -226,6 +228,7 @@ export const useStakingContractsList = () => {
minOperatingBalance: details?.minOperatingBalance,
minOperatingBalanceToken: details?.minOperatingBalanceToken || null,
minOperatingBalanceHint: details?.minOperatingBalanceHint || null,
availableRewards,
};
}) as StakingContract[];
}
Expand Down
13 changes: 11 additions & 2 deletions 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 @@ -50,13 +51,21 @@ const columns: ColumnsType<StakingContract> = [
render: (apy) => <Tag color="purple" className="m-0">{`${apy}%`}</Tag>,
className: 'text-end',
},
{
title: () => 'Available Rewards, OLAS',
dataIndex: 'availableRewards',
key: 'availableRewards',
render: (availableRewards) => <Text>{formatWeiNumber({ value: availableRewards })}</Text>,
className: 'text-end',
width: 120,
},
{
title: 'Stake required, OLAS',
dataIndex: 'stakeRequired',
key: 'stakeRequired',
render: (stakeRequired) => <Text>{stakeRequired}</Text>,
className: 'text-end',
width: 148,
width: 120,
},
{
title: 'Minimum operating balance required',
Expand All @@ -77,7 +86,7 @@ const columns: ColumnsType<StakingContract> = [
);
},
className: 'text-end',
width: 200,
width: 180,
},
{
title: () => (
Expand Down
1 change: 1 addition & 0 deletions apps/operate/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export type StakingContract = {
minOperatingBalance: number | null;
minOperatingBalanceToken: string | null;
minOperatingBalanceHint?: string;
availableRewards: string;
};
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 91ec7be

Please sign in to comment.