1 |
|
2 |
|
3 | const recordIdentity = record => record;
|
4 |
|
5 | function filterAndApply(events, section, methods, onFound) {
|
6 | return events.filter(({
|
7 | event
|
8 | }) => section === event.section && methods.includes(event.method)).map(record => onFound(record));
|
9 | }
|
10 |
|
11 | function getDispatchError({
|
12 | event: {
|
13 | data: [dispatchError]
|
14 | }
|
15 | }) {
|
16 | return dispatchError;
|
17 | }
|
18 |
|
19 | function getDispatchInfo({
|
20 | event: {
|
21 | data,
|
22 | method
|
23 | }
|
24 | }) {
|
25 | return method === 'ExtrinsicSuccess' ? data[0] : data[1];
|
26 | }
|
27 |
|
28 | function extractError(events = []) {
|
29 | return filterAndApply(events, 'system', ['ExtrinsicFailed'], getDispatchError)[0];
|
30 | }
|
31 |
|
32 | function extractInfo(events = []) {
|
33 | return filterAndApply(events, 'system', ['ExtrinsicFailed', 'ExtrinsicSuccess'], getDispatchInfo)[0];
|
34 | }
|
35 |
|
36 | export class SubmittableResult {
|
37 | constructor({
|
38 | dispatchError,
|
39 | dispatchInfo,
|
40 | events,
|
41 | internalError,
|
42 | status,
|
43 | txHash,
|
44 | txIndex
|
45 | }) {
|
46 | this.dispatchError = dispatchError || extractError(events);
|
47 | this.dispatchInfo = dispatchInfo || extractInfo(events);
|
48 | this.events = events || [];
|
49 | this.internalError = internalError;
|
50 | this.status = status;
|
51 | this.txHash = txHash;
|
52 | this.txIndex = txIndex;
|
53 | }
|
54 |
|
55 | get isCompleted() {
|
56 | return this.isError || this.status.isInBlock || this.status.isFinalized;
|
57 | }
|
58 |
|
59 | get isError() {
|
60 | return this.status.isDropped || this.status.isFinalityTimeout || this.status.isInvalid || this.status.isUsurped;
|
61 | }
|
62 |
|
63 | get isFinalized() {
|
64 | return this.status.isFinalized;
|
65 | }
|
66 |
|
67 | get isInBlock() {
|
68 | return this.status.isInBlock;
|
69 | }
|
70 |
|
71 | get isWarning() {
|
72 | return this.status.isRetracted;
|
73 | }
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 | filterRecords(section, method) {
|
80 | return filterAndApply(this.events, section, Array.isArray(method) ? method : [method], recordIdentity);
|
81 | }
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 | findRecord(section, method) {
|
88 | return this.filterRecords(section, method)[0];
|
89 | }
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 | toHuman(isExtended) {
|
96 | var _this$dispatchError, _this$dispatchInfo, _this$internalError;
|
97 |
|
98 | return {
|
99 | dispatchError: (_this$dispatchError = this.dispatchError) === null || _this$dispatchError === void 0 ? void 0 : _this$dispatchError.toHuman(),
|
100 | dispatchInfo: (_this$dispatchInfo = this.dispatchInfo) === null || _this$dispatchInfo === void 0 ? void 0 : _this$dispatchInfo.toHuman(),
|
101 | events: this.events.map(e => e.toHuman(isExtended)),
|
102 | internalError: (_this$internalError = this.internalError) === null || _this$internalError === void 0 ? void 0 : _this$internalError.message.toString(),
|
103 | status: this.status.toHuman(isExtended)
|
104 | };
|
105 | }
|
106 |
|
107 | } |
\ | No newline at end of file |