Files
api/packages/api-derive/src/chain/bestNumberLag.ts
T
Jaco 9fe2798b70 Use .js imports in src (#5526)
* Use .js imports in src

* Adjust

* Fix imports

* Bump dev

* Adjust with .js imports for typegen

* Adjust

* Adjust

* Remove target override
2023-03-09 12:19:12 +02:00

36 lines
1.0 KiB
TypeScript

// Copyright 2017-2023 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Observable } from 'rxjs';
import type { BlockNumber } from '@polkadot/types/interfaces';
import type { DeriveApi } from '../types.js';
import { combineLatest, map } from 'rxjs';
import { memo } from '../util/index.js';
/**
* @name bestNumberLag
* @returns A number of blocks
* @description Calculates the lag between finalized head and best head
* @example
* <BR>
*
* ```javascript
* api.derive.chain.bestNumberLag((lag) => {
* console.log(`finalized is ${lag} blocks behind head`);
* });
* ```
*/
export function bestNumberLag (instanceId: string, api: DeriveApi): () => Observable<BlockNumber> {
return memo(instanceId, (): Observable<BlockNumber> =>
combineLatest([
api.derive.chain.bestNumber(),
api.derive.chain.bestNumberFinalized()
]).pipe(
map(([bestNumber, bestNumberFinalized]): BlockNumber =>
api.registry.createType('BlockNumber', bestNumber.sub(bestNumberFinalized))
)
));
}