diff --git a/CHANGELOG.md b/CHANGELOG.md index d36dcefae9..4fc88a0a02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - **Breaking change** (TypeScript only) The `*.d.ts` files now contain TypeScript 3.8 features, `#private`, which is not usable in older versions - **Breaking change** - `api.derive.staking.*` has updated result types and detects support for lazy payouts queries - Rework type generation for `api.{query, tx}` to properly handle complex type inputs (Thanks to https://github.com/monitz87) +- Rework metadata `--strict` mode to effectively decorate chain metadata (Thanks to https://github.com/monitz87) - Add `api.rpc.chain.subscribeAllHeads` (not just best) (Thanks to https://github.com/jak-pan) - Add `api.rpc.engine.*` for manual seal support - Add `api.injectMetadata(metadata)` to initialize API with a specific metadata version diff --git a/packages/api/src/augment/consts.ts b/packages/api/src/augment/consts.ts index fd3372bba0..3cea72851e 100644 --- a/packages/api/src/augment/consts.ts +++ b/packages/api/src/augment/consts.ts @@ -1,4 +1,4 @@ -// Auto-generated via `yarn polkadot-types-from-defs`, do not edit +// Auto-generated via `yarn polkadot-types-from-chain`, do not edit /* eslint-disable @typescript-eslint/no-empty-interface */ import { Codec } from '@polkadot/types/types'; @@ -12,7 +12,7 @@ declare module '@polkadot/metadata/Decorated/consts/types' { export interface Constants { [index: string]: ModuleConstants; babe: { - [index: string]: Codec; + [index: string]: AugmentedConst; /** * The number of **slots** that an epoch takes. We couple sessions to * epochs, i.e. we start a new session once the new epoch begins. @@ -28,7 +28,7 @@ declare module '@polkadot/metadata/Decorated/consts/types' { expectedBlockTime: AugmentedConst; }; timestamp: { - [index: string]: Codec; + [index: string]: AugmentedConst; /** * The minimum period between blocks. Beware that this is different to the *expected* period * that the block production apparatus provides. Your chosen consensus system will generally @@ -38,14 +38,14 @@ declare module '@polkadot/metadata/Decorated/consts/types' { minimumPeriod: AugmentedConst; }; balances: { - [index: string]: Codec; + [index: string]: AugmentedConst; /** * The minimum amount required to keep an account open. **/ existentialDeposit: AugmentedConst; }; transactionPayment: { - [index: string]: Codec; + [index: string]: AugmentedConst; /** * The fee to be paid for making a transaction; the base. **/ @@ -56,7 +56,7 @@ declare module '@polkadot/metadata/Decorated/consts/types' { transactionByteFee: AugmentedConst; }; staking: { - [index: string]: Codec; + [index: string]: AugmentedConst; /** * Number of sessions per era. **/ @@ -67,7 +67,7 @@ declare module '@polkadot/metadata/Decorated/consts/types' { bondingDuration: AugmentedConst; }; session: { - [index: string]: Codec; + [index: string]: AugmentedConst; /** * Used as first key for `NextKeys` and `KeyOwner` to put all the data into the same branch * of the trie. @@ -75,7 +75,7 @@ declare module '@polkadot/metadata/Decorated/consts/types' { dedupKeyPrefix: AugmentedConst; }; democracy: { - [index: string]: Codec; + [index: string]: AugmentedConst; /** * The minimum period of locking and the period between a proposal being approved and enacted. * It should generally be a little more than the unstake period to ensure that @@ -109,7 +109,7 @@ declare module '@polkadot/metadata/Decorated/consts/types' { preimageByteDeposit: AugmentedConst; }; elections: { - [index: string]: Codec; + [index: string]: AugmentedConst; candidacyBond: AugmentedConst; votingBond: AugmentedConst; desiredMembers: AugmentedConst; @@ -117,7 +117,7 @@ declare module '@polkadot/metadata/Decorated/consts/types' { termDuration: AugmentedConst; }; finalityTracker: { - [index: string]: Codec; + [index: string]: AugmentedConst; /** * The number of recent samples to keep from this chain. Default is 101. **/ @@ -128,7 +128,7 @@ declare module '@polkadot/metadata/Decorated/consts/types' { reportLatency: AugmentedConst; }; treasury: { - [index: string]: Codec; + [index: string]: AugmentedConst; /** * Fraction of a proposal's value that should be bonded in order to place the proposal. * An accepted proposal gets these back. A rejected proposal does not. @@ -164,7 +164,7 @@ declare module '@polkadot/metadata/Decorated/consts/types' { tipReportDepositPerByte: AugmentedConst; }; contracts: { - [index: string]: Codec; + [index: string]: AugmentedConst; /** * Number of block delay an extrinsic claim surcharge has. * When claim surcharge is called by an extrinsic the rent is checked @@ -237,7 +237,7 @@ declare module '@polkadot/metadata/Decorated/consts/types' { blockGasLimit: AugmentedConst; }; society: { - [index: string]: Codec; + [index: string]: AugmentedConst; /** * The minimum amount of a deposit required for a bid to be made. **/ diff --git a/packages/api/src/augment/query.ts b/packages/api/src/augment/query.ts index 89909cd72d..e7bd0ebfcd 100644 --- a/packages/api/src/augment/query.ts +++ b/packages/api/src/augment/query.ts @@ -1,4 +1,4 @@ -// Auto-generated via `yarn polkadot-types-from-defs`, do not edit +// Auto-generated via `yarn polkadot-types-from-chain`, do not edit /* eslint-disable @typescript-eslint/no-empty-interface */ import { AnyNumber, ITuple } from '@polkadot/types/types'; diff --git a/packages/api/src/augment/rpc.ts b/packages/api/src/augment/rpc.ts index 03f8c6474c..caf9effdf8 100644 --- a/packages/api/src/augment/rpc.ts +++ b/packages/api/src/augment/rpc.ts @@ -1,4 +1,4 @@ -// Auto-generated via `yarn polkadot-types-from-defs`, do not edit +// Auto-generated via `yarn polkadot-types-from-chain`, do not edit /* eslint-disable @typescript-eslint/no-empty-interface */ import { AnyNumber, Codec, IExtrinsic } from '@polkadot/types/types'; diff --git a/packages/api/src/augment/tx.ts b/packages/api/src/augment/tx.ts index cace3aa548..6815f3866b 100644 --- a/packages/api/src/augment/tx.ts +++ b/packages/api/src/augment/tx.ts @@ -1,4 +1,4 @@ -// Auto-generated via `yarn polkadot-types-from-defs`, do not edit +// Auto-generated via `yarn polkadot-types-from-chain`, do not edit /* eslint-disable @typescript-eslint/no-empty-interface */ import { AnyNumber, ITuple } from '@polkadot/types/types'; @@ -17,8 +17,7 @@ import { SocietyJudgement } from '@polkadot/types/interfaces/society'; import { EraIndex, RewardDestination, ValidatorPrefs } from '@polkadot/types/interfaces/staking'; import { Key } from '@polkadot/types/interfaces/system'; import { Timepoint } from '@polkadot/types/interfaces/utility'; -import { SubmittableExtrinsic } from '@polkadot/api/submittable/types'; -import { ApiTypes } from '@polkadot/api/types'; +import { ApiTypes, SubmittableExtrinsic } from '@polkadot/api/types'; declare module '@polkadot/api/types/submittable' { export interface AugmentedSubmittables { diff --git a/packages/typegen/src/generate/consts.ts b/packages/typegen/src/generate/consts.ts index e5cbb2edc6..458684a0f6 100644 --- a/packages/typegen/src/generate/consts.ts +++ b/packages/typegen/src/generate/consts.ts @@ -19,10 +19,12 @@ function generateModule (allDefs: object, { constants, name }: ModuleMetadataLat return []; } - setImports(allDefs, imports, ['Codec']); + if (!isStrict) { + setImports(allDefs, imports, ['Codec']); + } return [indent(4)(`${stringCamelCase(name.toString())}: {`)] - .concat(isStrict ? '' : indent(6)('[index: string]: Codec;')) + .concat(isStrict ? '' : indent(6)('[index: string]: AugmentedConst;')) .concat(constants.map(({ documentation, name, type }): string => { setImports(allDefs, imports, [type.toString()]); @@ -43,7 +45,7 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: Record { return acc.concat(generateModule(allDefs, mod, imports, isStrict)); }, [] as string[]); - const header = createImportCode(HEADER, imports, [ + const header = createImportCode(HEADER('chain'), imports, [ ...Object.keys(imports.localTypes).sort().map((moduleName): { file: string; types: string[] } => ({ file: `${imports.moduleToPackage[moduleName]}/${moduleName}`, types: Object.keys(imports.localTypes[moduleName]) diff --git a/packages/typegen/src/generate/interfaceRegistry.ts b/packages/typegen/src/generate/interfaceRegistry.ts index 3772a83a8d..468225a2dc 100644 --- a/packages/typegen/src/generate/interfaceRegistry.ts +++ b/packages/typegen/src/generate/interfaceRegistry.ts @@ -44,7 +44,7 @@ export function generateInterfaceTypes (importDefinitions: { [importPath: string ].join('\n'); }, ''); - const header = createImportCode(HEADER, imports, [ + const header = createImportCode(HEADER('defs'), imports, [ ...Object.keys(imports.localTypes).sort().map((moduleName): { file: string; types: string[] } => ({ file: `${imports.moduleToPackage[moduleName]}/${moduleName}`, types: Object.keys(imports.localTypes[moduleName]) diff --git a/packages/typegen/src/generate/query.ts b/packages/typegen/src/generate/query.ts index 24bca66b45..333addf21f 100644 --- a/packages/typegen/src/generate/query.ts +++ b/packages/typegen/src/generate/query.ts @@ -46,7 +46,7 @@ function entrySignature (allDefs: object, registry: Registry, storageEntry: Stor formatType(allDefs, addModifier(storageEntry, storageEntry.type.asMap.value.toString()), imports) ]; } else if (storageEntry.type.isDoubleMap) { - // Find similartypes of `key1` and `key2` types + // Find similar types of `key1` and `key2` types const similarTypes1 = getSimilarTypes(allDefs, registry, storageEntry.type.asDoubleMap.key1.toString(), imports); const similarTypes2 = getSimilarTypes(allDefs, registry, storageEntry.type.asDoubleMap.key2.toString(), imports); @@ -102,7 +102,7 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr const body = meta.asLatest.modules.reduce((acc: string[], mod): string[] => { return acc.concat(generateModule(allDefs, registry, mod, imports, isStrict)); }, []); - const header = createImportCode(HEADER, imports, [ + const header = createImportCode(HEADER('chain'), imports, [ ...Object.keys(imports.localTypes).sort().map((moduleName): { file: string; types: string[] } => ({ file: `${imports.moduleToPackage[moduleName]}/${moduleName}`, types: Object.keys(imports.localTypes[moduleName]) diff --git a/packages/typegen/src/generate/rpc.ts b/packages/typegen/src/generate/rpc.ts index e3a01e7b7b..b3b671acd2 100644 --- a/packages/typegen/src/generate/rpc.ts +++ b/packages/typegen/src/generate/rpc.ts @@ -48,7 +48,7 @@ export default function generateRpcTypes (dest = 'packages/api/src/augment/rpc.t ); }, []).join('\n'); - const header = createImportCode(HEADER, imports, [ + const header = createImportCode(HEADER('chain'), imports, [ ...Object.keys(imports.localTypes).sort().map((moduleName): { file: string; types: string[] } => ({ file: `@polkadot/types/interfaces/${moduleName}`, types: Object.keys(imports.localTypes[moduleName]) diff --git a/packages/typegen/src/generate/tsDef.ts b/packages/typegen/src/generate/tsDef.ts index d9567474ff..b3e551af57 100644 --- a/packages/typegen/src/generate/tsDef.ts +++ b/packages/typegen/src/generate/tsDef.ts @@ -184,7 +184,7 @@ function generateTsDefFor (importDefinitions: { [importPath: string]: object }, const interfaces = generateInterfaces(definitions, { types }, imports); const sortedDefs = interfaces.sort((a, b): number => a[0].localeCompare(b[0])).map(([, definition]): string => definition).join('\n\n'); - const header = createImportCode(HEADER, imports, [ + const header = createImportCode(HEADER('defs'), imports, [ ...Object.keys(imports.localTypes).sort().map((moduleName): { file: string; types: string[] } => ({ file: `${imports.moduleToPackage[moduleName]}/${moduleName}`, types: Object.keys(imports.localTypes[moduleName]) @@ -192,7 +192,7 @@ function generateTsDefFor (importDefinitions: { [importPath: string]: object }, ]); fs.writeFileSync(path.join(outputDir, defName, 'types.ts'), header.concat(sortedDefs).concat(FOOTER), { flag: 'w' }); - fs.writeFileSync(path.join(outputDir, defName, 'index.ts'), HEADER.concat('export * from \'./types\';').concat(FOOTER), { flag: 'w' }); + fs.writeFileSync(path.join(outputDir, defName, 'index.ts'), HEADER('defs').concat('export * from \'./types\';').concat(FOOTER), { flag: 'w' }); } /** @internal */ @@ -206,7 +206,7 @@ export function generateTsDef (importDefinitions: { [importPath: string]: object generateTsDefFor(importDefinitions, defName, obj, outputDir); }); - return HEADER + return HEADER('defs') .concat( Object .keys(definitions) @@ -216,7 +216,7 @@ export function generateTsDef (importDefinitions: { [importPath: string]: object .concat(FOOTER); }); - fs.writeFileSync(path.join(outputDir, 'index.ts'), HEADER.concat('export * from \'./types\';').concat(FOOTER), { flag: 'w' }); + fs.writeFileSync(path.join(outputDir, 'index.ts'), HEADER('defs').concat('export * from \'./types\';').concat(FOOTER), { flag: 'w' }); } /** @internal */ diff --git a/packages/typegen/src/generate/tx.ts b/packages/typegen/src/generate/tx.ts index 07a11b5256..50375b1423 100644 --- a/packages/typegen/src/generate/tx.ts +++ b/packages/typegen/src/generate/tx.ts @@ -70,18 +70,14 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr const body = meta.asLatest.modules.reduce((acc, mod): string[] => { return acc.concat(generateModule(registry, allDefs, mod, imports, isStrict)); }, [] as string[]); - const header = createImportCode(HEADER, imports, [ + const header = createImportCode(HEADER('chain'), imports, [ ...Object.keys(imports.localTypes).sort().map((moduleName): { file: string; types: string[] } => ({ file: `${imports.moduleToPackage[moduleName]}/${moduleName}`, types: Object.keys(imports.localTypes[moduleName]) })), - { - file: '@polkadot/api/submittable/types', - types: ['SubmittableExtrinsic'] - }, { file: '@polkadot/api/types', - types: ['ApiTypes'] + types: ['ApiTypes', 'SubmittableExtrinsic'] } ]); const interfaceStart = [ diff --git a/packages/typegen/src/util/formatting.ts b/packages/typegen/src/util/formatting.ts index 4024aa7820..8a37a718d9 100644 --- a/packages/typegen/src/util/formatting.ts +++ b/packages/typegen/src/util/formatting.ts @@ -9,7 +9,7 @@ import { paramsNotation } from '@polkadot/types/codec/utils'; import { setImports, TypeImports } from './imports'; -export const HEADER = '// Auto-generated via `yarn polkadot-types-from-defs`, do not edit\n/* eslint-disable @typescript-eslint/no-empty-interface */\n\n'; +export const HEADER = (type: 'chain' | 'defs'): string => `// Auto-generated via \`yarn polkadot-types-from-${type}\`, do not edit\n/* eslint-disable @typescript-eslint/no-empty-interface */\n\n`; export const FOOTER = '\n'; const TYPES_NON_PRIMITIVE = ['Metadata'];