Skip to content

Commit b5599b3

Browse files
authored
Shown era duration correctly in staking (#11972)
* fix: era duration in staking * fix: vesting schedule timer
1 parent a7ee0e1 commit b5599b3

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

packages/page-staking-async/src/Validators/SummarySession.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ function SummarySession ({ className, withEra = true, withSession = true }: Prop
5353

5454
const eraDuration = useMemo(
5555
() => {
56-
// default to 1800 blocks i.e. 6 hours
57-
return new BN(6 * 60 * 60 * 1000).div(blockTime);
56+
const epochDuration = rcApi?.consts.babe?.epochDuration;
57+
const sessionsPerEra = api?.consts.staking?.sessionsPerEra;
58+
59+
return epochDuration?.mul(sessionsPerEra);
5860
},
59-
[blockTime]
61+
[api?.consts.staking?.sessionsPerEra, rcApi?.consts.babe?.epochDuration]
6062
);
6163

6264
return (
@@ -91,6 +93,7 @@ function SummarySession ({ className, withEra = true, withSession = true }: Prop
9193
)
9294
)}
9395
<CardSummary
96+
apiOverride={rcApi}
9497
className={className}
9598
label={eraLabel}
9699
progress={{

packages/react-components/src/AddressInfo.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2017-2025 @polkadot/react-components authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import type { ApiPromise } from '@polkadot/api';
45
import type { DeriveBalancesAccountData, DeriveBalancesAll, DeriveDemocracyLock, DeriveStakingAccount } from '@polkadot/api-derive/types';
56
import type { Raw } from '@polkadot/types';
67
import type { BlockNumber, ValidatorPrefsTo145, Voting } from '@polkadot/types/interfaces';
@@ -10,7 +11,7 @@ import type { BN } from '@polkadot/util';
1011
import React, { useRef } from 'react';
1112

1213
import { withCalls, withMulti } from '@polkadot/react-api/hoc';
13-
import { useBestNumberRelay } from '@polkadot/react-hooks';
14+
import { useBestNumberRelay, useStakingAsyncApis } from '@polkadot/react-hooks';
1415
import { BlockToTime, FormatBalance } from '@polkadot/react-query';
1516
import { BN_MAX_INTEGER, BN_ZERO, bnMax, formatBalance, formatNumber, isObject } from '@polkadot/util';
1617

@@ -50,6 +51,7 @@ export interface ValidatorPrefsType {
5051
}
5152

5253
interface Props {
54+
apiOverride?: ApiPromise;
5355
address: string;
5456
balancesAll?: DeriveBalancesAll;
5557
children?: React.ReactNode;
@@ -237,7 +239,7 @@ function renderValidatorPrefs ({ stakingInfo, withValidatorPrefs = false }: Prop
237239
);
238240
}
239241

240-
function createBalanceItems (formatIndex: number, lookup: Record<string, string>, t: TFunction, { address, balanceDisplay, balancesAll, bestNumber, convictionLocks, democracyLocks, isAllLocked, otherBonded, ownBonded, stakingInfo, votingOf, withBalanceToggle, withLabel }: { address: string; balanceDisplay: BalanceActiveType; balancesAll?: DeriveBalancesAll | DeriveBalancesAccountData; bestNumber?: BlockNumber; convictionLocks?: RefLock[]; democracyLocks?: DeriveDemocracyLock[]; isAllLocked: boolean; otherBonded: BN[]; ownBonded: BN; stakingInfo?: DeriveStakingAccount; votingOf?: Voting; withBalanceToggle: boolean, withLabel: boolean }): React.ReactNode {
242+
function createBalanceItems (formatIndex: number, lookup: Record<string, string>, t: TFunction, { address, apiOverride, balanceDisplay, balancesAll, bestNumber, convictionLocks, democracyLocks, isAllLocked, otherBonded, ownBonded, stakingInfo, votingOf, withBalanceToggle, withLabel }: { address: string; apiOverride: ApiPromise | undefined, balanceDisplay: BalanceActiveType; balancesAll?: DeriveBalancesAll | DeriveBalancesAccountData; bestNumber?: BlockNumber; convictionLocks?: RefLock[]; democracyLocks?: DeriveDemocracyLock[]; isAllLocked: boolean; otherBonded: BN[]; ownBonded: BN; stakingInfo?: DeriveStakingAccount; votingOf?: Voting; withBalanceToggle: boolean, withLabel: boolean }): React.ReactNode {
241243
const allItems: React.ReactNode[] = [];
242244
const deriveBalances = balancesAll as DeriveBalancesAll;
243245

@@ -293,7 +295,10 @@ function createBalanceItems (formatIndex: number, lookup: Record<string, string>
293295
>
294296
<div>
295297
<p>{formatBalance(locked, { forceUnit: '-' })} {t('fully vested in')}</p>
296-
<BlockToTime value={endBlock.sub(bestNumber)} />
298+
<BlockToTime
299+
api={apiOverride}
300+
value={endBlock.sub(bestNumber)}
301+
/>
297302
</div>
298303
<div className='middle'>
299304
(Block {formatNumber(endBlock)} @ {formatBalance(perBlock)}/block)
@@ -526,7 +531,7 @@ function createBalanceItems (formatIndex: number, lookup: Record<string, string>
526531
);
527532
}
528533

529-
function renderBalances (props: Props, lookup: Record<string, string>, bestNumber: BlockNumber | undefined, t: TFunction): React.ReactNode[] {
534+
function renderBalances (props: Props, lookup: Record<string, string>, bestNumber: BlockNumber | undefined, apiOverride: ApiPromise | undefined, t: TFunction): React.ReactNode[] {
530535
const { address, balancesAll, convictionLocks, democracyLocks, stakingInfo, votingOf, withBalance = true, withBalanceToggle = false, withLabel = false } = props;
531536
const balanceDisplay = withBalance === true
532537
? DEFAULT_BALANCES
@@ -538,7 +543,7 @@ function renderBalances (props: Props, lookup: Record<string, string>, bestNumbe
538543

539544
const [ownBonded, otherBonded] = calcBonded(stakingInfo, balanceDisplay.bonded);
540545
const isAllLocked = !!balancesAll && balancesAll.lockedBreakdown.some(({ amount }): boolean => amount?.isMax());
541-
const baseOpts = { address, balanceDisplay, bestNumber, convictionLocks, democracyLocks, isAllLocked, otherBonded, ownBonded, votingOf, withBalanceToggle, withLabel };
546+
const baseOpts = { address, apiOverride, balanceDisplay, bestNumber, convictionLocks, democracyLocks, isAllLocked, otherBonded, ownBonded, votingOf, withBalanceToggle, withLabel };
542547
const items = [createBalanceItems(0, lookup, t, { ...baseOpts, balancesAll, stakingInfo })];
543548

544549
withBalanceToggle && balancesAll?.additional.length && balancesAll.additional.forEach((balancesAll, index): void => {
@@ -551,6 +556,7 @@ function renderBalances (props: Props, lookup: Record<string, string>, bestNumbe
551556
function AddressInfo (props: Props): React.ReactElement<Props> {
552557
const { t } = useTranslation();
553558
const bestNumber = useBestNumberRelay();
559+
const { isStakingAsync, rcApi } = useStakingAsyncApis();
554560
const { children, className = '', extraInfo, withBalanceToggle, withHexSessionId } = props;
555561

556562
const lookup = useRef<Record<string, string>>({
@@ -564,7 +570,7 @@ function AddressInfo (props: Props): React.ReactElement<Props> {
564570
return (
565571
<div className={`${className} ui--AddressInfo ${withBalanceToggle ? 'ui--AddressInfo-expander' : ''}`}>
566572
<div className={`column${withBalanceToggle ? ' column--expander' : ''}`}>
567-
{renderBalances(props, lookup.current, bestNumber, t)}
573+
{renderBalances(props, lookup.current, bestNumber, isStakingAsync ? rcApi : undefined, t)}
568574
{withHexSessionId?.[0] && (
569575
<>
570576
<Label label={t('session keys')} />

0 commit comments

Comments
 (0)