Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add additionalTypes, and typesBundle to getRegistryBase #294

Merged
merged 5 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: KUSAMA_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: KUSAMA_TEST_OPTIONS.metadataRpc,
additionalTypes: { TestingType: 'u32' },
});

const testType = testRegistry.createType('TestingType', 10);
expect(testType.toString()).toEqual('10');
});
});
22 changes: 21 additions & 1 deletion packages/txwrapper-core/src/core/metadata/getRegistryBase.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -42,6 +54,8 @@ export function getRegistryBase({
asCallsOnlyArg,
signedExtensions,
userExtensions,
typesBundle,
additionalTypes,
}: GetRegistryBaseArgs): TypeRegistry {
const registry = new TypeRegistry();

Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions packages/txwrapper-core/src/types/registry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types';
import { OverrideBundleType, RegistryTypes } from '@polkadot/types/types';

import { ChainProperties } from './codec';

Expand Down Expand Up @@ -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;
}
16 changes: 15 additions & 1 deletion packages/txwrapper-dev/src/registries/mockGetRegistry.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,6 +16,8 @@ export interface MockGetRegistryBaseArgs {
asCallsOnlyArg?: boolean;
signedExtensions?: string[];
userExtensions?: ExtDef;
typesBundle?: OverrideBundleType;
additionalTypes?: RegistryTypes;
}

export function mockGetRegistryBase({
Expand All @@ -21,6 +27,8 @@ export function mockGetRegistryBase({
asCallsOnlyArg,
signedExtensions,
userExtensions,
typesBundle,
additionalTypes,
}: MockGetRegistryBaseArgs): TypeRegistry {
const registry = new TypeRegistry();

Expand All @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions packages/txwrapper-polkadot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -107,5 +109,7 @@ export function getRegistry({
asCallsOnlyArg,
signedExtensions,
userExtensions,
typesBundle,
additionalTypes,
});
}
10 changes: 7 additions & 3 deletions packages/txwrapper-registry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ function parseTypesBundle(
return parsedJson;
}

const typesBundle: OverrideBundleType | undefined = parseTypesBundle(
const envTypesBundle: OverrideBundleType | undefined = parseTypesBundle(
process.env.TX_TYPES_BUNDLE
);

/**
* 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;
Expand All @@ -97,6 +97,8 @@ export function getRegistry({
metadataRpc,
properties,
asCallsOnlyArg,
typesBundle,
additionalTypes,
}: GetRegistryOpts): TypeRegistry {
const registry = createRegistry();

Expand All @@ -106,5 +108,7 @@ export function getRegistry({
specTypes: getSpecTypes(registry, chainName, specName, specVersion),
metadataRpc,
asCallsOnlyArg,
typesBundle,
additionalTypes,
});
}