Move Metadata to interfaces (#1598)

* Move Metadata to interfaces

* Align imports

* Suppress errors

* Remove TS overrides, createTypeUnsafe returns Codec

* Add type unwrap

* Fixup doc generation

* Remove V8 references
This commit is contained in:
Jaco Greeff
2019-11-22 13:38:20 +01:00
committed by GitHub
parent 4b6b0305e6
commit 8b1a7a8584
42 changed files with 644 additions and 1396 deletions
@@ -1,85 +0,0 @@
// 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 { EventMetadataV4, FunctionMetadataV4 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
import { MetadataInterface } from '../types';
import Option from '@polkadot/types/codec/Option';
import Struct from '@polkadot/types/codec/Struct';
import Vec from '@polkadot/types/codec/Vec';
import Text from '@polkadot/types/primitive/Text';
import { StorageFunctionMetadata } from './Storage';
/**
* @name ModuleMetadataV4
* @description
* The definition of a module in the system
*/
export class ModuleMetadataV4 extends Struct {
constructor (registry: Registry, value?: any) {
super(registry, {
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 (registry: Registry, value?: any) {
super(registry, {
modules: Vec.with(ModuleMetadataV4)
}, value);
}
/**
* @description The associated modules for this structure
*/
public get modules (): Vec<ModuleMetadataV4> {
return this.get('modules') as Vec<ModuleMetadataV4>;
}
}
@@ -1,132 +0,0 @@
// 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 { DoubleMapTypeV4, MapTypeV4, PlainTypeV4, StorageFunctionModifierV4 } from '@polkadot/types/interfaces/metadata';
import { AnyNumber, Registry } from '@polkadot/types/types';
import { assert } from '@polkadot/util';
import Enum from '@polkadot/types/codec/Enum';
import Struct from '@polkadot/types/codec/Struct';
import Vec from '@polkadot/types/codec/Vec';
import Bytes from '@polkadot/types/primitive/Bytes';
import Text from '@polkadot/types/primitive/Text';
export class StorageFunctionType extends Enum {
constructor (registry: Registry, value?: any, index?: number) {
super(registry, {
Plain: '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 asPlain (): PlainTypeV4 {
assert(this.isPlain, `Cannot convert '${this.type}' via asPlain`);
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 isPlain (): boolean {
return this.toNumber() === 0;
}
}
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 (registry: Registry, value?: StorageFunctionMetadataValue | Uint8Array) {
super(registry, {
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;
}
}
@@ -1,11 +0,0 @@
// 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 MetadataV4, { ModuleMetadataV4 } from './Metadata';
export default MetadataV4;
export {
ModuleMetadataV4
};
+9 -13
View File
@@ -2,18 +2,14 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { MetadataV4, MetadataV5, ModuleMetadataV5, StorageFunctionMetadataV4, StorageFunctionMetadataV5 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
import { assert } from '@polkadot/util';
import { createType, Option, Vec } from '@polkadot/types/codec';
import { createType, Option } from '@polkadot/types/codec';
import StorageHasher from '@polkadot/types/primitive/StorageHasher';
import Text from '@polkadot/types/primitive/Text';
import MetadataV4 from './Metadata';
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'],
@@ -34,7 +30,7 @@ function toStorageHasher (registry: Registry, text: Text): StorageHasher {
/**
* Convert V4 StorageFunction to V5 StorageFunction
*/
function toV5StorageFunction (registry: Registry, storageFn: StorageFunctionMetadataV4): StorageFunctionMetadata {
function toV5StorageFunction (registry: Registry, storageFn: StorageFunctionMetadataV4): StorageFunctionMetadataV5 {
const { documentation, fallback, modifier, name, type } = storageFn;
const [newType, index] = type.isPlain
? [type.asPlain, 0]
@@ -48,12 +44,12 @@ function toV5StorageFunction (registry: Registry, storageFn: StorageFunctionMeta
key2Hasher: toStorageHasher(registry, type.asDoubleMap.key2Hasher)
}), 2];
return new StorageFunctionMetadata(registry, {
return createType(registry, 'StorageFunctionMetadataV5', {
documentation,
fallback,
name,
modifier,
type: new StorageFunctionType(registry, newType, index)
type: createType(registry, 'StorageFunctionTypeV5', newType, index)
});
}
@@ -62,16 +58,16 @@ function toV5StorageFunction (registry: Registry, storageFn: StorageFunctionMeta
* See https://github.com/paritytech/substrate/pull/2836/files for details
*/
export default function toV5 (registry: Registry, { modules }: MetadataV4): MetadataV5 {
return new MetadataV5(registry, {
return createType(registry, 'MetadataV5', {
modules: modules.map(({ calls, events, name, prefix, storage }): ModuleMetadataV5 =>
new ModuleMetadataV5(registry, {
createType(registry, 'ModuleMetadataV5', {
name,
prefix,
storage: storage.isSome
? new Option(
registry,
Vec.with(StorageFunctionMetadata),
storage.unwrap().map((v): StorageFunctionMetadata => toV5StorageFunction(registry, v))
'Vec<StorageFunctionMetadataV5>',
storage.unwrap().map((v): StorageFunctionMetadataV5 => toV5StorageFunction(registry, v))
)
: undefined,
calls,