Event data carries section + method (#369)

* Event data carries section + method

* Expose method & section on event itself

* Expose typeDef on Event via EventData
This commit is contained in:
Jaco Greeff
2018-10-31 17:45:51 +01:00
committed by GitHub
parent cb8a683f2a
commit b09cbf6707
+30 -3
View File
@@ -4,7 +4,7 @@
import { Constructor, ConstructorDef } from './types';
import { isUndefined, u8aToHex } from '@polkadot/util';
import { isUndefined, stringCamelCase, u8aToHex } from '@polkadot/util';
import Struct from './codec/Struct';
import Tuple from './codec/Tuple';
@@ -15,14 +15,26 @@ import Metadata from './Metadata';
const EventTypes: { [index: string]: Constructor<EventData> } = {};
class EventData extends Tuple {
private _method: string;
private _section: string;
private _typeDef: Array<TypeDef>;
constructor (Types: ConstructorDef, value: Uint8Array, typeDef: Array<TypeDef>) {
constructor (Types: ConstructorDef, value: Uint8Array, typeDef: Array<TypeDef>, section: string, method: string) {
super(Types, value);
this._method = method;
this._section = section;
this._typeDef = typeDef;
}
get method (): string {
return this._method;
}
get section (): string {
return this._section;
}
get typeDef (): Array<TypeDef> {
return this._typeDef;
}
@@ -68,7 +80,10 @@ export default class Event extends Struct {
// the available system events to be used in lookups
static injectMetadata (metadata: Metadata): void {
metadata.events.forEach((section, sectionIndex) => {
const sectionName = stringCamelCase(section.name.toString());
section.events.forEach((event, methodIndex) => {
const methodName = event.name.toString();
const eventIndex = new Uint8Array([sectionIndex, methodIndex]);
const typeDef = event.arguments.map((arg) => getTypeDef(arg));
const Types = typeDef.reduce((result, def, index) => {
@@ -79,7 +94,7 @@ export default class Event extends Struct {
EventTypes[eventIndex.toString()] = class extends EventData {
constructor (value: Uint8Array) {
super(Types, value, typeDef);
super(Types, value, typeDef, sectionName, methodName);
}
};
});
@@ -93,4 +108,16 @@ export default class Event extends Struct {
get index (): EventIndex {
return this.get('index') as EventIndex;
}
get method (): string {
return this.data.method;
}
get section (): string {
return this.data.section;
}
get typeDef (): Array<TypeDef> {
return this.data.typeDef;
}
}