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:
committed by
Jaco Greeff
parent
c87ba33893
commit
16e0ea9315
@@ -0,0 +1,15 @@
|
||||
// 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 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);
|
||||
});
|
||||
@@ -0,0 +1,84 @@
|
||||
// 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 { 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 (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>;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
// 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 } 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 (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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@@ -0,0 +1,76 @@
|
||||
// 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 { assert } from '@polkadot/util';
|
||||
|
||||
import { createType, Option, Vec } 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'],
|
||||
['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
|
||||
})
|
||||
)
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user