Files
api/packages/api-derive/src/chain/getBlockByNumber.ts
T
Valentin Fernandez ed62d164ea Document available Api Derives (#6106)
* document derives from accounts to democracy

* Derive Documentation progress

* Derive Documentation progress

* tx and staking docs

* Staking docs progress

* more staking docs

* Staking module complete

* imonline, elections and progress in democracy

* Document missing derives

* lint
2025-02-27 10:56:34 -03:00

31 lines
1.1 KiB
TypeScript

// Copyright 2017-2025 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Observable } from 'rxjs';
import type { AnyNumber } from '@polkadot/types/types';
import type { SignedBlockExtended } from '../type/types.js';
import type { DeriveApi } from '../types.js';
import { switchMap } from 'rxjs';
import { memo } from '../util/index.js';
/**
* @name getBlockByNumber
* @param {( BN | bigint | Uint8Array | number | string )} blockNumber
* @description Get a specific block (e.g. rpc.chain.getBlock) and extend it with the author by block number
* @example
* ```javascript
* const { author, block } = await api.derive.chain.getBlockByNumber(123);
*
* console.log(`block #${block.header.number} was authored by ${author}`);
* ```
*/
export function getBlockByNumber (instanceId: string, api: DeriveApi): (blockNumber: AnyNumber) => Observable<SignedBlockExtended> {
return memo(instanceId, (blockNumber: AnyNumber): Observable<SignedBlockExtended> =>
api.rpc.chain.getBlockHash(blockNumber).pipe(
switchMap((h) => api.derive.chain.getBlock(h))
)
);
}