From f3000b150b8122ddb8a171d2fdd92c9a4fcab2de Mon Sep 17 00:00:00 2001 From: tarikgul Date: Wed, 3 May 2023 20:36:31 -0400 Subject: [PATCH 1/4] Set lint:fix command --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index f4c9db8a..ec1b7faf 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "version": "yarn install && git stage yarn.lock", "build": "lerna run build", "build:workspace": "substrate-exec-rimraf $INIT_CWD/lib/ && cd $INIT_CWD && tsc -p tsconfig.build.json", - "lint": "substrate-dev-run-lint --fix", + "lint": "substrate-dev-run-lint", + "lint:fix": "substrate-dev-run-lint --fix", "lint:ci": "substrate-dev-run-lint", "test": "substrate-exec-jest --detectOpenHandles", "test:watch": "substrate-exec-jest --watch", From 377447838004086425e6c81a9607a63adfbb9653 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Wed, 3 May 2023 20:36:59 -0400 Subject: [PATCH 2/4] Add additionalTypes, and typesBundle fields for getRegistryBase --- .../src/core/metadata/getRegistryBase.spec.ts | 43 +++++++++++++++++++ .../src/core/metadata/getRegistryBase.ts | 22 +++++++++- packages/txwrapper-core/src/types/registry.ts | 9 ++++ .../src/registries/mockGetRegistry.ts | 16 ++++++- packages/txwrapper-polkadot/src/index.ts | 4 ++ packages/txwrapper-registry/src/index.ts | 4 ++ 6 files changed, 96 insertions(+), 2 deletions(-) diff --git a/packages/txwrapper-core/src/core/metadata/getRegistryBase.spec.ts b/packages/txwrapper-core/src/core/metadata/getRegistryBase.spec.ts index 98688f69..766d547d 100644 --- a/packages/txwrapper-core/src/core/metadata/getRegistryBase.spec.ts +++ b/packages/txwrapper-core/src/core/metadata/getRegistryBase.spec.ts @@ -30,4 +30,47 @@ describe('getRegistryBase', () => { Buffer.from(callsOnlyRegistry.metadata.toHex(), 'utf-8').byteLength ); }); + + it('Should accept a `typesBundle` and properly set the known types', () => { + const registry = new TypeRegistry(); + const typesBundle = { + spec: { + polkadot: { + types: [ + { + minmax: [0, null], + types: { + TestingType: 'u32', + }, + }, + ], + }, + }, + }; + + const testRegistry = getRegistryBase({ + chainProperties: knownChainProperties['polkadot'], + specTypes: getSpecTypes(registry, 'Polkadot', 'polkadot', 9122), + metadataRpc: POLKADOT_9122_TEST_OPTIONS.metadataRpc, + typesBundle, + }); + expect( + testRegistry.knownTypes.typesBundle?.spec?.['polkadot'].types?.[0].types + ).toEqual({ + TestingType: 'u32', + }); + }); + + it('Should inject types when `additionalTypes` is available', () => { + const registry = new TypeRegistry(); + const testRegistry = getRegistryBase({ + chainProperties: knownChainProperties['polkadot'], + specTypes: getSpecTypes(registry, 'Polkadot', 'polkadot', 9122), + metadataRpc: POLKADOT_9122_TEST_OPTIONS.metadataRpc, + additionalTypes: { TestingType: 'u32' }, + }); + + const testType = testRegistry.createType('TestingType', 10); + expect(testType.toString()).toEqual('10'); + }); }); diff --git a/packages/txwrapper-core/src/core/metadata/getRegistryBase.ts b/packages/txwrapper-core/src/core/metadata/getRegistryBase.ts index 9149ab47..0f02d264 100644 --- a/packages/txwrapper-core/src/core/metadata/getRegistryBase.ts +++ b/packages/txwrapper-core/src/core/metadata/getRegistryBase.ts @@ -1,6 +1,10 @@ import { TypeRegistry } from '@polkadot/types'; import { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types'; -import { AnyJson, RegistryTypes } from '@polkadot/types/types'; +import { + AnyJson, + OverrideBundleType, + RegistryTypes, +} from '@polkadot/types/types'; import { ChainProperties } from '../../types'; import { createMetadata } from './createMetadata'; @@ -30,6 +34,14 @@ export interface GetRegistryBaseArgs { * User extensions used to inject into the type registry */ userExtensions?: ExtDef; + /** + * OverrideTypesBundle to set to registry + */ + typesBundle?: OverrideBundleType; + /** + * Additional types to register in the registry. + */ + additionalTypes?: RegistryTypes; } /** @@ -42,6 +54,8 @@ export function getRegistryBase({ asCallsOnlyArg, signedExtensions, userExtensions, + typesBundle, + additionalTypes, }: GetRegistryBaseArgs): TypeRegistry { const registry = new TypeRegistry(); @@ -53,8 +67,14 @@ export function getRegistryBase({ registry.register(specTypes); + if (additionalTypes) { + registry.register(additionalTypes); + } + registry.setMetadata(generatedMetadata, signedExtensions, userExtensions); + registry.setKnownTypes({ typesBundle }); + // Register the chain properties for this registry registry.setChainProperties( registry.createType('ChainProperties', chainProperties) diff --git a/packages/txwrapper-core/src/types/registry.ts b/packages/txwrapper-core/src/types/registry.ts index d94f8803..27903364 100644 --- a/packages/txwrapper-core/src/types/registry.ts +++ b/packages/txwrapper-core/src/types/registry.ts @@ -1,4 +1,5 @@ import { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types'; +import { OverrideBundleType, RegistryTypes } from '@polkadot/types/types'; import { ChainProperties } from './codec'; @@ -38,4 +39,12 @@ export interface GetRegistryOptsCore { * User extensions used to inject into the type registry */ userExtensions?: ExtDef; + /** + * OverrideTypesBundle to set to registry + */ + typesBundle?: OverrideBundleType; + /** + * Additional types to register in the registry. + */ + additionalTypes?: RegistryTypes; } diff --git a/packages/txwrapper-dev/src/registries/mockGetRegistry.ts b/packages/txwrapper-dev/src/registries/mockGetRegistry.ts index c1c92fe9..83144b4e 100644 --- a/packages/txwrapper-dev/src/registries/mockGetRegistry.ts +++ b/packages/txwrapper-dev/src/registries/mockGetRegistry.ts @@ -1,6 +1,10 @@ import { GenericChainProperties, TypeRegistry } from '@polkadot/types'; import { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types'; -import { AnyJson, RegistryTypes } from '@polkadot/types/types'; +import { + AnyJson, + OverrideBundleType, + RegistryTypes, +} from '@polkadot/types/types'; import { mockCreateMetadata } from '../metadata'; import { ChainProperties } from '../mock-types'; @@ -12,6 +16,8 @@ export interface MockGetRegistryBaseArgs { asCallsOnlyArg?: boolean; signedExtensions?: string[]; userExtensions?: ExtDef; + typesBundle?: OverrideBundleType; + additionalTypes?: RegistryTypes; } export function mockGetRegistryBase({ @@ -21,6 +27,8 @@ export function mockGetRegistryBase({ asCallsOnlyArg, signedExtensions, userExtensions, + typesBundle, + additionalTypes, }: MockGetRegistryBaseArgs): TypeRegistry { const registry = new TypeRegistry(); @@ -32,8 +40,14 @@ export function mockGetRegistryBase({ registry.register(specTypes); + if (additionalTypes) { + registry.register(additionalTypes); + } + registry.setMetadata(generatedMetadata, signedExtensions, userExtensions); + registry.setKnownTypes({ typesBundle }); + // Register the chain properties for this registry registry.setChainProperties( registry.createType( diff --git a/packages/txwrapper-polkadot/src/index.ts b/packages/txwrapper-polkadot/src/index.ts index 063d40ab..8dac1a41 100644 --- a/packages/txwrapper-polkadot/src/index.ts +++ b/packages/txwrapper-polkadot/src/index.ts @@ -87,6 +87,8 @@ export function getRegistry({ asCallsOnlyArg, signedExtensions, userExtensions, + typesBundle, + additionalTypes, }: GetRegistryOpts): TypeRegistry { // The default type registry has polkadot types const registry = new TypeRegistry(); @@ -107,5 +109,7 @@ export function getRegistry({ asCallsOnlyArg, signedExtensions, userExtensions, + typesBundle, + additionalTypes, }); } diff --git a/packages/txwrapper-registry/src/index.ts b/packages/txwrapper-registry/src/index.ts index e4f51c67..81e681d4 100644 --- a/packages/txwrapper-registry/src/index.ts +++ b/packages/txwrapper-registry/src/index.ts @@ -97,6 +97,8 @@ export function getRegistry({ metadataRpc, properties, asCallsOnlyArg, + typesBundle, + additionalTypes, }: GetRegistryOpts): TypeRegistry { const registry = createRegistry(); @@ -106,5 +108,7 @@ export function getRegistry({ specTypes: getSpecTypes(registry, chainName, specName, specVersion), metadataRpc, asCallsOnlyArg, + typesBundle, + additionalTypes, }); } From de802ee1e1df67a141707ba6941eadfd919019aa Mon Sep 17 00:00:00 2001 From: tarikgul Date: Wed, 3 May 2023 20:44:19 -0400 Subject: [PATCH 3/4] ensure env typesBundle doesnt interfere with passed in types bundle --- packages/txwrapper-registry/src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/txwrapper-registry/src/index.ts b/packages/txwrapper-registry/src/index.ts index 81e681d4..79e95ba5 100644 --- a/packages/txwrapper-registry/src/index.ts +++ b/packages/txwrapper-registry/src/index.ts @@ -67,7 +67,7 @@ function parseTypesBundle( return parsedJson; } -const typesBundle: OverrideBundleType | undefined = parseTypesBundle( +const envTypesBundle: OverrideBundleType | undefined = parseTypesBundle( process.env.TX_TYPES_BUNDLE ); @@ -75,10 +75,10 @@ const typesBundle: OverrideBundleType | undefined = parseTypesBundle( * Create a registry with `knownTypes` via env variables. * ie: STX_TYPES_BUNDLE; STX_TYPES_CHAIN */ -export function createRegistry(): TypeRegistry { +export function createRegistry(typesBundle?: OverrideBundleType): TypeRegistry { const registry = new TypeRegistry(); registry.setKnownTypes({ - typesBundle: typesBundle, + typesBundle: typesBundle || envTypesBundle, }); return registry; From 8446c8d5ff0bdef371cc30514ae4a43e38aff6eb Mon Sep 17 00:00:00 2001 From: tarikgul Date: Thu, 4 May 2023 21:26:34 -0400 Subject: [PATCH 4/4] fix merge issues --- .../txwrapper-core/src/core/metadata/getRegistryBase.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/txwrapper-core/src/core/metadata/getRegistryBase.spec.ts b/packages/txwrapper-core/src/core/metadata/getRegistryBase.spec.ts index 4d595411..77e506d4 100644 --- a/packages/txwrapper-core/src/core/metadata/getRegistryBase.spec.ts +++ b/packages/txwrapper-core/src/core/metadata/getRegistryBase.spec.ts @@ -51,7 +51,7 @@ describe('getRegistryBase', () => { const testRegistry = getRegistryBase({ chainProperties: knownChainProperties['polkadot'], specTypes: getSpecTypes(registry, 'Polkadot', 'polkadot', 9122), - metadataRpc: POLKADOT_9122_TEST_OPTIONS.metadataRpc, + metadataRpc: KUSAMA_TEST_OPTIONS.metadataRpc, typesBundle, }); expect( @@ -66,7 +66,7 @@ describe('getRegistryBase', () => { const testRegistry = getRegistryBase({ chainProperties: knownChainProperties['polkadot'], specTypes: getSpecTypes(registry, 'Polkadot', 'polkadot', 9122), - metadataRpc: POLKADOT_9122_TEST_OPTIONS.metadataRpc, + metadataRpc: KUSAMA_TEST_OPTIONS.metadataRpc, additionalTypes: { TestingType: 'u32' }, });