31 lines
859 B
TypeScript
31 lines
859 B
TypeScript
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { Observable } from 'rxjs';
|
|
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
|
import type { EventRecord, Hash, SignedBlock } from '@polkadot/types/interfaces';
|
|
|
|
import { combineLatest, map, switchMap } from 'rxjs';
|
|
|
|
import { memo } from '../util';
|
|
|
|
interface Result {
|
|
block: SignedBlock;
|
|
events: EventRecord[];
|
|
}
|
|
|
|
export function events (instanceId: string, api: ApiInterfaceRx): (at: Hash) => Observable<Result> {
|
|
return memo(instanceId, (blockHash: Hash) =>
|
|
combineLatest([
|
|
api.rpc.chain.getBlock(blockHash),
|
|
api.queryAt(blockHash).pipe(
|
|
switchMap((queryAt) =>
|
|
queryAt.system.events()
|
|
)
|
|
)
|
|
]).pipe(
|
|
map(([block, events]): Result => ({ block, events }))
|
|
)
|
|
);
|
|
}
|