* Use import type { ... }
* value imports
* TypeDefInfo
* Apply suggestions from code review
* fixes... am going blind.
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
// Copyright 2017-2020 @polkadot/app-staking authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { Forcing } from '@polkadot/types/interfaces';
|
|
import type { DeriveSessionProgress } from '@polkadot/api-derive/types';
|
|
|
|
import BN from 'bn.js';
|
|
import { useMemo } from 'react';
|
|
import { useApi, useCall } from '@polkadot/react-hooks';
|
|
import { BN_ONE } from '@polkadot/util';
|
|
|
|
export default function useEraBlocks (era?: BN): BN | undefined {
|
|
const { api } = useApi();
|
|
const depth = useCall<BN>(api.query.staking.historyDepth);
|
|
const progress = useCall<DeriveSessionProgress>(api.derive.session.progress);
|
|
const forcing = useCall<Forcing>(api.query.staking.forceEra);
|
|
|
|
return useMemo(
|
|
() => (depth && era && forcing && progress && progress.sessionLength.gt(BN_ONE))
|
|
? (
|
|
forcing.isForceAlways
|
|
? progress.sessionLength
|
|
: progress.eraLength
|
|
).mul(
|
|
depth
|
|
.sub(progress.activeEra)
|
|
.iadd(era)
|
|
.iadd(BN_ONE)
|
|
).isub(
|
|
forcing.isForceAlways
|
|
? progress.sessionProgress
|
|
: progress.eraProgress
|
|
)
|
|
: undefined,
|
|
[depth, era, forcing, progress]
|
|
);
|
|
}
|