Add registry error lookup (#1723)

This commit is contained in:
Jaco Greeff
2020-01-16 14:46:21 +01:00
committed by GitHub
parent be258a1f7c
commit dd97e9daee
9 changed files with 79 additions and 31 deletions
+1 -1
View File
@@ -32,6 +32,6 @@
"@polkadot/types": "^1.0.0-beta.7"
},
"devDependencies": {
"@polkadot/keyring": "^2.0.0-beta.2"
"@polkadot/keyring": "^2.0.0-beta.4"
}
}
+3 -3
View File
@@ -28,14 +28,14 @@
"dependencies": {
"@babel/runtime": "^7.7.7",
"@polkadot/api-derive": "^1.0.0-beta.7",
"@polkadot/keyring": "^2.0.0-beta.2",
"@polkadot/keyring": "^2.0.0-beta.4",
"@polkadot/metadata": "^1.0.0-beta.7",
"@polkadot/rpc-core": "^1.0.0-beta.7",
"@polkadot/rpc-provider": "^1.0.0-beta.7",
"@polkadot/types": "^1.0.0-beta.7",
"@polkadot/util-crypto": "^2.0.0-beta.2"
"@polkadot/util-crypto": "^2.0.0-beta.4"
},
"devDependencies": {
"@polkadot/keyring": "^2.0.0-beta.2"
"@polkadot/keyring": "^2.0.0-beta.4"
}
}
+3 -3
View File
@@ -28,10 +28,10 @@
"dependencies": {
"@babel/runtime": "^7.7.7",
"@polkadot/types": "^1.0.0-beta.7",
"@polkadot/util": "^2.0.0-beta.2",
"@polkadot/util-crypto": "^2.0.0-beta.2"
"@polkadot/util": "^2.0.0-beta.4",
"@polkadot/util-crypto": "^2.0.0-beta.4"
},
"devDependencies": {
"@polkadot/keyring": "^2.0.0-beta.2"
"@polkadot/keyring": "^2.0.0-beta.4"
}
}
+1 -1
View File
@@ -30,7 +30,7 @@
"@polkadot/jsonrpc": "^1.0.0-beta.7",
"@polkadot/rpc-provider": "^1.0.0-beta.7",
"@polkadot/types": "^1.0.0-beta.7",
"@polkadot/util": "^2.0.0-beta.2",
"@polkadot/util": "^2.0.0-beta.4",
"rxjs": "^6.5.4"
}
}
+3 -3
View File
@@ -28,14 +28,14 @@
"dependencies": {
"@babel/runtime": "^7.7.7",
"@polkadot/metadata": "^1.0.0-beta.7",
"@polkadot/util": "^2.0.0-beta.2",
"@polkadot/util-crypto": "^2.0.0-beta.2",
"@polkadot/util": "^2.0.0-beta.4",
"@polkadot/util-crypto": "^2.0.0-beta.4",
"eventemitter3": "^4.0.0",
"isomorphic-fetch": "^2.2.1",
"websocket": "^1.0.31"
},
"devDependencies": {
"@polkadot/keyring": "^2.0.0-beta.2",
"@polkadot/keyring": "^2.0.0-beta.4",
"mock-socket": "^9.0.2",
"nock": "^11.7.1"
}
+3 -3
View File
@@ -28,12 +28,12 @@
"dependencies": {
"@babel/runtime": "^7.7.7",
"@polkadot/metadata": "^1.0.0-beta.7",
"@polkadot/util": "^2.0.0-beta.2",
"@polkadot/util-crypto": "^2.0.0-beta.2",
"@polkadot/util": "^2.0.0-beta.4",
"@polkadot/util-crypto": "^2.0.0-beta.4",
"@types/memoizee": "^0.4.3",
"memoizee": "^0.4.14"
},
"devDependencies": {
"@polkadot/keyring": "^2.0.0-beta.2"
"@polkadot/keyring": "^2.0.0-beta.4"
}
}
+32 -1
View File
@@ -3,7 +3,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { CallFunction, Codec, Constructor, RegistryTypes, Registry, RegistryMetadata, TypeDef } from '../../types';
import { CallFunction, Codec, Constructor, RegistryError, RegistryTypes, Registry, RegistryMetadata, TypeDef } from '../../types';
import extrinsicsFromMeta from '@polkadot/metadata/Decorated/extrinsics/fromMetadata';
import { assert, isFunction, isString, isUndefined, stringCamelCase, u8aToHex } from '@polkadot/util';
@@ -14,6 +14,25 @@ import { createClass } from './createClass';
import { getTypeClass } from './getTypeClass';
import { getTypeDef } from './getTypeDef';
// create error mapping from metadata
function decorateErrors (_: Registry, metadata: RegistryMetadata, metadataErrors: Record<string, RegistryError>): void {
// decorate the errors
metadata.asLatest.modules.forEach((section, sectionIndex): void => {
const sectionName = stringCamelCase(section.name.toString());
section.errors.forEach(({ name, documentation }, index): void => {
const eventIndex = new Uint8Array([sectionIndex, index]);
metadataErrors[u8aToHex(eventIndex)] = {
documentation: documentation.map((doc): string => doc.toString()),
index,
name: name.toString(),
section: sectionName
};
});
});
}
// create event classes from metadata
function decorateEvents (registry: Registry, metadata: RegistryMetadata, metadataEvents: Record<string, Constructor<EventData>>): void {
// decorate the events
@@ -62,6 +81,8 @@ export class TypeRegistry implements Registry {
private _metadataCalls: Record<string, CallFunction> = {};
private _metadataErrors: Record<string, RegistryError> = {};
private _metadataEvents: Record<string, Constructor<EventData>> = {};
constructor () {
@@ -89,6 +110,15 @@ export class TypeRegistry implements Registry {
return fn;
}
public findMetaError (errorIndex: Uint8Array): RegistryError {
const hexIndex = u8aToHex(errorIndex);
const error = this._metadataErrors[hexIndex];
assert(!isUndefined(error), `findMetaError: Unable to find Error with index ${hexIndex}/[${errorIndex}]`);
return error;
}
public findMetaEvent (eventIndex: Uint8Array): Constructor<EventData> {
const hexIndex = u8aToHex(eventIndex);
const Event = this._metadataEvents[hexIndex];
@@ -189,6 +219,7 @@ export class TypeRegistry implements Registry {
// sets the metadata
public setMetadata (metadata: RegistryMetadata): void {
decorateExtrinsics(this, metadata, this._metadataCalls);
decorateErrors(this, metadata, this._metadataErrors);
decorateEvents(this, metadata, this._metadataEvents);
}
}
+18 -1
View File
@@ -336,6 +336,22 @@ export interface RegistryMetadataCalls {
unwrap (): RegistryMetadataCall[];
}
export interface RegistryError {
documentation: string[];
index: number;
name: string;
section: string;
}
export interface RegistryMetadataError {
// eslint-disable-next-line @typescript-eslint/ban-types
name: String;
// eslint-disable-next-line @typescript-eslint/ban-types
documentation: String[];
}
export type RegistryMetadataErrors = RegistryMetadataError[];
export interface RegistryMetadataEvent {
args: any[];
// eslint-disable-next-line @typescript-eslint/ban-types
@@ -349,6 +365,7 @@ export interface RegistryMetadataEvents {
export interface RegistryMetadataModule {
calls: RegistryMetadataCalls;
errors: RegistryMetadataErrors;
events: RegistryMetadataEvents;
// eslint-disable-next-line @typescript-eslint/ban-types
name: String & Codec;
@@ -364,7 +381,7 @@ export interface RegistryMetadata {
export interface Registry {
findMetaCall (callIndex: Uint8Array): CallFunction;
findMetaError (errorIndex: Uint8Array): any;
// due to same circular imports where types don't really want to import from EventData,
// keep this as a generic Codec, however the actual impl. returns the correct
findMetaEvent (eventIndex: Uint8Array): Constructor<any>;