UNPKG

1.63 kBJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/types authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3// migrate a storage hasher type
4// see https://github.com/paritytech/substrate/pull/4462
5
6/** @internal */
7function createStorageHasher(registry, hasher) {
8 // Blake2_128_Concat has been added at index 2, so we increment all the
9 // indexes greater than 2
10 if (hasher.toNumber() >= 2) {
11 return registry.createTypeUnsafe('StorageHasherV10', [hasher.toNumber() + 1]);
12 }
13
14 return registry.createTypeUnsafe('StorageHasherV10', [hasher]);
15}
16/** @internal */
17
18
19function createStorageType(registry, entryType) {
20 if (entryType.isMap) {
21 return [{ ...entryType.asMap,
22 hasher: createStorageHasher(registry, entryType.asMap.hasher)
23 }, 1];
24 }
25
26 if (entryType.isDoubleMap) {
27 return [{ ...entryType.asDoubleMap,
28 hasher: createStorageHasher(registry, entryType.asDoubleMap.hasher),
29 key2Hasher: createStorageHasher(registry, entryType.asDoubleMap.key2Hasher)
30 }, 2];
31 }
32
33 return [entryType.asPlain, 0];
34}
35/** @internal */
36
37
38function convertModule(registry, mod) {
39 const storage = mod.storage.unwrapOr(null);
40 return registry.createTypeUnsafe('ModuleMetadataV10', [{ ...mod,
41 storage: storage ? { ...storage,
42 items: storage.items.map(item => ({ ...item,
43 type: registry.createTypeUnsafe('StorageEntryTypeV10', createStorageType(registry, item.type))
44 }))
45 } : null
46 }]);
47}
48/** @internal */
49
50
51export function toV10(registry, {
52 modules
53}) {
54 return registry.createTypeUnsafe('MetadataV10', [{
55 modules: modules.map(mod => convertModule(registry, mod))
56 }]);
57}
\No newline at end of file