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

fix: typegen generic configurables #2859

Merged
merged 7 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .changeset/strange-moose-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-typegen": patch
---

fix: typegen generic configurables
9 changes: 1 addition & 8 deletions packages/abi-typegen/src/abi/Abi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
} from '../../test/fixtures/forc-projects/index';
import { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum';
import type { JsonAbiType } from '../types/interfaces/JsonAbi';
import * as parseConfigurablesMod from '../utils/parseConfigurables';
import * as parseFunctionsMod from '../utils/parseFunctions';
import * as parseTypesMod from '../utils/parseTypes';

Expand All @@ -29,14 +28,9 @@ describe('Abi.ts', () => {
.spyOn(parseFunctionsMod, 'parseFunctions')
.mockImplementation(() => []);

const parseConfigurables = vi
.spyOn(parseConfigurablesMod, 'parseConfigurables')
.mockImplementation(() => []);

return {
parseTypes,
parseFunctions,
parseConfigurables,
};
}

Expand Down Expand Up @@ -78,13 +72,12 @@ describe('Abi.ts', () => {
test('should create a new abi instance and parse root nodes', () => {
const {
abi,
mocks: { parseTypes, parseFunctions, parseConfigurables },
mocks: { parseTypes, parseFunctions },
} = getMockedAbi();

expect(abi).toBeTruthy();
expect(parseTypes).toHaveBeenCalledTimes(1);
expect(parseFunctions).toHaveBeenCalledTimes(1);
expect(parseConfigurables).toHaveBeenCalledTimes(1);
});

test('should compute array of custom types in use', () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/abi-typegen/src/abi/Abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import type { IConfigurable } from '../types/interfaces/IConfigurable';
import type { IFunction } from '../types/interfaces/IFunction';
import type { IType } from '../types/interfaces/IType';
import type { JsonAbi } from '../types/interfaces/JsonAbi';
import { parseConfigurables } from '../utils/parseConfigurables';
import { parseFunctions } from '../utils/parseFunctions';
import { parseTypes } from '../utils/parseTypes';

import { Configurable } from './configurable/Configurable';

/*
Manages many instances of Types and Functions
*/
Expand Down Expand Up @@ -88,7 +89,9 @@ export class Abi {

const types = parseTypes({ rawAbiTypes });
const functions = parseFunctions({ rawAbiFunctions, types });
const configurables = parseConfigurables({ rawAbiConfigurables, types });
const configurables = rawAbiConfigurables.map(
(rawAbiConfigurable) => new Configurable({ types, rawAbiConfigurable })
);

return {
types,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ describe('Configurable.ts', () => {

expect(findType).toHaveBeenCalledTimes(1);
expect(configurable.name).toEqual('FEE');
expect(configurable.type).toEqual(type);
expect(configurable.rawAbiConfigurable).toEqual(rawAbiConfigurable);
expect(configurable.inputLabel).toEqual('mockType');
});
});
19 changes: 12 additions & 7 deletions packages/abi-typegen/src/abi/configurable/Configurable.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import type { IConfigurable } from '../../types/interfaces/IConfigurable';
import type { IType } from '../../types/interfaces/IType';
import type { JsonAbiConfigurable } from '../../types/interfaces/JsonAbi';
import { findType } from '../../utils/findType';
import { resolveInputLabel } from '../../utils/getTypeDeclaration';

export class Configurable implements IConfigurable {
public name: string;
public type: IType;
public rawAbiConfigurable: JsonAbiConfigurable;
public inputLabel: string;

constructor(params: { types: IType[]; rawAbiConfigurable: JsonAbiConfigurable }) {
const { types, rawAbiConfigurable } = params;
const {
types,
rawAbiConfigurable: {
name,
configurableType: { type, typeArguments },
},
} = params;

this.name = rawAbiConfigurable.name;
this.rawAbiConfigurable = rawAbiConfigurable;
this.type = findType({ types, typeId: rawAbiConfigurable.configurableType.type });
this.name = name;

this.inputLabel = resolveInputLabel(types, type, typeArguments);
}
}
18 changes: 2 additions & 16 deletions packages/abi-typegen/src/abi/functions/Function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { IFunction, JsonAbiFunction, IFunctionAttributes } from '../../inde
import { TargetEnum } from '../../types/enums/TargetEnum';
import type { IType } from '../../types/interfaces/IType';
import { findType } from '../../utils/findType';
import { resolveInputLabel } from '../../utils/getTypeDeclaration';
import { parseTypeArguments } from '../../utils/parseTypeArguments';
import { EmptyType } from '../types/EmptyType';

Expand Down Expand Up @@ -35,22 +36,7 @@ export class Function implements IFunction {
.map((input) => {
const { name, type: typeId, typeArguments } = input;

const type = findType({ types, typeId });

let typeDecl: string;

if (typeArguments) {
// recursively process child `typeArguments`
typeDecl = parseTypeArguments({
types,
target: TargetEnum.INPUT,
parentTypeId: typeId,
typeArguments,
});
} else {
// or just collect type declaration
typeDecl = type.attributes.inputLabel;
}
const typeDecl = resolveInputLabel(types, typeId, typeArguments);

// assemble it in `[key: string]: <Type>` fashion
if (shouldPrefixParams) {
Expand Down
6 changes: 3 additions & 3 deletions packages/abi-typegen/src/templates/contract/dts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ export type {{structName}}Output{{typeAnnotations}} = { {{outputValues}} };
{{/if}}
{{/each}}

{{#if formattedConfigurables}}
{{#if configurables}}
export type {{capitalizedName}}Configurables = Partial<{
{{#each formattedConfigurables}}
{{configurableName}}: {{configurableType}};
{{#each configurables}}
{{name}}: {{inputLabel}};
{{/each}}
}>;
{{/if}}
Expand Down
4 changes: 1 addition & 3 deletions packages/abi-typegen/src/templates/contract/dts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Abi } from '../../abi/Abi';
import { renderHbsTemplate } from '../renderHbsTemplate';
import { formatConfigurables } from '../utils/formatConfigurables';
import { formatEnums } from '../utils/formatEnums';
import { formatImports } from '../utils/formatImports';
import { formatStructs } from '../utils/formatStructs';
Expand Down Expand Up @@ -39,7 +38,6 @@ export function renderDtsTemplate(params: { abi: Abi }) {
'InvokeFunction',
],
});
const { formattedConfigurables } = formatConfigurables({ configurables });

/*
And finally render template
Expand All @@ -56,7 +54,7 @@ export function renderDtsTemplate(params: { abi: Abi }) {
structs,
enums,
imports,
formattedConfigurables,
configurables,
},
});

Expand Down
4 changes: 2 additions & 2 deletions packages/abi-typegen/src/templates/predicate/factory.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export type {{structName}}Output{{typeAnnotations}} = { {{outputValues}} };
{{/each}}

export type {{capitalizedName}}Configurables = Partial<{
{{#each formattedConfigurables}}
{{configurableName}}: {{configurableType}};
{{#each configurables}}
{{name}}: {{inputLabel}};
{{/each}}
}>;

Expand Down
4 changes: 1 addition & 3 deletions packages/abi-typegen/src/templates/predicate/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ErrorCode, FuelError } from '@fuel-ts/errors';

import type { Abi } from '../../abi/Abi';
import { renderHbsTemplate } from '../renderHbsTemplate';
import { formatConfigurables } from '../utils/formatConfigurables';
import { formatEnums } from '../utils/formatEnums';
import { formatImports } from '../utils/formatImports';
import { formatStructs } from '../utils/formatStructs';
Expand Down Expand Up @@ -35,7 +34,6 @@ export function renderFactoryTemplate(params: { abi: Abi }) {
types,
baseMembers: ['Predicate', 'Provider', 'InputValue'],
});
const { formattedConfigurables } = formatConfigurables({ configurables });

const { prefixedInputs: inputs, output } = func.attributes;

Expand All @@ -50,7 +48,7 @@ export function renderFactoryTemplate(params: { abi: Abi }) {
hexlifiedBinString,
capitalizedName,
imports,
formattedConfigurables,
configurables,
commonTypesInUse: commonTypesInUse.join(', '),
},
});
Expand Down
6 changes: 3 additions & 3 deletions packages/abi-typegen/src/templates/script/factory.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export type {{structName}}Output{{typeAnnotations}} = { {{outputValues}} };
type {{capitalizedName}}Inputs = [{{inputs}}];
type {{capitalizedName}}Output = {{output}};

{{#if formattedConfigurables}}
{{#if configurables}}
export type {{capitalizedName}}Configurables = Partial<{
{{#each formattedConfigurables}}
{{configurableName}}: {{configurableType}};
{{#each configurables}}
{{name}}: {{inputLabel}};
{{/each}}
}>;
{{/if}}
Expand Down
4 changes: 1 addition & 3 deletions packages/abi-typegen/src/templates/script/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ErrorCode, FuelError } from '@fuel-ts/errors';

import type { Abi } from '../../abi/Abi';
import { renderHbsTemplate } from '../renderHbsTemplate';
import { formatConfigurables } from '../utils/formatConfigurables';
import { formatEnums } from '../utils/formatEnums';
import { formatImports } from '../utils/formatImports';
import { formatStructs } from '../utils/formatStructs';
Expand Down Expand Up @@ -32,7 +31,6 @@ export function renderFactoryTemplate(params: { abi: Abi }) {
const { enums } = formatEnums({ types });
const { structs } = formatStructs({ types });
const { imports } = formatImports({ types, baseMembers: ['Script', 'Account'] });
const { formattedConfigurables } = formatConfigurables({ configurables });

const { prefixedInputs: inputs, output } = func.attributes;

Expand All @@ -47,7 +45,7 @@ export function renderFactoryTemplate(params: { abi: Abi }) {
hexlifiedBinString,
capitalizedName,
imports,
formattedConfigurables,
configurables,
commonTypesInUse: commonTypesInUse.join(', '),
},
});
Expand Down

This file was deleted.

21 changes: 0 additions & 21 deletions packages/abi-typegen/src/templates/utils/formatConfigurables.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type { IType } from './IType';
import type { JsonAbiConfigurable } from './JsonAbi';

export interface IConfigurable {
name: string;
type: IType;
rawAbiConfigurable: JsonAbiConfigurable;
inputLabel: string;
}
31 changes: 31 additions & 0 deletions packages/abi-typegen/src/utils/getTypeDeclaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { TargetEnum } from '../types/enums/TargetEnum';
import type { IType } from '../types/interfaces/IType';
import type { JsonAbiArgument } from '../types/interfaces/JsonAbi';

import { findType } from './findType';
import { parseTypeArguments } from './parseTypeArguments';

export function resolveInputLabel(
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved
types: IType[],
typeId: number,
typeArguments: JsonAbiArgument['typeArguments']
) {
const type = findType({ types, typeId });

let typeDecl: string;

if (typeArguments) {
// recursively process child `typeArguments`
typeDecl = parseTypeArguments({
types,
target: TargetEnum.INPUT,
parentTypeId: typeId,
typeArguments,
});
} else {
// or just collect type declaration
typeDecl = type.attributes.inputLabel;
}

return typeDecl;
}
Loading
Loading