Expose dispatch{Error, Info} on SubmittableResult (#2735)

* Expose dispatch{Error, Info} on SubmittableResult

* Cleanups

* De-dup
This commit is contained in:
Jaco Greeff
2020-10-15 17:07:14 +02:00
committed by GitHub
parent d389c6737e
commit e25272c986
2 changed files with 37 additions and 9 deletions
+34 -8
View File
@@ -1,16 +1,44 @@
// Copyright 2017-2020 @polkadot/api authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { ExtrinsicStatus, EventRecord } from '@polkadot/types/interfaces';
import { DispatchError, DispatchInfo, ExtrinsicStatus, EventRecord } from '@polkadot/types/interfaces';
import { AnyJson, ISubmittableResult } from '@polkadot/types/types';
import { SubmittableResultValue } from './types';
const recordIdentity = (record: EventRecord) => record;
function filterAndApply <T> (events: EventRecord[], section: string, methods: string[], onFound: (record: EventRecord) => T): T[] {
return events
.filter(({ event }) => section === event.section && methods.includes(event.method))
.map((record) => onFound(record));
}
function extractError (events: EventRecord[] = []): DispatchError | undefined {
return filterAndApply(events, 'system', ['ExtrinsicFailed'], ({ event: { data } }) =>
data[0] as DispatchError
)[0];
}
function extractInfo (events: EventRecord[] = []): DispatchInfo | undefined {
return filterAndApply(events, 'system', ['ExtrinsicFailed', 'ExtrinsicSuccess'], ({ event: { data, method } }) =>
method === 'ExtrinsicSuccess'
? data[0] as DispatchInfo
: data[1] as DispatchInfo
)[0];
}
export default class SubmittableResult implements ISubmittableResult {
public readonly dispatchError?: DispatchError;
public readonly dispatchInfo?: DispatchInfo;
public readonly events: EventRecord[];
public readonly status: ExtrinsicStatus;
constructor ({ events, status }: SubmittableResultValue) {
constructor ({ dispatchError, dispatchInfo, events, status }: SubmittableResultValue) {
this.dispatchError = dispatchError || extractError(events);
this.dispatchInfo = dispatchInfo || extractInfo(events);
this.events = events || [];
this.status = status;
}
@@ -39,18 +67,14 @@ export default class SubmittableResult implements ISubmittableResult {
* @description Filters EventRecords for the specified method & section (there could be multiple)
*/
public filterRecords (section: string, method: string): EventRecord[] {
return this.events.filter(({ event }): boolean =>
event.section === section && event.method === method
);
return filterAndApply(this.events, section, [method], recordIdentity);
}
/**
* @description Finds an EventRecord for the specified method & section
*/
public findRecord (section: string, method: string): EventRecord | undefined {
return this.events.find(({ event }): boolean =>
event.section === section && event.method === method
);
return this.filterRecords(section, method)[0];
}
/**
@@ -58,6 +82,8 @@ export default class SubmittableResult implements ISubmittableResult {
*/
public toHuman (isExtended?: boolean): AnyJson {
return {
dispatchError: this.dispatchError?.toHuman(),
dispatchInfo: this.dispatchInfo?.toHuman(),
events: this.events.map((event) => event.toHuman(isExtended)),
status: this.status.toHuman(isExtended)
};
+3 -1
View File
@@ -1,13 +1,15 @@
// Copyright 2017-2020 @polkadot/api authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { AccountId, Address, Extrinsic, ExtrinsicStatus, EventRecord, Hash, RuntimeDispatchInfo } from '@polkadot/types/interfaces';
import { AccountId, Address, DispatchError, DispatchInfo, Extrinsic, ExtrinsicStatus, EventRecord, Hash, RuntimeDispatchInfo } from '@polkadot/types/interfaces';
import { AnyNumber, Callback, Codec, IExtrinsicEra, IKeyringPair, Signer, ISubmittableResult } from '@polkadot/types/types';
import { ApiTypes } from '../types';
import { Observable } from 'rxjs';
export interface SubmittableResultValue {
dispatchError?: DispatchError;
dispatchInfo?: DispatchInfo;
events?: EventRecord[];
status: ExtrinsicStatus;
}