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: support configurables in typegen #1031

Merged
merged 25 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
04cf5a6
chore: fix spellings in typegen
May 30, 2023
72e9fde
feat: support configurables in predicate typegen
May 30, 2023
28baa3c
chore: changeset
May 30, 2023
34d86cb
chore: linting
May 30, 2023
8ca507d
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into db…
May 30, 2023
3ad3100
chore: fix package version bumped by mistake
May 30, 2023
1af24e2
test: fix typo in predicate factory configurable test
May 30, 2023
a403693
fix: fix get supported versions
May 30, 2023
54908d1
feat: support configurables in typegen for scripts and contracts
May 31, 2023
8b8e1df
test: add mising configurables from abi typegen test
May 31, 2023
c847494
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into db…
May 31, 2023
97bfec0
Revert "fix: fix get supported versions"
May 31, 2023
2c7a54f
Revert "chore: fix package version bumped by mistake"
May 31, 2023
6da372e
test: change make configurable test name
danielbate May 31, 2023
9523a01
Update packages/abi-typegen/src/templates/contract/dts.hbs
danielbate May 31, 2023
458dbc3
Update packages/abi-typegen/src/utils/parseConfigurables.test.ts
danielbate May 31, 2023
aaa1781
Merge branch 'master' into db/feat/support-configurables-in-typegen
danielbate May 31, 2023
ef1acf2
test: contract dts configurable test
May 31, 2023
668b990
Merge branch 'master' into db/feat/support-configurables-in-typegen
danielbate May 31, 2023
023b1e7
Merge branch 'master' into db/feat/support-configurables-in-typegen
danielbate May 31, 2023
7515d87
test: fix indent in configurables templates
Jun 1, 2023
79de9b5
Merge branch 'db/feat/support-configurables-in-typegen' of https://gi…
Jun 1, 2023
a800263
Merge branch 'master' into db/feat/support-configurables-in-typegen
danielbate Jun 1, 2023
6d247ac
Merge branch 'master' into db/feat/support-configurables-in-typegen
danielbate Jun 2, 2023
5d042f4
Merge branch 'master' into db/feat/support-configurables-in-typegen
danielbate Jun 5, 2023
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/many-chairs-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-typegen": patch
---

Support configurables in typegen
9 changes: 8 additions & 1 deletion packages/abi-typegen/src/abi/Abi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { safeExec } from '@fuel-ts/utils/test';
import { ForcProjectsEnum, getProjectResources } from '../../test/fixtures/forc-projects/index';
import { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum';
import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType';
import * as parseConfigurablesMod from '../utils/parseConfigurables';
import * as parseFunctionsMod from '../utils/parseFunctions';
import * as parseTypesMod from '../utils/parseTypes';

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

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

return {
parseTypes,
parseFunctions,
parseConfigurables,
};
}

