Skip to content

Commit

Permalink
fix: Generate custom type definitions (#4224)
Browse files Browse the repository at this point in the history
* fix: Generate custom type definitions

* Generate separate lookup types

* fix: Custom imports

* fix linting errors

Co-authored-by: Github Actions <action@github.com>
  • Loading branch information
daniel-savu and actions-user authored Nov 19, 2021
1 parent 194f6ae commit e5a67d8
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 40 deletions.
21 changes: 17 additions & 4 deletions packages/typegen/src/fromChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import path from 'path';
import yargs from 'yargs';

import { Definitions } from '@polkadot/types/types';
import { formatNumber } from '@polkadot/util';
import { WebSocket } from '@polkadot/x-ws';

Expand All @@ -18,12 +19,24 @@ function generate (metaHex: string, pkg: string | undefined, output: string, isS
? { [pkg]: require(path.join(process.cwd(), output, 'definitions')) as Record<string, any> }
: {};

generateDefaultConsts(path.join(process.cwd(), output, 'augment-api-consts.ts'), metaHex, extraTypes, isStrict);
let customLookupDefinitions;

try {
customLookupDefinitions = {
rpc: {},
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
types: require(path.join(process.cwd(), output, 'lookup.ts')).default
} as Definitions;
} catch (error) {
console.log('No custom definitions found.');
}

generateDefaultConsts(path.join(process.cwd(), output, 'augment-api-consts.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions);
generateDefaultErrors(path.join(process.cwd(), output, 'augment-api-errors.ts'), metaHex, extraTypes, isStrict);
generateDefaultEvents(path.join(process.cwd(), output, 'augment-api-events.ts'), metaHex, extraTypes, isStrict);
generateDefaultQuery(path.join(process.cwd(), output, 'augment-api-query.ts'), metaHex, extraTypes, isStrict);
generateDefaultEvents(path.join(process.cwd(), output, 'augment-api-events.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions);
generateDefaultQuery(path.join(process.cwd(), output, 'augment-api-query.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions);
generateDefaultRpc(path.join(process.cwd(), output, 'augment-api-rpc.ts'), extraTypes);
generateDefaultTx(path.join(process.cwd(), output, 'augment-api-tx.ts'), metaHex, extraTypes, isStrict);
generateDefaultTx(path.join(process.cwd(), output, 'augment-api-tx.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions);

writeFile(path.join(process.cwd(), output, 'augment-api.ts'), (): string =>
[
Expand Down
16 changes: 14 additions & 2 deletions packages/typegen/src/fromDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import * as substrateDefs from '@polkadot/types/interfaces/definitions';

import { generateInterfaceTypes } from './generate/interfaceRegistry';
import { generateTsDef } from './generate/tsDef';
import { generateDefaultLookup } from './generate';

type ArgV = { input: string; package: string };
type ArgV = { input: string; package: string; endpoint?: string; };

export function main (): void {
const { input, package: pkg } = yargs.strict().options({
const { endpoint, input, package: pkg } = yargs.strict().options({
endpoint: {
description: 'The endpoint to connect to (e.g. wss://kusama-rpc.polkadot.io) or relative path to a file containing the JSON output of an RPC state_getMetadata call',
type: 'string'
},
input: {
description: 'The directory to use for the user definitions',
required: true,
Expand All @@ -25,6 +30,13 @@ export function main (): void {
}
}).argv as ArgV;

if (endpoint) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const metaHex = (require(path.join(process.cwd(), endpoint)) as Record<string, string>).result;

generateDefaultLookup(path.join(process.cwd(), input), metaHex);
}

// eslint-disable-next-line @typescript-eslint/no-var-requires
const userDefs = require(path.join(process.cwd(), input, 'definitions.ts')) as Record<string, any>;
const userKeys = Object.keys(userDefs);
Expand Down
14 changes: 10 additions & 4 deletions packages/typegen/src/generate/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Handlebars from 'handlebars';

import lookupDefinitions from '@polkadot/types/augment/lookup/definitions';
import * as defaultDefs from '@polkadot/types/interfaces/definitions';
import { Definitions } from '@polkadot/types/types';
import { stringCamelCase } from '@polkadot/util';

import { compareName, createImports, formatType, initMeta, readTemplate, setImports, writeFile } from '../util';
Expand All @@ -16,10 +17,15 @@ const template = readTemplate('consts');
const generateForMetaTemplate = Handlebars.compile(template);

/** @internal */
function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean): void {
function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void {
writeFile(dest, (): string => {
const allTypes = {
'@polkadot/types/augment': { lookup: lookupDefinitions },
'@polkadot/types/augment': {
lookup: {
...lookupDefinitions,
...customLookupDefinitions
}
},
'@polkadot/types/interfaces': defaultDefs,
...extraTypes
};
Expand Down Expand Up @@ -79,8 +85,8 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes,

// Call `generateForMeta()` with current static metadata
/** @internal */
export function generateDefaultConsts (dest = 'packages/api/src/augment/consts.ts', data?: string, extraTypes: ExtraTypes = {}, isStrict = false): void {
export function generateDefaultConsts (dest = 'packages/api/src/augment/consts.ts', data?: string, extraTypes: ExtraTypes = {}, isStrict = false, customLookupDefinitions?: Definitions): void {
const { metadata } = initMeta(data, extraTypes);

return generateForMeta(metadata, dest, extraTypes, isStrict);
return generateForMeta(metadata, dest, extraTypes, isStrict, customLookupDefinitions);
}
14 changes: 10 additions & 4 deletions packages/typegen/src/generate/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Handlebars from 'handlebars';

import lookupDefinitions from '@polkadot/types/augment/lookup/definitions';
import * as defaultDefs from '@polkadot/types/interfaces/definitions';
import { Definitions } from '@polkadot/types/types';
import { stringCamelCase } from '@polkadot/util';

import { compareName, createImports, formatType, initMeta, readTemplate, setImports, writeFile } from '../util';
Expand All @@ -16,10 +17,15 @@ const template = readTemplate('events');
const generateForMetaTemplate = Handlebars.compile(template);

/** @internal */
function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean): void {
function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void {
writeFile(dest, (): string => {
const allTypes = {
'@polkadot/types/augment': { lookup: lookupDefinitions },
'@polkadot/types/augment': {
lookup: {
...lookupDefinitions,
...customLookupDefinitions
}
},
'@polkadot/types/interfaces': defaultDefs,
...extraTypes
};
Expand Down Expand Up @@ -71,8 +77,8 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes,

// Call `generateForMeta()` with current static metadata
/** @internal */
export function generateDefaultEvents (dest = 'packages/api/src/augment/events.ts', data?: string, extraTypes: ExtraTypes = {}, isStrict = false): void {
export function generateDefaultEvents (dest = 'packages/api/src/augment/events.ts', data?: string, extraTypes: ExtraTypes = {}, isStrict = false, customLookupDefinitions?: Definitions): void {
const { metadata } = initMeta(data, extraTypes);

return generateForMeta(metadata, dest, extraTypes, isStrict);
return generateForMeta(metadata, dest, extraTypes, isStrict, customLookupDefinitions);
}
2 changes: 1 addition & 1 deletion packages/typegen/src/generate/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export function generateDefaultLookup (destDir = 'packages/types/src/augment/loo
generateLookup(
destDir,
staticData
? [[undefined, staticData]]
? [['lookup', staticData]]
: [
['substrate', staticSubstrate],
['polkadot', staticPolkadot],
Expand Down
15 changes: 10 additions & 5 deletions packages/typegen/src/generate/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import type { StorageEntryMetadataLatest } from '@polkadot/types/interfaces';
import type { Metadata, PortableRegistry } from '@polkadot/types/metadata';
import type { Registry } from '@polkadot/types/types';
import type { Definitions, Registry } from '@polkadot/types/types';
import type { ExtraTypes } from './types';

import Handlebars from 'handlebars';
Expand Down Expand Up @@ -66,10 +66,15 @@ const template = readTemplate('query');
const generateForMetaTemplate = Handlebars.compile(template);

/** @internal */
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean): void {
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void {
writeFile(dest, (): string => {
const allTypes: ExtraTypes = {
'@polkadot/types/augment': { lookup: lookupDefinitions },
'@polkadot/types/augment': {
lookup: {
...lookupDefinitions,
...customLookupDefinitions
}
},
'@polkadot/types/interfaces': defaultDefs,
...extraTypes
};
Expand Down Expand Up @@ -129,8 +134,8 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr

// Call `generateForMeta()` with current static metadata
/** @internal */
export function generateDefaultQuery (dest = 'packages/api/src/augment/query.ts', data?: string, extraTypes: ExtraTypes = {}, isStrict = false): void {
export function generateDefaultQuery (dest = 'packages/api/src/augment/query.ts', data?: string, extraTypes: ExtraTypes = {}, isStrict = false, customLookupDefinitions?: Definitions): void {
const { metadata, registry } = initMeta(data, extraTypes);

return generateForMeta(registry, metadata, dest, extraTypes, isStrict);
return generateForMeta(registry, metadata, dest, extraTypes, isStrict, customLookupDefinitions);
}
15 changes: 10 additions & 5 deletions packages/typegen/src/generate/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import type { Metadata } from '@polkadot/types/metadata/Metadata';
import type { Registry } from '@polkadot/types/types';
import type { Definitions, Registry } from '@polkadot/types/types';
import type { ExtraTypes } from './types';

import Handlebars from 'handlebars';
Expand All @@ -29,10 +29,15 @@ const template = readTemplate('tx');
const generateForMetaTemplate = Handlebars.compile(template);

/** @internal */
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean): void {
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void {
writeFile(dest, (): string => {
const allTypes: ExtraTypes = {
'@polkadot/types/augment': { lookup: lookupDefinitions },
'@polkadot/types/augment': {
lookup: {
...lookupDefinitions,
...customLookupDefinitions
}
},
'@polkadot/types/interfaces': defaultDefs,
...extraTypes
};
Expand Down Expand Up @@ -114,8 +119,8 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr

// Call `generateForMeta()` with current static metadata
/** @internal */
export function generateDefaultTx (dest = 'packages/api/src/augment/tx.ts', data?: string, extraTypes: ExtraTypes = {}, isStrict = false): void {
export function generateDefaultTx (dest = 'packages/api/src/augment/tx.ts', data?: string, extraTypes: ExtraTypes = {}, isStrict = false, customLookupDefinitions?: Definitions): void {
const { metadata, registry } = initMeta(data, extraTypes);

return generateForMeta(registry, metadata, dest, extraTypes, isStrict);
return generateForMeta(registry, metadata, dest, extraTypes, isStrict, customLookupDefinitions);
}
4 changes: 1 addition & 3 deletions packages/typegen/src/templates/lookup/defs-named.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

/* eslint-disable sort-keys */

import type { DefinitionsTypes } from '../../types';

export default {
{{#each defs}}
{{> docs}}
{{#each defs}}
{{{this}}}
{{/each}}
{{/each}}
} as DefinitionsTypes;
};
{{> footer }}
4 changes: 1 addition & 3 deletions packages/typegen/src/templates/lookup/defs.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

/* eslint-disable sort-keys */

import type { Definitions } from '../../types';

export default {
rpc: {},
types: {
Expand All @@ -14,5 +12,5 @@ export default {
{{/each}}
{{/each}}
}
} as Definitions;
};
{{> footer }}
4 changes: 1 addition & 3 deletions packages/types/src/augment/lookup/kusama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

/* eslint-disable sort-keys */

import type { DefinitionsTypes } from '../../types';

export default {
/**
* Lookup74: kusama_runtime::ProxyType
Expand Down Expand Up @@ -929,4 +927,4 @@ export default {
* Lookup711: kusama_runtime::Runtime
**/
KusamaRuntimeRuntime: 'Null'
} as DefinitionsTypes;
};
4 changes: 1 addition & 3 deletions packages/types/src/augment/lookup/polkadot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

/* eslint-disable sort-keys */

import type { DefinitionsTypes } from '../../types';

export default {
/**
* Lookup65: polkadot_runtime_common::claims::pallet::Event<T>
Expand Down Expand Up @@ -1260,4 +1258,4 @@ export default {
* Lookup592: polkadot_runtime::Runtime
**/
PolkadotRuntimeRuntime: 'Null'
} as DefinitionsTypes;
};
4 changes: 1 addition & 3 deletions packages/types/src/augment/lookup/substrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

/* eslint-disable sort-keys */

import type { DefinitionsTypes } from '../../types';

export default {
/**
* Lookup3: frame_system::AccountInfo<Index, pallet_balances::AccountData<Balance>>
Expand Down Expand Up @@ -3345,4 +3343,4 @@ export default {
* Lookup527: node_runtime::Runtime
**/
NodeRuntimeRuntime: 'Null'
} as DefinitionsTypes;
};

0 comments on commit e5a67d8

Please sign in to comment.