From cb1d13e04c3e4b94aee8e755bd90e4635d7d603d Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 9 Mar 2022 12:29:31 +0100 Subject: [PATCH] Add lookup types to the registry (#4642) * Add lookup types to the registry * Adjust --- packages/api-augment/src/kusama/index.ts | 1 + packages/api-augment/src/kusama/registry.ts | 6 + packages/api-augment/src/polkadot/index.ts | 1 + packages/api-augment/src/polkadot/registry.ts | 5 + packages/api-augment/src/substrate/index.ts | 1 + .../api-augment/src/substrate/registry.ts | 4 + packages/typegen/src/generate/lookup.ts | 24 +- packages/typegen/src/util/derived.spec.ts | 1 + packages/typegen/src/util/formatting.spec.ts | 1 + packages/typegen/src/util/formatting.ts | 4 + packages/typegen/src/util/imports.ts | 2 + packages/types-augment/src/registry/kusama.ts | 14 + .../types-augment/src/registry/polkadot.ts | 152 +++++++++ .../types-augment/src/registry/substrate.ts | 320 ++++++++++++++++++ 14 files changed, 535 insertions(+), 1 deletion(-) create mode 100644 packages/api-augment/src/kusama/registry.ts create mode 100644 packages/api-augment/src/polkadot/registry.ts create mode 100644 packages/api-augment/src/substrate/registry.ts create mode 100644 packages/types-augment/src/registry/kusama.ts create mode 100644 packages/types-augment/src/registry/polkadot.ts create mode 100644 packages/types-augment/src/registry/substrate.ts diff --git a/packages/api-augment/src/kusama/index.ts b/packages/api-augment/src/kusama/index.ts index a7bd980e3c..ab66a055ae 100644 --- a/packages/api-augment/src/kusama/index.ts +++ b/packages/api-augment/src/kusama/index.ts @@ -6,4 +6,5 @@ import './consts'; import './errors'; import './events'; import './query'; +import './registry'; import './tx'; diff --git a/packages/api-augment/src/kusama/registry.ts b/packages/api-augment/src/kusama/registry.ts new file mode 100644 index 0000000000..b82d4e0d12 --- /dev/null +++ b/packages/api-augment/src/kusama/registry.ts @@ -0,0 +1,6 @@ +// Copyright 2017-2022 @polkadot/api-augment authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import '@polkadot/types-augment/registry/substrate'; +import '@polkadot/types-augment/registry/polkadot'; +import '@polkadot/types-augment/registry/kusama'; diff --git a/packages/api-augment/src/polkadot/index.ts b/packages/api-augment/src/polkadot/index.ts index a7bd980e3c..ab66a055ae 100644 --- a/packages/api-augment/src/polkadot/index.ts +++ b/packages/api-augment/src/polkadot/index.ts @@ -6,4 +6,5 @@ import './consts'; import './errors'; import './events'; import './query'; +import './registry'; import './tx'; diff --git a/packages/api-augment/src/polkadot/registry.ts b/packages/api-augment/src/polkadot/registry.ts new file mode 100644 index 0000000000..55d701494d --- /dev/null +++ b/packages/api-augment/src/polkadot/registry.ts @@ -0,0 +1,5 @@ +// Copyright 2017-2022 @polkadot/api-augment authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import '@polkadot/types-augment/registry/substrate'; +import '@polkadot/types-augment/registry/polkadot'; diff --git a/packages/api-augment/src/substrate/index.ts b/packages/api-augment/src/substrate/index.ts index a7bd980e3c..ab66a055ae 100644 --- a/packages/api-augment/src/substrate/index.ts +++ b/packages/api-augment/src/substrate/index.ts @@ -6,4 +6,5 @@ import './consts'; import './errors'; import './events'; import './query'; +import './registry'; import './tx'; diff --git a/packages/api-augment/src/substrate/registry.ts b/packages/api-augment/src/substrate/registry.ts new file mode 100644 index 0000000000..4dd5583176 --- /dev/null +++ b/packages/api-augment/src/substrate/registry.ts @@ -0,0 +1,4 @@ +// Copyright 2017-2022 @polkadot/api-augment authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import '@polkadot/types-augment/registry/substrate'; diff --git a/packages/typegen/src/generate/lookup.ts b/packages/typegen/src/generate/lookup.ts index f82a8f082c..d97b6f70fa 100644 --- a/packages/typegen/src/generate/lookup.ts +++ b/packages/typegen/src/generate/lookup.ts @@ -25,6 +25,7 @@ const generateLookupDefsTmpl = Handlebars.compile(readTemplate('lookup/defs')); const generateLookupDefsNamedTmpl = Handlebars.compile(readTemplate('lookup/defs-named')); const generateLookupIndexTmpl = Handlebars.compile(readTemplate('lookup/index')); const generateLookupTypesTmpl = Handlebars.compile(readTemplate('lookup/types')); +const generateRegistryTmpl = Handlebars.compile(readTemplate('interfaceRegistry')); function generateParamType (registry: Registry, { name, type }: SiTypeParameter): string { if (type.isSome) { @@ -214,13 +215,34 @@ function generateLookupTypes (registry: Registry, filtered: [PortableType, TypeD writeFile(path.join(destDir, 'index.ts'), () => generateLookupIndexTmpl({ headerType: 'defs' }), true); } -function generateLookup (destDir: string, entries: [string | undefined, HexString][]): void { +function generateRegistry (registry: Registry, filtered: [PortableType, TypeDef][], destDir: string, subPath: string): void { + writeFile(path.join(destDir, `${subPath}.ts`), (): string => { + const items = filtered + .map(([, { lookupName }]) => lookupName) + .filter((n): n is string => !!n) + .sort() + .reduce((all: string[], n) => all.includes(n) ? all : all.concat(n), []); + const imports = createImports({}, { types: {} }); + + imports.lookupTypes = items.reduce((all, n) => ({ ...all, [n]: true }), {}); + + return generateRegistryTmpl({ + headerType: 'defs', + imports, + items, + types: [] + }); + }, true); +} + +function generateLookup (destDir: string, entries: [string, HexString][]): void { entries.reduce((exclude, [subPath, staticMeta]): string[] => { const { lookup, registry } = initMeta(staticMeta).metadata.asLatest; const filtered = getFilteredTypes(lookup, exclude); generateLookupDefs(registry, filtered, destDir, subPath); generateLookupTypes(registry, filtered, destDir, subPath); + generateRegistry(registry, filtered, destDir, subPath === 'lookup' ? 'registry' : `../registry/${subPath}`); return exclude.concat( ...filtered diff --git a/packages/typegen/src/util/derived.spec.ts b/packages/typegen/src/util/derived.spec.ts index 1447fb3af5..6f89a71454 100644 --- a/packages/typegen/src/util/derived.spec.ts +++ b/packages/typegen/src/util/derived.spec.ts @@ -20,6 +20,7 @@ describe('getSimilarTypes', (): void => { genericTypes: {}, ignoredTypes: [], localTypes: {}, + lookupTypes: {}, metadataTypes: {}, primitiveTypes: {}, typeToModule: {}, diff --git a/packages/typegen/src/util/formatting.spec.ts b/packages/typegen/src/util/formatting.spec.ts index 8e4280317f..d8c4793fa8 100644 --- a/packages/typegen/src/util/formatting.spec.ts +++ b/packages/typegen/src/util/formatting.spec.ts @@ -17,6 +17,7 @@ describe('formatType', (): void => { genericTypes: {}, ignoredTypes: [], localTypes: {}, + lookupTypes: {}, metadataTypes: {}, primitiveTypes: {}, typeToModule: {}, diff --git a/packages/typegen/src/util/formatting.ts b/packages/typegen/src/util/formatting.ts index 8dc0a47283..86bf872aee 100644 --- a/packages/typegen/src/util/formatting.ts +++ b/packages/typegen/src/util/formatting.ts @@ -47,6 +47,10 @@ function extractImports ({ imports, types }: This): string[] { !NO_CODEC.includes(n) && !ON_CODEC.includes(n) ) }, + { + file: '@polkadot/types/lookup', + types: Object.keys(imports.lookupTypes) + }, { file: '@polkadot/types/types', types: Object.keys(imports.typesTypes).filter((n) => diff --git a/packages/typegen/src/util/imports.ts b/packages/typegen/src/util/imports.ts index ce0c8cf239..1abe487ef8 100644 --- a/packages/typegen/src/util/imports.ts +++ b/packages/typegen/src/util/imports.ts @@ -25,6 +25,7 @@ export interface TypeImports { genericTypes: TypeExist; // `import {} from '@polkadot/types/generic` ignoredTypes: string[]; // No need to import these types localTypes: TypeExistMap; // `import {} from '../something'` + lookupTypes: TypeExistMap; // `import {} from '@polkadot/types/lookup` primitiveTypes: TypeExist; // `import {} from '@polkadot/types/primitive` metadataTypes: TypeExist; // `import {} from '@polkadot/types/metadata` typesTypes: TypeExist; // `import {} from '@polkadot/types/types` @@ -119,6 +120,7 @@ export function createImports (importDefinitions: Record