Files
api/packages/metadata/src/Metadata/v3/toV4.ts
T
Jaco GreeffandAmaury Martiny 011e24bd49 Pass registry to all codec types (#1592)
* Pass registry to all codec types

* Pass through registry inside API (where the rubber meets the road)

* Small additions

* Derive w/ registry

* Types are more-or-less covered

* Some test adjustments (guided by linting)

* Rid the world of some ../injector

* Additional cleanups, merge injector into registry

* Cleanup extrinsics (always passing registry)

* Remove injector imports (not all replaced)

* Typ spec cleanups

* Phase 1 - types seem lint-free

* rpc-{core,provider} lint free

* Start working through metadata

* Small metadata additions

* Some e2e tests (out-of-order)

* Down to a handful

* Fixup tests for packages/types/src/codec/*.spec.ts

* packages/types/src/codec passes all tests

* packages/types ok (Convert storage with registry fn)

* progress (sort out injection, dreaded find-before msg)

* Down to 1 lint issue...

* Linting done... along with on-demand import

* Exposed ordering

* Remove global injects

* Cleanup comments

* Add CHANGELOG & UPGRADING

* Apply suggestions from code review

Co-Authored-By: Amaury Martiny <amaury.martiny@protonmail.com>

* Update Metadata description
2019-11-21 16:32:39 +01:00

74 lines
2.5 KiB
TypeScript

// 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 { Registry } from '@polkadot/types/types';
import StorageHasher from '@polkadot/types/primitive/StorageHasher';
import { createType, Option, Vec } from '@polkadot/types/codec';
import MetadataV3 from './Metadata';
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 (registry: Registry, 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(registry, 'MapTypeV4', {
hasher: new StorageHasher(registry, 'Twox128'),
key: type.asMap.key,
value: type.asMap.value,
linked: type.asMap.linked
}), 1]
: [createType(registry, 'DoubleMapTypeV4', {
hasher: new StorageHasher(registry, 'Twox128'),
key1: type.asDoubleMap.key1,
key2: type.asDoubleMap.key2,
value: type.asDoubleMap.value,
key2Hasher: type.asDoubleMap.key2Hasher
}), 2];
return new StorageFunctionMetadata(registry, {
documentation,
fallback,
name,
modifier,
type: new StorageFunctionType(registry, newType, index)
});
}
/**
* Convert from MetadataV3 to MetadataV4
* See https://github.com/paritytech/substrate/pull/2268 for details
*/
export default function toV4 (registry: Registry, { modules }: MetadataV3): MetadataV4 {
return new MetadataV4(registry, {
modules: modules.map(({ calls, events, name, prefix, storage }): ModuleMetadataV4 =>
new ModuleMetadataV4(registry, {
calls,
events,
name,
prefix,
storage: storage.isSome
? new Option(
registry,
Vec.with(StorageFunctionMetadata),
storage.unwrap().map((v): StorageFunctionMetadata => toV4StorageFunction(registry, v))
)
: undefined
})
)
});
}