diff --git a/.changeset/rare-snails-tan.md b/.changeset/rare-snails-tan.md new file mode 100644 index 00000000000..f5681bc5ff3 --- /dev/null +++ b/.changeset/rare-snails-tan.md @@ -0,0 +1,6 @@ +--- +"@fuel-ts/abi-typegen": minor +"@fuel-ts/abi-coder": minor +--- + +chore!: support new ABI format diff --git a/apps/docs-snippets/test/fixtures/abi/encode-and-decode.jsonc b/apps/docs-snippets/test/fixtures/abi/encode-and-decode.jsonc index 7b1da8fc184..942ff9c495e 100644 --- a/apps/docs-snippets/test/fixtures/abi/encode-and-decode.jsonc +++ b/apps/docs-snippets/test/fixtures/abi/encode-and-decode.jsonc @@ -1,9 +1,11 @@ // #region encode-and-decode-2 { + "abiVersion": "1", + "specVersion": "1", "encoding": "1", "types": [ { - "typeId": 0, + "typeId": "0", "type": "u32", "components": null, "typeParameters": null, @@ -14,14 +16,14 @@ "inputs": [ { "name": "inputted_amount", - "type": 0, + "type": "0", "typeArguments": null, }, ], "name": "main", "output": { "name": "", - "type": 0, + "type": "0", "typeArguments": null, }, "attributes": null, @@ -34,7 +36,7 @@ "name": "AMOUNT", "configurableType": { "name": "", - "type": 0, + "type": "0", "typeArguments": null, }, "offset": 824, diff --git a/packages/abi-coder/src/Interface.ts b/packages/abi-coder/src/Interface.ts index c2f9404bb69..32439ccf8f5 100644 --- a/packages/abi-coder/src/Interface.ts +++ b/packages/abi-coder/src/Interface.ts @@ -85,7 +85,7 @@ export class Interface { }); } - getTypeById(typeId: number) { + getTypeById(typeId: string) { return findTypeById(this.jsonAbi, typeId); } } diff --git a/packages/abi-coder/src/ResolvedAbiType.ts b/packages/abi-coder/src/ResolvedAbiType.ts index c97e077a672..eaef31680d4 100644 --- a/packages/abi-coder/src/ResolvedAbiType.ts +++ b/packages/abi-coder/src/ResolvedAbiType.ts @@ -26,7 +26,7 @@ export class ResolvedAbiType { } this.type = jsonABIType.type; - this.originalTypeArguments = argument.typeArguments; + this.originalTypeArguments = argument.typeArguments as JsonAbiArgument[]; this.components = ResolvedAbiType.getResolvedGenericComponents( abi, argument, @@ -40,7 +40,7 @@ export class ResolvedAbiType { abi: JsonAbi, arg: JsonAbiArgument, components: readonly JsonAbiArgument[] | null, - typeParameters: readonly number[] | null + typeParameters: readonly string[] | null ) { if (components === null) { return null; @@ -51,7 +51,7 @@ export class ResolvedAbiType { const typeParametersAndArgsMap = typeParameters.reduce( (obj, typeParameter, typeParameterIndex) => { - const o: Record = { ...obj }; + const o: Record = { ...obj }; o[typeParameter] = structuredClone( arg.typeArguments?.[typeParameterIndex] ) as JsonAbiArgument; @@ -72,7 +72,7 @@ export class ResolvedAbiType { private static resolveGenericArgTypes( abi: JsonAbi, args: readonly JsonAbiArgument[], - typeParametersAndArgsMap: Record + typeParametersAndArgsMap: Record ): readonly JsonAbiArgument[] { return args.map((arg) => { if (typeParametersAndArgsMap[arg.type] !== undefined) { @@ -87,7 +87,7 @@ export class ResolvedAbiType { ...structuredClone(arg), typeArguments: this.resolveGenericArgTypes( abi, - arg.typeArguments, + arg.typeArguments as JsonAbiArgument[], typeParametersAndArgsMap ), }; @@ -110,13 +110,13 @@ export class ResolvedAbiType { private static getImplicitGenericTypeParameters( abi: JsonAbi, args: readonly JsonAbiArgument[] | null, - implicitGenericParametersParam?: number[] + implicitGenericParametersParam?: string[] ) { if (!Array.isArray(args)) { return null; } - const implicitGenericParameters: number[] = implicitGenericParametersParam ?? []; + const implicitGenericParameters: string[] = implicitGenericParametersParam ?? []; args.forEach((a) => { const argType = findTypeById(abi, a.type); diff --git a/packages/abi-coder/src/types/JsonAbi.ts b/packages/abi-coder/src/types/JsonAbi.ts index 2e481d27aaf..e8e51f5b5e4 100644 --- a/packages/abi-coder/src/types/JsonAbi.ts +++ b/packages/abi-coder/src/types/JsonAbi.ts @@ -9,23 +9,25 @@ export interface JsonAbi { readonly messagesTypes: readonly JsonAbiMessagesType[]; readonly configurables: readonly JsonAbiConfigurable[]; readonly encoding?: string; + readonly specVersion: string; + readonly abiVersion: string; } export interface JsonAbiType { - readonly typeId: number; + readonly typeId: string; readonly type: string; readonly components: readonly JsonAbiArgument[] | null; - readonly typeParameters: readonly number[] | null; + readonly typeParameters: readonly string[] | null; } export interface JsonAbiArgument { - readonly type: number; readonly name: string; - readonly typeArguments: readonly JsonAbiArgument[] | null; + readonly type: string; + readonly typeArguments: readonly JsonAbiArgumentWithoutName[] | null; } export interface JsonAbiArgumentWithoutName { - readonly type: number; + readonly type: string; readonly typeArguments: readonly JsonAbiArgumentWithoutName[] | null; } diff --git a/packages/abi-coder/src/utils/json-abi.test.ts b/packages/abi-coder/src/utils/json-abi.test.ts index 853530c87bb..ccb69d1cfd2 100644 --- a/packages/abi-coder/src/utils/json-abi.test.ts +++ b/packages/abi-coder/src/utils/json-abi.test.ts @@ -1,5 +1,5 @@ import type { ResolvedAbiType } from '../ResolvedAbiType'; -import type { JsonAbi, JsonAbiArgument } from '../types/JsonAbi'; +import type { JsonAbi, JsonAbiArgument, JsonAbiFunction } from '../types/JsonAbi'; import { ENCODING_V1 } from './constants'; import { @@ -11,12 +11,16 @@ import { } from './json-abi'; const MOCK_ABI: JsonAbi = { + abiVersion: '1', + messagesTypes: [], + specVersion: '1', + encoding: '1', types: [ - { typeId: 1, type: '()', components: [], typeParameters: [] }, - { typeId: 2, type: 'u256', components: [], typeParameters: [] }, + { typeId: '1', type: '()', components: [], typeParameters: [] }, + { typeId: '2', type: 'u256', components: [], typeParameters: [] }, ], functions: [ - { name: 'foo', attributes: [], inputs: [], output: { name: '', type: 1, typeArguments: [] } }, + { name: 'foo', attributes: [], inputs: [], output: { name: '', type: '1', typeArguments: [] } }, ], loggedTypes: [], configurables: [], @@ -58,11 +62,11 @@ describe('json-abi', () => { describe('findFunctionByName', () => { it('should find a function by name', () => { - const expected = { + const expected: JsonAbiFunction = { name: 'foo', attributes: [], inputs: [], - output: { name: '', type: 1, typeArguments: [] }, + output: { name: '', type: '1', typeArguments: [] }, }; const actual = findFunctionByName(MOCK_ABI, 'foo'); @@ -80,19 +84,19 @@ describe('json-abi', () => { describe('findTypeById', () => { it('should find a type by id', () => { const expected = { - typeId: 1, + typeId: '1', type: '()', components: [], typeParameters: [], }; - const actual = findTypeById(MOCK_ABI, 1); + const actual = findTypeById(MOCK_ABI, '1'); expect(actual).toEqual(expected); }); it('should throw an error if the type is not found', () => { - expect(() => findTypeById(MOCK_ABI, -1)).toThrowError( + expect(() => findTypeById(MOCK_ABI, '-1')).toThrowError( `Type with typeId '-1' doesn't exist in the ABI.` ); }); @@ -101,10 +105,10 @@ describe('json-abi', () => { describe('findNonEmptyInputs', () => { it('should find non-empty inputs', () => { const inputs: JsonAbiArgument[] = [ - { name: 'a', type: 1, typeArguments: [] }, - { name: 'b', type: 2, typeArguments: [] }, + { name: 'a', type: '1', typeArguments: [] }, + { name: 'b', type: '2', typeArguments: [] }, ]; - const expected = [{ name: 'b', type: 2, typeArguments: [] }]; + const expected = [{ name: 'b', type: '2', typeArguments: [] }]; const actual = findNonEmptyInputs(MOCK_ABI, inputs); @@ -112,7 +116,7 @@ describe('json-abi', () => { }); it('should throw an error if the type is not found', () => { - const inputs: JsonAbiArgument[] = [{ name: 'a', type: -1, typeArguments: [] }]; + const inputs: JsonAbiArgument[] = [{ name: 'a', type: '-1', typeArguments: [] }]; expect(() => findNonEmptyInputs(MOCK_ABI, inputs)).toThrowError( `Type with typeId '-1' doesn't exist in the ABI.` diff --git a/packages/abi-coder/src/utils/json-abi.ts b/packages/abi-coder/src/utils/json-abi.ts index 753244174a8..b4c23956fea 100644 --- a/packages/abi-coder/src/utils/json-abi.ts +++ b/packages/abi-coder/src/utils/json-abi.ts @@ -51,7 +51,7 @@ export const findFunctionByName = (abi: JsonAbi, name: string): JsonAbiFunction * @param typeId - the typeId of the type to find * @returns the JsonAbi type object */ -export const findTypeById = (abi: JsonAbi, typeId: number): JsonAbiType => { +export const findTypeById = (abi: JsonAbi, typeId: string): JsonAbiType => { const type = abi.types.find((t) => t.typeId === typeId); if (!type) { throw new FuelError( diff --git a/packages/abi-typegen/package.json b/packages/abi-typegen/package.json index 234bbb903c3..336d626dac4 100644 --- a/packages/abi-typegen/package.json +++ b/packages/abi-typegen/package.json @@ -43,11 +43,12 @@ "dist" ], "scripts": { - "pretest": "run-s build:forc-callpaths build:forc", + "pretest": "run-s build:forc-callpaths build:forc transform:abi", "build": "tsup", "build:forc": "pnpm fuels-forc build -p test/fixtures/forc-projects --release", "build:forc-callpaths": "pnpm fuels-forc build -p test/fixtures/forc-projects --json-abi-with-callpaths", - "postbuild": "tsx ../../scripts/postbuild.ts" + "postbuild": "tsx ../../scripts/postbuild.ts", + "transform:abi": "tsx ../abi-typegen/src/transform-abi.ts test/fixtures" }, "license": "Apache-2.0", "dependencies": { diff --git a/packages/abi-typegen/src/AbiTypeGen.ts b/packages/abi-typegen/src/AbiTypeGen.ts index dc1e2754dde..935792f529e 100644 --- a/packages/abi-typegen/src/AbiTypeGen.ts +++ b/packages/abi-typegen/src/AbiTypeGen.ts @@ -1,6 +1,7 @@ import { ErrorCode, FuelError } from '@fuel-ts/errors'; import { Abi } from './abi/Abi'; +import { mapAbi } from './transform-abi-mapper'; import { ProgramTypeEnum } from './types/enums/ProgramTypeEnum'; import type { IFile } from './types/interfaces/IFile'; import { assembleContracts } from './utils/assembleContracts'; @@ -56,7 +57,7 @@ export class AbiTypeGen { const abi = new Abi({ filepath: abiFile.path, - rawContents: JSON.parse(abiFile.contents as string), + rawContents: mapAbi(JSON.parse(abiFile.contents as string)), hexlifiedBinContents: relatedBinFile?.contents, storageSlotsContents: relatedStorageSlotsFile?.contents, outputDir, diff --git a/packages/abi-typegen/src/abi/Abi.test.ts b/packages/abi-typegen/src/abi/Abi.test.ts index ec48cd42507..61d756c18dd 100644 --- a/packages/abi-typegen/src/abi/Abi.test.ts +++ b/packages/abi-typegen/src/abi/Abi.test.ts @@ -64,7 +64,7 @@ describe('Abi.ts', () => { function getRawTypeFor(params: { type: string }) { const rawAbiType: JsonAbiType = { - typeId: 1, + typeId: '1', type: params.type, components: null, typeParameters: null, diff --git a/packages/abi-typegen/src/abi/configurable/Configurable.test.ts b/packages/abi-typegen/src/abi/configurable/Configurable.test.ts index db66630325f..4bce2fac30d 100644 --- a/packages/abi-typegen/src/abi/configurable/Configurable.test.ts +++ b/packages/abi-typegen/src/abi/configurable/Configurable.test.ts @@ -14,7 +14,7 @@ import { Configurable } from './Configurable'; describe('Configurable.ts', () => { function mockAllDeps() { const rawAbiType: JsonAbiType = { - typeId: 1, + typeId: '1', type: 'mockType', components: null, typeParameters: null, diff --git a/packages/abi-typegen/src/abi/types/ArrayType.test.ts b/packages/abi-typegen/src/abi/types/ArrayType.test.ts index 3516400f4d8..cf769422807 100644 --- a/packages/abi-typegen/src/abi/types/ArrayType.test.ts +++ b/packages/abi-typegen/src/abi/types/ArrayType.test.ts @@ -35,7 +35,7 @@ describe('ArrayType.ts', () => { const types = rawTypes.map((rawAbiType: JsonAbiType) => makeType({ rawAbiType })); // validating `struct B`, with simple tuples on property `x` - const b = findType({ types, typeId: 0 }) as ArrayType; + const b = findType({ types, typeId: '0' }) as ArrayType; expect(b.attributes.inputLabel).toEqual('[BigNumberish, BigNumberish]'); expect(b.attributes.outputLabel).toEqual('[number, number]'); @@ -52,7 +52,7 @@ describe('ArrayType.ts', () => { const rawTypes = project.abiContents.types; const types = rawTypes.map((rawAbiType: JsonAbiType) => makeType({ rawAbiType })); - const a = findType({ types, typeId: 1 }) as ArrayType; + const a = findType({ types, typeId: '1' }) as ArrayType; expect(a.attributes.inputLabel).toEqual( '[Generic1Input, string>, Generic1Input, string>]' diff --git a/packages/abi-typegen/src/abi/types/AssetIdType.test.ts b/packages/abi-typegen/src/abi/types/AssetIdType.test.ts index e4dcf999dbb..92be7a8ba5a 100644 --- a/packages/abi-typegen/src/abi/types/AssetIdType.test.ts +++ b/packages/abi-typegen/src/abi/types/AssetIdType.test.ts @@ -10,7 +10,7 @@ describe('AssetIdType.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: AssetIdType.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/B256Type.test.ts b/packages/abi-typegen/src/abi/types/B256Type.test.ts index 904d57ffb16..e94b8dc5c15 100644 --- a/packages/abi-typegen/src/abi/types/B256Type.test.ts +++ b/packages/abi-typegen/src/abi/types/B256Type.test.ts @@ -10,7 +10,7 @@ describe('B256Type.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: B256Type.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/B512Type.test.ts b/packages/abi-typegen/src/abi/types/B512Type.test.ts index 65be8cda76f..c9b161fe103 100644 --- a/packages/abi-typegen/src/abi/types/B512Type.test.ts +++ b/packages/abi-typegen/src/abi/types/B512Type.test.ts @@ -10,7 +10,7 @@ describe('B512Type.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: B512Type.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/BoolType.test.ts b/packages/abi-typegen/src/abi/types/BoolType.test.ts index 203f4ff2031..bca3f09ebe6 100644 --- a/packages/abi-typegen/src/abi/types/BoolType.test.ts +++ b/packages/abi-typegen/src/abi/types/BoolType.test.ts @@ -10,7 +10,7 @@ describe('BoolType.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: BoolType.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/BytesType.test.ts b/packages/abi-typegen/src/abi/types/BytesType.test.ts index 44ace6cc41a..f4f64cc5abc 100644 --- a/packages/abi-typegen/src/abi/types/BytesType.test.ts +++ b/packages/abi-typegen/src/abi/types/BytesType.test.ts @@ -10,7 +10,7 @@ describe('BytesType.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: BytesType.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/EmptyType.test.ts b/packages/abi-typegen/src/abi/types/EmptyType.test.ts index 8b2a745d4b0..d95b715ca26 100644 --- a/packages/abi-typegen/src/abi/types/EmptyType.test.ts +++ b/packages/abi-typegen/src/abi/types/EmptyType.test.ts @@ -9,7 +9,7 @@ describe('EmptyType.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 0, + typeId: '0', type: EmptyType.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/EnumType.test.ts b/packages/abi-typegen/src/abi/types/EnumType.test.ts index 7e462ff8374..3304cc46e4b 100644 --- a/packages/abi-typegen/src/abi/types/EnumType.test.ts +++ b/packages/abi-typegen/src/abi/types/EnumType.test.ts @@ -50,7 +50,7 @@ describe('EnumType.ts', () => { test('should properly parse type attributes for: simple enums', () => { const { types } = getTypesForContract(AbiTypegenProjectsEnum.ENUM_SIMPLE); - const myEnum = findType({ types, typeId: 1 }) as EnumType; + const myEnum = findType({ types, typeId: '1' }) as EnumType; validateCommonEnumAttributes({ enum: myEnum }); @@ -64,7 +64,7 @@ describe('EnumType.ts', () => { test('should properly parse type attributes for: use native TS enum on a simple enum', () => { const { types } = getTypesForContract(AbiTypegenProjectsEnum.ENUM_SIMPLE_NATIVE); - const myEnum = findType({ types, typeId: 1 }) as EnumType; + const myEnum = findType({ types, typeId: '1' }) as EnumType; validateCommonEnumAttributes({ enum: myEnum }); @@ -78,7 +78,7 @@ describe('EnumType.ts', () => { test('should properly parse type attributes for: use SDK enum on a complex enum', () => { const { types } = getTypesForContract(AbiTypegenProjectsEnum.ENUM_OF_STRUCTS); - const myEnum = findType({ types, typeId: 0 }) as EnumType; + const myEnum = findType({ types, typeId: '0' }) as EnumType; validateCommonEnumAttributes({ enum: myEnum }); @@ -92,7 +92,7 @@ describe('EnumType.ts', () => { test('should properly parse type attributes for: enums of enums', () => { const { types } = getTypesForContract(AbiTypegenProjectsEnum.ENUM_OF_ENUMS); - const myEnum = findType({ types, typeId: 2 }) as EnumType; + const myEnum = findType({ types, typeId: '2' }) as EnumType; validateCommonEnumAttributes({ enum: myEnum }); @@ -106,7 +106,7 @@ describe('EnumType.ts', () => { test('should properly parse type attributes for: enums of structs', () => { const { types } = getTypesForContract(AbiTypegenProjectsEnum.ENUM_OF_STRUCTS); - const myEnum = findType({ types, typeId: 0 }) as EnumType; + const myEnum = findType({ types, typeId: '0' }) as EnumType; validateCommonEnumAttributes({ enum: myEnum }); @@ -120,7 +120,7 @@ describe('EnumType.ts', () => { test('should properly parse type attributes for: array of enums', () => { const { types } = getTypesForContract(AbiTypegenProjectsEnum.ARRAY_OF_ENUMS); - const myEnum = findType({ types, typeId: 3 }) as EnumType; + const myEnum = findType({ types, typeId: '3' }) as EnumType; expect(myEnum.attributes.structName).toEqual('MyStruct'); expect(myEnum.attributes.inputLabel).toEqual('MyStructInput'); diff --git a/packages/abi-typegen/src/abi/types/EnumType.ts b/packages/abi-typegen/src/abi/types/EnumType.ts index 72e1e0d9ce4..7d39e4f303f 100644 --- a/packages/abi-typegen/src/abi/types/EnumType.ts +++ b/packages/abi-typegen/src/abi/types/EnumType.ts @@ -47,7 +47,7 @@ export class EnumType extends AType implements IType { public getNativeEnum(params: { types: IType[] }) { const { types } = params; - const typeHash: { [key: number]: IType['rawAbiType']['type'] } = types.reduce( + const typeHash: { [key: string]: IType['rawAbiType']['type'] } = types.reduce( (hash, row) => ({ ...hash, [row.rawAbiType.typeId]: row.rawAbiType.type, @@ -80,7 +80,7 @@ export class EnumType extends AType implements IType { const contents = enumComponents.map((component) => { const { name, type: typeId, typeArguments } = component; - if (typeId === 0) { + if (typeId === '0') { return `${name}: []`; } diff --git a/packages/abi-typegen/src/abi/types/EvmAddressType.test.ts b/packages/abi-typegen/src/abi/types/EvmAddressType.test.ts index 53ec490eb9c..31e96e59504 100644 --- a/packages/abi-typegen/src/abi/types/EvmAddressType.test.ts +++ b/packages/abi-typegen/src/abi/types/EvmAddressType.test.ts @@ -33,7 +33,7 @@ describe('EvmAddressType.ts', () => { parseTypeArguments.mockClear(); - const evmAddress = findType({ types, typeId: 1 }) as EvmAddressType; + const evmAddress = findType({ types, typeId: '1' }) as EvmAddressType; expect(evmAddress.attributes.inputLabel).toEqual('EvmAddress'); expect(evmAddress.attributes.outputLabel).toEqual('EvmAddress'); diff --git a/packages/abi-typegen/src/abi/types/GenericType.test.ts b/packages/abi-typegen/src/abi/types/GenericType.test.ts index be4d6c1b3bb..67eb38c7a95 100644 --- a/packages/abi-typegen/src/abi/types/GenericType.test.ts +++ b/packages/abi-typegen/src/abi/types/GenericType.test.ts @@ -10,7 +10,7 @@ describe('GenericType.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: GenericType.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/OptionType.test.ts b/packages/abi-typegen/src/abi/types/OptionType.test.ts index c827dea99db..4d88e549c3b 100644 --- a/packages/abi-typegen/src/abi/types/OptionType.test.ts +++ b/packages/abi-typegen/src/abi/types/OptionType.test.ts @@ -37,7 +37,7 @@ describe('OptionType.ts', () => { const { types } = getTypesForContract(); // validating option - const b = findType({ types, typeId: 1 }) as OptionType; + const b = findType({ types, typeId: '1' }) as OptionType; expect(b.attributes.inputLabel).toEqual('Option'); expect(b.attributes.outputLabel).toEqual('Option'); diff --git a/packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts b/packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts index 21f34af07b2..d901c9c051a 100644 --- a/packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts +++ b/packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts @@ -10,7 +10,7 @@ describe('RawUntypedPtrType.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: RawUntypedPtr.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/RawUntypedSlice.test.ts b/packages/abi-typegen/src/abi/types/RawUntypedSlice.test.ts index a300099c7da..0fe4861a0f0 100644 --- a/packages/abi-typegen/src/abi/types/RawUntypedSlice.test.ts +++ b/packages/abi-typegen/src/abi/types/RawUntypedSlice.test.ts @@ -10,7 +10,7 @@ describe('RawUntypedSlice.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: RawUntypedSlice.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/StdStringType.test.ts b/packages/abi-typegen/src/abi/types/StdStringType.test.ts index 46624dc9d14..6d7893c3602 100644 --- a/packages/abi-typegen/src/abi/types/StdStringType.test.ts +++ b/packages/abi-typegen/src/abi/types/StdStringType.test.ts @@ -10,7 +10,7 @@ describe('StdStringType.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: StdStringType.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/StrSlicesType.test.ts b/packages/abi-typegen/src/abi/types/StrSlicesType.test.ts index 2dfa02647e1..405fc9419a1 100644 --- a/packages/abi-typegen/src/abi/types/StrSlicesType.test.ts +++ b/packages/abi-typegen/src/abi/types/StrSlicesType.test.ts @@ -12,7 +12,7 @@ describe('StrSlicesType.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: StrSliceType.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/StrType.test.ts b/packages/abi-typegen/src/abi/types/StrType.test.ts index ff141931005..3ca729a2ad6 100644 --- a/packages/abi-typegen/src/abi/types/StrType.test.ts +++ b/packages/abi-typegen/src/abi/types/StrType.test.ts @@ -10,7 +10,7 @@ describe('StrType.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: StrType.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/StructType.test.ts b/packages/abi-typegen/src/abi/types/StructType.test.ts index e3609138a5e..9f3dbbd7c13 100644 --- a/packages/abi-typegen/src/abi/types/StructType.test.ts +++ b/packages/abi-typegen/src/abi/types/StructType.test.ts @@ -40,7 +40,7 @@ describe('StructType.ts', () => { // validating `struct C`, with nested `typeArguments` parseTypeArguments.mockClear(); - const c = findType({ types, typeId: 4 }) as StructType; + const c = findType({ types, typeId: '4' }) as StructType; expect(c.getStructName()).toEqual('StructC'); expect(c.getStructDeclaration({ types })).toEqual(''); @@ -59,7 +59,7 @@ describe('StructType.ts', () => { expect(parseTypeArguments).toHaveBeenCalledTimes(2); // called twice // validating `struct A`, with multiple `typeParameters` (generics) - const a = findType({ types, typeId: 2 }) as StructType; + const a = findType({ types, typeId: '2' }) as StructType; parseTypeArguments.mockClear(); expect(a.getStructName()).toEqual('StructA'); diff --git a/packages/abi-typegen/src/abi/types/TupleType.test.ts b/packages/abi-typegen/src/abi/types/TupleType.test.ts index 792b275a968..d1084341e4d 100644 --- a/packages/abi-typegen/src/abi/types/TupleType.test.ts +++ b/packages/abi-typegen/src/abi/types/TupleType.test.ts @@ -30,7 +30,7 @@ describe('TupleType.ts', () => { // validating `struct B`, with simple tuples on property `x` parseTypeArguments.mockClear(); - const b = findType({ types, typeId: 0 }) as TupleType; + const b = findType({ types, typeId: '0' }) as TupleType; expect(b.attributes.inputLabel).toEqual('[boolean, BigNumberish]'); expect(b.attributes.outputLabel).toEqual('[boolean, BN]'); @@ -40,7 +40,7 @@ describe('TupleType.ts', () => { // validating `struct C`, with nested (tuple) `typeArguments` on `b` property parseTypeArguments.mockClear(); - const c = findType({ types, typeId: 1 }) as TupleType; + const c = findType({ types, typeId: '1' }) as TupleType; expect(c.attributes.inputLabel).toEqual( '[BigNumberish, StructAInput, string>]' diff --git a/packages/abi-typegen/src/abi/types/U16Type.test.ts b/packages/abi-typegen/src/abi/types/U16Type.test.ts index 4ef9f20688d..543466321cf 100644 --- a/packages/abi-typegen/src/abi/types/U16Type.test.ts +++ b/packages/abi-typegen/src/abi/types/U16Type.test.ts @@ -10,7 +10,7 @@ describe('U16Type.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: U16Type.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/U256Type.test.ts b/packages/abi-typegen/src/abi/types/U256Type.test.ts index 9ee7e7b36b5..0658ce34f39 100644 --- a/packages/abi-typegen/src/abi/types/U256Type.test.ts +++ b/packages/abi-typegen/src/abi/types/U256Type.test.ts @@ -10,7 +10,7 @@ describe('U256Type.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: U256Type.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/U32Type.test.ts b/packages/abi-typegen/src/abi/types/U32Type.test.ts index 2d442c3ee4d..44949caa802 100644 --- a/packages/abi-typegen/src/abi/types/U32Type.test.ts +++ b/packages/abi-typegen/src/abi/types/U32Type.test.ts @@ -10,7 +10,7 @@ describe('U32Type.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: U32Type.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/U64Type.test.ts b/packages/abi-typegen/src/abi/types/U64Type.test.ts index 97c47d08ae3..4a5a6817d9a 100644 --- a/packages/abi-typegen/src/abi/types/U64Type.test.ts +++ b/packages/abi-typegen/src/abi/types/U64Type.test.ts @@ -10,7 +10,7 @@ describe('U64Type.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: U64Type.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/U8Type.test.ts b/packages/abi-typegen/src/abi/types/U8Type.test.ts index 87b9828c714..85539fc3fa8 100644 --- a/packages/abi-typegen/src/abi/types/U8Type.test.ts +++ b/packages/abi-typegen/src/abi/types/U8Type.test.ts @@ -10,7 +10,7 @@ describe('U8Type.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: U8Type.swayType, }, }); diff --git a/packages/abi-typegen/src/abi/types/VectorType.test.ts b/packages/abi-typegen/src/abi/types/VectorType.test.ts index ca6b22a0edb..213401f7a6c 100644 --- a/packages/abi-typegen/src/abi/types/VectorType.test.ts +++ b/packages/abi-typegen/src/abi/types/VectorType.test.ts @@ -10,7 +10,7 @@ describe('VectorType.ts', () => { rawAbiType: { components: null, typeParameters: null, - typeId: 1, + typeId: '1', type: VectorType.swayType, }, }); diff --git a/packages/abi-typegen/src/templates/utils/formatConfigurables.test.ts b/packages/abi-typegen/src/templates/utils/formatConfigurables.test.ts index 70425c68df1..da7e719ba61 100644 --- a/packages/abi-typegen/src/templates/utils/formatConfigurables.test.ts +++ b/packages/abi-typegen/src/templates/utils/formatConfigurables.test.ts @@ -6,7 +6,7 @@ import { formatConfigurables } from './formatConfigurables'; describe('formatConfigurables.ts', () => { function mockAllDeps() { const rawAbiType = { - typeId: 1, + typeId: '1', type: 'mockType', components: null, typeParameters: null, diff --git a/packages/abi-typegen/src/templates/utils/formatImports.test.ts b/packages/abi-typegen/src/templates/utils/formatImports.test.ts index ce4f79b2244..a761cef3aba 100644 --- a/packages/abi-typegen/src/templates/utils/formatImports.test.ts +++ b/packages/abi-typegen/src/templates/utils/formatImports.test.ts @@ -11,7 +11,7 @@ describe('formatImports.ts', () => { const u8 = new U8Type({ rawAbiType: { - typeId: 1, + typeId: '1', type: 'u8', components: null, typeParameters: null, @@ -20,7 +20,7 @@ describe('formatImports.ts', () => { const u16 = new U8Type({ rawAbiType: { - typeId: 2, + typeId: '2', type: 'u16', components: null, typeParameters: null, @@ -29,7 +29,7 @@ describe('formatImports.ts', () => { const bool = new BoolType({ rawAbiType: { - typeId: 3, + typeId: '3', type: 'bool', components: null, typeParameters: null, diff --git a/packages/abi-typegen/src/transform-abi-mapper.ts b/packages/abi-typegen/src/transform-abi-mapper.ts new file mode 100644 index 00000000000..52887c585da --- /dev/null +++ b/packages/abi-typegen/src/transform-abi-mapper.ts @@ -0,0 +1,46 @@ +import type { + JsonAbiArgument, + JsonAbiType, + JsonAbi, + JsonAbiArgumentWithoutName, +} from './types/interfaces/JsonAbi'; + +function mapComponent(c: JsonAbiArgumentWithoutName | JsonAbiArgument): JsonAbiArgument { + return { + name: 'name' in c ? c.name : '', + type: c.type.toString(), + typeArguments: c.typeArguments?.map(mapComponent) ?? null, + }; +} +function mapType(type: JsonAbiType): JsonAbiType { + return { + type: type.type, + components: type.components?.map(mapComponent) ?? null, + typeId: type.typeId.toString(), + typeParameters: type.typeParameters?.map((x) => x.toString()) ?? null, + }; +} + +export function mapAbi(contents: JsonAbi) { + return { + abiVersion: '1', + specVersion: '1', + encoding: '1', + types: contents.types.map(mapType), + functions: + contents.functions.map((x) => ({ + ...x, + inputs: x.inputs.map(mapComponent), + output: mapComponent(x.output), + })) ?? null, + loggedTypes: contents.loggedTypes?.map((l) => ({ + logId: l.logId.toString(), + loggedType: mapComponent(l.loggedType), + })), + messagesTypes: [], + configurables: contents.configurables.map((x) => ({ + ...x, + configurableType: mapComponent(x.configurableType), + })), + } as JsonAbi; +} diff --git a/packages/abi-typegen/src/transform-abi.ts b/packages/abi-typegen/src/transform-abi.ts new file mode 100644 index 00000000000..ec4177d0da6 --- /dev/null +++ b/packages/abi-typegen/src/transform-abi.ts @@ -0,0 +1,23 @@ +import { copyFileSync, readFileSync, writeFileSync } from 'fs'; +import { globSync } from 'glob'; +import { join, resolve } from 'path'; + +import { mapAbi } from './transform-abi-mapper'; +import type { JsonAbi } from './types/interfaces/JsonAbi'; + +const transformAbi = (filepath: string) => { + copyFileSync(filepath, filepath.replace(`.json`, `.json.bkp`)); + const contents: JsonAbi = JSON.parse(readFileSync(filepath, 'utf-8')); + + const newContents = mapAbi(contents); + + writeFileSync(filepath, JSON.stringify(newContents, null, 2)); +}; + +const root = resolve(process.argv[2]); +// eslint-disable-next-line no-console +console.log('the root', root); +const files = globSync('**/*-abi.json', { cwd: root }); +// eslint-disable-next-line no-console +console.log('the files', files); +files.map((f) => transformAbi(join(root, f))); diff --git a/packages/abi-typegen/src/types/interfaces/JsonAbi.ts b/packages/abi-typegen/src/types/interfaces/JsonAbi.ts index 2e481d27aaf..4cd2023dae2 100644 --- a/packages/abi-typegen/src/types/interfaces/JsonAbi.ts +++ b/packages/abi-typegen/src/types/interfaces/JsonAbi.ts @@ -9,23 +9,25 @@ export interface JsonAbi { readonly messagesTypes: readonly JsonAbiMessagesType[]; readonly configurables: readonly JsonAbiConfigurable[]; readonly encoding?: string; + readonly specVersion: string; + readonly abiVersion: string; } export interface JsonAbiType { - readonly typeId: number; + readonly typeId: string; readonly type: string; readonly components: readonly JsonAbiArgument[] | null; - readonly typeParameters: readonly number[] | null; + readonly typeParameters: readonly string[] | null; } export interface JsonAbiArgument { - readonly type: number; readonly name: string; - readonly typeArguments: readonly JsonAbiArgument[] | null; + readonly type: string; + readonly typeArguments: readonly JsonAbiArgumentWithoutName[] | null; } export interface JsonAbiArgumentWithoutName { - readonly type: number; + readonly type: string; readonly typeArguments: readonly JsonAbiArgumentWithoutName[] | null; } @@ -41,7 +43,7 @@ export interface JsonAbiMessagesType { export interface JsonAbiFunction { readonly name: string; readonly inputs: readonly JsonAbiArgument[]; - readonly output: JsonAbiArgument; + readonly output: JsonAbiArgumentWithoutName; readonly attributes: readonly JsonAbiFunctionAttribute[] | null; } diff --git a/packages/abi-typegen/src/utils/findType.test.ts b/packages/abi-typegen/src/utils/findType.test.ts index b15dc9b9ae1..7fa7b7a6c04 100644 --- a/packages/abi-typegen/src/utils/findType.test.ts +++ b/packages/abi-typegen/src/utils/findType.test.ts @@ -13,7 +13,7 @@ describe('findType.ts', () => { test('should find type', () => { const rawAbiType: JsonAbiType = { type: 'u8', - typeId: 1, + typeId: '1', components: null, typeParameters: null, }; @@ -22,7 +22,7 @@ describe('findType.ts', () => { const parseComponentsAttributesSpy = vi.spyOn(type, 'parseComponentsAttributes'); - const typeId = 1; + const typeId = '1'; const types: IType[] = [type]; // array with type to be found const found = findType({ typeId, types }); @@ -32,7 +32,7 @@ describe('findType.ts', () => { }); test('should throw for type not found', async () => { - const typeId = 1; + const typeId = '1'; const types: IType[] = []; // empty array here, will error const fn = () => findType({ typeId, types }); diff --git a/packages/abi-typegen/src/utils/findType.ts b/packages/abi-typegen/src/utils/findType.ts index 6af3fa2c9fa..309ac3ff0fc 100644 --- a/packages/abi-typegen/src/utils/findType.ts +++ b/packages/abi-typegen/src/utils/findType.ts @@ -2,7 +2,7 @@ import { ErrorCode, FuelError } from '@fuel-ts/errors'; import type { IType } from '../types/interfaces/IType'; -export function findType(params: { types: IType[]; typeId: number }) { +export function findType(params: { types: IType[]; typeId: string }) { const { types, typeId } = params; const foundType = types.find(({ rawAbiType: { typeId: tid } }) => tid === typeId); diff --git a/packages/abi-typegen/src/utils/makeConfigurable.test.ts b/packages/abi-typegen/src/utils/makeConfigurable.test.ts index 726c713a210..81c5898e54e 100644 --- a/packages/abi-typegen/src/utils/makeConfigurable.test.ts +++ b/packages/abi-typegen/src/utils/makeConfigurable.test.ts @@ -8,7 +8,7 @@ import { makeConfigurable } from './makeConfigurable'; describe('makeConfigurable.ts', () => { function mockAllDeps() { const rawAbiType = { - typeId: 1, + typeId: '1', type: 'mockType', components: null, typeParameters: null, @@ -29,7 +29,7 @@ describe('makeConfigurable.ts', () => { name: 'mockConfigurable', configurableType: { name: 'mockConfigurable', - type: 1, + type: '1', typeArguments: null, }, offset: 0, diff --git a/packages/abi-typegen/src/utils/makeFunction.test.ts b/packages/abi-typegen/src/utils/makeFunction.test.ts index aca082c573e..aabecb1815c 100644 --- a/packages/abi-typegen/src/utils/makeFunction.test.ts +++ b/packages/abi-typegen/src/utils/makeFunction.test.ts @@ -10,14 +10,14 @@ import { makeType } from './makeType'; describe('functions.ts', () => { test('should instantiate a new Function instance', () => { const rawU8: JsonAbiType = { - typeId: 1, + typeId: '1', type: 'u8', components: null, typeParameters: null, }; const rawU16: JsonAbiType = { - typeId: 2, + typeId: '2', type: 'u16', components: null, typeParameters: null, @@ -30,8 +30,9 @@ describe('functions.ts', () => { const rawAbiFunction: JsonAbiFunction = { name: 'f1', - inputs: [{ name: 'u8', type: 1, typeArguments: null }], - output: { name: 'u8', type: 1, typeArguments: null }, + attributes: [], + inputs: [{ name: 'u8', type: '1', typeArguments: null }], + output: { type: '1', typeArguments: null }, }; expect(makeFunction({ rawAbiFunction, types })).toBeTruthy(); diff --git a/packages/abi-typegen/src/utils/makeType.test.ts b/packages/abi-typegen/src/utils/makeType.test.ts index 004e35eed0d..b3538471744 100644 --- a/packages/abi-typegen/src/utils/makeType.test.ts +++ b/packages/abi-typegen/src/utils/makeType.test.ts @@ -11,7 +11,7 @@ import { makeType } from './makeType'; describe('makeType.ts', () => { test('should create a new Type instance just fine', () => { const rawAbiType: JsonAbiType = { - typeId: 1, + typeId: '1', type: 'u64', components: null, typeParameters: null, @@ -22,7 +22,7 @@ describe('makeType.ts', () => { test('should throw for unsupported types', async () => { const rawAbiType: JsonAbiType = { - typeId: 1, + typeId: '1', type: 'non existent', components: null, typeParameters: null, diff --git a/packages/abi-typegen/src/utils/parseConfigurables.test.ts b/packages/abi-typegen/src/utils/parseConfigurables.test.ts index 51a07b8dbc3..821c9968d2e 100644 --- a/packages/abi-typegen/src/utils/parseConfigurables.test.ts +++ b/packages/abi-typegen/src/utils/parseConfigurables.test.ts @@ -7,7 +7,7 @@ import { parseConfigurables } from './parseConfigurables'; describe('parseConfigurables.ts', () => { function mockAllDeps() { const rawAbiType = { - typeId: 1, + typeId: '1', type: 'mockType', components: null, typeParameters: null, @@ -28,7 +28,7 @@ describe('parseConfigurables.ts', () => { name: 'mockConfigurable', configurableType: { name: 'mockConfigurable', - type: 1, + type: '1', typeArguments: null, }, offset: 0, diff --git a/packages/abi-typegen/src/utils/parseFunctions.test.ts b/packages/abi-typegen/src/utils/parseFunctions.test.ts index 54f0e8697ea..0c709e39ff1 100644 --- a/packages/abi-typegen/src/utils/parseFunctions.test.ts +++ b/packages/abi-typegen/src/utils/parseFunctions.test.ts @@ -10,14 +10,14 @@ import { parseFunctions } from './parseFunctions'; describe('functions.ts', () => { test('should parse an array of raw abi functions', () => { const rawU8: JsonAbiType = { - typeId: 1, + typeId: '1', type: 'u8', components: null, typeParameters: null, }; const rawU16: JsonAbiType = { - typeId: 2, + typeId: '2', type: 'u16', components: null, typeParameters: null, @@ -30,14 +30,16 @@ describe('functions.ts', () => { const rawF1: JsonAbiFunction = { name: 'f1', - inputs: [{ name: 'u8', type: 1, typeArguments: null }], - output: { name: 'u8', type: 1, typeArguments: null }, + attributes: [], + inputs: [{ name: 'u8', type: '1', typeArguments: null }], + output: { type: '1', typeArguments: null }, }; const rawF2: JsonAbiFunction = { name: 'f2', - inputs: [{ name: 'u16', type: 2, typeArguments: null }], - output: { name: 'u16', type: 2, typeArguments: null }, + attributes: [], + inputs: [{ name: 'u16', type: '2', typeArguments: null }], + output: { type: '2', typeArguments: null }, }; const rawAbiFunctions = [rawF1, rawF2]; diff --git a/packages/abi-typegen/src/utils/parseTypeArguments.test.ts b/packages/abi-typegen/src/utils/parseTypeArguments.test.ts index dbb7b115f82..864b1a2fb9c 100644 --- a/packages/abi-typegen/src/utils/parseTypeArguments.test.ts +++ b/packages/abi-typegen/src/utils/parseTypeArguments.test.ts @@ -14,40 +14,39 @@ import { parseTypeArguments } from './parseTypeArguments'; */ const defautRawTypes: JsonAbiType[] = [ { - typeId: 0, + typeId: '0', type: 'bool', components: null, typeParameters: null, }, { - typeId: 1, + typeId: '1', type: 'generic T', components: null, typeParameters: null, }, { - typeId: 2, + typeId: '2', type: 'struct A', components: [ { name: 'a', - type: 1, + type: '1', typeArguments: null, }, ], - typeParameters: [1], + typeParameters: ['1'], }, { - typeId: 3, + typeId: '3', type: 'struct B', components: [ { name: 'b', - type: 2, + type: '2', typeArguments: [ { - name: '', - type: 4, + type: '4', typeArguments: null, }, ], @@ -56,7 +55,7 @@ const defautRawTypes: JsonAbiType[] = [ typeParameters: null, }, { - typeId: 4, + typeId: '4', type: 'u8', components: null, typeParameters: null, @@ -75,7 +74,7 @@ describe('parseTypeArguments.ts', () => { return types; } - function getTypeComponents(params: { typeId: number }) { + function getTypeComponents(params: { typeId: string }) { const found = defautRawTypes.find((rt) => rt.typeId === params.typeId); return (found as JsonAbiType).components as JsonAbiArgument[]; } @@ -85,7 +84,7 @@ describe('parseTypeArguments.ts', () => { */ test('should parse type arguments just fine', () => { const types = bundleTypes(); - const typeArguments = getTypeComponents({ typeId: 2 }); + const typeArguments = getTypeComponents({ typeId: '2' }); const asInput = parseTypeArguments({ types, target: TargetEnum.INPUT, typeArguments }); const asOutput = parseTypeArguments({ types, target: TargetEnum.OUTPUT, typeArguments }); @@ -96,7 +95,7 @@ describe('parseTypeArguments.ts', () => { test('should parse type arguments recursively', () => { const types = bundleTypes(); - const typeArguments = getTypeComponents({ typeId: 3 }); // this has `typeArguments` + const typeArguments = getTypeComponents({ typeId: '3' }); // this has `typeArguments` const asInput = parseTypeArguments({ types, target: TargetEnum.INPUT, typeArguments }); const asOutput = parseTypeArguments({ types, target: TargetEnum.OUTPUT, typeArguments }); diff --git a/packages/abi-typegen/src/utils/parseTypeArguments.ts b/packages/abi-typegen/src/utils/parseTypeArguments.ts index e2d0673b1cc..0b951ca31e9 100644 --- a/packages/abi-typegen/src/utils/parseTypeArguments.ts +++ b/packages/abi-typegen/src/utils/parseTypeArguments.ts @@ -1,6 +1,6 @@ import type { TargetEnum } from '../types/enums/TargetEnum'; import type { IType } from '../types/interfaces/IType'; -import type { JsonAbiArgument } from '../types/interfaces/JsonAbi'; +import type { JsonAbiArgument, JsonAbiArgumentWithoutName } from '../types/interfaces/JsonAbi'; import { findType } from './findType'; @@ -10,8 +10,8 @@ import { findType } from './findType'; export function parseTypeArguments(params: { types: IType[]; target: TargetEnum; - typeArguments: readonly JsonAbiArgument[]; - parentTypeId?: number; + typeArguments: readonly JsonAbiArgumentWithoutName[]; + parentTypeId?: string; }): string { const { types, typeArguments, parentTypeId, target } = params; diff --git a/packages/abi-typegen/src/utils/parseTypes.test.ts b/packages/abi-typegen/src/utils/parseTypes.test.ts index b7765a9f968..efb72c29509 100644 --- a/packages/abi-typegen/src/utils/parseTypes.test.ts +++ b/packages/abi-typegen/src/utils/parseTypes.test.ts @@ -8,21 +8,21 @@ import { parseTypes } from './parseTypes'; describe('types.ts', () => { test('should parse an array of raw abi types', () => { const rawU8: JsonAbiType = { - typeId: 1, + typeId: '1', type: 'u8', components: null, typeParameters: null, }; const rawStr: JsonAbiType = { - typeId: 2, + typeId: '2', type: 'str[2]', components: null, typeParameters: null, }; const rawVec: JsonAbiType = { - typeId: 3, + typeId: '3', type: 'struct RawVec', components: null, typeParameters: null, diff --git a/packages/abi-typegen/test/fixtures/templates/contract/factory.hbs b/packages/abi-typegen/test/fixtures/templates/contract/factory.hbs index 579b80ce355..98a5b9718d8 100644 --- a/packages/abi-typegen/test/fixtures/templates/contract/factory.hbs +++ b/packages/abi-typegen/test/fixtures/templates/contract/factory.hbs @@ -14,18 +14,20 @@ import type { Provider, Account, AbstractAddress, BytesLike, DeployContractOptio import type { MyContractAbi, MyContractAbiInterface } from "../MyContractAbi"; const _abi = { + "abiVersion": "1", + "specVersion": "1", "encoding": "1", "types": [ { - "typeId": 0, "type": "bool", "components": null, + "typeId": "0", "typeParameters": null }, { - "typeId": 1, "type": "str[10]", "components": null, + "typeId": "1", "typeParameters": null } ], @@ -34,19 +36,19 @@ const _abi = { "inputs": [ { "name": "x", - "type": 1, + "type": "1", "typeArguments": null }, { "name": "y", - "type": 1, + "type": "1", "typeArguments": null } ], "name": "main", "output": { "name": "", - "type": 0, + "type": "0", "typeArguments": null }, "attributes": null diff --git a/packages/abi-typegen/test/fixtures/templates/predicate-with-configurable/factory.hbs b/packages/abi-typegen/test/fixtures/templates/predicate-with-configurable/factory.hbs index bbda33210f2..4cda8804588 100644 --- a/packages/abi-typegen/test/fixtures/templates/predicate-with-configurable/factory.hbs +++ b/packages/abi-typegen/test/fixtures/templates/predicate-with-configurable/factory.hbs @@ -24,24 +24,26 @@ export type MyPredicateAbiConfigurables = { export type MyPredicateAbiInputs = [fee: BigNumberish, address: string]; const _abi = { + "abiVersion": "1", + "specVersion": "1", "encoding": "1", "types": [ { - "typeId": 0, "type": "b256", "components": null, + "typeId": "0", "typeParameters": null }, { - "typeId": 1, "type": "bool", "components": null, + "typeId": "1", "typeParameters": null }, { - "typeId": 2, "type": "u8", "components": null, + "typeId": "2", "typeParameters": null } ], @@ -50,19 +52,19 @@ const _abi = { "inputs": [ { "name": "fee", - "type": 2, + "type": "2", "typeArguments": null }, { "name": "address", - "type": 0, + "type": "0", "typeArguments": null } ], "name": "main", "output": { "name": "", - "type": 1, + "type": "1", "typeArguments": null }, "attributes": null @@ -75,7 +77,7 @@ const _abi = { "name": "FEE", "configurableType": { "name": "", - "type": 2, + "type": "2", "typeArguments": null }, "offset": 904 @@ -84,7 +86,7 @@ const _abi = { "name": "ADDRESS", "configurableType": { "name": "", - "type": 0, + "type": "0", "typeArguments": null }, "offset": 872 diff --git a/packages/abi-typegen/test/fixtures/templates/predicate/factory.hbs b/packages/abi-typegen/test/fixtures/templates/predicate/factory.hbs index 45dd5a82da7..1e986e4161a 100644 --- a/packages/abi-typegen/test/fixtures/templates/predicate/factory.hbs +++ b/packages/abi-typegen/test/fixtures/templates/predicate/factory.hbs @@ -33,188 +33,190 @@ export type MyPredicateAbiConfigurables = { export type MyPredicateAbiInputs = [vec: Vec, enm: MyGenericEnumInput, opt: Option, res: Result, BigNumberish>]; const _abi = { + "abiVersion": "1", + "specVersion": "1", "encoding": "1", "types": [ { - "typeId": 0, "type": "()", "components": [], + "typeId": "0", "typeParameters": null }, { - "typeId": 1, "type": "bool", "components": null, + "typeId": "1", "typeParameters": null }, { - "typeId": 2, "type": "enum MyGenericEnum", "components": [ { "name": "a", - "type": 6, + "type": "6", "typeArguments": null } ], + "typeId": "2", "typeParameters": [ - 6 + "6" ] }, { - "typeId": 3, "type": "enum Option", "components": [ { "name": "None", - "type": 0, + "type": "0", "typeArguments": null }, { "name": "Some", - "type": 6, + "type": "6", "typeArguments": null } ], + "typeId": "3", "typeParameters": [ - 6 + "6" ] }, { - "typeId": 4, "type": "enum Result", "components": [ { "name": "Ok", - "type": 6, + "type": "6", "typeArguments": null }, { "name": "Err", - "type": 5, + "type": "5", "typeArguments": null } ], + "typeId": "4", "typeParameters": [ - 6, - 5 + "6", + "5" ] }, { - "typeId": 5, "type": "generic E", "components": null, + "typeId": "5", "typeParameters": null }, { - "typeId": 6, "type": "generic T", "components": null, + "typeId": "6", "typeParameters": null }, { - "typeId": 7, "type": "raw untyped ptr", "components": null, + "typeId": "7", "typeParameters": null }, { - "typeId": 8, "type": "str[4]", "components": null, + "typeId": "8", "typeParameters": null }, { - "typeId": 9, "type": "struct MyGenericStruct", "components": [ { "name": "a", - "type": 6, + "type": "6", "typeArguments": null } ], + "typeId": "9", "typeParameters": [ - 6 + "6" ] }, { - "typeId": 10, "type": "struct RawVec", "components": [ { "name": "ptr", - "type": 7, + "type": "7", "typeArguments": null }, { "name": "cap", - "type": 14, + "type": "14", "typeArguments": null } ], + "typeId": "10", "typeParameters": [ - 6 + "6" ] }, { - "typeId": 11, "type": "struct Validation", "components": [ { "name": "has_account", - "type": 1, + "type": "1", "typeArguments": null }, { "name": "total_complete", - "type": 14, + "type": "14", "typeArguments": null } ], + "typeId": "11", "typeParameters": null }, { - "typeId": 12, "type": "struct Vec", "components": [ { "name": "buf", - "type": 10, + "type": "10", "typeArguments": [ { "name": "", - "type": 6, + "type": "6", "typeArguments": null } ] }, { "name": "len", - "type": 14, + "type": "14", "typeArguments": null } ], + "typeId": "12", "typeParameters": [ - 6 + "6" ] }, { - "typeId": 13, "type": "u16", "components": null, + "typeId": "13", "typeParameters": null }, { - "typeId": 14, "type": "u64", "components": null, + "typeId": "14", "typeParameters": null }, { - "typeId": 15, "type": "u8", "components": null, + "typeId": "15", "typeParameters": null } ], @@ -223,55 +225,55 @@ const _abi = { "inputs": [ { "name": "vec", - "type": 12, + "type": "12", "typeArguments": [ { "name": "", - "type": 11, + "type": "11", "typeArguments": null } ] }, { "name": "enm", - "type": 2, + "type": "2", "typeArguments": [ { "name": "", - "type": 13, + "type": "13", "typeArguments": null } ] }, { "name": "opt", - "type": 3, + "type": "3", "typeArguments": [ { "name": "", - "type": 15, + "type": "15", "typeArguments": null } ] }, { "name": "res", - "type": 4, + "type": "4", "typeArguments": [ { "name": "", - "type": 9, + "type": "9", "typeArguments": [ { "name": "", - "type": 8, + "type": "8", "typeArguments": null } ] }, { "name": "", - "type": 14, + "type": "14", "typeArguments": null } ] @@ -280,7 +282,7 @@ const _abi = { "name": "main", "output": { "name": "", - "type": 1, + "type": "1", "typeArguments": null }, "attributes": null diff --git a/packages/abi-typegen/test/fixtures/templates/script-with-configurable/factory.hbs b/packages/abi-typegen/test/fixtures/templates/script-with-configurable/factory.hbs index fbc6aa2d591..ca3e8ac4597 100644 --- a/packages/abi-typegen/test/fixtures/templates/script-with-configurable/factory.hbs +++ b/packages/abi-typegen/test/fixtures/templates/script-with-configurable/factory.hbs @@ -26,35 +26,37 @@ export type MyScriptAbiConfigurables = { }; const _abi = { + "abiVersion": "1", + "specVersion": "1", "encoding": "1", "types": [ { - "typeId": 0, "type": "bool", "components": null, + "typeId": "0", "typeParameters": null }, { - "typeId": 1, "type": "struct Score", "components": [ { "name": "user", - "type": 2, + "type": "2", "typeArguments": null }, { "name": "points", - "type": 2, + "type": "2", "typeArguments": null } ], + "typeId": "1", "typeParameters": null }, { - "typeId": 2, "type": "u8", "components": null, + "typeId": "2", "typeParameters": null } ], @@ -63,14 +65,14 @@ const _abi = { "inputs": [ { "name": "score", - "type": 1, + "type": "1", "typeArguments": null } ], "name": "main", "output": { "name": "", - "type": 0, + "type": "0", "typeArguments": null }, "attributes": null @@ -83,7 +85,7 @@ const _abi = { "name": "SHOULD_RETURN", "configurableType": { "name": "", - "type": 0, + "type": "0", "typeArguments": null }, "offset": 616 diff --git a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs index ca4c4151756..f0b7e9502fc 100644 --- a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs +++ b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs @@ -30,188 +30,190 @@ type MyScriptAbiInputs = [vec: Vec, enm: MyGenericEnumInput { connectors: [new MockConnector()], }); const isAdded = await fuel.addABI('0x001123', { + abiVersion: '1', + specVersion: '1', + encoding: '1', types: [], loggedTypes: [], functions: [], diff --git a/packages/fuel-gauge/fuels.config.ts b/packages/fuel-gauge/fuels.config.ts index 016d6327b74..3ae11afedd9 100644 --- a/packages/fuel-gauge/fuels.config.ts +++ b/packages/fuel-gauge/fuels.config.ts @@ -1,3 +1,4 @@ +import { execSync } from 'child_process'; import { createConfig } from 'fuels'; export default createConfig({ @@ -6,4 +7,7 @@ export default createConfig({ forcBuildFlags: ['--release'], forcPath: 'fuels-forc', fuelCorePath: 'fuels-core', + onBuild: () => { + execSync('pnpm transform:abi', { stdio: 'inherit' }); + }, }); diff --git a/packages/fuel-gauge/package.json b/packages/fuel-gauge/package.json index 409d2a215d2..369287ffb27 100644 --- a/packages/fuel-gauge/package.json +++ b/packages/fuel-gauge/package.json @@ -7,7 +7,8 @@ "scripts": { "pretest": "run-s build:forc build:process-predicates", "build:forc": "pnpm fuels build", - "build:process-predicates": "tsx ./scripts/process-predicates.ts" + "build:process-predicates": "tsx ./scripts/process-predicates.ts", + "transform:abi": "tsx ../abi-typegen/src/transform-abi.ts test/fixtures" }, "license": "Apache-2.0", "dependencies": { diff --git a/packages/program/src/contract.test.ts b/packages/program/src/contract.test.ts index d861b27d4d0..675966d4819 100644 --- a/packages/program/src/contract.test.ts +++ b/packages/program/src/contract.test.ts @@ -8,7 +8,7 @@ const CONTRACT_ID = '0x010101010101010101010101010101010101010101010101010101010 const ABI: JsonAbi = { types: [ { - typeId: 0, + typeId: '0', type: 'u64', typeParameters: null, components: null, @@ -19,13 +19,13 @@ const ABI: JsonAbi = { inputs: [ { name: 'input', - type: 0, + type: '0', typeArguments: null, }, ], name: 'foo', output: { - type: 0, + type: '0', typeArguments: null, name: '', }, @@ -35,6 +35,9 @@ const ABI: JsonAbi = { loggedTypes: [], messagesTypes: [], configurables: [], + specVersion: '1', + abiVersion: '1', + encoding: '1', }; /** diff --git a/packages/script/src/script.test.ts b/packages/script/src/script.test.ts index f11dfa59bb9..3dfabe507f2 100644 --- a/packages/script/src/script.test.ts +++ b/packages/script/src/script.test.ts @@ -13,7 +13,6 @@ import { ReceiptType } from '@fuel-ts/transactions'; import { arrayify } from '@fuel-ts/utils'; import { getScriptForcProject, ScriptProjectsEnum } from '../test/fixtures'; -import { jsonAbiMock, jsonAbiFragmentMock } from '../test/mocks'; import { Script } from './index'; @@ -127,7 +126,7 @@ describe('Script', () => { it('should throw if script has no configurable to be set', async () => { const wallet = await setup(); - const newScript = new Script(scriptBin, jsonAbiFragmentMock, wallet); + const newScript = new Script(scriptBin, scriptJsonAbi, wallet); const { error } = await safeExec(() => newScript.setConfigurableConstants({ FEE: 8 })); @@ -140,13 +139,13 @@ describe('Script', () => { const wallet = await setup(); const jsonAbiWithConfigurablesMock: JsonAbi = { - ...jsonAbiMock, + ...scriptJsonAbi, configurables: [ { name: 'FEE', configurableType: { name: '', - type: 1, + type: '1', typeArguments: null, }, offset: 44, diff --git a/packages/script/test/mocks.ts b/packages/script/test/mocks.ts deleted file mode 100644 index 6c7b8a9516f..00000000000 --- a/packages/script/test/mocks.ts +++ /dev/null @@ -1,94 +0,0 @@ -import type { JsonAbi } from '@fuel-ts/abi-coder'; - -export const jsonAbiFragmentMock: JsonAbi = { - configurables: [], - loggedTypes: [], - messagesTypes: [], - types: [ - { - typeId: 0, - type: 'bool', - components: null, - typeParameters: null, - }, - { - typeId: 1, - type: 'u64', - components: null, - typeParameters: null, - }, - { - typeId: 2, - type: 'struct MyStruct', - components: [ - { - type: 0, - name: 'arg_one', - typeArguments: null, - }, - { - type: 1, - name: 'arg_two', - typeArguments: null, - }, - ], - typeParameters: null, - }, - ], - functions: [ - { - name: 'main', - inputs: [ - { - name: 'my_struct', - type: 2, - typeArguments: null, - }, - ], - output: { - name: 'my_struct', - type: 2, - typeArguments: null, - }, - attributes: [], - }, - ], -}; - -export const jsonAbiMock: JsonAbi = { - types: [ - { - typeId: 0, - type: 'bool', - components: null, - typeParameters: null, - }, - { - typeId: 1, - type: 'u8', - components: null, - typeParameters: null, - }, - ], - functions: [ - { - inputs: [ - { - name: 'inputed_fee', - type: 1, - typeArguments: null, - }, - ], - name: 'main', - output: { - name: '', - type: 0, - typeArguments: null, - }, - attributes: null, - }, - ], - loggedTypes: [], - messagesTypes: [], - configurables: [], -}; diff --git a/templates/nextjs/fuels.config.ts b/templates/nextjs/fuels.config.ts index d14d32f6dd1..81b1f926bfb 100644 --- a/templates/nextjs/fuels.config.ts +++ b/templates/nextjs/fuels.config.ts @@ -1,6 +1,7 @@ import { createConfig } from 'fuels'; import dotenv from 'dotenv'; import { NODE_URL } from '@/lib'; +import { execSync } from 'child_process'; dotenv.config({ path: ['.env.local', '.env'], @@ -15,4 +16,7 @@ export default createConfig({ providerUrl: NODE_URL, forcPath: 'fuels-forc', fuelCorePath: 'fuels-core', + onBuild: () => { + execSync('pnpm transform:abi', { stdio: 'inherit' }); + }, }); diff --git a/templates/nextjs/package.json b/templates/nextjs/package.json index 7a25389393c..23405ccabe6 100644 --- a/templates/nextjs/package.json +++ b/templates/nextjs/package.json @@ -8,7 +8,8 @@ "dev": "next dev", "build": "pnpm run xprebuild && next build", "start": "next start", - "lint": "next lint" + "lint": "next lint", + "transform:abi": "tsx ../../packages/abi-typegen/src/transform-abi.ts sway-programs" }, "dependencies": { "@fuels/connectors": "^0.8.1",