Expand Down Expand Up @@ -66,12 +72,13 @@ describe('Abi.ts', () => {
test('should create a new abi instance and parse root nodes', () => {
const {
abi,
mocks: { parseTypes, parseFunctions },
mocks: { parseTypes, parseFunctions, parseConfigurables },
} = 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
14 changes: 12 additions & 2 deletions packages/abi-typegen/src/abi/Abi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { normalizeString } from '@fuel-ts/utils';

import type { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum';
import type { IConfigurable } from '../types/interfaces/IConfigurable';
import type { IFunction } from '../types/interfaces/IFunction';
import type { IRawAbi } from '../types/interfaces/IRawAbi';
import type { IType } from '../types/interfaces/IType';
import { parseConfigurables } from '../utils/parseConfigurables';
import { parseFunctions } from '../utils/parseFunctions';
import { parseTypes } from '../utils/parseTypes';

Expand All @@ -24,6 +26,7 @@ export class Abi {

public types: IType[];
public functions: IFunction[];
public configurables: IConfigurable[];

constructor(params: {
filepath: string;
Expand Down Expand Up @@ -53,23 +56,30 @@ export class Abi {
this.hexlifiedBinContents = hexlifiedBinContents;
this.outputDir = outputDir;

const { types, functions } = this.parse();
const { types, functions, configurables } = this.parse();

this.types = types;
this.functions = functions;
this.configurables = configurables;

this.computeCommonTypesInUse();
}

parse() {
const { types: rawAbiTypes, functions: rawAbiFunctions } = this.rawContents;
const {
types: rawAbiTypes,
functions: rawAbiFunctions,
configurables: rawAbiConfigurables,
} = this.rawContents;

const types = parseTypes({ rawAbiTypes });
const functions = parseFunctions({ rawAbiFunctions, types });
const configurables = parseConfigurables({ rawAbiConfigurables, types });

return {
types,
functions,
configurables,
};
}

Expand Down
52 changes: 52 additions & 0 deletions packages/abi-typegen/src/abi/configurable/Configurable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { getProjectResources, ForcProjectsEnum } from '../../../test/fixtures/forc-projects/index';
import type { IRawAbiTypeRoot } from '../../types/interfaces/IRawAbiType';
import type { IType } from '../../types/interfaces/IType';
import * as findTypeMod from '../../utils/findType';

import { Configurable } from './Configurable';

describe('Configurable.ts', () => {
function mockAllDeps() {
const rawAbiType: IRawAbiTypeRoot = {
typeId: 1,
type: 'mockType',
components: null,
typeParameters: null,
};

const type: IType = {
name: 'mockType',
attributes: {
inputLabel: 'mockType',
outputLabel: 'mockType',
},
rawAbiType,
requiredFuelsMembersImports: [],
parseComponentsAttributes: jest.fn(),
};

const findType = jest.spyOn(findTypeMod, 'findType').mockReturnValue(type);

return {
type,
findType,
};
}

it('should get configurable declaration with type', () => {
const { type, findType } = mockAllDeps();
const project = getProjectResources(ForcProjectsEnum.PREDICATE_WITH_CONFIGURABLE);

const { configurables } = project.abiContents;

const types: IType[] = [type];
const rawAbiConfigurable = configurables[0];

const configurable = new Configurable({ types, rawAbiConfigurable });

expect(findType).toHaveBeenCalledTimes(1);
expect(configurable.name).toEqual('FEE');
expect(configurable.type).toEqual(type);
expect(configurable.rawAbiConfigurable).toEqual(rawAbiConfigurable);
});
});
18 changes: 18 additions & 0 deletions packages/abi-typegen/src/abi/configurable/Configurable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { IConfigurable } from '../../types/interfaces/IConfigurable';
import type { IRawAbiConfigurable } from '../../types/interfaces/IRawAbiConfigurable';
import type { IType } from '../../types/interfaces/IType';
import { findType } from '../../utils/findType';

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

constructor(params: { types: IType[]; rawAbiConfigurable: IRawAbiConfigurable }) {
const { types, rawAbiConfigurable } = params;

this.name = rawAbiConfigurable.name;
this.rawAbiConfigurable = rawAbiConfigurable;
this.type = findType({ types, typeId: rawAbiConfigurable.configurableType.type });
}
}
2 changes: 2 additions & 0 deletions packages/abi-typegen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

export * from './AbiTypeGen';

export * from './types/interfaces/IConfigurable';
export * from './types/interfaces/IFile';
export * from './types/interfaces/IFunction';
export * from './types/interfaces/IRawAbi';
export * from './types/interfaces/IRawAbiConfigurable';
export * from './types/interfaces/IRawAbiFunction';
export * from './types/interfaces/IRawAbiType';
export * from './types/interfaces/IRawAbiLoggedTypes';
7 changes: 7 additions & 0 deletions packages/abi-typegen/src/templates/contract/dts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export type {{structName}}Output{{typeAnnotations}} = { {{outputValues}} };
{{/if}}
{{/each}}

{{#if formattedConfigurables}}
export type {{capitalizedName}}Configurables = {
{{#each formattedConfigurables}}
{{configurableName}}: {{configurableType}};
{{/each}}
};
{{/if}}

interface {{capitalizedName}}Interface extends Interface {
functions: {
Expand Down
22 changes: 22 additions & 0 deletions packages/abi-typegen/src/templates/contract/dts.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getProjectResources, ForcProjectsEnum } from '../../../test/fixtures/forc-projects/index';
import expectedDtsMinimalConfigurableTemplate from '../../../test/fixtures/templates/contract-with-configurable/dts.hbs';
import expectedDtsFullTemplate from '../../../test/fixtures/templates/contract/dts.hbs';
import { mockVersions } from '../../../test/utils/mockVersions';
import { Abi } from '../../abi/Abi';
Expand Down Expand Up @@ -30,6 +31,27 @@ describe('templates/dts', () => {
expect(rendered).toEqual(expectedDtsFullTemplate);
});

test('should render dts template with configurable', () => {
const { restore } = mockVersions();

const project = getProjectResources(ForcProjectsEnum.MINIMAL_WITH_CONFIGURABLE);

const rawContents = project.abiContents;

const abi = new Abi({
filepath: './my-contract-abi.json',
outputDir: 'stdout',
rawContents,
programType: ProgramTypeEnum.CONTRACT,
});

const rendered = renderDtsTemplate({ abi });

restore();

expect(rendered).toEqual(expectedDtsMinimalConfigurableTemplate);
});

test('should render dts template w/ custom common types', () => {
const project = getProjectResources(ForcProjectsEnum.VECTOR_SIMPLE);
const { abiContents: rawContents } = project;
Expand Down
5 changes: 4 additions & 1 deletion packages/abi-typegen/src/templates/contract/dts.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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';

import dtsTemplate from './dts.hbs';

export function renderDtsTemplate(params: { abi: Abi }) {
const { name: capitalizedName, types, functions, commonTypesInUse } = params.abi;
const { name: capitalizedName, types, functions, commonTypesInUse, configurables } = params.abi;

/*
First we format all attributes
Expand Down Expand Up @@ -38,6 +39,7 @@ export function renderDtsTemplate(params: { abi: Abi }) {
'InvokeFunction',
],
});
const { formattedConfigurables } = formatConfigurables({ configurables });

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

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

export type {{capitalizedName}}Configurables = {
{{#each formattedConfigurables}}
{{configurableName}}: {{configurableType}};
{{/each}}
};

type {{capitalizedName}}Inputs = [{{inputs}}];

const _abi = {{abiJsonString}}
Expand All @@ -51,11 +57,11 @@ export class {{capitalizedName}}__factory {
static readonly abi = _abi
static readonly bin = _bin;

static createInstance(provider?: Provider) {
static createInstance(provider?: Provider, configurables?: {{capitalizedName}}Configurables) {

const { abi, bin } = {{capitalizedName}}__factory

const predicate = new Predicate(bin, abi, provider);
const predicate = new Predicate(bin, abi, provider, configurables);

return predicate;

Expand Down
23 changes: 23 additions & 0 deletions packages/abi-typegen/src/templates/predicate/factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { safeExec } from '@fuel-ts/utils/test';

import { getProjectResources, ForcProjectsEnum } from '../../../test/fixtures/forc-projects/index';
import factoryWithConfigurablesTemplate from '../../../test/fixtures/templates/predicate-with-configurable/factory.hbs';
import factoryTemplate from '../../../test/fixtures/templates/predicate/factory.hbs';
import { mockVersions } from '../../../test/utils/mockVersions';
import { Abi } from '../../abi/Abi';
Expand Down Expand Up @@ -31,6 +32,28 @@ describe('factory.ts', () => {
expect(rendered).toEqual(factoryTemplate);
});

test('should render factory template with configurable', () => {
const { restore } = mockVersions();

const project = getProjectResources(ForcProjectsEnum.PREDICATE_WITH_CONFIGURABLE);

const rawContents = project.abiContents;

const abi = new Abi({
filepath: './my-predicate-abi.json',
hexlifiedBinContents: '0x000',
outputDir: 'stdout',
rawContents,
programType: ProgramTypeEnum.PREDICATE,
});

const rendered = renderFactoryTemplate({ abi });

restore();

expect(rendered).toEqual(factoryWithConfigurablesTemplate);
});

test('should throw for invalid Predicate ABI', async () => {
const { restore } = mockVersions();

Expand Down
5 changes: 4 additions & 1 deletion packages/abi-typegen/src/templates/predicate/factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 All @@ -9,7 +10,7 @@ import factoryTemplate from './factory.hbs';
export function renderFactoryTemplate(params: { abi: Abi }) {
const { abi } = params;

const { types } = abi;
const { types, configurables } = abi;

const {
rawContents,
Expand All @@ -28,6 +29,7 @@ export function renderFactoryTemplate(params: { abi: Abi }) {
const { enums } = formatEnums({ types });
const { structs } = formatStructs({ types });
const { imports } = formatImports({ types, baseMembers: ['Predicate', 'Provider'] });
const { formattedConfigurables } = formatConfigurables({ configurables });

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

Expand All @@ -42,6 +44,7 @@ export function renderFactoryTemplate(params: { abi: Abi }) {
hexlifiedBinString,
capitalizedName,
imports,
formattedConfigurables,
},
});

Expand Down
8 changes: 8 additions & 0 deletions packages/abi-typegen/src/templates/script/factory.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export type {{structName}}Output{{typeAnnotations}} = { {{outputValues}} };
type {{capitalizedName}}Inputs = [{{inputs}}];
type {{capitalizedName}}Output = {{output}};

{{#if formattedConfigurables}}
export type {{capitalizedName}}Configurables = {
{{#each formattedConfigurables}}
{{configurableName}}: {{configurableType}};
{{/each}}
};
{{/if}}

const _abi = {{abiJsonString}}

const _bin = '{{hexlifiedBinString}}'
Expand Down
Loading