diff --git a/packages/metadata/src/decorate/storage/getStorage.ts b/packages/metadata/src/decorate/storage/getStorage.ts index 2d1ed3fe7d..a78a7273bf 100644 --- a/packages/metadata/src/decorate/storage/getStorage.ts +++ b/packages/metadata/src/decorate/storage/getStorage.ts @@ -5,7 +5,7 @@ import type { StorageEntry } from '@polkadot/types/primitive/types'; import type { Registry } from '@polkadot/types/types'; import type { Storage } from '../types'; -import * as substrate from './substrate'; +import { substrate } from './substrate'; /** @internal */ export function getStorage (registry: Registry, metaVersion: number): Storage { diff --git a/packages/metadata/src/decorate/storage/substrate.ts b/packages/metadata/src/decorate/storage/substrate.ts index 8ab845cbfd..e5af6ab5f9 100644 --- a/packages/metadata/src/decorate/storage/substrate.ts +++ b/packages/metadata/src/decorate/storage/substrate.ts @@ -12,6 +12,8 @@ interface SubstrateMetadata { type: string; } +type Creator = (registry: Registry, metaVersion: number) => StorageEntry; + // Small helper function to factorize code on this page. /** @internal */ function createRuntimeFunction (method: string, key: string, { documentation, type }: SubstrateMetadata): (registry: Registry, metaVersion: number) => StorageEntry { @@ -29,27 +31,25 @@ function createRuntimeFunction (method: string, key: string, { documentation, ty }, { key, metaVersion, skipHashing: true }); } -export const code = createRuntimeFunction('code', ':code', { - documentation: 'Wasm code of the runtime.', - type: 'Bytes' -}); - -export const heapPages = createRuntimeFunction('heapPages', ':heappages', { - documentation: 'Number of wasm linear memory pages required for execution of the runtime.', - type: 'u64' -}); - -export const extrinsicIndex = createRuntimeFunction('extrinsicIndex', ':extrinsic_index', { - documentation: 'Current extrinsic index (u32) is stored under this key.', - type: 'u32' -}); - -export const changesTrieConfig = createRuntimeFunction('changesTrieConfig', ':changes_trie', { - documentation: 'Changes trie configuration is stored under this key.', - type: 'u32' -}); - -export const childStorageKeyPrefix = createRuntimeFunction('childStorageKeyPrefix', ':child_storage:', { - documentation: 'Prefix of child storage keys.', - type: 'u32' -}); +export const substrate: Record = { + changesTrieConfig: createRuntimeFunction('changesTrieConfig', ':changes_trie', { + documentation: 'Changes trie configuration is stored under this key.', + type: 'u32' + }), + childStorageKeyPrefix: createRuntimeFunction('childStorageKeyPrefix', ':child_storage:', { + documentation: 'Prefix of child storage keys.', + type: 'u32' + }), + code: createRuntimeFunction('code', ':code', { + documentation: 'Wasm code of the runtime.', + type: 'Bytes' + }), + extrinsicIndex: createRuntimeFunction('extrinsicIndex', ':extrinsic_index', { + documentation: 'Current extrinsic index (u32) is stored under this key.', + type: 'u32' + }), + heapPages: createRuntimeFunction('heapPages', ':heappages', { + documentation: 'Number of wasm linear memory pages required for execution of the runtime.', + type: 'u64' + }) +}; diff --git a/packages/typegen/src/metadataMd.ts b/packages/typegen/src/metadataMd.ts index edd14c3347..bcf3e2df51 100644 --- a/packages/typegen/src/metadataMd.ts +++ b/packages/typegen/src/metadataMd.ts @@ -7,6 +7,7 @@ import type { Codec, DefinitionRpcParam } from '@polkadot/types/types'; import fs from 'fs'; import { Metadata } from '@polkadot/metadata'; +import { getStorage as getSubstrateStorage } from '@polkadot/metadata/decorate/storage/getStorage'; import rpcdata from '@polkadot/metadata/static'; import { Vec } from '@polkadot/types/codec'; import { TypeRegistry } from '@polkadot/types/create'; @@ -16,15 +17,17 @@ import { Text } from '@polkadot/types/primitive'; import { unwrapStorageType } from '@polkadot/types/primitive/StorageKey'; import { stringCamelCase, stringLowerFirst } from '@polkadot/util'; +interface SectionItem { + link?: string; + name: string; + [bullet: string]: undefined | string | Vec; +} + interface Section { link?: string; name: string; description?: string; - items: { - link?: string; - name: string; - [bullet: string]: undefined | string | Vec; - }[]; + items: SectionItem[]; } interface Page { @@ -192,10 +195,11 @@ function addConstants (metadata: MetadataLatest): string { /** @internal */ function addStorage (metadata: MetadataLatest): string { + const { substrate } = getSubstrateStorage(metadata.registry, 12); const moduleSections = metadata.modules .sort(sortByName) .filter((moduleMetadata) => !moduleMetadata.storage.isNone) - .map((moduleMetadata) => { + .map((moduleMetadata): Section => { const sectionName = stringLowerFirst(moduleMetadata.name); return { @@ -220,12 +224,27 @@ function addStorage (metadata: MetadataLatest): string { }; }); - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - const knownSection: any = JSON.parse(fs.readFileSync('docs/substrate/storage-known-section.json', 'utf8')); - return renderPage({ description: DESC_STORAGE, - sections: moduleSections.concat([knownSection]), + sections: moduleSections.concat([{ + description: 'These are well-known keys that are always available to the runtime implementation of any Substrate-based network.', + items: Object.entries(substrate).map(([name, { meta }]) => { + const arg = meta.type.isMap + ? ('`' + meta.type.asMap.key.toString() + '`') + : meta.type.isDoubleMap + ? ('`' + meta.type.asDoubleMap.key1.toString() + ', ' + meta.type.asDoubleMap.key2.toString() + '`') + : ''; + const methodName = stringLowerFirst(name); + const outputType = unwrapStorageType(meta.type, meta.modifier.isOptional); + + return { + interface: '`' + `api.query.substrate.${methodName}` + '`', + name: `${methodName}(${arg}): ` + '`' + outputType + '`', + ...(meta.documentation.length && { summary: meta.documentation }) + }; + }), + name: 'substrate' + }]), title: 'Storage' }); }