diff --git a/packages/hydra-typegen/src/generators/helpers.spec.ts b/packages/hydra-typegen/src/generators/helpers.spec.ts index c9b33ae98..d52874b51 100644 --- a/packages/hydra-typegen/src/generators/helpers.spec.ts +++ b/packages/hydra-typegen/src/generators/helpers.spec.ts @@ -10,6 +10,13 @@ describe('helpers', () => { expect(c(prmsReturn())).to.equal(c(`[Type1, Type2 & Codec, Option]`)) }) + it('should render return type with tupes ', () => { + const prmsReturn = helper.paramsReturnType.bind({ + args: ['Type1', 'Vec<(Type1,Type2)>'], + }) + expect(c(prmsReturn())).to.equal(c(`[Type1, Vec<[Type1,Type2] & Codec>]`)) + }) + it('should render return type statement', () => { const prmsReturnStmt = helper.paramsReturnStmt.bind({ args: ['Type1', 'Type2 & Balance'], @@ -21,4 +28,14 @@ describe('helpers', () => { typeRegistry, 'Type2 & Balance', [this.ctx.params[1].value])]`) ) }) + + it('should handle tuple types', () => { + const prmsReturnStmt = helper.paramsReturnStmt.bind({ + args: ['Vec<(Type1,Type2,(Balance1,Balance2))>'], + }) + expect(c(prmsReturnStmt())).to.equal( + c(`return [createTypeUnsafe & Codec>( + typeRegistry, 'Vec<(Type1,Type2,(Balance1,Balance2))>', [this.ctx.params[0].value])]`) + ) + }) }) diff --git a/packages/hydra-typegen/src/generators/helpers.ts b/packages/hydra-typegen/src/generators/helpers.ts index a4aface17..6aa858039 100644 --- a/packages/hydra-typegen/src/generators/helpers.ts +++ b/packages/hydra-typegen/src/generators/helpers.ts @@ -47,7 +47,8 @@ export function renderNamedArgs( } export function renderTypedParams(argTypes: string[]): string { - const returnType = `[${argTypes.join(',')}]` + const returnType = `[${argTypes.map((a) => convertTuples(a)).join(',')}]` + console.log(`Return type: ${returnType}`) const returnObjects = argTypes.map((argType, index) => renderCreateTypeStmt(argType, eventParamValueGetter(index)) ) @@ -57,10 +58,28 @@ export function renderTypedParams(argTypes: string[]): string { } function renderCreateTypeStmt(argType: string, ctxValueGetter: string) { - return `createTypeUnsafe<${argType} & Codec>( + return `createTypeUnsafe<${convertTuples(argType)} & Codec>( typeRegistry, '${argType}', ${ctxValueGetter}) ` } +/** + * Converts tuple types (X, Y, Z) to [X, Y, Z] & Codec + * @param type + * @returns + */ +export function convertTuples(type: string): string { + let _type = type + + // recursively find typles and replace them + // eslint-disable-next-line no-useless-escape + while (_type.match(/\(([^\(].*)\)/g)) { + // eslint-disable-next-line no-useless-escape + _type = _type.replace(/\(([^\(].*)\)/, '[$1] & Codec') + } + + return _type +} + export const helper: Handlebars.HelperDeclareSpec = { imports() { const { imports } = (this as unknown) as { imports: ImportsDef } @@ -89,7 +108,7 @@ export const helper: Handlebars.HelperDeclareSpec = { paramsReturnType() { const { args } = (this as unknown) as { args: string[] } - return `[${args.join(',')}]` + return `[${args.map((a) => convertTuples(a)).join(',')}]` }, paramsReturnStmt() {