UNPKG

4.48 kBJavaScriptView Raw
1import { Null, Struct, Tuple } from '@polkadot/types-codec';
2import { objectProperties, objectSpread } from '@polkadot/util';
3/** @internal */
4function 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 * @name GenericEventData
19 * @description
20 * Wrapper for the actual data that forms part of an [[Event]]
21 */
22export 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 * @description The wrapped [[EventMetadata]]
45 */
46 get meta() {
47 return this.__internal__meta;
48 }
49 /**
50 * @description The method as a string
51 */
52 get method() {
53 return this.__internal__method;
54 }
55 /**
56 * @description The field names (as available)
57 */
58 get names() {
59 return this.__internal__names;
60 }
61 /**
62 * @description The section as a string
63 */
64 get section() {
65 return this.__internal__section;
66 }
67 /**
68 * @description The [[TypeDef]] for this event
69 */
70 get typeDef() {
71 return this.__internal__typeDef;
72 }
73 /**
74 * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
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 * @name GenericEvent
89 * @description
90 * A representation of a system event. These are generated via the [[Metadata]] interfaces and
91 * specific to a specific Substrate runtime
92 */
93export class GenericEvent extends Struct {
94 // Currently we _only_ decode from Uint8Array, since we expect it to
95 // be used via EventRecord
96 constructor(registry, _value) {
97 const { DataType, value } = decodeEvent(registry, _value);
98 super(registry, {
99 index: 'EventId',
100 // eslint-disable-next-line sort-keys
101 data: DataType
102 }, value);
103 }
104 /**
105 * @description The wrapped [[EventData]]
106 */
107 get data() {
108 return this.getT('data');
109 }
110 /**
111 * @description The [[EventId]], identifying the raw event
112 */
113 get index() {
114 return this.getT('index');
115 }
116 /**
117 * @description The [[EventMetadata]] with the documentation
118 */
119 get meta() {
120 return this.data.meta;
121 }
122 /**
123 * @description The method string identifying the event
124 */
125 get method() {
126 return this.data.method;
127 }
128 /**
129 * @description The section string identifying the event
130 */
131 get section() {
132 return this.data.section;
133 }
134 /**
135 * @description The [[TypeDef]] for the event
136 */
137 get typeDef() {
138 return this.data.typeDef;
139 }
140 /**
141 * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
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}