UNPKG

2.9 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.SubmittableResult = void 0;
4const recordIdentity = (record) => record;
5function filterAndApply(events, section, methods, onFound) {
6 return events
7 .filter(({ event }) => section === event.section &&
8 methods.includes(event.method))
9 .map((record) => onFound(record));
10}
11function getDispatchError({ event: { data: [dispatchError] } }) {
12 return dispatchError;
13}
14function getDispatchInfo({ event: { data, method } }) {
15 return method === 'ExtrinsicSuccess'
16 ? data[0]
17 : data[1];
18}
19function extractError(events = []) {
20 return filterAndApply(events, 'system', ['ExtrinsicFailed'], getDispatchError)[0];
21}
22function extractInfo(events = []) {
23 return filterAndApply(events, 'system', ['ExtrinsicFailed', 'ExtrinsicSuccess'], getDispatchInfo)[0];
24}
25class SubmittableResult {
26 dispatchError;
27 dispatchInfo;
28 internalError;
29 events;
30 status;
31 txHash;
32 txIndex;
33 blockNumber;
34 constructor({ blockNumber, dispatchError, dispatchInfo, events, internalError, status, txHash, txIndex }) {
35 this.dispatchError = dispatchError || extractError(events);
36 this.dispatchInfo = dispatchInfo || extractInfo(events);
37 this.events = events || [];
38 this.internalError = internalError;
39 this.status = status;
40 this.txHash = txHash;
41 this.txIndex = txIndex;
42 this.blockNumber = blockNumber;
43 }
44 get isCompleted() {
45 return this.isError || this.status.isInBlock || this.status.isFinalized;
46 }
47 get isError() {
48 return this.status.isDropped || this.status.isFinalityTimeout || this.status.isInvalid || this.status.isUsurped;
49 }
50 get isFinalized() {
51 return this.status.isFinalized;
52 }
53 get isInBlock() {
54 return this.status.isInBlock;
55 }
56 get isWarning() {
57 return this.status.isRetracted;
58 }
59 /**
60 * @description Filters EventRecords for the specified method & section (there could be multiple)
61 */
62 filterRecords(section, method) {
63 return filterAndApply(this.events, section, Array.isArray(method) ? method : [method], recordIdentity);
64 }
65 /**
66 * @description Finds an EventRecord for the specified method & section
67 */
68 findRecord(section, method) {
69 return this.filterRecords(section, method)[0];
70 }
71 /**
72 * @description Creates a human representation of the output
73 */
74 toHuman(isExtended) {
75 return {
76 dispatchError: this.dispatchError?.toHuman(),
77 dispatchInfo: this.dispatchInfo?.toHuman(),
78 events: this.events.map((e) => e.toHuman(isExtended)),
79 internalError: this.internalError?.message.toString(),
80 status: this.status.toHuman(isExtended)
81 };
82 }
83}
84exports.SubmittableResult = SubmittableResult;