From 1c86333b5f539e9571c06e4a9b828ce0e8555776 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 5 Jan 2021 12:21:58 +0100 Subject: [PATCH] derive.chain.getBlock() now maps events to extrinsics (#3020) * derive.chain.getBlock() now maps events to extrinsics * Also apply to subscribeNewBlocks * Cleanup * breaking --- CHANGELOG.md | 9 +++ packages/api-derive/src/chain/getBlock.ts | 5 +- .../src/chain/subscribeNewBlocks.ts | 18 +++--- packages/api-derive/src/chain/types.ts | 12 ---- .../api-derive/src/type/HeaderExtended.ts | 31 +++------- .../src/type/SignedBlockExtended.ts | 62 +++++++++++++------ packages/api-derive/src/types.ts | 1 - 7 files changed, 73 insertions(+), 65 deletions(-) delete mode 100644 packages/api-derive/src/chain/types.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d1c803464..ad841383e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # CHANGELOG +## 3.4.0-x + +- **Breaking change** The `derive.chain.{getBlock, subscribeNewBlocks}` now return `SignedBlockExtended`, all with the actual extrinsics and events mapped. Users of the latter interface may need some updates since the result structure may be different. + +Changes: + +- `derive.chain.getBlock()` now maps events to extrinsics via `.extrinsics` getter + + ## 3.3.1 Jan 4, 2021 Upgrade priority: Low. Recommended for users of frontier evm module with additional types. diff --git a/packages/api-derive/src/chain/getBlock.ts b/packages/api-derive/src/chain/getBlock.ts index ad2dac8a5d..b512698b5b 100644 --- a/packages/api-derive/src/chain/getBlock.ts +++ b/packages/api-derive/src/chain/getBlock.ts @@ -27,12 +27,13 @@ export function getBlock (instanceId: string, api: ApiInterfaceRx): (hash: Uint8 return memo(instanceId, (hash: Uint8Array | string): Observable => combineLatest([ api.rpc.chain.getBlock(hash), + api.query.system.events.at(hash), api.query.session ? api.query.session.validators.at(hash) : of([]) ]).pipe( - map(([signedBlock, validators]): SignedBlockExtended => - new SignedBlockExtended(api.registry, signedBlock, validators) + map(([signedBlock, events, validators]): SignedBlockExtended => + new SignedBlockExtended(api.registry, signedBlock, events, validators) ), catchError((): Observable => // where rpc.chain.getHeader throws, we will land here - it can happen that diff --git a/packages/api-derive/src/chain/subscribeNewBlocks.ts b/packages/api-derive/src/chain/subscribeNewBlocks.ts index f71b65474a..5a6d759501 100644 --- a/packages/api-derive/src/chain/subscribeNewBlocks.ts +++ b/packages/api-derive/src/chain/subscribeNewBlocks.ts @@ -2,32 +2,34 @@ // SPDX-License-Identifier: Apache-2.0 import type { ApiInterfaceRx } from '@polkadot/api/types'; -import type { EventRecord, Hash, SignedBlock } from '@polkadot/types/interfaces'; +import type { AccountId, EventRecord, SignedBlock } from '@polkadot/types/interfaces'; import type { Observable } from '@polkadot/x-rxjs'; -import type { FullNewBlock } from '../types'; import { combineLatest, of } from '@polkadot/x-rxjs'; import { map, switchMap } from '@polkadot/x-rxjs/operators'; +import { SignedBlockExtended } from '../type'; 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 => +export function subscribeNewBlocks (instanceId: string, api: ApiInterfaceRx): () => Observable { + return memo(instanceId, (): Observable => api.derive.chain.subscribeNewHeads().pipe( - switchMap((header): Observable<[Hash, EventRecord[], SignedBlock]> => { + switchMap((header): Observable<[SignedBlock, EventRecord[], AccountId[] | undefined]> => { const blockHash = header.hash; return combineLatest( - of(blockHash), + api.rpc.chain.getBlock(blockHash), api.query.system.events.at(blockHash), - api.rpc.chain.getBlock(header.hash) + of(header.validators) ); }), - map(([blockHash, events, block]) => ({ block: block.block, blockHash, blockNumber: block.block.header.number.unwrap(), events, justification: block.justification })) + map(([block, events, validators]) => + new SignedBlockExtended(api.registry, block, events, validators) + ) ) ); } diff --git a/packages/api-derive/src/chain/types.ts b/packages/api-derive/src/chain/types.ts deleted file mode 100644 index 105515f33a..0000000000 --- a/packages/api-derive/src/chain/types.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2021 @polkadot/api-derive authors & contributors -// SPDX-License-Identifier: Apache-2.0 - -import type { 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/type/HeaderExtended.ts b/packages/api-derive/src/type/HeaderExtended.ts index 1740dde8fb..b9e53c4e86 100644 --- a/packages/api-derive/src/type/HeaderExtended.ts +++ b/packages/api-derive/src/type/HeaderExtended.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { AccountId, Header } from '@polkadot/types/interfaces'; -import type { AnyJson, Constructor, Registry } from '@polkadot/types/types'; +import type { Constructor, Registry } from '@polkadot/types/types'; import { Struct } from '@polkadot/types'; import runtimeTypes from '@polkadot/types/interfaces/runtime/definitions'; @@ -19,11 +19,13 @@ const _Header = Struct.with(runtimeTypes.types.Header as any) as Constructor { - return { - ...super.toHuman(isExtended), - author: this.author - ? this.author.toHuman() - : undefined - }; - } - - /** - * @description Creates the JSON representation - */ - public toJSON (): Record { - return { - ...super.toJSON(), - author: this.author - ? this.author.toJSON() - : undefined - }; + public get validators (): AccountId[] | undefined { + return this.#validators; } } diff --git a/packages/api-derive/src/type/SignedBlockExtended.ts b/packages/api-derive/src/type/SignedBlockExtended.ts index 9e77233067..0104715a27 100644 --- a/packages/api-derive/src/type/SignedBlockExtended.ts +++ b/packages/api-derive/src/type/SignedBlockExtended.ts @@ -1,8 +1,8 @@ // Copyright 2017-2021 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { AccountId, SignedBlock } from '@polkadot/types/interfaces'; -import type { AnyJson, Constructor, Registry } from '@polkadot/types/types'; +import type { AccountId, DispatchError, DispatchInfo, Event, EventRecord, Extrinsic, SignedBlock } from '@polkadot/types/interfaces'; +import type { Constructor, Registry } from '@polkadot/types/types'; import { Struct } from '@polkadot/types'; import runtimeTypes from '@polkadot/types/interfaces/runtime/definitions'; @@ -12,6 +12,36 @@ import { extractAuthor } from './util'; // We can ignore the properties, added via Struct.with const _SignedBlock = Struct.with(runtimeTypes.types.SignedBlock as any) as Constructor; +interface TxWithEvent { + dispatchError?: DispatchError; + dispatchInfo?: DispatchInfo; + events: Event[]; + extrinsic: Extrinsic; +} + +function mapExtrinsics (extrinsics: Extrinsic[], records: EventRecord[]): TxWithEvent[] { + return extrinsics.map((extrinsic, index): TxWithEvent => { + let dispatchError: DispatchError | undefined; + let dispatchInfo: DispatchInfo | undefined; + + const events = records + .filter(({ phase }) => phase.isApplyExtrinsic && phase.asApplyExtrinsic.eq(index)) + .map(({ event }) => { + if (event.section === 'system') { + if (event.method === 'ExtrinsicSuccess') { + dispatchInfo = event.data[0] as DispatchInfo; + } else if (event.method === 'ExtrinsicFailed') { + dispatchError = event.data[0] as DispatchError; + } + } + + return event; + }); + + return { dispatchError, dispatchInfo, events, extrinsic }; + }); +} + /** * @name SignedBlockExtended * @description @@ -19,11 +49,15 @@ const _SignedBlock = Struct.with(runtimeTypes.types.SignedBlock as any) as Const */ export class SignedBlockExtended extends _SignedBlock { readonly #author?: AccountId; + readonly #events: EventRecord[]; + readonly #extrinsics: TxWithEvent[]; - constructor (registry: Registry, block?: SignedBlock, sessionValidators?: AccountId[]) { + constructor (registry: Registry, block?: SignedBlock, events?: EventRecord[], sessionValidators?: AccountId[]) { super(registry, block); this.#author = extractAuthor(this.block.header.digest, sessionValidators); + this.#events = events || ([] as EventRecord[]); + this.#extrinsics = mapExtrinsics(this.block.extrinsics, this.#events); } /** @@ -34,26 +68,16 @@ export class SignedBlockExtended extends _SignedBlock { } /** - * @description Creates a human-friendly JSON representation + * @description Convenience method, returns the events associated with the block */ - public toHuman (isExtended?: boolean): Record { - return { - ...super.toHuman(isExtended), - author: this.author - ? this.author.toHuman() - : undefined - }; + public get events (): EventRecord[] { + return this.#events; } /** - * @description Creates the JSON representation + * @description Returns the extrinsics and their events, mapped */ - public toJSON (): Record { - return { - ...super.toJSON(), - author: this.author - ? this.author.toJSON() - : undefined - }; + public get extrinsics (): TxWithEvent[] { + return this.#extrinsics; } } diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index e25b5ef17d..a9cb843475 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -6,7 +6,6 @@ import type { u32 } from '@polkadot/types'; import type { AccountId, Balance, BalanceLock, BalanceLockTo212, BalanceOf, Bid, BidKind, BlockNumber, Bounty, BountyIndex, Hash, Index, Proposal, ProposalIndex, SetIndex, SocietyVote, StrikeCount, TreasuryProposal, Votes, VouchingStatus } from '@polkadot/types/interfaces'; export * from './accounts/types'; -export * from './chain/types'; export * from './council/types'; export * from './democracy/types'; export * from './elections/types';