Cleanup to tx generation (#1784)
This commit is contained in:
@@ -10,7 +10,7 @@ import staticData from '@polkadot/metadata/Metadata/static';
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import { Metadata, TypeRegistry } from '../..';
|
||||
import { createImportCode, createImports, FOOTER, HEADER, indent, setImports, TypeImports } from '../util';
|
||||
import { createDocComments, createImportCode, createImports, FOOTER, HEADER, indent, setImports, TypeImports } from '../util';
|
||||
|
||||
// Generate types for one module
|
||||
/** @internal */
|
||||
@@ -19,13 +19,15 @@ function generateModule (allDefs: object, { constants, name }: ModuleMetadataLat
|
||||
return [];
|
||||
}
|
||||
|
||||
// NOTE Not removing this concat yet, first see the fallout (needs: setImports(allDefs, imports, ['Codec']);)
|
||||
return [indent(4)(`${stringCamelCase(name.toString())}: {`)]
|
||||
// .concat(indent(6)('[index: string]: Codec;'))
|
||||
.concat(constants.map((constant): string => {
|
||||
setImports(allDefs, imports, [constant.type.toString()]);
|
||||
setImports(allDefs, imports, ['Codec']);
|
||||
|
||||
return indent(6)(`${stringCamelCase(constant.name.toString())}: ${constant.type} & ConstantCodec;`);
|
||||
return [indent(4)(`${stringCamelCase(name.toString())}: {`)]
|
||||
.concat(indent(6)('[index: string]: Codec;'))
|
||||
.concat(constants.map(({ documentation, name, type }): string => {
|
||||
setImports(allDefs, imports, [type.toString()]);
|
||||
|
||||
return createDocComments(documentation).map((d): string => indent(6)(d)).join('\n') +
|
||||
indent(6)(`${stringCamelCase(name.toString())}: ${type} & ConstantCodec;`);
|
||||
}))
|
||||
.concat([indent(4)('};')]);
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import { stringLowerFirst } from '@polkadot/util';
|
||||
|
||||
import { Metadata } from '../..';
|
||||
import { TypeRegistry } from '../../codec';
|
||||
import { FOOTER, HEADER, TypeImports, createImportCode, createImports, formatType, getSimilarTypes, indent, setImports } from '../util';
|
||||
import { FOOTER, HEADER, TypeImports, createDocComments, createImportCode, createImports, formatType, getSimilarTypes, indent, setImports } from '../util';
|
||||
|
||||
// If the StorageEntry returns T, output `Option<T>` if the modifier is optional
|
||||
/** @internal */
|
||||
@@ -67,16 +67,6 @@ function entrySignature (allDefs: object, registry: Registry, storageEntry: Stor
|
||||
throw new Error(`entryArgs: Cannot parse args of entry ${storageEntry.name}`);
|
||||
}
|
||||
|
||||
// Generate types for one storage entry in a module
|
||||
/** @internal */
|
||||
function generateEntry (allDefs: object, registry: Registry, storageEntry: StorageEntryMetadataLatest, imports: TypeImports): string[] {
|
||||
const [args, returnType] = entrySignature(allDefs, registry, storageEntry, imports);
|
||||
|
||||
return [
|
||||
`${stringLowerFirst(storageEntry.name.toString())}: AugmentedQuery<ApiType, (${args}) => Observable<${returnType}>> & QueryableStorageEntry<ApiType>;`
|
||||
];
|
||||
}
|
||||
|
||||
// Generate types for one module
|
||||
/** @internal */
|
||||
function generateModule (allDefs: object, registry: Registry, { name, storage }: ModuleMetadataLatest, imports: TypeImports): string[] {
|
||||
@@ -84,15 +74,14 @@ function generateModule (allDefs: object, registry: Registry, { name, storage }:
|
||||
return [];
|
||||
}
|
||||
|
||||
// NOTE Not removing this concat yet, first see the fallout
|
||||
return [indent(4)(`${stringLowerFirst(name.toString())}: {`)]
|
||||
// .concat(indent(6)('[index: string]: QueryableStorageEntry<ApiType>;'))
|
||||
.concat(storage.unwrap()
|
||||
.items
|
||||
.reduce((acc, storageEntry): string[] => {
|
||||
return acc.concat(generateEntry(allDefs, registry, storageEntry, imports).map(indent(6)));
|
||||
}, [] as string[])
|
||||
.join('\n'))
|
||||
.concat(indent(6)('[index: string]: QueryableStorageEntry<ApiType>;'))
|
||||
.concat(storage.unwrap().items.map((storageEntry): string => {
|
||||
const [args, returnType] = entrySignature(allDefs, registry, storageEntry, imports);
|
||||
|
||||
return createDocComments(storageEntry.documentation).map((d): string => indent(6)(d)).join('\n') +
|
||||
indent(6)(`${stringLowerFirst(storageEntry.name.toString())}: AugmentedQuery<ApiType, (${args}) => Observable<${returnType}>> & QueryableStorageEntry<ApiType>;`);
|
||||
}))
|
||||
.concat([indent(4)('};')]);
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import { Text } from '@polkadot/types';
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import { Metadata, TypeRegistry } from '../..';
|
||||
import { FOOTER, HEADER, TypeImports, createImportCode, createImports, formatType, getSimilarTypes, indent, setImports } from '../util';
|
||||
import { FOOTER, HEADER, TypeImports, createDocComments, createImportCode, createImports, formatType, getSimilarTypes, indent, setImports } from '../util';
|
||||
|
||||
const MAPPED_NAMES: Record<string, string> = {
|
||||
new: 'updated'
|
||||
@@ -34,26 +34,28 @@ function generateModule (registry: Registry, allDefs: object, { calls, name }: M
|
||||
return [];
|
||||
}
|
||||
|
||||
setImports(allDefs, imports, ['CallFunction', 'SubmittableExtrinsic']);
|
||||
setImports(allDefs, imports, ['SubmittableExtrinsic']);
|
||||
|
||||
// NOTE Not removing this concat yet, first see the fallout
|
||||
return [indent(4)(`${stringCamelCase(name.toString())}: {`)]
|
||||
// .concat(indent(6)('[index: string]: Codec;'))
|
||||
.concat(allCalls.map(({ args, name }): string => {
|
||||
const params = args.map(({ name, type }): string => {
|
||||
.concat(indent(6)('[index: string]: SubmittableExtrinsicFunction<ApiType>;'))
|
||||
.concat(allCalls.map(({ args, documentation, name }): string => {
|
||||
const params = args.map(({ name, type }): [string, string, string] => {
|
||||
const typeStr = type.toString();
|
||||
const similarTypes = getSimilarTypes(allDefs, registry, typeStr, imports).map((type): string =>
|
||||
type.startsWith('(')
|
||||
? type
|
||||
: formatType(allDefs, type, imports)
|
||||
);
|
||||
const nameStr = mapName(name);
|
||||
|
||||
setImports(allDefs, imports, [...similarTypes.filter((type): boolean => !type.startsWith('(')), typeStr]);
|
||||
|
||||
return `${mapName(name)}: ${similarTypes.join(' | ')}`;
|
||||
return [`${nameStr}: ${similarTypes.join(' | ')}`, nameStr, typeStr];
|
||||
});
|
||||
|
||||
return indent(6)(`${stringCamelCase(name.toString())}: AugmentedSubmittable<ApiType, (${params.join(', ')}) => SubmittableExtrinsic<ApiType> & CallFunction>;`);
|
||||
return createDocComments(documentation).map((d): string => indent(6)(d)).join('\n') +
|
||||
indent(6)(`${stringCamelCase(name.toString())}: AugmentedExtrinsic<(${params.map(([full]): string => full).join(', ')}) => SubmittableExtrinsic<ApiType>>;`);
|
||||
}))
|
||||
.concat([indent(4)('};')]);
|
||||
}
|
||||
|
||||
@@ -62,9 +62,11 @@ export function getSimilarTypes (definitions: object, registry: Registry, type:
|
||||
setImports(definitions, imports, ['IExtrinsic']);
|
||||
return ['IExtrinsic'];
|
||||
} else if (type === 'StorageKey') {
|
||||
// FIXME This is a hack, it's hard to correctly type StorageKeys in the
|
||||
// current state
|
||||
return ['StorageKey', 'any'];
|
||||
return ['StorageKey', 'string', 'Uint8Array', 'any'];
|
||||
} else if (type === 'Address') {
|
||||
return ['Address', 'string', 'AccountId', 'AccountIndex', 'Uint8Array'];
|
||||
} else if (type === 'bool') {
|
||||
return ['bool', 'boolean', 'Uint8Array'];
|
||||
}
|
||||
|
||||
const instance = ClassOfUnsafe(registry, type);
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright 2017-2020 @polkadot/types 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 { Text } from '@polkadot/types';
|
||||
|
||||
type Arg = [Text | string, Text | string];
|
||||
|
||||
export function createDocComments (docs: Text[], args: Arg[] = []): string[] {
|
||||
const contents = [
|
||||
...(docs.length ? [` * ${docs.map((d): string => d.toString().trim()).join(' ')}`] : []),
|
||||
...args.map(([name, type]): string => ` * @param ${name} ${type}`)
|
||||
];
|
||||
|
||||
return contents.length
|
||||
? [
|
||||
'/**',
|
||||
...contents,
|
||||
' **/\n'
|
||||
]
|
||||
: [];
|
||||
}
|
||||
@@ -3,5 +3,6 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export * from './derived';
|
||||
export * from './docs';
|
||||
export * from './formatting';
|
||||
export * from './imports';
|
||||
|
||||
Reference in New Issue
Block a user