1 | import { Null, Struct, Tuple } from '@polkadot/types-codec';
|
2 | import { objectProperties, objectSpread } from '@polkadot/util';
|
3 |
|
4 | function decodeEvent(registry, value) {
|
5 | if (!value?.length) {
|
6 | return { DataType: Null };
|
7 | }
|
8 | const index = value.subarray(0, 2);
|
9 | return {
|
10 | DataType: registry.findMetaEvent(index),
|
11 | value: {
|
12 | data: value.subarray(2),
|
13 | index
|
14 | }
|
15 | };
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | export class GenericEventData extends Tuple {
|
23 | __internal__meta;
|
24 | __internal__method;
|
25 | __internal__names = null;
|
26 | __internal__section;
|
27 | __internal__typeDef;
|
28 | constructor(registry, value, meta, section = '<unknown>', method = '<unknown>') {
|
29 | const fields = meta?.fields || [];
|
30 | super(registry, fields.map(({ type }) => registry.createLookupType(type)), value);
|
31 | this.__internal__meta = meta;
|
32 | this.__internal__method = method;
|
33 | this.__internal__section = section;
|
34 | this.__internal__typeDef = fields.map(({ type }) => registry.lookup.getTypeDef(type));
|
35 | const names = fields
|
36 | .map(({ name }) => registry.lookup.sanitizeField(name)[0])
|
37 | .filter((n) => !!n);
|
38 | if (names.length === fields.length) {
|
39 | this.__internal__names = names;
|
40 | objectProperties(this, names, (_, i) => this[i]);
|
41 | }
|
42 | }
|
43 | |
44 |
|
45 |
|
46 | get meta() {
|
47 | return this.__internal__meta;
|
48 | }
|
49 | |
50 |
|
51 |
|
52 | get method() {
|
53 | return this.__internal__method;
|
54 | }
|
55 | |
56 |
|
57 |
|
58 | get names() {
|
59 | return this.__internal__names;
|
60 | }
|
61 | |
62 |
|
63 |
|
64 | get section() {
|
65 | return this.__internal__section;
|
66 | }
|
67 | |
68 |
|
69 |
|
70 | get typeDef() {
|
71 | return this.__internal__typeDef;
|
72 | }
|
73 | |
74 |
|
75 |
|
76 | toHuman(isExtended, disableAscii) {
|
77 | if (this.__internal__names !== null) {
|
78 | const json = {};
|
79 | for (let i = 0, count = this.__internal__names.length; i < count; i++) {
|
80 | json[this.__internal__names[i]] = this[i].toHuman(isExtended, disableAscii);
|
81 | }
|
82 | return json;
|
83 | }
|
84 | return super.toHuman(isExtended);
|
85 | }
|
86 | }
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | export class GenericEvent extends Struct {
|
94 |
|
95 |
|
96 | constructor(registry, _value) {
|
97 | const { DataType, value } = decodeEvent(registry, _value);
|
98 | super(registry, {
|
99 | index: 'EventId',
|
100 |
|
101 | data: DataType
|
102 | }, value);
|
103 | }
|
104 | |
105 |
|
106 |
|
107 | get data() {
|
108 | return this.getT('data');
|
109 | }
|
110 | |
111 |
|
112 |
|
113 | get index() {
|
114 | return this.getT('index');
|
115 | }
|
116 | |
117 |
|
118 |
|
119 | get meta() {
|
120 | return this.data.meta;
|
121 | }
|
122 | |
123 |
|
124 |
|
125 | get method() {
|
126 | return this.data.method;
|
127 | }
|
128 | |
129 |
|
130 |
|
131 | get section() {
|
132 | return this.data.section;
|
133 | }
|
134 | |
135 |
|
136 |
|
137 | get typeDef() {
|
138 | return this.data.typeDef;
|
139 | }
|
140 | |
141 |
|
142 |
|
143 | toHuman(isExpanded, disableAscii) {
|
144 | return objectSpread({
|
145 | method: this.method,
|
146 | section: this.section
|
147 | }, isExpanded
|
148 | ? { docs: this.meta.docs.map((d) => d.toString()) }
|
149 | : null, super.toHuman(isExpanded, disableAscii));
|
150 | }
|
151 | }
|