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>;
+15 -15
View File
@@ -1902,14 +1902,14 @@
typescript "^3.7.4"
vuepress "^1.2.0"
"@polkadot/keyring@^2.0.0-beta.2":
version "2.0.0-beta.2"
resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-2.0.0-beta.2.tgz#fbe171e1a07693a2a30c8a041adad0f258044cc8"
integrity sha512-joP1S5GzbQvhCyVqn/XRPuiN94H0bvtm0Nn2G4ZlAbSU78zyPDYpBtB29sITdv1dcRg/RQp1UjjKclBt1azcoA==
"@polkadot/keyring@^2.0.0-beta.4":
version "2.0.0-beta.4"
resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-2.0.0-beta.4.tgz#036b8c49a6ce34a17c2d070125c772f64356fc4f"
integrity sha512-Fz+LEIeJzV3uDrb/im2qkJORTO9037dDp4LZVtLvP7H63e401vYd0Q0RbWjKnDq47Um3nuPjog/HmxUUEEnNRw==
dependencies:
"@babel/runtime" "^7.7.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"
"@polkadot/ts@^0.1.90":
version "0.1.90"
@@ -1918,13 +1918,13 @@
dependencies:
"@types/chrome" "^0.0.91"
"@polkadot/util-crypto@^2.0.0-beta.2":
version "2.0.0-beta.2"
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-2.0.0-beta.2.tgz#b080e7f369faf026f5940a37021fd4a413ef1388"
integrity sha512-7QMyuHo3lWDFdLqGBxRJUk4caVLvouFYK516mMgWTle6btZyYHTAuuqHsj2d/7gWq3/qfginaByfPtHkdoMO6A==
"@polkadot/util-crypto@^2.0.0-beta.4":
version "2.0.0-beta.4"
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-2.0.0-beta.4.tgz#1828ccf34a4f7246f74cd51de29977263296eaaf"
integrity sha512-gKbLx4bm3EyR/C/P0ckFTuB17JRK31CKgY6a7bYxiNzOXLLEVBlFm8IznJv54trYb1St2xb3JpijMdJ2cuBL6w==
dependencies:
"@babel/runtime" "^7.7.7"
"@polkadot/util" "^2.0.0-beta.2"
"@polkadot/util" "^2.0.0-beta.4"
"@polkadot/wasm-crypto" "^0.20.0-beta.1"
"@types/bip39" "^2.4.2"
"@types/bs58" "^4.0.0"
@@ -1940,10 +1940,10 @@
tweetnacl "^1.0.1"
xxhashjs "^0.2.2"
"@polkadot/util@^2.0.0-beta.2":
version "2.0.0-beta.2"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-2.0.0-beta.2.tgz#9d2370c1c4b17ace4006820345ee904c331202c7"
integrity sha512-qzfPGeSnhd6soFDxmZH5ytcuB0/XlhxLFi0P1yWiAmTff8+Hu48E/edW5JW76aDcoZL0/BQ1jk4Db7ulcgSQqw==
"@polkadot/util@^2.0.0-beta.4":
version "2.0.0-beta.4"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-2.0.0-beta.4.tgz#f7be2e17abe85596c6860d980ee3af686154467a"
integrity sha512-OsmCYd5Go0QQcYPNVcwv8brEGTDw25SLaWgaaU2YPJqLnHkJp/1vZW6VP6TSW15BpAxN0+GhZs84Ja+1gYHM/g==
dependencies:
"@babel/runtime" "^7.7.7"
"@types/bn.js" "^4.11.6"