@@ -104,7 +104,6 @@ function createKeyDoubleMap (registry: Registry, itemFn: CreateItemFn, stringKey
|
||||
/** @internal */
|
||||
function createKey (registry: Registry, itemFn: CreateItemFn, stringKey: string, arg: CreateArgType, hasher: (value: Uint8Array) => Uint8Array, metaVersion: number): Uint8Array {
|
||||
const { meta: { name, type } } = itemFn;
|
||||
let key: Uint8Array | undefined;
|
||||
let param: Uint8Array = EMPTY_U8A;
|
||||
|
||||
if (type.isMap) {
|
||||
@@ -113,21 +112,14 @@ function createKey (registry: Registry, itemFn: CreateItemFn, stringKey: string,
|
||||
assert(!isUndefined(arg) && !isNull(arg), `${name} is a Map and requires one argument`);
|
||||
|
||||
param = createTypeUnsafe(registry, map.key.toString(), [arg]).toU8a();
|
||||
|
||||
// prefix maps are using prefixes to optimize the trie, so the key generation are
|
||||
// done differently where the prefix/method are hashed separately with only the
|
||||
// parameter for the key being attached via the hasher
|
||||
if (map.kind.isPrefixedMap) {
|
||||
key = u8aConcat(createPrefixedKey(itemFn), hasher(param));
|
||||
}
|
||||
}
|
||||
|
||||
// StorageKey is a Bytes, so is length-prefixed
|
||||
return Compact.addLengthPrefix(key || (
|
||||
return Compact.addLengthPrefix(
|
||||
metaVersion <= 8
|
||||
? hasher(u8aConcat(stringToU8a(stringKey), param))
|
||||
: u8aConcat(createPrefixedKey(itemFn), param.length ? hasher(param) : EMPTY_U8A)
|
||||
));
|
||||
);
|
||||
}
|
||||
|
||||
// attach the metadata to expand to a StorageFunction
|
||||
@@ -210,7 +202,7 @@ export default function createFunction (registry: Registry, itemFn: CreateItemFn
|
||||
if (type.isMap) {
|
||||
const map = type.asMap;
|
||||
|
||||
if (map.kind.isLinkedMap) {
|
||||
if (map.linked.isTrue) {
|
||||
extendLinkedMap(registry, itemFn, storageFn, stringKey, hasher, options.metaVersion);
|
||||
} else {
|
||||
extendPrefixedMap(registry, itemFn, storageFn);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { MetadataAll, MetadataLatest, MetadataV0, MetadataV1, MetadataV2, MetadataV3, MetadataV4, MetadataV5, MetadataV6, MetadataV7, MetadataV8, MetadataV9, MetadataV10, MetadataV11 } from '@polkadot/types/interfaces/metadata';
|
||||
import { MetadataAll, MetadataLatest, MetadataV0, MetadataV1, MetadataV2, MetadataV3, MetadataV4, MetadataV5, MetadataV6, MetadataV7, MetadataV8, MetadataV9, MetadataV10 } from '@polkadot/types/interfaces/metadata';
|
||||
import { Registry } from '@polkadot/types/types';
|
||||
|
||||
import { createType } from '@polkadot/types/codec/create/createType';
|
||||
@@ -20,12 +20,12 @@ import v6ToV7 from './v6/toV7';
|
||||
import v7ToV8 from './v7/toV8';
|
||||
import v8ToV9 from './v8/toV9';
|
||||
import v9ToV10 from './v9/toV10';
|
||||
import v10ToV11 from './v10/toV11';
|
||||
import v10ToLatest from './v10/toLatest';
|
||||
import { getUniqTypes, toCallsOnly } from './util';
|
||||
|
||||
type MetaMapped = MetadataV0 | MetadataV1 | MetadataV2 | MetadataV3 | MetadataV4 | MetadataV5 | MetadataV6 | MetadataV7 | MetadataV8 | MetadataV9 | MetadataV10 | MetadataV11;
|
||||
type MetaMapped = MetadataV0 | MetadataV1 | MetadataV2 | MetadataV3 | MetadataV4 | MetadataV5 | MetadataV6 | MetadataV7 | MetadataV8 | MetadataV9 | MetadataV10;
|
||||
type MetaVersions = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11;
|
||||
type MetaAsX = 'asV0' | 'asV1' | 'asV2' | 'asV3' | 'asV4' | 'asV5' | 'asV6' | 'asV7' | 'asV8' | 'asV9' | 'asV10' | 'asV11';
|
||||
type MetaAsX = 'asV0' | 'asV1' | 'asV2' | 'asV3' | 'asV4' | 'asV5' | 'asV6' | 'asV7' | 'asV8' | 'asV9' | 'asV10';
|
||||
|
||||
/**
|
||||
* @name MetadataVersioned
|
||||
@@ -154,18 +154,12 @@ export default class MetadataVersioned extends Struct {
|
||||
return this.getVersion(10, v9ToV10);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the wrapped values as a V10 object
|
||||
*/
|
||||
public get asV11 (): MetadataV11 {
|
||||
return this.getVersion(11, v10ToV11);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the wrapped values as a latest version object
|
||||
*/
|
||||
public get asLatest (): MetadataLatest {
|
||||
return this.asV11;
|
||||
// 11 is non-existent & latest - applied here to do to the type conversions
|
||||
return this.getVersion(11, v10ToLatest);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
// 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 './v9/static';
|
||||
import metadata from './v10/static';
|
||||
|
||||
export default metadata;
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
// Copyright 2017-2020 @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 { OverrideModuleType } from '@polkadot/types/known/types';
|
||||
import { FunctionMetadataV10, FunctionMetadataLatest, MetadataV10, MetadataLatest, ModuleMetadataV10, ModuleMetadataLatest } from '@polkadot/types/interfaces/metadata';
|
||||
import { Registry } from '@polkadot/types/types';
|
||||
|
||||
import { getModuleTypes } from '@polkadot/types/known';
|
||||
import { createType } from '@polkadot/types';
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
// apply module-specific type overrides - this should always be done as part of toLatest
|
||||
/** @internal */
|
||||
function convertCalls (registry: Registry, calls: FunctionMetadataV10[], sectionTypes: OverrideModuleType[]): FunctionMetadataLatest[] {
|
||||
return calls.map(({ args, documentation, name }): FunctionMetadataLatest => {
|
||||
args.forEach(({ type }): void => {
|
||||
const queued = sectionTypes.find(({ name }): boolean => type.eq(name));
|
||||
|
||||
if (queued) {
|
||||
type.setOverride(queued.override);
|
||||
}
|
||||
});
|
||||
|
||||
return createType(registry, 'FunctionMetadataLatest', { args, name, documentation });
|
||||
});
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function convertModule (registry: Registry, mod: ModuleMetadataV10): ModuleMetadataLatest {
|
||||
const calls = mod.calls.unwrapOr(null);
|
||||
const sectionTypes = getModuleTypes(stringCamelCase(mod.name.toString())) || [];
|
||||
|
||||
return createType(registry, 'ModuleMetadataLatest', {
|
||||
...mod,
|
||||
calls: calls
|
||||
? convertCalls(registry, calls, sectionTypes)
|
||||
: null
|
||||
});
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export default function toLatest (registry: Registry, { modules }: MetadataV10): MetadataLatest {
|
||||
return createType(registry, 'MetadataLatest', {
|
||||
modules: modules.map((mod): ModuleMetadataLatest => convertModule(registry, mod))
|
||||
});
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
// Copyright 2017-2020 @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 { OverrideModuleType } from '@polkadot/types/known/types';
|
||||
import { FunctionMetadataV10, FunctionMetadataV11, MetadataV10, MetadataV11, ModuleMetadataV10, ModuleMetadataV11, StorageEntryTypeV10 } from '@polkadot/types/interfaces/metadata';
|
||||
import { Registry } from '@polkadot/types/types';
|
||||
|
||||
import { getModuleTypes } from '@polkadot/types/known';
|
||||
import { createType } from '@polkadot/types';
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
// migrate a storage entry type - only map types are different (with a kind enum)
|
||||
// all return the actual value for the enum, followed by the numeric index
|
||||
/** @internal */
|
||||
function createStorageType (registry: Registry, entryType: StorageEntryTypeV10): [any, number] {
|
||||
if (entryType.isPlain) {
|
||||
return [entryType.asPlain, 0];
|
||||
} else if (entryType.isDoubleMap) {
|
||||
return [entryType.asDoubleMap, 2];
|
||||
}
|
||||
|
||||
const map = entryType.asMap;
|
||||
|
||||
// map needs a migration, removing linked and adding an explicit kind
|
||||
return [{
|
||||
...map,
|
||||
kind: createType(registry, 'MapKindV11', null, map.linked.isTrue ? 1 : 0)
|
||||
}, 1];
|
||||
}
|
||||
|
||||
// apply module-specific type overrides
|
||||
/** @internal */
|
||||
function convertCalls (registry: Registry, calls: FunctionMetadataV10[], sectionTypes: OverrideModuleType[]): FunctionMetadataV11[] {
|
||||
return calls.map(({ args, documentation, name }): FunctionMetadataV11 => {
|
||||
args.forEach(({ type }): void => {
|
||||
const queued = sectionTypes.find(({ name }): boolean => type.eq(name));
|
||||
|
||||
if (queued) {
|
||||
type.setOverride(queued.override);
|
||||
}
|
||||
});
|
||||
|
||||
return createType(registry, 'FunctionMetadataV11', { args, name, documentation });
|
||||
});
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function convertModule (registry: Registry, mod: ModuleMetadataV10): ModuleMetadataV11 {
|
||||
const calls = mod.calls.unwrapOr(null);
|
||||
const storage = mod.storage.unwrapOr(null);
|
||||
const sectionTypes = getModuleTypes(stringCamelCase(mod.name.toString())) || [];
|
||||
|
||||
return createType(registry, 'ModuleMetadataV11', {
|
||||
...mod,
|
||||
calls: calls
|
||||
? convertCalls(registry, calls, sectionTypes)
|
||||
: null,
|
||||
storage: storage
|
||||
? {
|
||||
...storage,
|
||||
items: storage.items.map((item): any => ({
|
||||
...item,
|
||||
type: createType(registry, 'StorageEntryTypeV11', ...createStorageType(registry, item.type))
|
||||
}))
|
||||
}
|
||||
: null
|
||||
});
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export default function toV11 (registry: Registry, { modules }: MetadataV10): MetadataV11 {
|
||||
return createType(registry, 'MetadataV11', {
|
||||
modules: modules.map((mod): ModuleMetadataV11 => convertModule(registry, mod))
|
||||
});
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
// Copyright 2017-2020 @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 { TypeRegistry } from '@polkadot/types';
|
||||
|
||||
import substrateJson from './static-substrate.json';
|
||||
import substrateData from './static';
|
||||
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
|
||||
|
||||
describe('MetadataV11 (substrate)', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
decodeLatestSubstrate(registry, 11, substrateData, substrateJson);
|
||||
|
||||
toLatest(registry, 11, substrateData);
|
||||
|
||||
defaultValues(registry, substrateData);
|
||||
});
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user