ghost-portal/packages/page-staking/src/Payouts/useEraBlocks.ts
Jaco 4929b1c6fd
Update headers for 2023 (#8665)
* Update headers for 2023

* Adjust env-config
2023-01-01 10:32:05 +02:00

40 lines
1.2 KiB
TypeScript

// Copyright 2017-2023 @polkadot/app-staking authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { DeriveSessionProgress } from '@polkadot/api-derive/types';
import type { Forcing } from '@polkadot/types/interfaces';
import type { BN } from '@polkadot/util';
import { useMemo } from 'react';
import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks';
import { BN_ONE } from '@polkadot/util';
function useEraBlocksImpl (historyDepth?: BN, era?: BN): BN | undefined {
const { api } = useApi();
const progress = useCall<DeriveSessionProgress>(api.derive.session.progress);
const forcing = useCall<Forcing>(api.query.staking.forceEra);
return useMemo(
() => (historyDepth && era && forcing && progress && progress.sessionLength.gt(BN_ONE))
? (
forcing.isForceAlways
? progress.sessionLength
: progress.eraLength
).mul(
historyDepth
.sub(progress.activeEra)
.iadd(era)
.iadd(BN_ONE)
).isub(
forcing.isForceAlways
? progress.sessionProgress
: progress.eraProgress
)
: undefined,
[era, forcing, historyDepth, progress]
);
}
export default createNamedHook('useEraBlocks', useEraBlocksImpl);