Add a Decorated class, which will be offline decorated Metadata (#1463)

* Move Metadata to api-metadata

* Make tests in api-metadata pass

* Make tests in api-metadata pass

* Refactor fromMetadata into decorated

* Fix some linting

* Replace headers

* Fix all tests

* Small lint

* Fix tests

* Fix metadata docs scripts

* Fix import ordering

* Add line breaks

* Grumbles

* Fix forgotten import

* rpcMetadata everywhere

* Fixlint

* s/api-metadata/metadata

* Fix latest tests

* Fix lint

* Fix tests
This commit is contained in:
Amaury Martiny
2019-11-12 12:06:18 +01:00
committed by Jaco Greeff
parent c87ba33893
commit 16e0ea9315
152 changed files with 510 additions and 413 deletions
@@ -1,15 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import MagicNumber, { MAGIC_NUMBER } from './MagicNumber';
describe('MagicNumber', (): void => {
it('succeeds when the magic number matches', (): void => {
expect((): MagicNumber => new MagicNumber(MAGIC_NUMBER)).not.toThrow();
});
it('fails when the magic number missmatches', (): void => {
expect((): MagicNumber => new MagicNumber(0x12345)).toThrow(/MagicNumber/);
});
});
@@ -1,17 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { assert } from '@polkadot/util';
import U32 from '../primitive/U32';
export const MAGIC_NUMBER = new U32(0x6174656d); // `meta`, reversed for Little Endian encoding
export default class MagicNumber extends U32 {
constructor (value?: any) {
super(value);
assert(this.eq(MAGIC_NUMBER), `MagicNumber mismatch: expected ${MAGIC_NUMBER.toHex()}, found ${this.toHex()}`);
}
}
-44
View File
@@ -1,44 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { isHex, hexToU8a, u8aConcat } from '@polkadot/util';
import { MAGIC_NUMBER } from './MagicNumber';
import MetadataVersioned from './MetadataVersioned';
/**
* @name Metadata
* @description
* The versioned runtime metadata as a decoded structure
*/
export default class Metadata extends MetadataVersioned {
constructor (value?: Uint8Array | string) {
super(Metadata.decodeMetadata(value));
}
// first we try and parse using the versioned structure, if this does fail,
// we adjust with the magic number and a manual version and re-try. As soon as
// we remove support for V0, we will just do a new here
private static decodeMetadata (_value: Uint8Array | string = new Uint8Array()): MetadataVersioned {
const value = isHex(_value)
? hexToU8a(_value)
: _value;
try {
return new MetadataVersioned(value);
} catch (error) {
if (error.message.indexOf('MagicNumber mismatch') === -1) {
throw error;
}
}
return new MetadataVersioned(
u8aConcat(
MAGIC_NUMBER.toU8a(), // manually add the magic number
Uint8Array.from([0]), // add the version for the original
value // the actual data as retrieved
)
);
}
}
@@ -1,342 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { MetadataV0, MetadataV1 } from '../interfaces/metadata';
import { assert } from '@polkadot/util';
import Enum from '../codec/Enum';
import Struct from '../codec/Struct';
import MagicNumber from './MagicNumber';
import MetadataV2 from './v2';
import MetadataV3 from './v3';
import MetadataV4 from './v4';
import MetadataV5 from './v5';
import MetadataV6 from './v6';
import MetadataV7 from './v7';
import MetadataV8 from './v8';
import v0ToV1 from './v0/toV1';
import v1ToV2 from './v1/toV2';
import v2ToV3 from './v2/toV3';
import v3ToV4 from './v3/toV4';
import v4ToV5 from './v4/toV5';
import v5ToV6 from './v5/toV6';
import v6ToV7 from './v6/toV7';
import v7ToV8 from './v7/toV8';
import { getUniqTypes, toCallsOnly } from './util';
type MetaMapped = MetadataV0 | MetadataV1 | MetadataV2 | MetadataV3 | MetadataV4 | MetadataV5 | MetadataV6 | MetadataV7;
type MetaVersions = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
type MetaAsX = 'asV0' | 'asV1' | 'asV2' | 'asV3' | 'asV4' | 'asV5' | 'asV6' | 'asV7';
class MetadataEnum extends Enum {
constructor (value?: any, index?: number) {
super({
V0: 'MetadataV0', // once rolled-out, can replace this with MetadataDeprecated
V1: 'MetadataV1', // once rolled-out, can replace this with MetadataDeprecated
V2: MetadataV2, // once rolled-out, can replace this with MetadataDeprecated
V3: MetadataV3, // once rolled-out, can replace this with MetadataDeprecated
V4: MetadataV4, // once rolled-out, can replace this with MetadataDeprecated
V5: MetadataV5, // once rolled-out, can replace this with MetadataDeprecated
V6: MetadataV6, // once rolled-out, can replace this with MetadataDeprecated
V7: MetadataV7, // once rolled-out, can replace this with MetadataDeprecated
V8: MetadataV8
}, value, index);
}
/**
* @description Returns the wrapped values as a V0 object
*/
public get asV0 (): MetadataV0 {
assert(this.isV0, `Cannot convert '${this.type}' via asV0`);
return this.value as MetadataV0;
}
/**
* @description Returns the wrapped values as a V1 object
*/
public get asV1 (): MetadataV1 {
assert(this.isV1, `Cannot convert '${this.type}' via asV1`);
return this.value as MetadataV1;
}
/**
* @description Returns the wrapped values as a V2 object
*/
public get asV2 (): MetadataV2 {
assert(this.isV2, `Cannot convert '${this.type}' via asV2`);
return this.value as MetadataV2;
}
/**
* @description Returns the wrapped values as a V3 object
*/
public get asV3 (): MetadataV3 {
assert(this.isV3, `Cannot convert '${this.type}' via asV3`);
return this.value as MetadataV3;
}
/**
* @description Returns the wrapped values as a V4 object
*/
public get asV4 (): MetadataV4 {
assert(this.isV4, `Cannot convert '${this.type}' via asV4`);
return this.value as MetadataV4;
}
/**
* @description Returns the wrapped values as a V5 object
*/
public get asV5 (): MetadataV5 {
assert(this.isV5, `Cannot convert '${this.type}' via asV5`);
return this.value as MetadataV5;
}
/**
* @description Returns the wrapped values as a V6 object
*/
public get asV6 (): MetadataV6 {
assert(this.isV6, `Cannot convert '${this.type}' via asV6`);
return this.value as MetadataV6;
}
/**
* @description Returns the wrapped values as a V7 object
*/
public get asV7 (): MetadataV7 {
assert(this.isV7, `Cannot convert '${this.type}' via asV7`);
return this.value as MetadataV7;
}
/**
* @description Returns the wrapped values as a V8 object
*/
public get asV8 (): MetadataV8 {
assert(this.isV8, `Cannot convert '${this.type}' via asV8`);
return this.value as MetadataV8;
}
/**
* @description `true` if Deprecated
*/
public get isDeprecated (): boolean {
return this.type === 'MetadataDeprectated';
}
/**
* @description `true` if V0
*/
public get isV0 (): boolean {
return this.type === 'V0';
}
/**
* @description `true` if V1
*/
public get isV1 (): boolean {
return this.type === 'V1';
}
/**
* @description `true` if V2
*/
public get isV2 (): boolean {
return this.type === 'V2';
}
/**
* @description `true` if V3
*/
public get isV3 (): boolean {
return this.type === 'V3';
}
/**
* @description `true` if V4
*/
public get isV4 (): boolean {
return this.type === 'V4';
}
/**
* @description `true` if V5
*/
public get isV5 (): boolean {
return this.type === 'V5';
}
/**
* @description `true` if V6
*/
public get isV6 (): boolean {
return this.type === 'V6';
}
/**
* @description `true` if V7
*/
public get isV7 (): boolean {
return this.type === 'V7';
}
/**
* @description `true` if V8
*/
public get isV8 (): boolean {
return this.type === 'V8';
}
}
/**
* @name MetadataVersioned
* @description
* The versioned runtime metadata as a decoded structure
*/
export default class MetadataVersioned extends Struct {
private _converted: Map<number, MetaMapped> = new Map();
constructor (value?: any) {
super({
magicNumber: MagicNumber,
metadata: MetadataEnum
}, value);
}
private assertVersion (version: number): boolean {
assert(this.version <= version, `Cannot convert metadata from v${this.version} to v${version}`);
return this.version === version;
}
private getVersion <T extends MetaMapped, F extends MetaMapped> (version: MetaVersions, fromPrev: (input: F) => T): T {
const asCurr: MetaAsX = `asV${version}` as any;
const asPrev: MetaAsX = `asV${version - 1}` as any;
if (this.assertVersion(version)) {
return this.metadata[asCurr] as T;
}
if (!this._converted.has(version)) {
this._converted.set(version, fromPrev(this[asPrev] as F));
}
return this._converted.get(version) as T;
}
/**
* @description Returns the wrapped metadata as a limited calls-only (latest) version
*/
public get asCallsOnly (): MetadataVersioned {
return new MetadataVersioned({
magicNumber: this.magicNumber,
metadata: new MetadataEnum(toCallsOnly(this.asLatest), this.version)
});
}
/**
* @description Returns the wrapped metadata as a V0 object
*/
public get asV0 (): MetadataV0 {
this.assertVersion(0);
return this.metadata.asV0;
}
/**
* @description Returns the wrapped values as a V1 object
*/
public get asV1 (): MetadataV1 {
return this.getVersion(1, v0ToV1);
}
/**
* @description Returns the wrapped values as a V2 object
*/
public get asV2 (): MetadataV2 {
return this.getVersion(2, v1ToV2);
}
/**
* @description Returns the wrapped values as a V3 object
*/
public get asV3 (): MetadataV3 {
return this.getVersion(3, v2ToV3);
}
/**
* @description Returns the wrapped values as a V4 object
*/
public get asV4 (): MetadataV4 {
return this.getVersion(4, v3ToV4);
}
/**
* @description Returns the wrapped values as a V5 object
*/
public get asV5 (): MetadataV5 {
return this.getVersion(5, v4ToV5);
}
/**
* @description Returns the wrapped values as a V6 object
*/
public get asV6 (): MetadataV6 {
return this.getVersion(6, v5ToV6);
}
/**
* @description Returns the wrapped values as a V7 object
*/
public get asV7 (): MetadataV7 {
return this.getVersion(7, v6ToV7);
}
/**
* @description Returns the wrapped values as a V8 object
*/
public get asV8 (): MetadataV8 {
return this.getVersion(8, v7ToV8);
}
/**
* @description Returns the wrapped values as a latest version object
*/
public get asLatest (): MetadataV8 {
return this.asV8;
}
/**
* @description
*/
public get magicNumber (): MagicNumber {
return this.get('magicNumber') as MagicNumber;
}
/**
* @description the metadata wrapped
*/
private get metadata (): MetadataEnum {
return this.get('metadata') as MetadataEnum;
}
/**
* @description the metadata version this structure represents
*/
public get version (): number {
return this.metadata.index;
}
public getUniqTypes (throwError: boolean): string[] {
return getUniqTypes(this.asLatest, throwError);
}
}
-13
View File
@@ -1,13 +0,0 @@
# metadata update
1. New branch :)
2. `curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "state_getMetadata", "params":[]}' http://localhost:9933`
3. Copy the latest version folder (vx) in `types/Metadata`
3. Copy the result (hex-only) into `static.ts`, as of now linked to `v6/static.ts`
4. Rust Metadata tests, `yarn run test packages/types/src/Metadata`
5. Add new class types as required (find missing in Rust code, add)
6. Add all classes to `src/classes.ts`
7. Repeat 4-6 until passing
8. Update parsed JSON to actual output version, `v6/latest.*.json`
9. All tests
10. Make PR
-7
View File
@@ -1,7 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import Metadata from './Metadata';
export default Metadata;
-7
View File
@@ -1,7 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import metadata from './v8/static';
export default metadata;
-10
View File
@@ -1,10 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Vec } from '../codec';
import { Codec } from '../types';
export interface MetadataInterface<Modules extends Codec> extends Codec {
modules: Vec<Modules>;
}
@@ -1,18 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
// Quick and dirty flatten (.flat() not available)
export default function flattenUniq (list: any[]): any[] {
const flat = list.reduce((result, entry): any[] => {
return result.concat(
Array.isArray(entry)
? flattenUniq(entry)
: entry
);
}, []);
return [...new Set(flat)]
.filter((value: any): any => value)
.sort();
}
@@ -1,173 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Codec } from '../../types';
import { Option, Vec } from '../../codec';
import { Text, Type } from '../../primitive';
import flattenUniq from './flattenUniq';
import validateTypes from './validateTypes';
type Arg = { type: Type } & Codec;
type Item = {
type: {
isDoubleMap?: boolean;
isMap: boolean;
asDoubleMap?: {
key1: Text;
key2: Text;
value: Text;
};
asMap: {
key: Text;
value: Text;
};
asType: Text;
};
} & Codec;
type Storage = Option<Vec<Item> | {
// v0
functions?: Vec<Item>;
// V7+
items?: Vec<Item>;
} & Codec>;
type Call = { args: Vec<Arg> } & Codec;
type Calls = Option<Vec<Call>>;
type Event = { args: Vec<Type> } & Codec;
type Events = Option<Vec<Event>>;
type Module = {
// V0
module?: {
call: {
functions: Vec<Call>;
};
};
// V1+
calls?: Calls;
// V6+
constants?: Vec<{ type: Text } & Codec>;
events?: Events;
storage?: Storage;
} & Codec;
interface ExtractionMetadata {
modules: Vec<Module>;
// V0
outerEvent?: {
events: Vec<[Text, Vec<Event>] & Codec>;
};
}
function unwrapCalls (mod: Module): Call[] {
return mod.calls
? mod.calls.unwrapOr([])
// V0
: mod.module
? mod.module.call.functions
: [];
}
function getCallNames ({ modules }: ExtractionMetadata): string[][][] {
return modules.map((mod): string[][] =>
unwrapCalls(mod).map(({ args }): string[] =>
args.map((arg): string =>
arg.type.toString()
)
)
);
}
function getConstantNames ({ modules }: ExtractionMetadata): string[][] {
return modules.map(({ constants }): string[] =>
constants
? constants.map((constant): string =>
constant.type.toString()
)
: []
);
}
function unwrapEvents (events?: Events): Event[] {
if (!events) {
return [];
}
return events.unwrapOr([]);
}
function getEventNames ({ modules, outerEvent }: ExtractionMetadata): string[][][] {
const mapArg = ({ args }: Event): string[] =>
args.map((arg): string =>
arg.toString()
);
// V0
if (outerEvent) {
return outerEvent.events.map(([, events]): string[][] =>
events.map(mapArg)
);
}
// V1+
return modules.map(({ events }): string[][] =>
unwrapEvents(events).map(mapArg)
);
}
function unwrapStorage (storage?: Storage): Item[] {
if (!storage) {
return [];
}
const data = storage.unwrapOr([]);
return Array.isArray(data)
? data
: (data.items || data.functions) as Item[];
}
function getStorageNames ({ modules }: ExtractionMetadata): string[][][] {
return modules.map(({ storage }): string[][] =>
unwrapStorage(storage).map(({ type }): string[] => {
if (type.isDoubleMap && type.asDoubleMap) {
return [
type.asDoubleMap.key1.toString(),
type.asDoubleMap.key2.toString(),
type.asDoubleMap.value.toString()
];
} else if (type.isMap) {
return [
type.asMap.key.toString(),
type.asMap.value.toString()
];
} else {
return [
type.asType.toString()
];
}
})
);
}
export default function getUniqTypes (meta: ExtractionMetadata, throwError: boolean): string[] {
const types = flattenUniq([
getCallNames(meta),
getConstantNames(meta),
getEventNames(meta),
getStorageNames(meta)
]);
validateTypes(types, throwError);
return types;
}
@@ -1,8 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export { default as flattenUniq } from './flattenUniq';
export { default as getUniqTypes } from './getUniqTypes';
export { default as toCallsOnly } from './toCallsOnly';
export { default as validateTypes } from './validateTypes';
@@ -1,72 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import { createTypeUnsafe } from '../../codec/create';
import Metadata from '../Metadata';
import Call from '../../primitive/Generic/Call';
import { MetadataInterface } from '../types';
import { Codec } from '../../types';
import getUniqTypes from './getUniqTypes';
/**
* Given the static `rpcData` and the `staticSubstrate` JSON file, Metadata
* should decode `rpcData` and output `staticSubstrate`.
*/
export function decodeLatestSubstrate<Modules extends Codec> (
version: number,
rpcData: string,
staticSubstrate: object
): void {
it('decodes latest substrate properly', (): void => {
const metadata = new Metadata(rpcData);
console.error(JSON.stringify(metadata.toJSON()));
expect(metadata.version).toBe(version);
expect((metadata[`asV${version}` as keyof Metadata] as unknown as MetadataInterface<Modules>).modules.length).not.toBe(0);
expect(metadata.toJSON()).toEqual(staticSubstrate);
});
}
/**
* Given a `version`, MetadataLatest and MetadataV{version} should output the same
* unique types.
*/
export function toLatest<Modules extends Codec> (version: number, rpcData: string): void {
it(`converts v${version} to v8`, (): void => {
const metadata = new Metadata(rpcData)[`asV${version}` as keyof Metadata];
const metadataV8 = new Metadata(rpcData).asLatest;
expect(
getUniqTypes(metadata as unknown as MetadataInterface<Modules>, true)
).toEqual(
getUniqTypes(metadataV8, true)
);
});
}
/**
* Given a Metadata, no type should throw when given its fallback value.
*/
export function defaultValues (rpcData: string): void {
describe('storage with default values', (): void => {
const metadata = new Metadata(rpcData);
Call.injectMetadata(metadata);
metadata.asLatest.modules
.filter(({ storage }): boolean => storage.isSome)
.forEach((mod): void => {
mod.storage.unwrap().items.forEach(({ fallback, name, type }): void => {
it(`creates default types for ${mod.name}.${name}, type ${type}`, (): void => {
expect(
(): Codec => createTypeUnsafe(type.toString(), [fallback])
).not.toThrow();
});
});
});
});
}
@@ -1,25 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import staticData from '../static';
import Metadata from '..';
describe('toCallsOnly', (): void => {
it('creates a calls-only version of the metadata', (): void => {
const stripped = new Metadata(staticData).asCallsOnly;
console.error(JSON.stringify(stripped));
expect(stripped).toBeDefined();
});
it('can serialize from the input', (): void => {
const s1 = new Metadata(staticData).asCallsOnly.toU8a();
const s2 = new Metadata(s1).asCallsOnly.toU8a();
expect(s1).toEqual(s2);
});
});
@@ -1,18 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import MetadataV8 from '../v8';
/**
* @description Convert from MetadataV8 to a stripped representation of MetadataV8
*/
export default function toCallsOnly ({ modules }: MetadataV8): any {
return new MetadataV8({
// FIXME, this needs typing, not any
modules: modules.map(({ calls, name }): any => ({
name,
calls
}))
}).toJSON();
}
@@ -1,54 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { TypeDef, TypeDefInfo, TypeDefExtVecFixed } from '../../codec/types';
import { getTypeDef } from '../../codec/create';
import flattenUniq from './flattenUniq';
import { getTypeRegistry } from '../../codec';
function extractTypes (types: string[]): any[] {
return types.map((type): any => {
const decoded = getTypeDef(type);
switch (decoded.info) {
case TypeDefInfo.Plain:
return decoded.type;
case TypeDefInfo.Compact:
case TypeDefInfo.Option:
case TypeDefInfo.Vec:
return extractTypes([(decoded.sub as TypeDef).type]);
case TypeDefInfo.VecFixed:
return extractTypes([(decoded.ext as TypeDefExtVecFixed).type]);
case TypeDefInfo.Result:
case TypeDefInfo.Tuple:
return extractTypes(
(decoded.sub as TypeDef[]).map((sub): string => sub.type)
);
default:
throw new Error(`Unhandled: Unable to create and validate type from ${type}`);
}
});
}
export default function validateTypes (types: string[], throwError: boolean): void {
const typeRegistry = getTypeRegistry();
const missing = flattenUniq(extractTypes(types)).filter((type): boolean =>
!typeRegistry.hasType(type)
);
if (missing.length !== 0) {
const message = `Unknown types found, no types for ${missing}`;
if (throwError) {
throw new Error(message);
} else {
console.warn(message);
}
}
}
@@ -1,33 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { createType } from '../../codec';
import Metadata from '../';
import latestSubstrateV0 from './static-substrate.json';
import rpcData from './static';
import { defaultValues, toLatest } from '../util/testUtil';
describe('Metadata', (): void => {
it('works with fallback', (): void => {
const metadata = new Metadata(rpcData);
const metadataV0 = createType('MetadataV0', rpcData);
expect(metadata.asV0.toString()).toEqual(metadataV0.toString());
});
it('decodes latest substrate properly', (): void => {
const metadata = new Metadata(rpcData);
const json = metadata.asV0.toJSON();
console.error(JSON.stringify(json));
expect(metadata.version).toBe(0);
expect(metadata.asV0.modules.length).not.toBe(0);
expect(json).toEqual(latestSubstrateV0);
});
toLatest(0, rpcData);
defaultValues(rpcData);
});
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
-53
View File
@@ -1,53 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { EventMetadataV1, FunctionMetadataV1, MetadataV0, MetadataV1, ModuleMetadataV1, RuntimeModuleMetadataV0, StorageFunctionMetadataV1 } from '../../interfaces/metadata';
import { stringUpperFirst } from '@polkadot/util';
import { createType, Option, Vec } from '../../codec';
import { Text } from '../../primitive';
function toV1Calls ({ module: { call: { functions } } }: RuntimeModuleMetadataV0): Option<FunctionMetadataV1> {
return functions.length
? new Option(Vec.with('FunctionMetadataV1'), functions)
: new Option(Vec.with('FunctionMetadataV1'));
}
function toV1Events (metadataV0: MetadataV0, prefix: Text): Option<EventMetadataV1> {
const events = metadataV0.outerEvent.events.find((event): boolean => event[0].eq(prefix));
return events
? new Option(Vec.with('EventMetadataV1'), events[1])
: new Option(Vec.with('EventMetadataV1'));
}
function toV1Storage ({ storage }: RuntimeModuleMetadataV0): Option<StorageFunctionMetadataV1> {
return storage.isSome
? new Option('Vec<StorageFunctionMetadataV1>', storage.unwrap().functions)
: new Option('Vec<StorageFunctionMetadataV1>');
}
/**
* Function that converts MetadataV0 to MetadataV1
*/
export default function toV1 (metadataV0: MetadataV0): MetadataV1 {
return createType('MetadataV1', {
modules: metadataV0.modules.map((mod): ModuleMetadataV1 => {
// The prefix of this module (capitalized)
const prefix = mod.storage.isSome
? mod.storage.unwrap().prefix.toString()
: stringUpperFirst(mod.prefix.toString()); // If this module doesn't have storage, we just assume the prefix is the name capitalized
return createType('ModuleMetadataV1', {
name: mod.prefix, // Not capitalized
prefix, // Capitalized
storage: toV1Storage(mod),
calls: toV1Calls(mod),
events: toV1Events(metadataV0, mod.prefix)
});
})
});
}
@@ -1,15 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import staticSubstrate from './static-substrate.json';
import rpcData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV1', (): void => {
decodeLatestSubstrate(1, rpcData, staticSubstrate);
toLatest(1, rpcData);
defaultValues(rpcData);
});
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
-11
View File
@@ -1,11 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { MetadataV1 } from '../../interfaces/metadata';
import MetadataV2 from '../v2';
export default function toV2 (metadataV1: MetadataV1): MetadataV2 {
return new MetadataV2(metadataV1);
}
@@ -1,15 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import staticSubstrate from './static-substrate.json';
import rpcData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV2', (): void => {
decodeLatestSubstrate(2, rpcData, staticSubstrate);
toLatest(2, rpcData);
defaultValues(rpcData);
});
@@ -1,85 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { EventMetadataV2, FunctionMetadataV2 } from '../../interfaces/metadata';
import { MetadataInterface } from '../types';
import Option from '../../codec/Option';
import Struct from '../../codec/Struct';
import Vec from '../../codec/Vec';
import Text from '../../primitive/Text';
import { StorageFunctionMetadata } from './Storage';
/**
* @name ModuleMetadataV2
* @description
* The definition of a module in the system
*/
export class ModuleMetadataV2 extends Struct {
constructor (value?: any) {
super({
name: 'Text',
prefix: 'Text',
storage: Option.with(Vec.with(StorageFunctionMetadata)),
calls: Option.with('Vec<FunctionMetadataV2>'),
events: Option.with('Vec<EventMetadataV2>')
}, value);
}
/**
* @description the module calls
*/
public get calls (): Option<Vec<FunctionMetadataV2>> {
return this.get('calls') as Option<Vec<FunctionMetadataV2>>;
}
/**
* @description the module events
*/
public get events (): Option<Vec<EventMetadataV2>> {
return this.get('events') as Option<Vec<EventMetadataV2>>;
}
/**
* @description the module name
*/
public get name (): Text {
return this.get('name') as Text;
}
/**
* @description the module prefix
*/
public get prefix (): Text {
return this.get('prefix') as Text;
}
/**
* @description the associated module storage
*/
public get storage (): Option<Vec<StorageFunctionMetadata>> {
return this.get('storage') as Option<Vec<StorageFunctionMetadata>>;
}
}
/**
* @name MetadataV2
* @description
* The runtime metadata as a decoded structure
*/
export default class MetadataV2 extends Struct implements MetadataInterface<ModuleMetadataV2> {
constructor (value?: any) {
super({
modules: Vec.with(ModuleMetadataV2)
}, value);
}
/**
* @description The associated modules for this structure
*/
public get modules (): Vec<ModuleMetadataV2> {
return this.get('modules') as Vec<ModuleMetadataV2>;
}
}
-130
View File
@@ -1,130 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { MapTypeV2, PlainTypeV2, StorageFunctionModifierV2 } from '../../interfaces/metadata';
import { AnyNumber } from '../../types';
import { assert } from '@polkadot/util';
import Enum from '../../codec/Enum';
import Struct from '../../codec/Struct';
import Vec from '../../codec/Vec';
import Bytes from '../../primitive/Bytes';
import Text from '../../primitive/Text';
export class StorageFunctionType extends Enum {
constructor (value?: any, index?: number) {
super({
Type: 'PlainTypeV2',
Map: 'MapTypeV2'
}, value, index);
}
/**
* @description The value as a mapped value
*/
public get asMap (): MapTypeV2 {
assert(this.isMap, `Cannot convert '${this.type}' via asMap`);
return this.value as MapTypeV2;
}
/**
* @description The value as a [[Type]] value
*/
public get asType (): PlainTypeV2 {
assert(this.isPlainType, `Cannot convert '${this.type}' via asType`);
return this.value as PlainTypeV2;
}
/**
* @description `true` if the storage entry is a map
*/
public get isMap (): boolean {
return this.toNumber() === 1;
}
/**
* @description `true` if the storage entry is a plain type
*/
public get isPlainType (): boolean {
return this.toNumber() === 0;
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
if (this.isMap) {
if (this.asMap.linked.isTrue) {
return `(${this.asMap.value.toString()}, Linkage<${this.asMap.key.toString()}>)`;
}
return this.asMap.value.toString();
}
return this.asType.toString();
}
}
export interface StorageFunctionMetadataValue {
name: string | Text;
modifier: StorageFunctionModifierV2 | AnyNumber;
type: StorageFunctionType;
fallback: Bytes;
documentation: Vec<Text> | string[];
}
/**
* @name ModuleMetadata
* @description
* The definition of a storage function
*/
export class StorageFunctionMetadata extends Struct {
constructor (value?: StorageFunctionMetadataValue | Uint8Array) {
super({
name: 'Text',
modifier: 'StorageFunctionModifierV2',
type: StorageFunctionType,
fallback: 'Bytes',
documentation: 'Vec<Text>'
}, value);
}
/**
* @description The [[Text]] documentation
*/
public get documentation (): Vec<Text> {
return this.get('documentation') as Vec<Text>;
}
/**
* @description The [[Bytes]] fallback default
*/
public get fallback (): Bytes {
return this.get('fallback') as Bytes;
}
/**
* @description The [[StorageFunctionModifierV2]] for arguments
*/
public get modifier (): StorageFunctionModifierV2 {
return this.get('modifier') as StorageFunctionModifierV2;
}
/**
* @description The call name
*/
public get name (): Text {
return this.get('name') as Text;
}
/**
* @description The [[StorageFunctionType]]
*/
public get type (): StorageFunctionType {
return this.get('type') as StorageFunctionType;
}
}
-11
View File
@@ -1,11 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import MetadataV2, { ModuleMetadataV2 } from './Metadata';
export default MetadataV2;
export {
ModuleMetadataV2
};
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
-10
View File
@@ -1,10 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import MetadataV2 from './Metadata';
import MetadataV3 from '../v3';
export default function toV3 (metadataV2: MetadataV2): MetadataV3 {
return new MetadataV3(metadataV2);
}
@@ -1,15 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import staticSubstrate from './static-substrate.json';
import rpcData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV3', (): void => {
decodeLatestSubstrate(3, rpcData, staticSubstrate);
toLatest(3, rpcData);
defaultValues(rpcData);
});
@@ -1,85 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { EventMetadataV3, FunctionMetadataV3 } from '../../interfaces/metadata';
import { MetadataInterface } from '../types';
import Option from '../../codec/Option';
import Struct from '../../codec/Struct';
import Vec from '../../codec/Vec';
import Text from '../../primitive/Text';
import { StorageFunctionMetadata } from './Storage';
/**
* @name ModuleMetadataV3
* @description
* The definition of a module in the system
*/
export class ModuleMetadataV3 extends Struct {
constructor (value?: any) {
super({
name: 'Text',
prefix: 'Text',
storage: Option.with(Vec.with(StorageFunctionMetadata)),
calls: Option.with('Vec<FunctionMetadataV3>'),
events: Option.with('Vec<EventMetadataV3>')
}, value);
}
/**
* @description the module calls
*/
public get calls (): Option<Vec<FunctionMetadataV3>> {
return this.get('calls') as Option<Vec<FunctionMetadataV3>>;
}
/**
* @description the module events
*/
public get events (): Option<Vec<EventMetadataV3>> {
return this.get('events') as Option<Vec<EventMetadataV3>>;
}
/**
* @description the module name
*/
public get name (): Text {
return this.get('name') as Text;
}
/**
* @description the module prefix
*/
public get prefix (): Text {
return this.get('prefix') as Text;
}
/**
* @description the associated module storage
*/
public get storage (): Option<Vec<StorageFunctionMetadata>> {
return this.get('storage') as Option<Vec<StorageFunctionMetadata>>;
}
}
/**
* @name MetadataV3
* @description
* The runtime metadata as a decoded structure
*/
export default class MetadataV3 extends Struct implements MetadataInterface<ModuleMetadataV3> {
constructor (value?: any) {
super({
modules: Vec.with(ModuleMetadataV3)
}, value);
}
/**
* @description The associated modules for this structure
*/
public get modules (): Vec<ModuleMetadataV3> {
return this.get('modules') as Vec<ModuleMetadataV3>;
}
}
-151
View File
@@ -1,151 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { DoubleMapTypeV3, MapTypeV3, PlainTypeV3, StorageFunctionModifierV3 } from '../../interfaces/metadata';
import { AnyNumber } from '../../types';
import { assert } from '@polkadot/util';
import Enum from '../../codec/Enum';
import Struct from '../../codec/Struct';
import Vec from '../../codec/Vec';
import Bytes from '../../primitive/Bytes';
import Text from '../../primitive/Text';
export class StorageFunctionType extends Enum {
constructor (value?: any, index?: number) {
super({
Type: 'PlainTypeV3',
Map: 'MapTypeV3',
DoubleMap: 'DoubleMapTypeV3'
}, value, index);
}
/**
* @description The value as a mapped value
*/
public get asDoubleMap (): DoubleMapTypeV3 {
assert(this.isDoubleMap, `Cannot convert '${this.type}' via asDoubleMap`);
return this.value as DoubleMapTypeV3;
}
/**
* @description The value as a mapped value
*/
public get asMap (): MapTypeV3 {
assert(this.isMap, `Cannot convert '${this.type}' via asMap`);
return this.value as MapTypeV3;
}
/**
* @description The value as a [[Type]] value
*/
public get asType (): PlainTypeV3 {
assert(this.isPlainType, `Cannot convert '${this.type}' via asType`);
return this.value as PlainTypeV3;
}
/**
* @description `true` if the storage entry is a doublemap
*/
public get isDoubleMap (): boolean {
return this.toNumber() === 2;
}
/**
* @description `true` if the storage entry is a map
*/
public get isMap (): boolean {
return this.toNumber() === 1;
}
/**
* @description `true` if the storage entry is a plain type
*/
public get isPlainType (): boolean {
return this.toNumber() === 0;
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
if (this.isDoubleMap) {
return `DoubleMap<${this.asDoubleMap.toString()}>`;
}
if (this.isMap) {
if (this.asMap.linked.isTrue) {
return `(${this.asMap.value.toString()}, Linkage<${this.asMap.key.toString()}>)`;
}
return this.asMap.value.toString();
}
return this.asType.toString();
}
}
export interface StorageFunctionMetadataValue {
name: string | Text;
modifier: StorageFunctionModifierV3 | AnyNumber;
type: StorageFunctionType;
fallback: Bytes;
documentation: Vec<Text> | string[];
}
/**
* @name StorageFunctionMetadata
* @description
* The definition of a storage function
*/
export class StorageFunctionMetadata extends Struct {
constructor (value?: StorageFunctionMetadataValue | Uint8Array) {
super({
name: 'Text',
modifier: 'StorageFunctionModifierV3',
type: StorageFunctionType,
fallback: 'Bytes',
documentation: 'Vec<Text>'
}, value);
}
/**
* @description The [[Text]] documentation
*/
public get documentation (): Vec<Text> {
return this.get('documentation') as Vec<Text>;
}
/**
* @description The [[Bytes]] fallback default
*/
public get fallback (): Bytes {
return this.get('fallback') as Bytes;
}
/**
* @description The [[StorageFunctionModifierV3]] for arguments
*/
public get modifier (): StorageFunctionModifierV3 {
return this.get('modifier') as StorageFunctionModifierV3;
}
/**
* @description The call name
*/
public get name (): Text {
return this.get('name') as Text;
}
/**
* @description The [[StorageFunctionType]]
*/
public get type (): StorageFunctionType {
return this.get('type') as StorageFunctionType;
}
}
-11
View File
@@ -1,11 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import MetadataV3, { ModuleMetadataV3 } from './Metadata';
export default MetadataV3;
export {
ModuleMetadataV3
};
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
-66
View File
@@ -1,66 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { createType, Option, Vec } from '../../codec';
import MetadataV3 from './Metadata';
import StorageHasher from '../../primitive/StorageHasher';
import MetadataV4 from '../v4';
import { ModuleMetadataV4 } from '../v4/Metadata';
import { StorageFunctionMetadata, StorageFunctionType } from '../v4/Storage';
import { StorageFunctionMetadata as StorageFunctionMetadataV3 } from './Storage';
/**
* Convert V3 StorageFunction to V4 StorageFunction
*/
function toV4StorageFunction (storageFn: StorageFunctionMetadataV3): StorageFunctionMetadata {
const { documentation, fallback, modifier, name, type } = storageFn;
// Convert the old type to the new type: there is one new field
// called `hasher`, which we initialize to xxHash (the default in
// v3).
const [newType, index] = type.isPlainType
? [type, 0]
: type.isMap
? [createType('MapTypeV4', {
hasher: new StorageHasher('Twox128'),
key: type.asMap.key,
value: type.asMap.value,
linked: type.asMap.linked
}), 1]
: [createType('DoubleMapTypeV4', {
hasher: new StorageHasher('Twox128'),
key1: type.asDoubleMap.key1,
key2: type.asDoubleMap.key2,
value: type.asDoubleMap.value,
key2Hasher: type.asDoubleMap.key2Hasher
}), 2];
return new StorageFunctionMetadata({
documentation,
fallback,
name,
modifier,
type: new StorageFunctionType(newType, index)
});
}
/**
* Convert from MetadataV3 to MetadataV4
* See https://github.com/paritytech/substrate/pull/2268 for details
*/
export default function toV4 ({ modules }: MetadataV3): MetadataV4 {
return new MetadataV4({
modules: modules.map(({ calls, events, name, prefix, storage }): ModuleMetadataV4 =>
new ModuleMetadataV4({
calls,
events,
name,
prefix,
storage: storage.isSome
? new Option(Vec.with(StorageFunctionMetadata), storage.unwrap().map(toV4StorageFunction))
: undefined
})
)
});
}
@@ -1,15 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import staticSubstrate from './static-substrate.json';
import rpcData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV4', (): void => {
decodeLatestSubstrate(4, rpcData, staticSubstrate);
toLatest(4, rpcData);
defaultValues(rpcData);
});
@@ -1,84 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { EventMetadataV4, FunctionMetadataV4 } from '../../interfaces/metadata';
import { MetadataInterface } from '../types';
import Option from '../../codec/Option';
import Struct from '../../codec/Struct';
import Vec from '../../codec/Vec';
import Text from '../../primitive/Text';
import { StorageFunctionMetadata } from './Storage';
/**
* @name ModuleMetadataV4
* @description
* The definition of a module in the system
*/
export class ModuleMetadataV4 extends Struct {
constructor (value?: any) {
super({
name: 'Text',
prefix: 'Text',
storage: Option.with(Vec.with(StorageFunctionMetadata)),
calls: Option.with('Vec<FunctionMetadataV4>'),
events: Option.with('Vec<EventMetadataV4>')
}, value);
}
/**
* @description the module calls
*/
public get calls (): Option<Vec<FunctionMetadataV4>> {
return this.get('calls') as Option<Vec<FunctionMetadataV4>>;
}
/**
* @description the module events
*/
public get events (): Option<Vec<EventMetadataV4>> {
return this.get('events') as Option<Vec<EventMetadataV4>>;
}
/**
* @description the module name
*/
public get name (): Text {
return this.get('name') as Text;
}
/**
* @description the module prefix
*/
public get prefix (): Text {
return this.get('prefix') as Text;
}
/**
* @description the associated module storage
*/
public get storage (): Option<Vec<StorageFunctionMetadata>> {
return this.get('storage') as Option<Vec<StorageFunctionMetadata>>;
}
}
/**
* @name MetadataV4
* @description
* The runtime metadata as a decoded structure
*/
export default class MetadataV4 extends Struct implements MetadataInterface<ModuleMetadataV4> {
constructor (value?: any) {
super({
modules: Vec.with(ModuleMetadataV4)
}, value);
}
/**
* @description The associated modules for this structure
*/
public get modules (): Vec<ModuleMetadataV4> {
return this.get('modules') as Vec<ModuleMetadataV4>;
}
}
-151
View File
@@ -1,151 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { DoubleMapTypeV4, MapTypeV4, PlainTypeV4, StorageFunctionModifierV4 } from '../../interfaces/metadata';
import { AnyNumber } from '../../types';
import { assert } from '@polkadot/util';
import Enum from '../../codec/Enum';
import Struct from '../../codec/Struct';
import Vec from '../../codec/Vec';
import Bytes from '../../primitive/Bytes';
import Text from '../../primitive/Text';
export class StorageFunctionType extends Enum {
constructor (value?: any, index?: number) {
super({
Type: 'PlainTypeV4',
Map: 'MapTypeV4',
DoubleMap: 'DoubleMapTypeV4'
}, value, index);
}
/**
* @description The value as a mapped value
*/
public get asDoubleMap (): DoubleMapTypeV4 {
assert(this.isDoubleMap, `Cannot convert '${this.type}' via asDoubleMap`);
return this.value as DoubleMapTypeV4;
}
/**
* @description The value as a mapped value
*/
public get asMap (): MapTypeV4 {
assert(this.isMap, `Cannot convert '${this.type}' via asMap`);
return this.value as MapTypeV4;
}
/**
* @description The value as a [[Type]] value
*/
public get asType (): PlainTypeV4 {
assert(this.isPlainType, `Cannot convert '${this.type}' via asType`);
return this.value as PlainTypeV4;
}
/**
* @description `true` if the storage entry is a doublemap
*/
public get isDoubleMap (): boolean {
return this.toNumber() === 2;
}
/**
* @description `true` if the storage entry is a map
*/
public get isMap (): boolean {
return this.toNumber() === 1;
}
/**
* @description `true` if the storage entry is a plain type
*/
public get isPlainType (): boolean {
return this.toNumber() === 0;
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
if (this.isDoubleMap) {
return `DoubleMap<${this.asDoubleMap.value.toString()}>`;
}
if (this.isMap) {
if (this.asMap.linked.isTrue) {
return `(${this.asMap.value.toString()}, Linkage<${this.asMap.key.toString()}>)`;
}
return this.asMap.value.toString();
}
return this.asType.toString();
}
}
export interface StorageFunctionMetadataValue {
name: string | Text;
modifier: StorageFunctionModifierV4 | AnyNumber;
type: StorageFunctionType;
fallback: Bytes;
documentation: Vec<Text> | string[];
}
/**
* @name StorageFunctionMetadata
* @description
* The definition of a storage function
*/
export class StorageFunctionMetadata extends Struct {
constructor (value?: StorageFunctionMetadataValue | Uint8Array) {
super({
name: 'Text',
modifier: 'StorageFunctionModifierV4',
type: StorageFunctionType,
fallback: 'Bytes',
documentation: 'Vec<Text>'
}, value);
}
/**
* @description The default value of the storage function
*/
public get fallback (): Bytes {
return this.get('fallback') as Bytes;
}
/**
* @description The [[Text]] documentation
*/
public get documentation (): Vec<Text> {
return this.get('documentation') as Vec<Text>;
}
/**
* @description The key name
*/
public get name (): Text {
return this.get('name') as Text;
}
/**
* @description The modifier
*/
public get modifier (): StorageFunctionModifierV4 {
return this.get('modifier') as StorageFunctionModifierV4;
}
/**
* @description The [[StorageFunctionType]]
*/
public get type (): StorageFunctionType {
return this.get('type') as StorageFunctionType;
}
}
-11
View File
@@ -1,11 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import MetadataV4, { ModuleMetadataV4 } from './Metadata';
export default MetadataV4;
export {
ModuleMetadataV4
};
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
-76
View File
@@ -1,76 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { assert } from '@polkadot/util';
import { createType, Option, Vec } from '../../codec';
import Text from '../../primitive/Text';
import MetadataV4 from './Metadata';
import StorageHasher from '../../primitive/StorageHasher';
import MetadataV5 from '../v5';
import { ModuleMetadataV5 } from '../v5/Metadata';
import { StorageFunctionMetadata, StorageFunctionType } from '../v5/Storage';
import { StorageFunctionMetadata as StorageFunctionMetadataV4 } from './Storage';
const hasherMap: Map<string, string> = new Map([
['blake2_128', 'Blake2_128'],
['blake2_256', 'Blake2_256'],
['twox_128', 'Twox128'],
['twox_256', 'Twox256'],
['twox_64_concat', 'Twox64Concat']
]);
function toStorageHasher (text: Text): StorageHasher {
const mapped = hasherMap.get(text.toString());
assert(mapped, `Invalid Storage hasher: ${text.toString()}`);
return new StorageHasher(mapped);
}
/**
* Convert V4 StorageFunction to V5 StorageFunction
*/
function toV5StorageFunction (storageFn: StorageFunctionMetadataV4): StorageFunctionMetadata {
const { documentation, fallback, modifier, name, type } = storageFn;
const [newType, index] = type.isPlainType
? [type, 0]
: type.isMap
? [type.asMap, 1]
: [createType('DoubleMapTypeV5', {
hasher: type.asDoubleMap.hasher,
key1: type.asDoubleMap.key1,
key2: type.asDoubleMap.key2,
value: type.asDoubleMap.value,
key2Hasher: toStorageHasher(type.asDoubleMap.key2Hasher)
}), 2];
return new StorageFunctionMetadata({
documentation,
fallback,
name,
modifier,
type: new StorageFunctionType(newType, index)
});
}
/**
* Convert from MetadataV4 to MetadataV5
* See https://github.com/paritytech/substrate/pull/2836/files for details
*/
export default function toV5 ({ modules }: MetadataV4): MetadataV5 {
return new MetadataV5({
modules: modules.map(({ calls, events, name, prefix, storage }): ModuleMetadataV5 =>
new ModuleMetadataV5({
name,
prefix,
storage: storage.isSome
? new Option(Vec.with(StorageFunctionMetadata), storage.unwrap().map(toV5StorageFunction))
: undefined,
calls,
events
})
)
});
}
@@ -1,25 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import polkadotJson from './static-polkadot.json';
import substrateJson from './static-substrate.json';
import polkadotData from './static.polkadot';
import substrateData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV5 (substrate)', (): void => {
decodeLatestSubstrate(5, substrateData, substrateJson);
toLatest(5, substrateData);
defaultValues(substrateData);
});
describe('MetadataV5 (polkadot)', (): void => {
decodeLatestSubstrate(5, polkadotData, polkadotJson);
toLatest(5, polkadotData);
defaultValues(polkadotData);
});
@@ -1,84 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { EventMetadataV5, FunctionMetadataV5 } from '../../interfaces/metadata';
import { MetadataInterface } from '../types';
import Option from '../../codec/Option';
import Struct from '../../codec/Struct';
import Vec from '../../codec/Vec';
import Text from '../../primitive/Text';
import { StorageFunctionMetadata } from './Storage';
/**
* @name ModuleMetadataV5
* @description
* The definition of a module in the system
*/
export class ModuleMetadataV5 extends Struct {
constructor (value?: any) {
super({
name: 'Text',
prefix: 'Text',
storage: Option.with(Vec.with(StorageFunctionMetadata)),
calls: Option.with('Vec<FunctionMetadataV5>'),
events: Option.with('Vec<EventMetadataV5>')
}, value);
}
/**
* @description the module calls
*/
public get calls (): Option<Vec<FunctionMetadataV5>> {
return this.get('calls') as Option<Vec<FunctionMetadataV5>>;
}
/**
* @description the module events
*/
public get events (): Option<Vec<EventMetadataV5>> {
return this.get('events') as Option<Vec<EventMetadataV5>>;
}
/**
* @description the module name
*/
public get name (): Text {
return this.get('name') as Text;
}
/**
* @description the module prefix
*/
public get prefix (): Text {
return this.get('prefix') as Text;
}
/**
* @description the associated module storage
*/
public get storage (): Option<Vec<StorageFunctionMetadata>> {
return this.get('storage') as Option<Vec<StorageFunctionMetadata>>;
}
}
/**
* @name MetadataV5
* @description
* The runtime metadata as a decoded structure
*/
export default class MetadataV5 extends Struct implements MetadataInterface<ModuleMetadataV5> {
constructor (value?: any) {
super({
modules: Vec.with(ModuleMetadataV5)
}, value);
}
/**
* @description The associated modules for this structure
*/
public get modules (): Vec<ModuleMetadataV5> {
return this.get('modules') as Vec<ModuleMetadataV5>;
}
}
-151
View File
@@ -1,151 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { DoubleMapTypeV5, MapTypeV5, PlainTypeV5, StorageFunctionModifierV5 } from '../../interfaces/metadata';
import { AnyNumber } from '../../types';
import { assert } from '@polkadot/util';
import Enum from '../../codec/Enum';
import Struct from '../../codec/Struct';
import Vec from '../../codec/Vec';
import Bytes from '../../primitive/Bytes';
import Text from '../../primitive/Text';
export class StorageFunctionType extends Enum {
constructor (value?: any, index?: number) {
super({
Type: 'PlainTypeV5',
Map: 'MapTypeV5',
DoubleMap: 'DoubleMapTypeV5'
}, value, index);
}
/**
* @description The value as a mapped value
*/
public get asDoubleMap (): DoubleMapTypeV5 {
assert(this.isDoubleMap, `Cannot convert '${this.type}' via asDoubleMap`);
return this.value as DoubleMapTypeV5;
}
/**
* @description The value as a mapped value
*/
public get asMap (): MapTypeV5 {
assert(this.isMap, `Cannot convert '${this.type}' via asMap`);
return this.value as MapTypeV5;
}
/**
* @description The value as a [[Type]] value
*/
public get asType (): PlainTypeV5 {
assert(this.isPlainType, `Cannot convert '${this.type}' via asType`);
return this.value as PlainTypeV5;
}
/**
* @description `true` if the storage entry is a doublemap
*/
public get isDoubleMap (): boolean {
return this.toNumber() === 2;
}
/**
* @description `true` if the storage entry is a map
*/
public get isMap (): boolean {
return this.toNumber() === 1;
}
/**
* @description `true` if the storage entry is a plain type
*/
public get isPlainType (): boolean {
return this.toNumber() === 0;
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
if (this.isDoubleMap) {
return `DoubleMap<${this.asDoubleMap.value.toString()}>`;
}
if (this.isMap) {
if (this.asMap.linked.isTrue) {
return `(${this.asMap.value.toString()}, Linkage<${this.asMap.key.toString()}>)`;
}
return this.asMap.value.toString();
}
return this.asType.toString();
}
}
export interface StorageFunctionMetadataValue {
name: string | Text;
modifier: StorageFunctionModifierV5 | AnyNumber;
type: StorageFunctionType;
fallback: Bytes;
documentation: Vec<Text> | string[];
}
/**
* @name StorageFunctionMetadata
* @description
* The definition of a storage function
*/
export class StorageFunctionMetadata extends Struct {
constructor (value?: StorageFunctionMetadataValue | Uint8Array) {
super({
name: 'Text',
modifier: 'StorageFunctionModifierV5',
type: StorageFunctionType,
fallback: 'Bytes',
documentation: 'Vec<Text>'
}, value);
}
/**
* @description The default value of the storage function
*/
public get fallback (): Bytes {
return this.get('fallback') as Bytes;
}
/**
* @description The [[Text]] documentation
*/
public get documentation (): Vec<Text> {
return this.get('documentation') as Vec<Text>;
}
/**
* @description The key name
*/
public get name (): Text {
return this.get('name') as Text;
}
/**
* @description The modifier
*/
public get modifier (): StorageFunctionModifierV5 {
return this.get('modifier') as StorageFunctionModifierV5;
}
/**
* @description The [[StorageFunctionType]]
*/
public get type (): StorageFunctionType {
return this.get('type') as StorageFunctionType;
}
}
-11
View File
@@ -1,11 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import MetadataV5, { ModuleMetadataV5 } from './Metadata';
export default MetadataV5;
export {
ModuleMetadataV5
};
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
-26
View File
@@ -1,26 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import MetadataV5 from './Metadata';
import MetadataV6 from '../v6';
import { ModuleMetadataV6 } from '../v6/Metadata';
/**
* Convert from MetadataV5 to MetadataV6
* See https://github.com/polkadot-js/api/issues/1043 for details
*/
export default function toV6 ({ modules }: MetadataV5): MetadataV6 {
return new MetadataV6({
modules: modules.map(({ calls, events, name, prefix, storage }): ModuleMetadataV6 =>
new ModuleMetadataV6({
calls,
constants: [],
events,
name,
prefix,
storage
})
)
});
}
@@ -1,25 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import polkadotJson from './static-polkadot.json';
import substrateJson from './static-substrate.json';
import polkadotData from './static.polkadot';
import substrateData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV6 (substrate)', (): void => {
decodeLatestSubstrate(6, substrateData, substrateJson);
toLatest(6, substrateData);
defaultValues(substrateData);
});
describe('MetadataV6 (polkadot)', (): void => {
decodeLatestSubstrate(6, polkadotData, polkadotJson);
toLatest(6, polkadotData);
defaultValues(polkadotData);
});
@@ -1,92 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { EventMetadataV6, FunctionMetadataV6, ModuleConstantMetadataV6 } from '../../interfaces/metadata';
import { MetadataInterface } from '../types';
import Option from '../../codec/Option';
import Struct from '../../codec/Struct';
import Vec from '../../codec/Vec';
import Text from '../../primitive/Text';
import { StorageEntryMetadata } from './Storage';
/**
* @name ModuleMetadataV6
* @description
* The definition of a module in the system
*/
export class ModuleMetadataV6 extends Struct {
constructor (value?: any) {
super({
name: 'Text',
prefix: 'Text',
storage: Option.with(Vec.with(StorageEntryMetadata)),
calls: Option.with('Vec<FunctionMetadataV6>'),
events: Option.with('Vec<EventMetadataV6>'),
constants: 'Vec<ModuleConstantMetadataV6>'
}, value);
}
/**
* @description the module calls
*/
public get calls (): Option<Vec<FunctionMetadataV6>> {
return this.get('calls') as Option<Vec<FunctionMetadataV6>>;
}
/**
* @description the module constants
*/
public get constants (): Vec<ModuleConstantMetadataV6> {
return this.get('constants') as Vec<ModuleConstantMetadataV6>;
}
/**
* @description the module events
*/
public get events (): Option<Vec<EventMetadataV6>> {
return this.get('events') as Option<Vec<EventMetadataV6>>;
}
/**
* @description the module name
*/
public get name (): Text {
return this.get('name') as Text;
}
/**
* @description the module prefix
*/
public get prefix (): Text {
return this.get('prefix') as Text;
}
/**
* @description the associated module storage
*/
public get storage (): Option<Vec<StorageEntryMetadata>> {
return this.get('storage') as Option<Vec<StorageEntryMetadata>>;
}
}
/**
* @name MetadataV6
* @description
* The runtime metadata as a decoded structure
*/
export default class MetadataV6 extends Struct implements MetadataInterface<ModuleMetadataV6> {
constructor (value?: any) {
super({
modules: Vec.with(ModuleMetadataV6)
}, value);
}
/**
* @description The associated modules for this structure
*/
public get modules (): Vec<ModuleMetadataV6> {
return this.get('modules') as Vec<ModuleMetadataV6>;
}
}
-12
View File
@@ -1,12 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { StorageFunctionMetadata, StorageFunctionMetadataValue, StorageFunctionType } from '../v5/Storage';
// Re-export classes that haven't changed between V5 and V6
export {
StorageFunctionMetadata as StorageEntryMetadata,
StorageFunctionMetadataValue as StorageEntryMetadataValue,
StorageFunctionType as StorageEntryType
};
-11
View File
@@ -1,11 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import MetadataV6, { ModuleMetadataV6 } from './Metadata';
export default MetadataV6;
export {
ModuleMetadataV6
};
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
-27
View File
@@ -1,27 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import MetadataV6 from './Metadata';
import MetadataV7 from '../v7';
import { ModuleMetadataV7 } from '../v7/Metadata';
import { StorageMetadata as StorageV7 } from '../v7/Storage';
/**
* Convert from MetadataV6 to MetadataV7
*/
export default function toV7 ({ modules }: MetadataV6): MetadataV7 {
return new MetadataV7({
modules: modules.map(({ calls, constants, events, name, prefix, storage }): ModuleMetadataV7 =>
new ModuleMetadataV7({
calls,
constants,
events,
name,
storage: storage.isSome
? new StorageV7({ prefix, items: storage.unwrap() })
: null
})
)
});
}
@@ -1,25 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import polkadotJson from './static-polkadot.json';
import substrateJson from './static-substrate.json';
import polkadotData from './static.polkadot';
import substrateData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV7 (substrate)', (): void => {
decodeLatestSubstrate(7, substrateData, substrateJson);
toLatest(7, substrateData);
defaultValues(substrateData);
});
describe('MetadataV7 (polkadot)', (): void => {
decodeLatestSubstrate(7, polkadotData, polkadotJson);
toLatest(7, polkadotData);
defaultValues(polkadotData);
});
@@ -1,84 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { EventMetadataV7, FunctionMetadataV7, ModuleConstantMetadataV7 } from '../../interfaces/metadata';
import { MetadataInterface } from '../types';
import Option from '../../codec/Option';
import Struct from '../../codec/Struct';
import Vec from '../../codec/Vec';
import Text from '../../primitive/Text';
import { StorageMetadata } from './Storage';
/**
* @name ModuleMetadataV7
* @description
* The definition of a module in the system
*/
export class ModuleMetadataV7 extends Struct {
constructor (value?: any) {
super({
name: 'Text',
storage: Option.with(StorageMetadata),
calls: Option.with('Vec<FunctionMetadataV7>'),
events: Option.with('Vec<EventMetadataV7>'),
constants: 'Vec<ModuleConstantMetadataV7>'
}, value);
}
/**
* @description the module calls
*/
public get calls (): Option<Vec<FunctionMetadataV7>> {
return this.get('calls') as Option<Vec<FunctionMetadataV7>>;
}
/**
* @description the module constants
*/
public get constants (): Vec<ModuleConstantMetadataV7> {
return this.get('constants') as Vec<ModuleConstantMetadataV7>;
}
/**
* @description the module events
*/
public get events (): Option<Vec<EventMetadataV7>> {
return this.get('events') as Option<Vec<EventMetadataV7>>;
}
/**
* @description the module name
*/
public get name (): Text {
return this.get('name') as Text;
}
/**
* @description the associated module storage
*/
public get storage (): Option<StorageMetadata> {
return this.get('storage') as Option<StorageMetadata>;
}
}
/**
* @name MetadataV7
* @description
* The runtime metadata as a decoded structure
*/
export default class MetadataV7 extends Struct implements MetadataInterface<ModuleMetadataV7> {
constructor (value?: any) {
super({
modules: Vec.with(ModuleMetadataV7)
}, value);
}
/**
* @description The associated modules for this structure
*/
public get modules (): Vec<ModuleMetadataV7> {
return this.get('modules') as Vec<ModuleMetadataV7>;
}
}
-39
View File
@@ -1,39 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import Struct from '../../codec/Struct';
import Vec from '../../codec/Vec';
import Text from '../../primitive/Text';
import { StorageEntryMetadata, StorageEntryMetadataValue, StorageEntryType } from '../v6/Storage';
// Re-export classes that haven't changed between V6 and V7
export {
StorageEntryMetadata,
StorageEntryMetadataValue,
StorageEntryType
};
export class StorageMetadata extends Struct {
constructor (value?: any) {
super({
prefix: 'Text',
// NOTE renamed to items from entries (since Struct already has entries from Map)
items: Vec.with(StorageEntryMetadata)
}, value);
}
/**
* @description the storage entries
*/
public get items (): Vec<StorageEntryMetadata> {
return this.get('items') as Vec<StorageEntryMetadata>;
}
/**
* @description the prefix for this module
*/
public get prefix (): Text {
return this.get('prefix') as Text;
}
}
-11
View File
@@ -1,11 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import MetadataV7, { ModuleMetadataV7 } from './Metadata';
export default MetadataV7;
export {
ModuleMetadataV7
};
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
-25
View File
@@ -1,25 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import MetadataV7 from './Metadata';
import MetadataV8 from '../v8';
import { ModuleMetadataV8 } from '../v8/Metadata';
/**
* Convert from MetadataV7 to MetadataV8
*/
export default function toV8 ({ modules }: MetadataV7): MetadataV8 {
return new MetadataV8({
modules: modules.map(({ calls, constants, events, name, storage }): ModuleMetadataV8 =>
new ModuleMetadataV8({
calls,
constants,
errors: [],
events,
name,
storage
})
)
});
}
@@ -1,15 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import substrateJson from './static-substrate.json';
import substrateData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV8 (substrate)', (): void => {
decodeLatestSubstrate(8, substrateData, substrateJson);
toLatest(8, substrateData);
defaultValues(substrateData);
});
-108
View File
@@ -1,108 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ErrorMetadataV8, EventMetadataV8, FunctionMetadataV8, ModuleConstantMetadataV8 } from '../../interfaces/metadata';
import { MetadataInterface } from '../types';
import Option from '../../codec/Option';
import Struct from '../../codec/Struct';
import Vec from '../../codec/Vec';
import Text from '../../primitive/Text';
import { StorageMetadata } from './Storage';
// Note the following errors are non-duplicated and fill the first 2 slots for
// each of the modules - this means that other errors start at index 2
//
// {
// "name": "Other",
// "documentation": [
// "Other unspecified error"
// ]
// },
// {
// "name": "CannotLookup",
// "documentation": [
// "Can not lookup"
// ]
// }
/**
* @name ModuleMetadataV8
* @description
* The definition of a module in the system
*/
export class ModuleMetadataV8 extends Struct {
constructor (value?: any) {
super({
name: 'Text',
storage: Option.with(StorageMetadata),
calls: Option.with('Vec<FunctionMetadataV8>'),
events: Option.with('Vec<EventMetadataV8>'),
constants: 'Vec<ModuleConstantMetadataV8>',
errors: 'Vec<ErrorMetadataV8>'
}, value);
}
/**
* @description the module calls
*/
public get calls (): Option<Vec<FunctionMetadataV8>> {
return this.get('calls') as Option<Vec<FunctionMetadataV8>>;
}
/**
* @description the module constants
*/
public get constants (): Vec<ModuleConstantMetadataV8> {
return this.get('constants') as Vec<ModuleConstantMetadataV8>;
}
/**
* @description the module errors
*/
public get errors (): Vec<ErrorMetadataV8> {
return this.get('errors') as Vec<ErrorMetadataV8>;
}
/**
* @description the module events
*/
public get events (): Option<Vec<EventMetadataV8>> {
return this.get('events') as Option<Vec<EventMetadataV8>>;
}
/**
* @description the module name
*/
public get name (): Text {
return this.get('name') as Text;
}
/**
* @description the associated module storage
*/
public get storage (): Option<StorageMetadata> {
return this.get('storage') as Option<StorageMetadata>;
}
}
/**
* @name MetadataV8
* @description
* The runtime metadata as a decoded structure
*/
export default class MetadataV8 extends Struct implements MetadataInterface<ModuleMetadataV8> {
constructor (value?: any) {
super({
modules: Vec.with(ModuleMetadataV8)
}, value);
}
/**
* @description The associated modules for this structure
*/
public get modules (): Vec<ModuleMetadataV8> {
return this.get('modules') as Vec<ModuleMetadataV8>;
}
}
@@ -1,5 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export { StorageEntryMetadata, StorageEntryMetadataValue, StorageEntryType, StorageMetadata } from '../v7/Storage';
-11
View File
@@ -1,11 +0,0 @@
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import MetadataV8, { ModuleMetadataV8 } from './Metadata';
export default MetadataV8;
export {
ModuleMetadataV8
};
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
+4 -3
View File
@@ -8,9 +8,10 @@ import { VoteThreshold } from '../interfaces/elections';
import { BlockNumber } from '../interfaces/runtime';
import { CodecTo } from '../types';
import Metadata from '@polkadot/metadata/Metadata';
import rpcMetadata from '@polkadot/metadata/Metadata/static';
import { ClassOf } from './create';
import Metadata from '../Metadata';
import metadataStatic from '../Metadata/static';
import Call from '../primitive/Generic/Call';
import Text from '../primitive/Text';
import U32 from '../primitive/U32';
@@ -66,7 +67,7 @@ describe('Tuple', (): void => {
});
it('creates properly via actual hex string', (): void => {
Call.injectMetadata(new Metadata(metadataStatic));
Call.injectMetadata(new Metadata(rpcMetadata));
const test = new (Tuple.with([
ClassOf('BlockNumber'), ClassOf('VoteThreshold')
+4 -3
View File
@@ -7,8 +7,9 @@ import '../injector';
import { PropIndex } from '../interfaces/democracy';
import { Codec, CodecTo } from '../types';
import Metadata from '../Metadata';
import metadataStatic from '../Metadata/static';
import Metadata from '@polkadot/metadata/Metadata';
import rpcMetadata from '@polkadot/metadata/Metadata/static';
import AccountId from '../primitive/Generic/AccountId';
import Call from '../primitive/Generic/Call';
import Text from '../primitive/Text';
@@ -22,7 +23,7 @@ describe('Vec', (): void => {
beforeEach((): void => {
vector = new Vec(Text, ['1', '23', '345', '4567', new Text('56789')]);
Call.injectMetadata(new Metadata(metadataStatic));
Call.injectMetadata(new Metadata(rpcMetadata));
});
it('wraps a sequence of values', (): void => {
+3 -2
View File
@@ -4,8 +4,9 @@
import './injector';
import Metadata from './Metadata';
import metadataStatic from './Metadata/static';
import Metadata from '@polkadot/metadata/Metadata';
import metadataStatic from '@polkadot/metadata/Metadata/static';
import { createTypeUnsafe } from './codec/create';
import GenericCall from './primitive/Generic/Call';
import { Codec } from './types';
+1 -1
View File
@@ -5,5 +5,5 @@
/**
* @summary Type definitions that are used in the system
*/
export { default as Metadata } from './Metadata';
export { default as Metadata } from '@polkadot/metadata/Metadata';
export * from './primitive';
@@ -4,8 +4,9 @@
import '../../injector';
import Metadata from '../../Metadata';
import metadataStatic from '../../Metadata/static';
import Metadata from '@polkadot/metadata/Metadata';
import metadataStatic from '@polkadot/metadata/Metadata/static';
import { createType } from '../../codec/create';
import Call from '../../primitive/Generic/Call';
import events from '../../json/SignedBlock.002.json';
@@ -6,20 +6,21 @@ import '../../injector';
import { EventRecord } from './types';
import Metadata from '@polkadot/metadata/Metadata';
import rpcMetadataV0 from '@polkadot/metadata/Metadata/v0/static';
import rpcMetadata from '@polkadot/metadata/Metadata/static';
import { createType } from '../../codec/create';
import Vec from '../../codec/Vec';
import json1 from '../../json/EventRecord.001.json';
import json3 from '../../json/EventRecord.003.json';
import GenericEvent from '../../primitive/Generic/Event';
import Metadata from '../../Metadata';
import metadataV0 from '../../Metadata/v0/static';
import metadata from '../../Metadata/static';
describe('EventRecord', (): void => {
describe('EventRecord_0_76', (): void => {
beforeEach((): void => {
GenericEvent.injectMetadata(
new Metadata(metadataV0)
new Metadata(rpcMetadataV0)
);
});
@@ -34,7 +35,7 @@ describe('EventRecord', (): void => {
describe('EventRecord (current)', (): void => {
beforeEach((): void => {
GenericEvent.injectMetadata(
new Metadata(metadata)
new Metadata(rpcMetadata)
);
});
@@ -4,16 +4,16 @@
import '../../injector';
import Metadata from '@polkadot/metadata/Metadata';
import rpcMetadata from '@polkadot/metadata/Metadata/static';
import { hexToU8a } from '@polkadot/util';
import Call from '../Generic/Call';
import Metadata from '../../Metadata';
import metadataStatic from '../../Metadata/static';
import Extrinsic from './Extrinsic';
describe('Extrinsic', (): void => {
beforeAll((): void => {
Call.injectMetadata(new Metadata(metadataStatic));
Call.injectMetadata(new Metadata(rpcMetadata));
});
describe('V1', (): void => {
@@ -4,8 +4,8 @@
import { createType, GenericCall } from '@polkadot/types';
import Metadata from '../../Metadata';
import metadataStatic from '../../Metadata/static';
import Metadata from '@polkadot/metadata/Metadata';
import rpcMetadata from '@polkadot/metadata/Metadata/static';
import SignerPayload from './SignerPayload';
describe('SignerPayload', (): void => {
@@ -23,7 +23,7 @@ describe('SignerPayload', (): void => {
};
beforeEach((): void => {
GenericCall.injectMetadata(new Metadata(metadataStatic));
GenericCall.injectMetadata(new Metadata(rpcMetadata));
});
it('creates a valid JSON output', (): void => {
@@ -5,19 +5,20 @@
import '../../../injector';
import BN from 'bn.js';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import Decorated from '@polkadot/metadata/Decorated';
import Metadata from '@polkadot/metadata/Metadata';
import rpcMetadata from '@polkadot/metadata/Metadata/static';
import testingPairs from '@polkadot/keyring/testingPairs';
import Metadata from '../../../Metadata';
import metadataStatic from '../../../Metadata/static';
import Call from '../../Generic/Call';
import Extrinsic from './Extrinsic';
const decorated = new Decorated(rpcMetadata);
const keyring = testingPairs({ type: 'ed25519' }, false);
describe('ExtrinsicV2', (): void => {
beforeEach((): void => {
Call.injectMetadata(new Metadata(metadataStatic));
Call.injectMetadata(new Metadata(rpcMetadata));
});
it('constructs a sane Uint8Array (default)', (): void => {
@@ -29,7 +30,7 @@ describe('ExtrinsicV2', (): void => {
it('creates a unsigned extrinsic', (): void => {
expect(
new Extrinsic(
extrinsics.balances.transfer(keyring.bob.publicKey, 6969)
decorated.tx.balances.transfer(keyring.bob.publicKey, 6969)
).toHex()
).toEqual(
'0x' +
@@ -43,7 +44,7 @@ describe('ExtrinsicV2', (): void => {
it('creates a signed extrinsic', (): void => {
expect(
new Extrinsic(
extrinsics.balances.transfer(keyring.bob.publicKey, 6969)
decorated.tx.balances.transfer(keyring.bob.publicKey, 6969)
).sign(keyring.alice, {
blockHash: '0xec7afaf1cca720ce88c1d1b689d81f0583cc15a97d621cf046dd9abf605ef22f',
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
@@ -5,19 +5,20 @@
import '../../../injector';
import BN from 'bn.js';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import Decorated from '@polkadot/metadata/Decorated';
import Metadata from '@polkadot/metadata/Metadata';
import rpcMetadata from '@polkadot/metadata/Metadata/static';
import testingPairs from '@polkadot/keyring/testingPairs';
import Metadata from '../../../Metadata';
import metadataStatic from '../../../Metadata/static';
import Call from '../../Generic/Call';
import Extrinsic from './Extrinsic';
const decorated = new Decorated(rpcMetadata);
const keyring = testingPairs({ type: 'ed25519' }, false);
describe('ExtrinsicV3', (): void => {
beforeEach((): void => {
Call.injectMetadata(new Metadata(metadataStatic));
Call.injectMetadata(new Metadata(rpcMetadata));
});
it('constructs a sane Uint8Array (default)', (): void => {
@@ -29,7 +30,7 @@ describe('ExtrinsicV3', (): void => {
it('creates a unsigned extrinsic', (): void => {
expect(
new Extrinsic(
extrinsics.balances.transfer(keyring.bob.publicKey, 6969)
decorated.tx.balances.transfer(keyring.bob.publicKey, 6969)
).toHex()
).toEqual(
'0x' +
@@ -43,7 +44,7 @@ describe('ExtrinsicV3', (): void => {
it('creates a signed extrinsic', (): void => {
expect(
new Extrinsic(
extrinsics.balances.transfer(keyring.bob.publicKey, 6969)
decorated.tx.balances.transfer(keyring.bob.publicKey, 6969)
).sign(keyring.alice, {
blockHash: '0xec7afaf1cca720ce88c1d1b689d81f0583cc15a97d621cf046dd9abf605ef22f',
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
@@ -5,14 +5,15 @@
import '../../../injector';
import BN from 'bn.js';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import testingPairs from '@polkadot/keyring/testingPairs';
import Decorated from '@polkadot/metadata';
import Metadata from '@polkadot/metadata/Metadata';
import metadataStatic from '@polkadot/metadata/Metadata/static';
import Metadata from '../../../Metadata';
import metadataStatic from '../../../Metadata/static';
import Call from '../../Generic/Call';
import Extrinsic from './Extrinsic';
const decorated = new Decorated(metadataStatic);
const keyring = testingPairs({ type: 'ed25519' }, false);
describe('ExtrinsicV4', (): void => {
@@ -29,7 +30,7 @@ describe('ExtrinsicV4', (): void => {
it('creates a unsigned extrinsic', (): void => {
expect(
new Extrinsic(
extrinsics.balances.transfer(keyring.bob.publicKey, 6969)
decorated.tx.balances.transfer(keyring.bob.publicKey, 6969)
).toHex()
).toEqual(
'0x' +
@@ -43,7 +44,7 @@ describe('ExtrinsicV4', (): void => {
it('creates a signed extrinsic', (): void => {
expect(
new Extrinsic(
extrinsics.balances.transfer(keyring.bob.publicKey, 6969)
decorated.tx.balances.transfer(keyring.bob.publicKey, 6969)
).sign(keyring.alice, {
blockHash: '0xec7afaf1cca720ce88c1d1b689d81f0583cc15a97d621cf046dd9abf605ef22f',
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
@@ -4,15 +4,16 @@
import '../../injector';
import Metadata from '../../Metadata';
import metadataStatic from '../../Metadata/static';
import Metadata from '@polkadot/metadata/Metadata';
import rpcMetadata from '@polkadot/metadata/Metadata/static';
import block00300 from '../../json/SignedBlock.003.00.json';
import Call from './Call';
import Block from './Block';
describe('Block', (): void => {
beforeEach((): void => {
Call.injectMetadata(new Metadata(metadataStatic));
Call.injectMetadata(new Metadata(rpcMetadata));
});
it('has a valid toRawType', (): void => {
@@ -1,11 +1,12 @@
// Copyright 2017-2019 @polkadot/api-metadata authors & contributors
// Copyright 2017-2019 @polkadot/metadata authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import Metadata from '../../Metadata';
import metadataStatic from '../../Metadata/static';
import Metadata from '@polkadot/metadata/Metadata';
import metadataStatic from '@polkadot/metadata/Metadata/static';
import Call from './Call';
describe('Call', (): void => {
+8 -3
View File
@@ -1,17 +1,19 @@
// Copyright 2017-2019 @polkadot/api-metadata authors & contributors
// Copyright 2017-2019 @polkadot/metadata authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { FunctionArgumentMetadataLatest, FunctionMetadataLatest } from '../../interfaces/metadata';
import { AnyU8a, ArgsDef, CallFunction, Codec, IMethod } from '../../types';
import extrinsicsFromMeta from '@polkadot/api-metadata/extrinsics/fromMetadata';
// FIXME Here we unfortunately cannot use Decorated, because that already calls
// fromMetadata for storage, and we have then a type import ordering problem
import extrinsicsFromMeta from '@polkadot/metadata/Decorated/extrinsics/fromMetadata';
import { assert, isHex, isObject, isU8a, u8aToU8a } from '@polkadot/util';
import Metadata from '@polkadot/metadata/Metadata';
import { getTypeDef, getTypeClass } from '../../codec/create';
import Struct from '../../codec/Struct';
import U8aFixed from '../../codec/U8aFixed';
import Metadata from '../../Metadata';
interface DecodeMethodInput {
args: any;
@@ -156,6 +158,9 @@ export default class Call extends Struct implements IMethod {
}, {} as unknown as ArgsDef);
}
// FIXME Should take the Decorated metadata (`import Metadata from '@polkadot/metadata'`)
// instead of the Codec Metadata
// https://github.com/polkadot-js/api/pull/1463#pullrequestreview-300618425
public static injectMetadata (metadata: Metadata): void {
const extrinsics = extrinsicsFromMeta(metadata);
@@ -7,12 +7,12 @@ import { EventMetadataLatest } from '../../interfaces/metadata';
import { EventId } from '../../interfaces/system';
import { Constructor, Codec } from '../../types';
import Metadata from '@polkadot/metadata/Metadata';
import { assert, isUndefined, stringCamelCase, u8aToHex } from '@polkadot/util';
import { getTypeClass, getTypeDef } from '../../codec/create';
import Struct from '../../codec/Struct';
import Tuple from '../../codec/Tuple';
import Metadata from '../../Metadata';
import Null from '../Null';
const EventTypes: Record<string, Constructor<EventData>> = {};
@@ -110,6 +110,9 @@ export default class Event extends Struct {
// This is called/injected by the API on init, allowing a snapshot of
// the available system events to be used in lookups
// FIXME Should take the Decorated metadata (`import Metadata from '@polkadot/metadata'`)
// instead of the Codec Metadata
// https://github.com/polkadot-js/api/pull/1463#pullrequestreview-300618425
public static injectMetadata (metadata: Metadata): void {
metadata.asLatest.modules
.filter(({ events }): boolean => events.isSome)
+41 -28
View File
@@ -4,26 +4,26 @@
import '../injector';
import fromMetadata from '@polkadot/api-metadata/storage/fromMetadata';
import Metadata from '@polkadot/metadata';
import rpcDataV3 from '@polkadot/metadata/Metadata/v3/static';
import rpcDataV4 from '@polkadot/metadata/Metadata/v4/static';
import rpcDataV5 from '@polkadot/metadata/Metadata/v5/static';
import rpcDataV6 from '@polkadot/metadata/Metadata/v6/static';
import rpcDataV7 from '@polkadot/metadata/Metadata/v7/static';
import rpcDataV8 from '@polkadot/metadata/Metadata/v8/static';
import { hexToU8a } from '@polkadot/util';
import rpcDataV3 from '@polkadot/types/Metadata/v3/static';
import rpcDataV4 from '@polkadot/types/Metadata/v4/static';
import rpcDataV5 from '@polkadot/types/Metadata/v5/static';
import rpcDataV6 from '@polkadot/types/Metadata/v6/static';
import rpcDataV7 from '@polkadot/types/Metadata/v7/static';
import rpcDataV8 from '@polkadot/types/Metadata/v8/static';
import Metadata from '../Metadata';
import StorageKey from './StorageKey';
describe('StorageKey', (): void => {
describe('with MetadataV3 (uses xxHash by default)', (): void => {
const storage = fromMetadata(new Metadata(rpcDataV3));
const metadata = new Metadata(rpcDataV3);
it('should correctly get Alice\'s freeBalance storage key (hex)', (): void => {
expect(
new StorageKey(
storage
metadata
.query
.balances
.freeBalance('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY')
)
@@ -34,7 +34,8 @@ describe('StorageKey', (): void => {
it('should correctly get Alice\'s freeBalance storage key (u8a)', (): void => {
expect(
new StorageKey(
storage
metadata
.query
.balances
.freeBalance('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY')
)
@@ -47,12 +48,13 @@ describe('StorageKey', (): void => {
});
describe('with MetadataV4 (uses xxHash by default)', (): void => {
const storage = fromMetadata(new Metadata(rpcDataV4));
const metadata = new Metadata(rpcDataV4);
it('should correctly get Alice\'s freeBalance storage key (hex)', (): void => {
expect(
new StorageKey(
storage
metadata
.query
.balances
.freeBalance('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY')
)
@@ -63,7 +65,8 @@ describe('StorageKey', (): void => {
it('should correctly get Alice\'s freeBalance storage key (u8a)', (): void => {
expect(
new StorageKey(
storage
metadata
.query
.balances
.freeBalance('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY')
)
@@ -79,7 +82,8 @@ describe('StorageKey', (): void => {
it('should correctly get the EventTopics double map storage key (hex)', (): void => {
expect(
new StorageKey([
storage
metadata
.query
.system
.eventTopics,
['any', [1, 2, 3]]
@@ -91,7 +95,8 @@ describe('StorageKey', (): void => {
it('should correctly get the EventTopics double map storage key (u8a)', (): void => {
expect(
new StorageKey([
storage
metadata
.query
.system
.eventTopics,
['any', [1, 2, 3]]
@@ -102,12 +107,13 @@ describe('StorageKey', (): void => {
});
describe('with MetadataV5', (): void => {
const storage = fromMetadata(new Metadata(rpcDataV5));
const metadata = new Metadata(rpcDataV5);
it('should correctly get the EventTopics double map storage key (hex)', (): void => {
expect(
new StorageKey([
storage
metadata
.query
.system
.eventTopics,
['any', [1, 2, 3]]
@@ -119,7 +125,8 @@ describe('StorageKey', (): void => {
it('should correctly get the EventTopics double map storage key (u8a)', (): void => {
expect(
new StorageKey([
storage
metadata
.query
.system
.eventTopics,
['any', [1, 2, 3]]
@@ -130,12 +137,13 @@ describe('StorageKey', (): void => {
});
describe('with MetadataV6', (): void => {
const storage = fromMetadata(new Metadata(rpcDataV6));
const metadata = new Metadata(rpcDataV6);
it('should correctly get the EventTopics double map storage key (hex)', (): void => {
expect(
new StorageKey([
storage
metadata
.query
.system
.eventTopics,
['any', [1, 2, 3]]
@@ -147,7 +155,8 @@ describe('StorageKey', (): void => {
it('should correctly get the EventTopics double map storage key (u8a)', (): void => {
expect(
new StorageKey([
storage
metadata
.query
.system
.eventTopics,
['any', [1, 2, 3]]
@@ -158,12 +167,13 @@ describe('StorageKey', (): void => {
});
describe('with MetadataV7', (): void => {
const storage = fromMetadata(new Metadata(rpcDataV7));
const metadata = new Metadata(rpcDataV7);
it('should correctly get the EventTopics double map storage key (hex)', (): void => {
expect(
new StorageKey([
storage
metadata
.query
.system
.eventTopics,
['any', [1, 2, 3]]
@@ -175,7 +185,8 @@ describe('StorageKey', (): void => {
it('should correctly get the EventTopics double map storage key (u8a)', (): void => {
expect(
new StorageKey([
storage
metadata
.query
.system
.eventTopics,
['any', [1, 2, 3]]
@@ -186,12 +197,13 @@ describe('StorageKey', (): void => {
});
describe('with MetadataV8', (): void => {
const storage = fromMetadata(new Metadata(rpcDataV8));
const metadata = new Metadata(rpcDataV8);
it('should correctly get the EventTopics double map storage key (hex)', (): void => {
expect(
new StorageKey([
storage
metadata
.query
.system
.eventTopics,
['any', [1, 2, 3]]
@@ -203,7 +215,8 @@ describe('StorageKey', (): void => {
it('should correctly get the EventTopics double map storage key (u8a)', (): void => {
expect(
new StorageKey([
storage
metadata
.query
.system
.eventTopics,
['any', [1, 2, 3]]
+1 -1
View File
@@ -2,9 +2,9 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { StorageEntryMetadata as MetaV8 } from '@polkadot/metadata/Metadata/v8/Storage';
import { assert, isFunction, isString, isU8a } from '@polkadot/util';
import { StorageEntryMetadata as MetaV8 } from '../Metadata/v8/Storage';
import { AnyU8a } from '../types';
import Bytes from './Bytes';
+4 -4
View File
@@ -5,13 +5,13 @@
import '../injector';
import fs from 'fs';
import Metadata from '@polkadot/metadata';
import rpcdata from '@polkadot/metadata/Metadata/static';
import MetadataV8, { ModuleMetadataV8 } from '@polkadot/metadata/Metadata/v8';
import { stringCamelCase, stringLowerFirst } from '@polkadot/util';
import interfaces from '../../../type-jsonrpc/src';
import Call from '../primitive/Generic/Call';
import Metadata from '../Metadata';
import rpcdata from '../Metadata/static';
import MetadataV8, { ModuleMetadataV8 } from '../Metadata/v8';
const ANCHOR_TOP = '';
const LINK_BACK_TO_TOP = '';
@@ -237,7 +237,7 @@ function writeToEventsMd (metadata: MetadataV8): void {
writeFile('docs/substrate/events.md', addEvents(metadata));
}
const metadata = new Metadata(rpcdata).asLatest;
const metadata = new Metadata(rpcdata).metadata.asLatest;
writeToRpcMd();
writeToConstantsMd(metadata);
@@ -9,7 +9,7 @@ require('@babel/register')({
plugins: [
['module-resolver', {
alias: {
'^@polkadot/api-metadata(.*)': './packages/api-metadata/src\\1',
'^@polkadot/metadata(.*)': './packages/metadata/src\\1',
'^@polkadot/types(.*)': './packages/types/src\\1'
}
}]
@@ -10,7 +10,7 @@ require('@babel/register')({
['module-resolver', {
alias: {
'^@polkadot/api-derive(.*)': './packages/api-derive/src\\1',
'^@polkadot/api-metadata(.*)': './packages/api-metadata/src\\1',
'^@polkadot/metadata(.*)': './packages/metadata/src\\1',
'^@polkadot/api(.*)': './packages/api/src/\\1',
'^@polkadot/jsonrpc(.*)': './packages/type-jsonrpc/src\\1',
'^@polkadot/rpc-core(.*)': './packages/rpc-core/src\\1',
@@ -3,12 +3,12 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import fs from 'fs';
import { ModuleMetadataV8 } from '@polkadot/metadata/Metadata/v8/Metadata';
import { StorageEntryMetadata } from '@polkadot/metadata/Metadata/v8/Storage';
import staticData from '@polkadot/metadata/Metadata/static';
import { stringLowerFirst } from '@polkadot/util';
import { Metadata } from '../..';
import { ModuleMetadataV8 } from '../../Metadata/v8/Metadata';
import { StorageEntryMetadata } from '../../Metadata/v8/Storage';
import staticData from '../../Metadata/static';
import { createImportCode, createImports, FOOTER, formatType, getSimilarTypes, HEADER, indent, setImports, TypeImports } from '../util';
// From a storage entry metadata, we return [args, returnType]
@@ -9,7 +9,7 @@ require('@babel/register')({
plugins: [
['module-resolver', {
alias: {
'^@polkadot/api-metadata(.*)': './packages/api-metadata/src\\1',
'^@polkadot/metadata(.*)': './packages/metadata/src\\1',
'^@polkadot/types(.*)': './packages/types/src\\1'
}
}]