From 455a8d2a914eece8f4bc9869bed574209518a771 Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 7 Feb 2022 11:44:17 +0100 Subject: [PATCH] Adjust map header output type with lookup (#4536) * Adjust map header output type with lookup * Adjust typings --- CHANGELOG.md | 7 +++++++ .../decorate/storage/createFunction.spec.ts | 7 ++++--- .../decorate/storage/createFunction.ts | 21 +++++++++++-------- packages/types/src/primitive/types.ts | 7 ++++++- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f189cd7d79..dbc7cc1117 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## master + +Changes: + +- Adjust map header output type with lookup id + + ## 7.7.1 Feb 6, 2022 Upgrade priority: Low. Recommended for users with long-running scripts. diff --git a/packages/types/src/metadata/decorate/storage/createFunction.spec.ts b/packages/types/src/metadata/decorate/storage/createFunction.spec.ts index adc68c5a0a..5fd57ac1fc 100644 --- a/packages/types/src/metadata/decorate/storage/createFunction.spec.ts +++ b/packages/types/src/metadata/decorate/storage/createFunction.spec.ts @@ -75,9 +75,7 @@ describe('createFunction', (): void => { expect( u8aToHex( - storageFn( - 'DB2mp5nNhbFN86J9hxoAog8JALMhDXgwvWMxrRMLNUFMEY4' - ) + storageFn('DB2mp5nNhbFN86J9hxoAog8JALMhDXgwvWMxrRMLNUFMEY4') ) ).toEqual( '0x' + @@ -87,6 +85,9 @@ describe('createFunction', (): void => { '79c598d130209ab0dea15637a5b16be7' + // blake 128 '1a7938fede32e1275281b3eee5708706d88444a6dc898a4dec463f1eb298463f' // AccountId ); + expect( + storageFn.iterKey && storageFn.iterKey.meta.type.asPlain.toNumber() + ).toEqual(0); }); it('allows creating of known 2 Map keys (with Bytes)', (): void => { diff --git a/packages/types/src/metadata/decorate/storage/createFunction.ts b/packages/types/src/metadata/decorate/storage/createFunction.ts index 4739b245af..31eae07200 100644 --- a/packages/types/src/metadata/decorate/storage/createFunction.ts +++ b/packages/types/src/metadata/decorate/storage/createFunction.ts @@ -3,7 +3,7 @@ import type { ICompact, INumber } from '@polkadot/types-codec/types'; import type { StorageEntryMetadataLatest, StorageHasher } from '../../../interfaces/metadata'; -import type { StorageEntry } from '../../../primitive/types'; +import type { StorageEntry, StorageEntryIterator } from '../../../primitive/types'; import type { Registry } from '../../../types'; import { Raw } from '@polkadot/types-codec'; @@ -126,22 +126,25 @@ function createWithMeta (registry: Registry, itemFn: CreateItemFn, options: Crea } /** @internal */ -function extendHeadMeta (registry: Registry, { meta: { docs, name, type }, section }: CreateItemFn, { method }: StorageEntry, iterFn: (...args: unknown[]) => Raw): (...args: unknown[]) => StorageKey { - const outputType = registry.createLookupType(type.asMap.key); - +function extendHeadMeta (registry: Registry, { meta: { docs, name, type }, section }: CreateItemFn, { method }: StorageEntry, iterFn: (...args: unknown[]) => Raw): StorageEntryIterator { // metadata with a fallback value using the type of the key, the normal // meta fallback only applies to actual entry values, create one for head - (iterFn as IterFn).meta = registry.createTypeUnsafe('StorageEntryMetadataLatest', [{ + const meta = registry.createTypeUnsafe('StorageEntryMetadataLatest', [{ docs, fallback: registry.createTypeUnsafe('Bytes', []), modifier: registry.createTypeUnsafe('StorageEntryModifierLatest', [1]), // required name, - // FIXME??? - type: registry.createTypeUnsafe('StorageEntryTypeLatest', [outputType, 0]) + type: registry.createTypeUnsafe('StorageEntryTypeLatest', [type.asMap.key, 0]) }]); - return (...args: unknown[]) => - registry.createTypeUnsafe('StorageKey', [iterFn(...args), { method, section }]); + (iterFn as IterFn).meta = meta; + + const fn = (...args: unknown[]) => + registry.createTypeUnsafe('StorageKey', [iterFn(...args), { method, section }]); + + fn.meta = meta; + + return fn; } /** @internal */ diff --git a/packages/types/src/primitive/types.ts b/packages/types/src/primitive/types.ts index 2718f8b7a5..5c2f688d01 100644 --- a/packages/types/src/primitive/types.ts +++ b/packages/types/src/primitive/types.ts @@ -4,9 +4,14 @@ import type { StorageEntryMetadataLatest } from '../interfaces/metadata'; import type { Codec } from '../types'; +export interface StorageEntryIterator { + (...args: unknown[]): Uint8Array & Codec; + meta: StorageEntryMetadataLatest; +} + export interface StorageEntry { (...args: unknown[]): Uint8Array; - iterKey?: (...args: unknown[]) => Uint8Array & Codec; + iterKey?: StorageEntryIterator; keyPrefix: (...args: unknown[]) => Uint8Array; meta: StorageEntryMetadataLatest; method: string;