Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(explorer): add staking ratio calculation #5614

Merged
merged 4 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/explorer/src/lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
// Copyright (c) 2025 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

export * from './validator.types';
3 changes: 3 additions & 0 deletions apps/explorer/src/lib/types/validator.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) 2025 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { type IotaValidatorSummary } from '@iota/iota-sdk/client';

export type IotaValidatorSummaryExtended = IotaValidatorSummary & { isPending?: boolean };
25 changes: 18 additions & 7 deletions apps/explorer/src/pages/validators/Validators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { type JSX, useMemo } from 'react';
import {
roundFloat,
useFormatCoin,
formatPercentageDisplay,
useGetDynamicFields,
useGetValidatorsApy,
useGetValidatorsEvents,
Expand All @@ -29,7 +30,7 @@ import { Warning } from '@iota/apps-ui-icons';
import { useQuery } from '@tanstack/react-query';
import { useEnhancedRpcClient } from '~/hooks';
import { sanitizePendingValidators } from '~/lib';
import { normalizeIotaAddress } from '@iota/iota-sdk/utils';
import { IOTA_TYPE_ARG, normalizeIotaAddress } from '@iota/iota-sdk/utils';

function ValidatorPageResult(): JSX.Element {
const { data, isPending, isSuccess, isError } = useIotaClientQuery('getLatestIotaSystemState');
Expand Down Expand Up @@ -60,6 +61,9 @@ function ValidatorPageResult(): JSX.Element {
const sanitizePendingValidatorsData = sanitizePendingValidators(pendingValidatorsData);

const { data: validatorsApy } = useGetValidatorsApy();
const { data: totalSupplyData } = useIotaClientQuery('getTotalSupply', {
coinType: IOTA_TYPE_ARG,
});

const totalStaked = useMemo(() => {
if (!data) return 0;
Expand Down Expand Up @@ -105,12 +109,19 @@ function ValidatorPageResult(): JSX.Element {
const lastEpochRewardOnAllValidators =
epochData?.data[0].endOfEpochInfo?.totalStakeRewardsDistributed;

const sortedValidators = activeValidatorsData?.sort(() => 0.5 - Math.random());
const stakingRatio = (() => {
let ratio = null;
if (totalSupplyData?.value && totalStaked) {
const totalSupplyValue = Number(totalSupplyData.value);
ratio = Number(((totalStaked / totalSupplyValue) * 100).toFixed(2));
}
return formatPercentageDisplay(ratio);
})();

const tableData = data
? Number(data.pendingActiveValidatorsSize) > 0
? sortedValidators?.concat(sanitizePendingValidatorsData)
: sortedValidators
? activeValidatorsData?.concat(sanitizePendingValidatorsData)
: activeValidatorsData
: [];

const tableColumns = useMemo(() => {
Expand Down Expand Up @@ -147,9 +158,9 @@ function ValidatorPageResult(): JSX.Element {
'The combined IOTA staked by validators and delegators on the network to support validation and generate rewards.',
},
{
title: 'Participation',
value: '--',
tooltipText: 'Coming soon',
title: 'Staking Ratio',
value: stakingRatio,
tooltipText: 'The ratio of the total staked IOTA to the total supply of IOTA.',
},
{
title: 'Last Epoch Rewards',
Expand Down
Loading