derive.chain.getBlock() now maps events to extrinsics (#3020)
* derive.chain.getBlock() now maps events to extrinsics * Also apply to subscribeNewBlocks * Cleanup * breaking
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -27,12 +27,13 @@ export function getBlock (instanceId: string, api: ApiInterfaceRx): (hash: Uint8
|
||||
return memo(instanceId, (hash: Uint8Array | string): Observable<SignedBlockExtended | undefined> =>
|
||||
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<undefined> =>
|
||||
// where rpc.chain.getHeader throws, we will land here - it can happen that
|
||||
|
||||
@@ -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<FullNewBlock> {
|
||||
return memo(instanceId, (): Observable<FullNewBlock> =>
|
||||
export function subscribeNewBlocks (instanceId: string, api: ApiInterfaceRx): () => Observable<SignedBlockExtended> {
|
||||
return memo(instanceId, (): Observable<SignedBlockExtended> =>
|
||||
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)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<Hea
|
||||
*/
|
||||
export class HeaderExtended extends _Header {
|
||||
readonly #author?: AccountId;
|
||||
readonly #validators?: AccountId[];
|
||||
|
||||
constructor (registry: Registry, header?: Header, sessionValidators?: AccountId[]) {
|
||||
constructor (registry: Registry, header?: Header, validators?: AccountId[]) {
|
||||
super(registry, header);
|
||||
|
||||
this.#author = extractAuthor(this.digest, sessionValidators);
|
||||
this.#author = extractAuthor(this.digest, validators);
|
||||
this.#validators = validators;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,26 +36,9 @@ export class HeaderExtended extends _Header {
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Creates a human-friendly JSON representation
|
||||
* @description Convenience method, returns the validators for the block
|
||||
*/
|
||||
public toHuman (isExtended?: boolean): Record<string, AnyJson> {
|
||||
return {
|
||||
...super.toHuman(isExtended),
|
||||
author: this.author
|
||||
? this.author.toHuman()
|
||||
: undefined
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Creates the JSON representation
|
||||
*/
|
||||
public toJSON (): Record<string, AnyJson> {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
author: this.author
|
||||
? this.author.toJSON()
|
||||
: undefined
|
||||
};
|
||||
public get validators (): AccountId[] | undefined {
|
||||
return this.#validators;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<SignedBlock>;
|
||||
|
||||
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<string, AnyJson> {
|
||||
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<string, AnyJson> {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
author: this.author
|
||||
? this.author.toJSON()
|
||||
: undefined
|
||||
};
|
||||
public get extrinsics (): TxWithEvent[] {
|
||||
return this.#extrinsics;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user