diff --git a/packages/api-derive/src/chain/index.ts b/packages/api-derive/src/chain/index.ts index 55718d40b3..01fd9a63ba 100644 --- a/packages/api-derive/src/chain/index.ts +++ b/packages/api-derive/src/chain/index.ts @@ -5,4 +5,5 @@ export * from './bestNumber'; export * from './bestNumberFinalized'; export * from './bestNumberLag'; export * from './getHeader'; +export * from './subscribeNewBlocks'; export * from './subscribeNewHeads'; diff --git a/packages/api-derive/src/chain/subscribeNewBlocks.ts b/packages/api-derive/src/chain/subscribeNewBlocks.ts new file mode 100644 index 0000000000..bf48f9eb6d --- /dev/null +++ b/packages/api-derive/src/chain/subscribeNewBlocks.ts @@ -0,0 +1,32 @@ +// Copyright 2017-2020 @polkadot/api-derive authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { ApiInterfaceRx } from '@polkadot/api/types'; +import { EventRecord, Hash, SignedBlock } from '@polkadot/types/interfaces'; +import { FullNewBlock } from '../types'; + +import { Observable, combineLatest, of } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; + +import { memo } from '../util'; + +/** + * @name subscribeNewBlocks + * @returns The latest block & events for that block + */ +export function subscribeNewBlocks (instanceId: string, api: ApiInterfaceRx): () => Observable { + return memo(instanceId, (): Observable => + api.derive.chain.subscribeNewHeads().pipe( + switchMap((header): Observable<[Hash, EventRecord[], SignedBlock]> => { + const blockHash = header.hash; + + return combineLatest( + of(blockHash), + api.query.system.events.at(blockHash), + api.rpc.chain.getBlock(header.hash) + ); + }), + map(([blockHash, events, block]) => ({ block: block.block, blockHash, blockNumber: block.block.header.number.unwrap(), events, justification: block.justification })) + ) + ); +} diff --git a/packages/api-derive/src/chain/subscribeNewHeads.ts b/packages/api-derive/src/chain/subscribeNewHeads.ts index 82fa3fc058..5ca357e559 100644 --- a/packages/api-derive/src/chain/subscribeNewHeads.ts +++ b/packages/api-derive/src/chain/subscribeNewHeads.ts @@ -11,7 +11,7 @@ import { memo } from '../util'; /** * @name subscribeNewHeads - * @returns An array containing the block header and the block author + * @returns A header with the current header (including extracted author) * @description An observable of the current block header and it's author * @example *
diff --git a/packages/api-derive/src/chain/types.ts b/packages/api-derive/src/chain/types.ts new file mode 100644 index 0000000000..ac54b1f54c --- /dev/null +++ b/packages/api-derive/src/chain/types.ts @@ -0,0 +1,12 @@ +// Copyright 2017-2020 @polkadot/api-derive authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { Block, BlockNumber, EventRecord, Hash, Justification } from '@polkadot/types/interfaces'; + +export interface FullNewBlock { + block: Block; + blockHash: Hash; + blockNumber: BlockNumber; + events: EventRecord[]; + justification: Justification; +} diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index 1dfbe5ef66..15fda3c178 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -7,6 +7,7 @@ import { AccountId, Balance, BalanceLock, BalanceLockTo212, BalanceOf, Bid, BidK import { u32 } from '@polkadot/types'; export * from './accounts/types'; +export * from './chain/types'; export * from './council/types'; export * from './democracy/types'; export * from './elections/types';