From 5698534cf7afe7579ba24f17413aa7862abfff4c Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 19 Jan 2023 09:39:36 -0300 Subject: [PATCH 001/108] Adjusting interface and typings for `loggedTypes` ABI property --- packages/abi-typegen/src/interfaces/IRawAbi.ts | 3 ++- packages/abi-typegen/src/interfaces/IRawAbiLoggedTypes.ts | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 packages/abi-typegen/src/interfaces/IRawAbiLoggedTypes.ts diff --git a/packages/abi-typegen/src/interfaces/IRawAbi.ts b/packages/abi-typegen/src/interfaces/IRawAbi.ts index b31d1bc6842..f7229552ac4 100644 --- a/packages/abi-typegen/src/interfaces/IRawAbi.ts +++ b/packages/abi-typegen/src/interfaces/IRawAbi.ts @@ -1,8 +1,9 @@ import type { IRawAbiFunction } from './IRawAbiFunction'; +import type { IRawAbiLoggedTypes } from './IRawAbiLoggedTypes'; import type { IRawAbiTypeRoot } from './IRawAbiType'; export interface IRawAbi { types: IRawAbiTypeRoot[]; functions: IRawAbiFunction[]; - loggedTypes: unknown[]; + loggedTypes: IRawAbiLoggedTypes[]; } diff --git a/packages/abi-typegen/src/interfaces/IRawAbiLoggedTypes.ts b/packages/abi-typegen/src/interfaces/IRawAbiLoggedTypes.ts new file mode 100644 index 00000000000..5e9e09e3c11 --- /dev/null +++ b/packages/abi-typegen/src/interfaces/IRawAbiLoggedTypes.ts @@ -0,0 +1,5 @@ +import type { IRawAbiFunctionIO } from './IRawAbiFunction'; + +export interface IRawAbiLoggedTypes extends IRawAbiFunctionIO { + logId: string; +} From 6bb183ac69519dc3a00f2331a11df39dda82c007 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 19 Jan 2023 18:32:08 -0300 Subject: [PATCH 002/108] Exporting all interfaces on package index --- packages/abi-typegen/src/index.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/abi-typegen/src/index.ts b/packages/abi-typegen/src/index.ts index ca0af914deb..2eb578bdd1b 100644 --- a/packages/abi-typegen/src/index.ts +++ b/packages/abi-typegen/src/index.ts @@ -1 +1,8 @@ export * from './AbiTypeGen'; + +export * from './interfaces/IFile'; +export * from './interfaces/IFunction'; +export * from './interfaces/IRawAbi'; +export * from './interfaces/IRawAbiFunction'; +export * from './interfaces/IRawAbiType'; +export * from './interfaces/IRawAbiLoggedTypes'; From 9569380d2318c8661ad7459e4c783978ea963e77 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 19 Jan 2023 18:40:19 -0300 Subject: [PATCH 003/108] Adding new dependency --- packages/script/package.json | 1 + pnpm-lock.yaml | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/packages/script/package.json b/packages/script/package.json index 5c6bcc5bb7e..fd128119a7d 100644 --- a/packages/script/package.json +++ b/packages/script/package.json @@ -29,6 +29,7 @@ "build-forc-projects": "./scripts/build-forc-projects.sh" }, "dependencies": { + "@fuel-ts/abi-typegen": "workspace:*", "@ethersproject/bytes": "^5.7.0", "@fuel-ts/abi-coder": "workspace:*", "@fuel-ts/constants": "workspace:*", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8ac16984547..66ce5ee5044 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -107,6 +107,7 @@ importers: '@ethersproject/properties': ^5.7.0 '@ethersproject/sha2': ^5.7.0 '@ethersproject/strings': ^5.7.0 + '@fuel-ts/abi-typegen': workspace:* '@fuel-ts/math': workspace:* '@fuel-ts/versions': workspace:* type-fest: ^3.1.0 @@ -118,6 +119,7 @@ importers: '@ethersproject/properties': 5.7.0 '@ethersproject/sha2': 5.7.0 '@ethersproject/strings': 5.7.0 + '@fuel-ts/abi-typegen': link:../abi-typegen '@fuel-ts/math': link:../math '@fuel-ts/versions': link:../versions type-fest: 3.2.0 @@ -473,6 +475,7 @@ importers: specifiers: '@ethersproject/bytes': ^5.7.0 '@fuel-ts/abi-coder': workspace:* + '@fuel-ts/abi-typegen': workspace:* '@fuel-ts/constants': workspace:* '@fuel-ts/math': workspace:* '@fuel-ts/providers': workspace:* @@ -482,6 +485,7 @@ importers: dependencies: '@ethersproject/bytes': 5.7.0 '@fuel-ts/abi-coder': link:../abi-coder + '@fuel-ts/abi-typegen': link:../abi-typegen '@fuel-ts/constants': link:../constants '@fuel-ts/math': link:../math '@fuel-ts/providers': link:../providers From 6a21da2b2c7391a312843813b2ff7f4b8c4043a3 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 19 Jan 2023 18:42:14 -0300 Subject: [PATCH 004/108] Drafting `ScriptWrapper` class --- packages/script/src/wrapper.ts | 98 ++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 packages/script/src/wrapper.ts diff --git a/packages/script/src/wrapper.ts b/packages/script/src/wrapper.ts new file mode 100644 index 00000000000..a3e7f6e7f8b --- /dev/null +++ b/packages/script/src/wrapper.ts @@ -0,0 +1,98 @@ +import { arrayify } from '@ethersproject/bytes'; +import type { InputValue } from '@fuel-ts/abi-coder'; +import { AbiCoder } from '@fuel-ts/abi-coder'; +import type { IRawAbi } from '@fuel-ts/abi-typegen'; +import type { BigNumberish } from '@fuel-ts/math'; +import { bn } from '@fuel-ts/math'; +import type { CoinQuantityLike } from '@fuel-ts/providers'; +import { ScriptTransactionRequest } from '@fuel-ts/providers'; +import { ReceiptType } from '@fuel-ts/transactions'; +import type { BaseWalletLocked } from '@fuel-ts/wallet'; + +import type { ScriptResult } from './script'; +import { Script } from './script'; + +// Duplicating ingerface `TxParams` — can't import it normally +// from `@fuel-ts/contract`, due to cyclic dependency problem +export type TxParams = Partial<{ + gasPrice: BigNumberish; + gasLimit: BigNumberish; + variableOutput: number; +}>; + +/** + * This class provides a facade on top of the Script class, + * providing a more fluent API for common script calls. + * + * It draws inspiration from the Contract class, however it + * does not uses an `Interface`, but instead follows the + * `call-script` examples present in the unit tests. + */ +export class ScriptWrapper { + public readonly coder: AbiCoder; + public readonly script: Script; + + public readonly txParameters?: TxParams; + public readonly transactionRequest = new ScriptTransactionRequest(); + + constructor(abi: IRawAbi, bin: Uint8Array) { + this.coder = new AbiCoder(); + + this.script = new Script( + bin, + (data: Inputs) => { + // TODO: Review mismatch: TYPEGEN IRawAbiFunctionIO[] vs CODER 'JsonAbiFragmentType[]' + const encoded = this.coder.encode(abi.functions[0].inputs as any, [data]); + return arrayify(encoded); + }, + (scriptResult: ScriptResult): Output => { + if (scriptResult.returnReceipt.type === ReceiptType.Revert) { + throw new Error('Script call reverted'); + } + if (scriptResult.returnReceipt.type !== ReceiptType.ReturnData) { + throw new Error('Script call failed'); + } + const decoded = this.coder.decode( + // TODO: Review mismatch: TYPEGEN 'IRawAbiFunctionIO' vs CODER 'JsonAbiFragmentType'. + [abi.functions[0].output as any], + scriptResult.returnReceipt.data + ); + return decoded?.[0] as Output; + } + ); + } + + txParams(txParams: TxParams) { + const request = this.transactionRequest; + + request.gasLimit = bn(txParams.gasLimit || request.gasLimit); + request.gasPrice = bn(txParams.gasPrice || request.gasPrice); + + request.addVariableOutputs(this.txParameters?.variableOutput || 0); + + return this; + } + + async call(params: { data: Inputs; wallet: BaseWalletLocked }) { + const request = this.transactionRequest; + + const { script } = this; + const { data, wallet } = params; + + request.setScript(script, data); + + const requiredCoinQuantities: CoinQuantityLike[] = []; + requiredCoinQuantities.push(request.calculateFee()); + + if (requiredCoinQuantities.length) { + const resources = await wallet.getResourcesToSpend(requiredCoinQuantities); + request.addResources(resources); + } + + const response = await wallet.sendTransaction(request); + const transactionResult = await response.waitForResult(); + const result = script.decodeCallResult(transactionResult); + + return { transactionResult, result, response }; + } +} From af6a7b88bef7f7156c45be26d030decc433e2874 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 19 Jan 2023 19:08:24 -0300 Subject: [PATCH 005/108] Adding new enum --- packages/abi-typegen/src/interfaces/CategoryEnum.ts | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 packages/abi-typegen/src/interfaces/CategoryEnum.ts diff --git a/packages/abi-typegen/src/interfaces/CategoryEnum.ts b/packages/abi-typegen/src/interfaces/CategoryEnum.ts new file mode 100644 index 00000000000..37d304266de --- /dev/null +++ b/packages/abi-typegen/src/interfaces/CategoryEnum.ts @@ -0,0 +1,4 @@ +export enum CategoryEnum { + CONTRACT = 'contract', + SCRIPT = 'script', +} From 431185d838510af11313687ac4194334d09357e1 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 19 Jan 2023 19:09:36 -0300 Subject: [PATCH 006/108] =?UTF-8?q?Individualizing=20file=E2=80=99s=20asse?= =?UTF-8?q?mblers=20for=20Contract=20and=20Script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/assemblers/assembleContracts.ts | 55 +++++++++++++++++++ .../src/assemblers/assembleScripts.ts | 48 ++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 packages/abi-typegen/src/assemblers/assembleContracts.ts create mode 100644 packages/abi-typegen/src/assemblers/assembleScripts.ts diff --git a/packages/abi-typegen/src/assemblers/assembleContracts.ts b/packages/abi-typegen/src/assemblers/assembleContracts.ts new file mode 100644 index 00000000000..b39007fce8d --- /dev/null +++ b/packages/abi-typegen/src/assemblers/assembleContracts.ts @@ -0,0 +1,55 @@ +import { join } from 'path'; + +import type { Abi } from '../Abi'; +import type { IFile } from '../index'; +import { renderCommonTemplate } from '../templates/common/common'; +import { renderIndexTemplate } from '../templates/contract/index'; + +interface Params { + abis: Abi[]; + outputDir: string; +} + +export function assembleContracts(params: Params) { + const { abis, outputDir } = params; + + const usesCommonTypes = abis.find((a) => a.commonTypesInUse.length > 0); + + // Assemble all DTS and Factory typescript files + const files: IFile[] = []; + + abis.forEach((abi) => { + const dts: IFile = { + path: abi.dtsFilepath, + contents: abi.getDtsDeclaration(), + }; + + const factory: IFile = { + path: abi.factoryFilepath, + contents: abi.getFactoryDeclaration(), + }; + + files.push(dts); + files.push(factory); + }); + + // Includes index file + const indexFile: IFile = { + path: `${outputDir}/index.ts`, + contents: renderIndexTemplate({ abis }), + }; + + files.push(indexFile); + + // Conditionally includes `common.d.ts` file if needed + if (usesCommonTypes) { + const commonsFilepath = join(outputDir, 'common.d.ts'); + const file: IFile = { + path: commonsFilepath, + contents: renderCommonTemplate(), + }; + files.push(file); + } + + return files; +} diff --git a/packages/abi-typegen/src/assemblers/assembleScripts.ts b/packages/abi-typegen/src/assemblers/assembleScripts.ts new file mode 100644 index 00000000000..9dc5e9bd1d2 --- /dev/null +++ b/packages/abi-typegen/src/assemblers/assembleScripts.ts @@ -0,0 +1,48 @@ +import { join } from 'path'; + +import type { Abi } from '../Abi'; +import type { IFile } from '../index'; +import { renderCommonTemplate } from '../templates/common/common'; +import { renderIndexTemplate } from '../templates/script/index'; + +interface Params { + abis: Abi[]; + outputDir: string; +} + +export function assembleScripts(params: Params) { + const { abis, outputDir } = params; + + const usesCommonTypes = abis.find((a) => a.commonTypesInUse.length > 0); + + // Assemble all Factory typescript files + const files: IFile[] = []; + + abis.forEach((abi) => { + const factory: IFile = { + path: abi.factoryFilepath, + contents: abi.getFactoryDeclaration(), + }; + files.push(factory); + }); + + // Includes index file + const indexFile: IFile = { + path: `${outputDir}/index.ts`, + contents: renderIndexTemplate({ abis }), + }; + + files.push(indexFile); + + // Conditionally includes `common.d.ts` file if needed + if (usesCommonTypes) { + const commonsFilepath = join(outputDir, 'common.d.ts'); + const file: IFile = { + path: commonsFilepath, + contents: renderCommonTemplate(), + }; + files.push(file); + } + + return files; +} From 567062afb0180b02b98e1329b0c557b999f6072c Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Sun, 22 Jan 2023 08:11:47 -0300 Subject: [PATCH 007/108] Improving regex --- jest.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jest.config.ts b/jest.config.ts index fbddade971e..56d496be2a7 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -9,7 +9,7 @@ const config: Config.InitialOptions = { coveragePathIgnorePatterns: ['/dist/', '/test/', '.test.ts', '.d.ts'], testTimeout: 15000, transform: { - '\\.hbs': 'jest-text-transformer', + '\\.hbs$': 'jest-text-transformer', }, }; From 11ec5b632839b0f119d86d1eb33fcbde0f277665 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Sun, 22 Jan 2023 08:14:11 -0300 Subject: [PATCH 008/108] Trimming non-essential bridges, sub-methods should be used directly --- packages/abi-typegen/src/Abi.test.ts | 31 +--------------------------- packages/abi-typegen/src/Abi.ts | 10 --------- 2 files changed, 1 insertion(+), 40 deletions(-) diff --git a/packages/abi-typegen/src/Abi.test.ts b/packages/abi-typegen/src/Abi.test.ts index 2e93ac71de4..b424c55ba08 100644 --- a/packages/abi-typegen/src/Abi.test.ts +++ b/packages/abi-typegen/src/Abi.test.ts @@ -1,11 +1,9 @@ -import { contractPaths } from '../test/fixtures'; +import { contractPaths } from '../test/fixtures/index'; import { executeAndCatch } from '../test/utils/executeAndCatch'; import { compileSwayToJson } from '../test/utils/sway/compileSwayToJson'; import { Abi } from './Abi'; import type { IRawAbiTypeRoot } from './interfaces/IRawAbiType'; -import * as renderDtsTemplateMod from './templates/contract/dts'; -import * as renderFactoryTemplateMod from './templates/contract/factory'; import { EnumType } from './types/EnumType'; import { OptionType } from './types/OptionType'; import { VectorType } from './types/VectorType'; @@ -23,19 +21,9 @@ describe('Abi.ts', () => { .spyOn(parseFunctionsMod, 'parseFunctions') .mockImplementation(() => []); - const renderDtsTemplate = jest - .spyOn(renderDtsTemplateMod, 'renderDtsTemplate') - .mockImplementation(() => 'dts'); - - const renderFactoryTemplate = jest - .spyOn(renderFactoryTemplateMod, 'renderFactoryTemplate') - .mockImplementation(() => 'factory'); - return { parseTypes, parseFunctions, - renderDtsTemplate, - renderFactoryTemplate, }; } @@ -84,23 +72,6 @@ describe('Abi.ts', () => { expect(parseFunctions).toHaveBeenCalledTimes(1); }); - test('should get rendered DTS and Factory typescript code', async () => { - const { - abi, - mocks: { renderDtsTemplate, renderFactoryTemplate }, - } = getMockedAbi(); - - const dts = abi.getDtsDeclaration(); - const factory = abi.getFactoryDeclaration(); - - expect(dts).toEqual('dts'); - expect(factory).toEqual('factory'); - - expect(abi).toBeTruthy(); - expect(renderDtsTemplate).toHaveBeenCalledTimes(1); - expect(renderFactoryTemplate).toHaveBeenCalledTimes(1); - }); - test('should compute array of custom types in use', async () => { const { abi } = getMockedAbi(); diff --git a/packages/abi-typegen/src/Abi.ts b/packages/abi-typegen/src/Abi.ts index f15848378b0..4310f9c674d 100644 --- a/packages/abi-typegen/src/Abi.ts +++ b/packages/abi-typegen/src/Abi.ts @@ -1,8 +1,6 @@ import type { IFunction } from './interfaces/IFunction'; import type { IRawAbi } from './interfaces/IRawAbi'; import type { IType } from './interfaces/IType'; -import { renderDtsTemplate } from './templates/contract/dts'; -import { renderFactoryTemplate } from './templates/contract/factory'; import { normalizeName } from './utils/normalize'; import { parseFunctions } from './utils/parseFunctions'; import { parseTypes } from './utils/parseTypes'; @@ -66,14 +64,6 @@ export class Abi { }; } - getDtsDeclaration() { - return renderDtsTemplate({ abi: this }); - } - - getFactoryDeclaration() { - return renderFactoryTemplate({ abi: this }); - } - computeCommonTypesInUse() { const customTypesTable: Record = { option: 'Option', From 8e7fd02a52ff69d46483b065634e2b9ec4e40286 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Sun, 22 Jan 2023 08:16:30 -0300 Subject: [PATCH 009/108] Configuring new options for Typegen CLI; updating programmatic API --- packages/abi-typegen/src/cli.test.ts | 2 ++ packages/abi-typegen/src/cli.ts | 34 +++++++++++++++++++-- packages/abi-typegen/src/runTypegen.test.ts | 7 +++++ packages/abi-typegen/src/runTypegen.ts | 5 ++- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/packages/abi-typegen/src/cli.test.ts b/packages/abi-typegen/src/cli.test.ts index 373a1cc22cb..af29cbb3b8a 100644 --- a/packages/abi-typegen/src/cli.test.ts +++ b/packages/abi-typegen/src/cli.test.ts @@ -5,6 +5,7 @@ import { executeAndCatch } from '../test/utils/executeAndCatch'; import { createTempSwayProject } from '../test/utils/sway/createTempSwayProject'; import { run } from './cli'; +import { CategoryEnum } from './interfaces/CategoryEnum'; import * as runTypegenMod from './runTypegen'; describe('cli.ts', () => { @@ -39,6 +40,7 @@ describe('cli.ts', () => { cwd: process.cwd(), inputs, output, + category: CategoryEnum.CONTRACT, silent: false, }); }); diff --git a/packages/abi-typegen/src/cli.ts b/packages/abi-typegen/src/cli.ts index e570d4dab41..fed77946eee 100644 --- a/packages/abi-typegen/src/cli.ts +++ b/packages/abi-typegen/src/cli.ts @@ -1,25 +1,53 @@ import { versions } from '@fuel-ts/versions'; import { Command } from 'commander'; +import { CategoryEnum } from './interfaces/CategoryEnum'; import { runTypegen } from './runTypegen'; export interface ICliParams { inputs: string[]; output: string; silent: boolean; + contract: boolean; + script: boolean; +} + +export function resolveCategory(params: { contract: boolean; script: boolean }) { + const { contract, script } = params; + + if (contract) { + return CategoryEnum.CONTRACT; + } + + if (script) { + return CategoryEnum.SCRIPT; + } + + throw new Error(`Could not resolve cateogry: ${params}`); } export function runCliAction(options: ICliParams) { + const { inputs, output, silent, contract, script } = options; + const cwd = process.cwd(); - const { inputs, output, silent } = options; - runTypegen({ cwd, inputs, output, silent: !!silent }); + const category = resolveCategory({ contract, script }); + + runTypegen({ + cwd, + inputs, + output, + category, + silent: !!silent, + }); } export function configureCliOptions(program: Command) { program .requiredOption('-i, --inputs ', 'input paths/globals to your abi json files') .requiredOption('-o, --output ', 'directory path for generated files') - .option('-s, --silent', 'omit output messages') + .option('-c, --contract', 'generate code for contracts [default]', true) + .option('-s, --script', 'generate code for scripts', false) + .option('--silent', 'omit output messages') .action(runCliAction); } diff --git a/packages/abi-typegen/src/runTypegen.test.ts b/packages/abi-typegen/src/runTypegen.test.ts index d473431ba12..36cbe21fb79 100644 --- a/packages/abi-typegen/src/runTypegen.test.ts +++ b/packages/abi-typegen/src/runTypegen.test.ts @@ -9,6 +9,7 @@ import { contractPaths } from '../test/fixtures'; import { executeAndCatch } from '../test/utils/executeAndCatch'; import { createTempSwayProject } from '../test/utils/sway/createTempSwayProject'; +import { CategoryEnum } from './interfaces/CategoryEnum'; import { runTypegen } from './runTypegen'; describe('runTypegen.js', () => { @@ -32,6 +33,7 @@ describe('runTypegen.js', () => { const cwd = process.cwd(); const inputs = [join(tempDir, '/out/debug/*-abi.json')]; const output = join(tempDir, 'generated'); + const category = CategoryEnum.CONTRACT; const silent = true; // executes program @@ -40,6 +42,7 @@ describe('runTypegen.js', () => { cwd, inputs, output, + category, silent, }); @@ -80,6 +83,7 @@ describe('runTypegen.js', () => { const cwd = process.cwd(); const input = join(tempDir, '/out/debug/*-abi.json'); const output = join(tempDir, 'generated'); + const category = CategoryEnum.CONTRACT; const silent = true; const filepaths = globSync(input, { cwd }); @@ -90,6 +94,7 @@ describe('runTypegen.js', () => { cwd, filepaths, output, + category, silent, }); @@ -128,6 +133,7 @@ describe('runTypegen.js', () => { // compute filepaths const cwd = process.cwd(); const output = join(tempDir, 'generated'); + const category = CategoryEnum.CONTRACT; const silent = true; // executes program @@ -135,6 +141,7 @@ describe('runTypegen.js', () => { runTypegen({ cwd, output, + category, silent, }); diff --git a/packages/abi-typegen/src/runTypegen.ts b/packages/abi-typegen/src/runTypegen.ts index 3184e6a04d9..006e67db75f 100644 --- a/packages/abi-typegen/src/runTypegen.ts +++ b/packages/abi-typegen/src/runTypegen.ts @@ -5,6 +5,7 @@ import { basename } from 'path'; import rimraf from 'rimraf'; import { AbiTypeGen } from './AbiTypeGen'; +import type { CategoryEnum } from './interfaces/CategoryEnum'; import type { IFile } from './interfaces/IFile'; export interface IGenerateFilesParams { @@ -13,10 +14,11 @@ export interface IGenerateFilesParams { inputs?: string[]; output: string; silent?: boolean; + category: CategoryEnum; } export function runTypegen(params: IGenerateFilesParams) { - const { cwd, inputs, output, silent, filepaths: originalFilepaths } = params; + const { cwd, inputs, output, silent, category, filepaths: originalFilepaths } = params; const cwdBasename = basename(cwd); @@ -55,6 +57,7 @@ export function runTypegen(params: IGenerateFilesParams) { const abiTypeGen = new AbiTypeGen({ outputDir: output, abiFiles, + category, }); /* From ce1ebd756574547688077d681dc5c71e7239b4e8 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Sun, 22 Jan 2023 08:17:15 -0300 Subject: [PATCH 010/108] Drafting initial templates for Scripts --- .../src/templates/script/factory.hbs | 52 +++++++++++++++++++ .../src/templates/script/factory.ts | 16 ++++++ .../src/templates/script/index.hbs | 5 ++ .../abi-typegen/src/templates/script/index.ts | 15 ++++++ 4 files changed, 88 insertions(+) create mode 100644 packages/abi-typegen/src/templates/script/factory.hbs create mode 100644 packages/abi-typegen/src/templates/script/factory.ts create mode 100644 packages/abi-typegen/src/templates/script/index.hbs create mode 100644 packages/abi-typegen/src/templates/script/index.ts diff --git a/packages/abi-typegen/src/templates/script/factory.hbs b/packages/abi-typegen/src/templates/script/factory.hbs new file mode 100644 index 00000000000..f3f04515024 --- /dev/null +++ b/packages/abi-typegen/src/templates/script/factory.hbs @@ -0,0 +1,52 @@ +{{header}} + +import type { IRawAbi, BigNumberish } from 'fuels'; +import { ScriptCaller } from 'fuels'; + +{{#if commonTypesInUse}} +import type { {{commonTypesInUse}} } from "./common"; +{{/if}} + + +{{#each enums}} +export type {{structName}}Input = Enum<{ {{inputValues}} }>; +{{#if recycleRef}} +export type {{structName}}Output = {{structName}}Input; +{{else}} +export type {{structName}}Output = Enum<{ {{outputValues}} }>; +{{/if}} +{{/each}} + + +{{#each structs}} +export type {{structName}}Input{{typeAnnotations}} = { {{inputValues}} }; +{{#if recycleRef}} +export type {{structName}}Output{{typeAnnotations}} = {{structName}}Input{{typeAnnotations}}; +{{else}} +export type {{structName}}Output{{typeAnnotations}} = { {{outputValues}} }; +{{/if}} +{{/each}} + +type {{capitalizedName}}Inputs = {{inputs}} +type {{capitalizedName}}Output = {{output}} + + +export class {{capitalizedName}}__factory { + + static readonly abi: IRawAbi = {} as IRawAbi; + static readonly bin: Uint8Array = {} as Uint8Array; + + static createInstance() { + + const { abi, bin } = {{capitalizedName}}__factory + + const script = new ScriptCaller< + {{capitalizedName}}Inputs, + {{capitalizedName}}Output + >({ abi, bin }); + + return script; + + } + +} diff --git a/packages/abi-typegen/src/templates/script/factory.ts b/packages/abi-typegen/src/templates/script/factory.ts new file mode 100644 index 00000000000..ff6d0e56393 --- /dev/null +++ b/packages/abi-typegen/src/templates/script/factory.ts @@ -0,0 +1,16 @@ +import type { Abi } from '../../Abi'; +import { renderHbsTemplate } from '../utils/renderHbsTemplate'; + +import factoryTemplate from './factory.hbs'; + +export function renderFactoryTemplate(params: { abi: Abi }) { + const { name: capitalizedName, rawContents } = params.abi; + const abiJsonString = JSON.stringify(rawContents, null, 2); + + const text = renderHbsTemplate({ + template: factoryTemplate, + data: { capitalizedName, abiJsonString }, + }); + + return text; +} diff --git a/packages/abi-typegen/src/templates/script/index.hbs b/packages/abi-typegen/src/templates/script/index.hbs new file mode 100644 index 00000000000..94826241d48 --- /dev/null +++ b/packages/abi-typegen/src/templates/script/index.hbs @@ -0,0 +1,5 @@ +{{header}} + +{{#each abiCapitalizedNames}} +export { {{this}}__factory } from './factories/{{this}}__factory'; +{{/each}} diff --git a/packages/abi-typegen/src/templates/script/index.ts b/packages/abi-typegen/src/templates/script/index.ts new file mode 100644 index 00000000000..b0572a6a467 --- /dev/null +++ b/packages/abi-typegen/src/templates/script/index.ts @@ -0,0 +1,15 @@ +import type { Abi } from '../../Abi'; +import { renderHbsTemplate } from '../utils/renderHbsTemplate'; + +import indexTemplate from './index.hbs'; + +export function renderIndexTemplate(params: { abis: Abi[] }) { + const abiCapitalizedNames = params.abis.map(({ name }) => name); + + const text = renderHbsTemplate({ + template: indexTemplate, + data: { abiCapitalizedNames }, + }); + + return text; +} From f5c9850a7a9f214e2e9efa4fdc39a413a8f9e807 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Sun, 22 Jan 2023 08:21:28 -0300 Subject: [PATCH 011/108] Updating lock file --- pnpm-lock.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 66ce5ee5044..6ea90f3c212 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -107,7 +107,6 @@ importers: '@ethersproject/properties': ^5.7.0 '@ethersproject/sha2': ^5.7.0 '@ethersproject/strings': ^5.7.0 - '@fuel-ts/abi-typegen': workspace:* '@fuel-ts/math': workspace:* '@fuel-ts/versions': workspace:* type-fest: ^3.1.0 @@ -119,7 +118,6 @@ importers: '@ethersproject/properties': 5.7.0 '@ethersproject/sha2': 5.7.0 '@ethersproject/strings': 5.7.0 - '@fuel-ts/abi-typegen': link:../abi-typegen '@fuel-ts/math': link:../math '@fuel-ts/versions': link:../versions type-fest: 3.2.0 From 65d50bd4b82722e86e76d7d6876937e7a957d4fa Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Sun, 22 Jan 2023 08:23:32 -0300 Subject: [PATCH 012/108] Rendering templates directly --- .../src/assemblers/assembleContracts.ts | 17 ++++++----------- .../src/assemblers/assembleScripts.ts | 14 ++++---------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/packages/abi-typegen/src/assemblers/assembleContracts.ts b/packages/abi-typegen/src/assemblers/assembleContracts.ts index b39007fce8d..63c064b8c0b 100644 --- a/packages/abi-typegen/src/assemblers/assembleContracts.ts +++ b/packages/abi-typegen/src/assemblers/assembleContracts.ts @@ -3,30 +3,25 @@ import { join } from 'path'; import type { Abi } from '../Abi'; import type { IFile } from '../index'; import { renderCommonTemplate } from '../templates/common/common'; +import { renderDtsTemplate } from '../templates/contract/dts'; +import { renderFactoryTemplate } from '../templates/contract/factory'; import { renderIndexTemplate } from '../templates/contract/index'; -interface Params { - abis: Abi[]; - outputDir: string; -} - -export function assembleContracts(params: Params) { +export function assembleContracts(params: { abis: Abi[]; outputDir: string }) { const { abis, outputDir } = params; - const usesCommonTypes = abis.find((a) => a.commonTypesInUse.length > 0); - - // Assemble all DTS and Factory typescript files const files: IFile[] = []; + const usesCommonTypes = abis.find((a) => a.commonTypesInUse.length > 0); abis.forEach((abi) => { const dts: IFile = { path: abi.dtsFilepath, - contents: abi.getDtsDeclaration(), + contents: renderDtsTemplate({ abi }), }; const factory: IFile = { path: abi.factoryFilepath, - contents: abi.getFactoryDeclaration(), + contents: renderFactoryTemplate({ abi }), }; files.push(dts); diff --git a/packages/abi-typegen/src/assemblers/assembleScripts.ts b/packages/abi-typegen/src/assemblers/assembleScripts.ts index 9dc5e9bd1d2..9c71293b30e 100644 --- a/packages/abi-typegen/src/assemblers/assembleScripts.ts +++ b/packages/abi-typegen/src/assemblers/assembleScripts.ts @@ -3,25 +3,19 @@ import { join } from 'path'; import type { Abi } from '../Abi'; import type { IFile } from '../index'; import { renderCommonTemplate } from '../templates/common/common'; +import { renderFactoryTemplate } from '../templates/script/factory'; import { renderIndexTemplate } from '../templates/script/index'; -interface Params { - abis: Abi[]; - outputDir: string; -} - -export function assembleScripts(params: Params) { +export function assembleScripts(params: { abis: Abi[]; outputDir: string }) { const { abis, outputDir } = params; - const usesCommonTypes = abis.find((a) => a.commonTypesInUse.length > 0); - - // Assemble all Factory typescript files const files: IFile[] = []; + const usesCommonTypes = abis.find((a) => a.commonTypesInUse.length > 0); abis.forEach((abi) => { const factory: IFile = { path: abi.factoryFilepath, - contents: abi.getFactoryDeclaration(), + contents: renderFactoryTemplate({ abi }), }; files.push(factory); }); From 7ae14fb4b41b31917b811c4ef0c4781727e5b137 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Sun, 22 Jan 2023 08:23:52 -0300 Subject: [PATCH 013/108] Start using standalone file assemblers --- packages/abi-typegen/src/AbiTypeGen.test.ts | 23 +++----- packages/abi-typegen/src/AbiTypeGen.ts | 60 ++++++--------------- 2 files changed, 24 insertions(+), 59 deletions(-) diff --git a/packages/abi-typegen/src/AbiTypeGen.test.ts b/packages/abi-typegen/src/AbiTypeGen.test.ts index 7948b96e964..5f54e51ff51 100644 --- a/packages/abi-typegen/src/AbiTypeGen.test.ts +++ b/packages/abi-typegen/src/AbiTypeGen.test.ts @@ -1,5 +1,5 @@ -import { Abi } from './Abi'; import { AbiTypeGen } from './AbiTypeGen'; +import { CategoryEnum } from './interfaces/CategoryEnum'; import type { IFile } from './interfaces/IFile'; import * as renderCommonTemplateMod from './templates/common/common'; import * as renderIndexTemplateMod from './templates/contract/index'; @@ -10,13 +10,6 @@ describe('AbiTypegen.ts', () => { */ function mockAll() { // mocking ABI class, methods and properties - - const getDtsDeclaration = jest.fn(() => 'dts'); - const getFactoryDeclaration = jest.fn(() => 'factory'); - - jest.spyOn(Abi.prototype, 'getDtsDeclaration').mockImplementation(getDtsDeclaration); - jest.spyOn(Abi.prototype, 'getFactoryDeclaration').mockImplementation(getFactoryDeclaration); - // mocking mehtod helpers const renderCommonTemplate = jest .spyOn(renderCommonTemplateMod, 'renderCommonTemplate') @@ -27,14 +20,14 @@ describe('AbiTypegen.ts', () => { .mockImplementation(); return { - getDtsDeclaration, - getFactoryDeclaration, renderCommonTemplate, renderIndexTemplate, }; } - function getNewAbiTypegen(includeOptionType: boolean = false) { + function getNewAbiTypegen(params: { includeOptionType: boolean } = { includeOptionType: false }) { + const { includeOptionType } = params; + const optionTypes = [ { typeId: 1, @@ -78,7 +71,7 @@ describe('AbiTypegen.ts', () => { const outputDir = './contracts'; - const typegen = new AbiTypeGen({ abiFiles, outputDir }); + const typegen = new AbiTypeGen({ abiFiles, outputDir, category: CategoryEnum.CONTRACT }); return { typegen }; } @@ -90,7 +83,7 @@ describe('AbiTypegen.ts', () => { mockAll(); // causes `common.d.ts` to be excluded - const { typegen } = getNewAbiTypegen(false); + const { typegen } = getNewAbiTypegen({ includeOptionType: false }); expect(typegen).toBeTruthy(); expect(typegen.abis.length).toEqual(2); // 2x abi files @@ -101,14 +94,12 @@ describe('AbiTypegen.ts', () => { // causes `common.d.ts` to be included const mocks = mockAll(); - const { typegen } = getNewAbiTypegen(true); + const { typegen } = getNewAbiTypegen({ includeOptionType: true }); expect(typegen).toBeTruthy(); expect(typegen.abis.length).toEqual(2); expect(typegen.files.length).toEqual(6); // 2x dts + 1x factory + 1x common + 1x index - expect(mocks.getDtsDeclaration).toHaveBeenCalled(); - expect(mocks.getFactoryDeclaration).toHaveBeenCalled(); expect(mocks.renderIndexTemplate).toHaveBeenCalled(); expect(mocks.renderCommonTemplate).toHaveBeenCalled(); }); diff --git a/packages/abi-typegen/src/AbiTypeGen.ts b/packages/abi-typegen/src/AbiTypeGen.ts index b6dd110d24d..e5ae0de1827 100644 --- a/packages/abi-typegen/src/AbiTypeGen.ts +++ b/packages/abi-typegen/src/AbiTypeGen.ts @@ -1,9 +1,8 @@ -import { join } from 'path'; - import { Abi } from './Abi'; +import { assembleContracts } from './assemblers/assembleContracts'; +import { assembleScripts } from './assemblers/assembleScripts'; +import { CategoryEnum } from './interfaces/CategoryEnum'; import type { IFile } from './interfaces/IFile'; -import { renderCommonTemplate } from './templates/common/common'; -import { renderIndexTemplate } from './templates/contract/index'; /* Manages many instances of Abi @@ -15,10 +14,9 @@ export class AbiTypeGen { public readonly files: IFile[]; - constructor(params: { abiFiles: IFile[]; outputDir: string }) { - const { abiFiles, outputDir } = params; + constructor(params: { abiFiles: IFile[]; outputDir: string; category: CategoryEnum }) { + const { abiFiles, outputDir, category } = params; - this.files = []; this.outputDir = outputDir; this.abiFiles = abiFiles; @@ -33,44 +31,20 @@ export class AbiTypeGen { }); // Assemble list of files to be written to disk - this.assembleAllFiles(); + this.files = this.getAssembledFiles({ category }); } - private assembleAllFiles() { - const usesCommonTypes = this.abis.find((a) => a.commonTypesInUse.length > 0); - - // Assemble all DTS and Factory typescript files - this.abis.forEach((abi) => { - const dts: IFile = { - path: abi.dtsFilepath, - contents: abi.getDtsDeclaration(), - }; - - const factory: IFile = { - path: abi.factoryFilepath, - contents: abi.getFactoryDeclaration(), - }; - - this.files.push(dts); - this.files.push(factory); - }); - - // Includes index file - const indexFile: IFile = { - path: `${this.outputDir}/index.ts`, - contents: renderIndexTemplate({ abis: this.abis }), - }; - - this.files.push(indexFile); - - // Conditionally includes `common.d.ts` file if needed - if (usesCommonTypes) { - const commonsFilepath = join(this.outputDir, 'common.d.ts'); - const file: IFile = { - path: commonsFilepath, - contents: renderCommonTemplate(), - }; - this.files.push(file); + private getAssembledFiles(params: { category: CategoryEnum }): IFile[] { + const { abis, outputDir } = this; + const { category } = params; + + switch (category) { + case CategoryEnum.CONTRACT: + return assembleContracts({ abis, outputDir }); + case CategoryEnum.SCRIPT: + return assembleScripts({ abis, outputDir }); + default: + throw new Error(`Invalid Typegen cateogry: ${category}`); } } } From ce87784a24e5a4de0b0b6bbd12d50df18eb03250 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Sun, 22 Jan 2023 20:17:47 -0300 Subject: [PATCH 014/108] Untangling variables from the main `Abi` class --- packages/abi-typegen/src/Abi.ts | 14 ++++---------- .../src/assemblers/assembleContracts.ts | 14 ++++++++++++-- .../abi-typegen/src/assemblers/assembleScripts.ts | 12 +++++++++++- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/packages/abi-typegen/src/Abi.ts b/packages/abi-typegen/src/Abi.ts index 4310f9c674d..5f607ceb9e6 100644 --- a/packages/abi-typegen/src/Abi.ts +++ b/packages/abi-typegen/src/Abi.ts @@ -13,9 +13,6 @@ export class Abi { public filepath: string; public outputDir: string; - public dtsFilepath: string; - public factoryFilepath: string; - public commonTypesInUse: string[] = []; public rawContents: IRawAbi; @@ -25,21 +22,17 @@ export class Abi { constructor(params: { filepath: string; outputDir: string; rawContents: IRawAbi }) { const { filepath, outputDir, rawContents } = params; - // processing abi name const abiNameRegex = /([^/]+)-abi\.json$/m; const abiName = filepath.match(abiNameRegex); - if (!abiName || abiName.length === 0) { + const couldNotParseName = !abiName || abiName.length === 0; + + if (couldNotParseName) { throw new Error(`Could not parse name from abi file: ${filepath}`); } const name = `${normalizeName(abiName[1])}Abi`; - // processing output filepaths - this.dtsFilepath = `${outputDir}/${name}.d.ts`; - this.factoryFilepath = `${outputDir}/factories/${name}__factory.ts`; - - // saving properties to class scope this.name = name; this.filepath = filepath; this.rawContents = rawContents; @@ -49,6 +42,7 @@ export class Abi { this.types = types; this.functions = functions; + this.computeCommonTypesInUse(); } diff --git a/packages/abi-typegen/src/assemblers/assembleContracts.ts b/packages/abi-typegen/src/assemblers/assembleContracts.ts index 63c064b8c0b..677bcbb3de5 100644 --- a/packages/abi-typegen/src/assemblers/assembleContracts.ts +++ b/packages/abi-typegen/src/assemblers/assembleContracts.ts @@ -7,6 +7,11 @@ import { renderDtsTemplate } from '../templates/contract/dts'; import { renderFactoryTemplate } from '../templates/contract/factory'; import { renderIndexTemplate } from '../templates/contract/index'; +/** + * Render all Comtract-related templates and returns + * an array of `IFile` with them all. For here on, + * the only thing missing is to write them to disk. + */ export function assembleContracts(params: { abis: Abi[]; outputDir: string }) { const { abis, outputDir } = params; @@ -14,13 +19,18 @@ export function assembleContracts(params: { abis: Abi[]; outputDir: string }) { const usesCommonTypes = abis.find((a) => a.commonTypesInUse.length > 0); abis.forEach((abi) => { + const { name } = abi; + + const dtsFilepath = `${outputDir}/${name}.d.ts`; + const factoryFilepath = `${outputDir}/factories/${name}__factory.ts`; + const dts: IFile = { - path: abi.dtsFilepath, + path: dtsFilepath, contents: renderDtsTemplate({ abi }), }; const factory: IFile = { - path: abi.factoryFilepath, + path: factoryFilepath, contents: renderFactoryTemplate({ abi }), }; diff --git a/packages/abi-typegen/src/assemblers/assembleScripts.ts b/packages/abi-typegen/src/assemblers/assembleScripts.ts index 9c71293b30e..85eb02b70fb 100644 --- a/packages/abi-typegen/src/assemblers/assembleScripts.ts +++ b/packages/abi-typegen/src/assemblers/assembleScripts.ts @@ -6,6 +6,11 @@ import { renderCommonTemplate } from '../templates/common/common'; import { renderFactoryTemplate } from '../templates/script/factory'; import { renderIndexTemplate } from '../templates/script/index'; +/** + * Render all Script-related templates and returns + * an array of `IFile` with them all. For here on, + * the only thing missing is to write them to disk. + */ export function assembleScripts(params: { abis: Abi[]; outputDir: string }) { const { abis, outputDir } = params; @@ -13,10 +18,15 @@ export function assembleScripts(params: { abis: Abi[]; outputDir: string }) { const usesCommonTypes = abis.find((a) => a.commonTypesInUse.length > 0); abis.forEach((abi) => { + const { name } = abi; + + const factoryFilepath = `${outputDir}/factories/${name}__factory.ts`; + const factory: IFile = { - path: abi.factoryFilepath, + path: factoryFilepath, contents: renderFactoryTemplate({ abi }), }; + files.push(factory); }); From dcf72236a0dc7471a77ab8190c24707324240c3f Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Sun, 22 Jan 2023 20:28:16 -0300 Subject: [PATCH 015/108] Adding changeset --- .changeset/wicked-files-thank.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/wicked-files-thank.md diff --git a/.changeset/wicked-files-thank.md b/.changeset/wicked-files-thank.md new file mode 100644 index 00000000000..dd7ac6e6c69 --- /dev/null +++ b/.changeset/wicked-files-thank.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/abi-typegen": minor +--- + +Adding support for generating types from Script ABIs From a371db385e265f699823ba1872a0caef88ce51ad Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Mon, 23 Jan 2023 18:24:57 -0300 Subject: [PATCH 016/108] Externalizing shared testing utility --- .../test/utils/getNewAbiTypegen.ts | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 packages/abi-typegen/test/utils/getNewAbiTypegen.ts diff --git a/packages/abi-typegen/test/utils/getNewAbiTypegen.ts b/packages/abi-typegen/test/utils/getNewAbiTypegen.ts new file mode 100644 index 00000000000..19379e4896c --- /dev/null +++ b/packages/abi-typegen/test/utils/getNewAbiTypegen.ts @@ -0,0 +1,59 @@ +import type { IFile } from '../../src/index'; +import { AbiTypeGen } from '../../src/index'; +import { CategoryEnum } from '../../src/interfaces/CategoryEnum'; + +export function getNewAbiTypegen( + params: { + category?: CategoryEnum; + includeOptionType?: boolean; + } = {} +) { + const { includeOptionType = false, category = CategoryEnum.CONTRACT } = params; + + const optionTypes = [ + { + typeId: 1, + type: 'enum Option', + components: [ + { + name: 'None', + type: 0, + typeArguments: null, + }, + { + name: 'Some', + type: 2, + typeArguments: null, + }, + ], + typeParameters: [2], + }, + { + typeId: 2, + type: 'generic T', + components: null, + typeParameters: null, + }, + ]; + + const types = includeOptionType ? optionTypes : []; + + const stubAbi = JSON.stringify({ types, functions: [] }, null, 2); + + const abiFiles: IFile[] = [ + { + path: './first-abi.json', + contents: stubAbi, + }, + { + path: './second-abi.json', + contents: stubAbi, + }, + ]; + + const outputDir = './contracts'; + + const typegen = new AbiTypeGen({ abiFiles, outputDir, category }); + + return { typegen }; +} From bf45f0c58817cbc1c7c941405442eb058c689ec6 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Mon, 23 Jan 2023 18:25:27 -0300 Subject: [PATCH 017/108] Refactoring test, deleting unrelated parts from old implementation --- packages/abi-typegen/src/AbiTypeGen.test.ts | 110 +++++--------------- 1 file changed, 26 insertions(+), 84 deletions(-) diff --git a/packages/abi-typegen/src/AbiTypeGen.test.ts b/packages/abi-typegen/src/AbiTypeGen.test.ts index 5f54e51ff51..b9565340fe3 100644 --- a/packages/abi-typegen/src/AbiTypeGen.test.ts +++ b/packages/abi-typegen/src/AbiTypeGen.test.ts @@ -1,106 +1,48 @@ -import { AbiTypeGen } from './AbiTypeGen'; +import { getNewAbiTypegen } from '../test/utils/getNewAbiTypegen'; + +import * as assembleContractsMod from './assemblers/assembleContracts'; +import * as assembleScriptsMod from './assemblers/assembleScripts'; import { CategoryEnum } from './interfaces/CategoryEnum'; -import type { IFile } from './interfaces/IFile'; -import * as renderCommonTemplateMod from './templates/common/common'; -import * as renderIndexTemplateMod from './templates/contract/index'; describe('AbiTypegen.ts', () => { - /* - Test helpers - */ - function mockAll() { - // mocking ABI class, methods and properties - // mocking mehtod helpers - const renderCommonTemplate = jest - .spyOn(renderCommonTemplateMod, 'renderCommonTemplate') - .mockImplementation(); + // Use as example of HORRIBLE auto-code-formatting + function mockAllDeps() { + jest.resetAllMocks(); - const renderIndexTemplate = jest - .spyOn(renderIndexTemplateMod, 'renderIndexTemplate') + const assembleContracts = jest + .spyOn(assembleContractsMod, 'assembleContracts') .mockImplementation(); + const assembleScripts = jest.spyOn(assembleScriptsMod, 'assembleScripts').mockImplementation(); return { - renderCommonTemplate, - renderIndexTemplate, + assembleContracts, + assembleScripts, }; } - function getNewAbiTypegen(params: { includeOptionType: boolean } = { includeOptionType: false }) { - const { includeOptionType } = params; - - const optionTypes = [ - { - typeId: 1, - type: 'enum Option', - components: [ - { - name: 'None', - type: 0, - typeArguments: null, - }, - { - name: 'Some', - type: 2, - typeArguments: null, - }, - ], - typeParameters: [2], - }, - { - typeId: 2, - type: 'generic T', - components: null, - typeParameters: null, - }, - ]; - - const types = includeOptionType ? optionTypes : []; - - const stubAbi = JSON.stringify({ types, functions: [] }, null, 2); - - const abiFiles: IFile[] = [ - { - path: './first-abi.json', - contents: stubAbi, - }, - { - path: './second-abi.json', - contents: stubAbi, - }, - ]; - - const outputDir = './contracts'; + test('should create multiple ABI instances for: contracts', async () => { + const { assembleContracts, assembleScripts } = mockAllDeps(); - const typegen = new AbiTypeGen({ abiFiles, outputDir, category: CategoryEnum.CONTRACT }); - - return { typegen }; - } - - /* - Tests - */ - test('should create multiple ABI instances', async () => { - mockAll(); - - // causes `common.d.ts` to be excluded - const { typegen } = getNewAbiTypegen({ includeOptionType: false }); + const category = CategoryEnum.CONTRACT; + const { typegen } = getNewAbiTypegen({ category }); expect(typegen).toBeTruthy(); - expect(typegen.abis.length).toEqual(2); // 2x abi files - expect(typegen.files.length).toEqual(5); // 2x dts + 2x factories + 1x index + expect(typegen.abis.length).toEqual(2); + + expect(assembleContracts).toHaveBeenCalledTimes(1); + expect(assembleScripts).toHaveBeenCalledTimes(0); }); - test('should conditionally include `common.d.ts` file', async () => { - // causes `common.d.ts` to be included - const mocks = mockAll(); + test('should create multiple ABI instances for: scripts', async () => { + const { assembleContracts, assembleScripts } = mockAllDeps(); - const { typegen } = getNewAbiTypegen({ includeOptionType: true }); + const category = CategoryEnum.SCRIPT; + const { typegen } = getNewAbiTypegen({ category }); expect(typegen).toBeTruthy(); expect(typegen.abis.length).toEqual(2); - expect(typegen.files.length).toEqual(6); // 2x dts + 1x factory + 1x common + 1x index - expect(mocks.renderIndexTemplate).toHaveBeenCalled(); - expect(mocks.renderCommonTemplate).toHaveBeenCalled(); + expect(assembleContracts).toHaveBeenCalledTimes(0); + expect(assembleScripts).toHaveBeenCalledTimes(1); }); }); From 4d3617438401639b84e5e51e30fcb08344152740 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Mon, 23 Jan 2023 18:25:34 -0300 Subject: [PATCH 018/108] Tyop --- packages/abi-typegen/src/assemblers/assembleContracts.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/abi-typegen/src/assemblers/assembleContracts.ts b/packages/abi-typegen/src/assemblers/assembleContracts.ts index 677bcbb3de5..64394de7459 100644 --- a/packages/abi-typegen/src/assemblers/assembleContracts.ts +++ b/packages/abi-typegen/src/assemblers/assembleContracts.ts @@ -8,7 +8,7 @@ import { renderFactoryTemplate } from '../templates/contract/factory'; import { renderIndexTemplate } from '../templates/contract/index'; /** - * Render all Comtract-related templates and returns + * Render all Contract-related templates and returns * an array of `IFile` with them all. For here on, * the only thing missing is to write them to disk. */ From e9f7025999c321f6533a2493e435205189b430f3 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Mon, 23 Jan 2023 18:25:55 -0300 Subject: [PATCH 019/108] Implementing tests for newly added file assemblers --- .../src/assemblers/assembleContracts.test.ts | 69 +++++++++++++++++++ .../src/assemblers/assembleScripts.test.ts | 69 +++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 packages/abi-typegen/src/assemblers/assembleContracts.test.ts create mode 100644 packages/abi-typegen/src/assemblers/assembleScripts.test.ts diff --git a/packages/abi-typegen/src/assemblers/assembleContracts.test.ts b/packages/abi-typegen/src/assemblers/assembleContracts.test.ts new file mode 100644 index 00000000000..633612b8a3d --- /dev/null +++ b/packages/abi-typegen/src/assemblers/assembleContracts.test.ts @@ -0,0 +1,69 @@ +import { getNewAbiTypegen } from '../../test/utils/getNewAbiTypegen'; +import { CategoryEnum } from '../interfaces/CategoryEnum'; +import * as renderCommonTemplateMod from '../templates/common/common'; +import * as renderFactoryTemplateMod from '../templates/contract/factory'; +import * as renderIndexTemplateMod from '../templates/contract/index'; + +import { assembleContracts } from './assembleContracts'; + +describe('assembleContracts.ts', () => { + function mockAllDeps() { + jest.resetAllMocks(); + + const renderCommonTemplate = jest + .spyOn(renderCommonTemplateMod, 'renderCommonTemplate') + .mockImplementation(); + + const renderFactoryTemplate = jest + .spyOn(renderFactoryTemplateMod, 'renderFactoryTemplate') + .mockImplementation(); + + const renderIndexTemplate = jest + .spyOn(renderIndexTemplateMod, 'renderIndexTemplate') + .mockImplementation(); + + return { + renderCommonTemplate, + renderFactoryTemplate, + renderIndexTemplate, + }; + } + + test('should assemble all files from Contract ABI ', () => { + const { + typegen: { abis, outputDir }, + } = getNewAbiTypegen({ + category: CategoryEnum.CONTRACT, + includeOptionType: false, // will prevent `common` template from being included + }); + + const { renderCommonTemplate, renderFactoryTemplate, renderIndexTemplate } = mockAllDeps(); + + const files = assembleContracts({ abis, outputDir }); + + expect(files.length).toEqual(5); // 2x dts, 2x factories, 1x index (no `common`) + + expect(renderCommonTemplate).toHaveBeenCalledTimes(0); + expect(renderFactoryTemplate).toHaveBeenCalledTimes(2); + expect(renderIndexTemplate).toHaveBeenCalledTimes(1); + }); + + test('should assemble all files from Contract ABI, including `common` file', () => { + const { + typegen: { abis, outputDir }, + } = getNewAbiTypegen({ + category: CategoryEnum.CONTRACT, + includeOptionType: true, // will cause `common` template to be included + }); + + const { renderCommonTemplate, renderFactoryTemplate, renderIndexTemplate } = mockAllDeps(); + + const files = assembleContracts({ abis, outputDir }); + + expect(files.length).toEqual(6); // 2x dts, 2x factories, 1x index, 1x common + + expect(renderCommonTemplate).toHaveBeenCalledTimes(1); // must have been called + expect(renderFactoryTemplate).toHaveBeenCalledTimes(2); + expect(renderIndexTemplate).toHaveBeenCalledTimes(1); + }); +}); diff --git a/packages/abi-typegen/src/assemblers/assembleScripts.test.ts b/packages/abi-typegen/src/assemblers/assembleScripts.test.ts new file mode 100644 index 00000000000..5747e37f533 --- /dev/null +++ b/packages/abi-typegen/src/assemblers/assembleScripts.test.ts @@ -0,0 +1,69 @@ +import { getNewAbiTypegen } from '../../test/utils/getNewAbiTypegen'; +import { CategoryEnum } from '../interfaces/CategoryEnum'; +import * as renderCommonTemplateMod from '../templates/common/common'; +import * as renderFactoryTemplateMod from '../templates/script/factory'; +import * as renderIndexTemplateMod from '../templates/script/index'; + +import { assembleScripts } from './assembleScripts'; + +describe('assembleScripts.ts', () => { + function mockAllDeps() { + jest.resetAllMocks(); + + const renderCommonTemplate = jest + .spyOn(renderCommonTemplateMod, 'renderCommonTemplate') + .mockImplementation(); + + const renderFactoryTemplate = jest + .spyOn(renderFactoryTemplateMod, 'renderFactoryTemplate') + .mockImplementation(); + + const renderIndexTemplate = jest + .spyOn(renderIndexTemplateMod, 'renderIndexTemplate') + .mockImplementation(); + + return { + renderCommonTemplate, + renderFactoryTemplate, + renderIndexTemplate, + }; + } + + test('should assemble all files from Script ABI ', () => { + const { + typegen: { abis, outputDir }, + } = getNewAbiTypegen({ + category: CategoryEnum.SCRIPT, + includeOptionType: false, // will prevent common template from being included + }); + + const { renderCommonTemplate, renderFactoryTemplate, renderIndexTemplate } = mockAllDeps(); + + const files = assembleScripts({ abis, outputDir }); + + expect(files.length).toEqual(3); // 2x factories, 1x index + + expect(renderCommonTemplate).toHaveBeenCalledTimes(0); // never called + expect(renderFactoryTemplate).toHaveBeenCalledTimes(2); + expect(renderIndexTemplate).toHaveBeenCalledTimes(1); + }); + + test('should assemble all files from Script ABI, including `common` file', () => { + const { + typegen: { abis, outputDir }, + } = getNewAbiTypegen({ + category: CategoryEnum.SCRIPT, + includeOptionType: true, // will cause common template to be included + }); + + const { renderCommonTemplate, renderFactoryTemplate, renderIndexTemplate } = mockAllDeps(); + + const files = assembleScripts({ abis, outputDir }); + + expect(files.length).toEqual(4); // 2x factories, 1x index, 1x common + + expect(renderCommonTemplate).toHaveBeenCalledTimes(1); // called once + expect(renderFactoryTemplate).toHaveBeenCalledTimes(2); + expect(renderIndexTemplate).toHaveBeenCalledTimes(1); + }); +}); From 31bd76193b22d51abb0fb43960b1cc5c8959d5a6 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Mon, 23 Jan 2023 18:26:28 -0300 Subject: [PATCH 020/108] Temporarily hard-coding place-holder data for templates --- packages/abi-typegen/src/templates/script/factory.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/abi-typegen/src/templates/script/factory.ts b/packages/abi-typegen/src/templates/script/factory.ts index ff6d0e56393..d26cf17499f 100644 --- a/packages/abi-typegen/src/templates/script/factory.ts +++ b/packages/abi-typegen/src/templates/script/factory.ts @@ -9,7 +9,12 @@ export function renderFactoryTemplate(params: { abi: Abi }) { const text = renderHbsTemplate({ template: factoryTemplate, - data: { capitalizedName, abiJsonString }, + data: { + capitalizedName, + abiJsonString, + inputs: '{}', + output: '{}', + }, }); return text; From c10dfde9a81f766aa4296b2850e586cadeaeb6d8 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Mon, 23 Jan 2023 18:33:27 -0300 Subject: [PATCH 021/108] Adding test stubs --- packages/abi-typegen/src/templates/script/factory.test.ts | 7 +++++++ packages/abi-typegen/src/templates/script/index.test.ts | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 packages/abi-typegen/src/templates/script/factory.test.ts create mode 100644 packages/abi-typegen/src/templates/script/index.test.ts diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts new file mode 100644 index 00000000000..578d535c935 --- /dev/null +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -0,0 +1,7 @@ +// TODO: Implement test +describe('', () => { + test('', () => { + // TODO: implement test + expect(true).toBeTruthy(); + }); +}); diff --git a/packages/abi-typegen/src/templates/script/index.test.ts b/packages/abi-typegen/src/templates/script/index.test.ts new file mode 100644 index 00000000000..578d535c935 --- /dev/null +++ b/packages/abi-typegen/src/templates/script/index.test.ts @@ -0,0 +1,7 @@ +// TODO: Implement test +describe('', () => { + test('', () => { + // TODO: implement test + expect(true).toBeTruthy(); + }); +}); From 6afa35fdb7a3ed114c44a3b476e9555d4b5dce65 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 08:29:22 -0300 Subject: [PATCH 022/108] Improving CLI options, validating conflicting parameters --- packages/abi-typegen/src/cli.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/abi-typegen/src/cli.ts b/packages/abi-typegen/src/cli.ts index fed77946eee..ed65df1f29b 100644 --- a/packages/abi-typegen/src/cli.ts +++ b/packages/abi-typegen/src/cli.ts @@ -45,8 +45,16 @@ export function configureCliOptions(program: Command) { program .requiredOption('-i, --inputs ', 'input paths/globals to your abi json files') .requiredOption('-o, --output ', 'directory path for generated files') - .option('-c, --contract', 'generate code for contracts [default]', true) - .option('-s, --script', 'generate code for scripts', false) + .addOption( + new Option('-c, --contract', 'generate code for contracts [default]') + .conflicts('script') + .implies({ script: undefined }) + ) + .addOption( + new Option('-s, --script', 'generate code for scripts') + .conflicts('contract') + .implies({ contract: undefined }) + ) .option('--silent', 'omit output messages') .action(runCliAction); } From 35bfc30a99a14f5c105e4f57eb9bfa50cb918074 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 08:29:34 -0300 Subject: [PATCH 023/108] Tyop --- packages/abi-typegen/src/AbiTypeGen.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/abi-typegen/src/AbiTypeGen.ts b/packages/abi-typegen/src/AbiTypeGen.ts index e5ae0de1827..fa836b26d6c 100644 --- a/packages/abi-typegen/src/AbiTypeGen.ts +++ b/packages/abi-typegen/src/AbiTypeGen.ts @@ -44,7 +44,7 @@ export class AbiTypeGen { case CategoryEnum.SCRIPT: return assembleScripts({ abis, outputDir }); default: - throw new Error(`Invalid Typegen cateogry: ${category}`); + throw new Error(`Invalid Typegen category: ${category}`); } } } From 0654739718cfeef73d434ade4f7dcd59a57cccbd Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 08:30:25 -0300 Subject: [PATCH 024/108] Removing extra conditional, reducing case scenarios --- packages/abi-typegen/src/cli.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/abi-typegen/src/cli.ts b/packages/abi-typegen/src/cli.ts index ed65df1f29b..50bebb75901 100644 --- a/packages/abi-typegen/src/cli.ts +++ b/packages/abi-typegen/src/cli.ts @@ -1,5 +1,5 @@ import { versions } from '@fuel-ts/versions'; -import { Command } from 'commander'; +import { Command, Option } from 'commander'; import { CategoryEnum } from './interfaces/CategoryEnum'; import { runTypegen } from './runTypegen'; @@ -15,15 +15,13 @@ export interface ICliParams { export function resolveCategory(params: { contract: boolean; script: boolean }) { const { contract, script } = params; - if (contract) { - return CategoryEnum.CONTRACT; - } + const noneSpecified = !contract && !script; - if (script) { - return CategoryEnum.SCRIPT; + if (contract || noneSpecified) { + return CategoryEnum.CONTRACT; } - throw new Error(`Could not resolve cateogry: ${params}`); + return CategoryEnum.SCRIPT; } export function runCliAction(options: ICliParams) { From 570d4685970d0f70f0520ba270b0c56aa541e70e Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 08:30:57 -0300 Subject: [PATCH 025/108] Refactoring existent CLI tests and adding missing ones --- packages/abi-typegen/src/cli.test.ts | 57 +++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/packages/abi-typegen/src/cli.test.ts b/packages/abi-typegen/src/cli.test.ts index af29cbb3b8a..f78a510e41e 100644 --- a/packages/abi-typegen/src/cli.test.ts +++ b/packages/abi-typegen/src/cli.test.ts @@ -9,11 +9,14 @@ import { CategoryEnum } from './interfaces/CategoryEnum'; import * as runTypegenMod from './runTypegen'; describe('cli.ts', () => { - test('should call runTypegen with proper params', async () => { - // mocking + function mockDeps() { const runTypegen = jest.spyOn(runTypegenMod, 'runTypegen').mockImplementation(); + const exit = jest.spyOn(process, 'exit').mockImplementation(); - // setup temp sway project + return { exit, runTypegen }; + } + + function setupTestSwayProject() { const contractPath = contractPaths.full; const autoBuild = true; @@ -26,15 +29,19 @@ describe('cli.ts', () => { const inputs = [join(tempDir, '/out/debug/*-abi.json')]; const output = join(tempDir, 'generated'); - // executes program - const argv = ['node', 'fuels-typegen', '-i', inputs.join(' '), '-o', output]; - const fn = () => run({ argv, programName: 'cli.js:test' }); - const { error } = await executeAndCatch(fn); + return { inputs, output }; + } + + beforeEach(jest.resetAllMocks); + afterEach(jest.resetAllMocks); + + test('should call runTypegen with proper params: for Contracts', async () => { + const { runTypegen } = mockDeps(); + const { inputs, output } = await setupTestSwayProject(); - // validates execution was ok - expect(error).toBeFalsy(); + const argv = ['node', 'fuels-typegen', '-i', inputs.join(' '), '-o', output, '-c']; - expect(runTypegen).toHaveBeenCalledTimes(1); + await run({ argv, programName: 'cli.js:test' }); expect(runTypegen).toHaveBeenNthCalledWith(1, { cwd: process.cwd(), @@ -44,4 +51,34 @@ describe('cli.ts', () => { silent: false, }); }); + + test('should call runTypegen with proper params: for Scripts', async () => { + const { runTypegen, exit } = mockDeps(); + const { inputs, output } = await setupTestSwayProject(); + + const argv = ['node', 'fuels-typegen', '-i', inputs.join(' '), '-o', output, '-s']; + + await run({ argv, programName: 'cli.js:test' }); + + expect(runTypegen).toHaveBeenNthCalledWith(1, { + cwd: process.cwd(), + inputs, + output, + category: CategoryEnum.SCRIPT, + silent: false, + }); + + expect(exit).toHaveBeenCalledTimes(0); + }); + + test('should error if called with improper parameters', async () => { + const { exit } = mockDeps(); + const { inputs, output } = await setupTestSwayProject(); + + const argv = ['node', 'fuels-typegen', '-i', inputs.join(' '), '-o', output, '-s', '-c']; + + await run({ argv, programName: 'cli.js:test' }); + + expect(exit).toHaveBeenNthCalledWith(1, 1); + }); }); From 4f848bbdb3deafb89ba69b975d14c825b0ef1792 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 08:31:13 -0300 Subject: [PATCH 026/108] Adding missing test for `AbiTypegen` --- packages/abi-typegen/src/AbiTypeGen.test.ts | 22 +++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/abi-typegen/src/AbiTypeGen.test.ts b/packages/abi-typegen/src/AbiTypeGen.test.ts index b9565340fe3..11dd58222b7 100644 --- a/packages/abi-typegen/src/AbiTypeGen.test.ts +++ b/packages/abi-typegen/src/AbiTypeGen.test.ts @@ -1,3 +1,4 @@ +import { executeAndCatch } from '../test/utils/executeAndCatch'; import { getNewAbiTypegen } from '../test/utils/getNewAbiTypegen'; import * as assembleContractsMod from './assemblers/assembleContracts'; @@ -7,11 +8,10 @@ import { CategoryEnum } from './interfaces/CategoryEnum'; describe('AbiTypegen.ts', () => { // Use as example of HORRIBLE auto-code-formatting function mockAllDeps() { - jest.resetAllMocks(); - const assembleContracts = jest .spyOn(assembleContractsMod, 'assembleContracts') .mockImplementation(); + const assembleScripts = jest.spyOn(assembleScriptsMod, 'assembleScripts').mockImplementation(); return { @@ -20,6 +20,9 @@ describe('AbiTypegen.ts', () => { }; } + beforeEach(jest.resetAllMocks); + afterEach(jest.resetAllMocks); + test('should create multiple ABI instances for: contracts', async () => { const { assembleContracts, assembleScripts } = mockAllDeps(); @@ -45,4 +48,19 @@ describe('AbiTypegen.ts', () => { expect(assembleContracts).toHaveBeenCalledTimes(0); expect(assembleScripts).toHaveBeenCalledTimes(1); }); + + test('should throw for unknown category', async () => { + const { assembleContracts, assembleScripts } = mockAllDeps(); + + const category = 'nope' as any; + + const { error } = await executeAndCatch(() => { + getNewAbiTypegen({ category }); + }); + + expect(error?.message).toMatch(/Invalid Typegen category: nope/); + + expect(assembleContracts).toHaveBeenCalledTimes(0); + expect(assembleScripts).toHaveBeenCalledTimes(0); + }); }); From 0194767567729f1b121990c6acb47644c9f9454c Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 08:53:36 -0300 Subject: [PATCH 027/108] Drafting template rendering test, adding TODOs --- .../src/templates/script/factory.test.ts | 34 +++++++++++++++--- .../src/templates/script/factory.ts | 4 +-- .../fixtures/templates/script/factory.hbs | 36 +++++++++++++++++++ .../test/fixtures/templates/script/index.hbs | 14 ++++++++ 4 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 packages/abi-typegen/test/fixtures/templates/script/factory.hbs create mode 100644 packages/abi-typegen/test/fixtures/templates/script/index.hbs diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts index 578d535c935..a82b54186ce 100644 --- a/packages/abi-typegen/src/templates/script/factory.test.ts +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -1,7 +1,31 @@ -// TODO: Implement test -describe('', () => { - test('', () => { - // TODO: implement test - expect(true).toBeTruthy(); +import { contractPaths } from '../../../test/fixtures'; +import factoryTemplate from '../../../test/fixtures/templates/script/factory.hbs'; +import { mockVersions } from '../../../test/utils/mockVersions'; +import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import { Abi } from '../../Abi'; + +import { renderFactoryTemplate } from './factory'; + +describe('factory.ts', () => { + test('should render factory template', () => { + const { restore } = mockVersions(); + + const contractPath = contractPaths.minimal; + + const { rawContents } = compileSwayToJson({ contractPath }); + + const abi = new Abi({ + filepath: './my-contract-abi.json', + outputDir: 'stdout', + rawContents, + }); + + const rendered = renderFactoryTemplate({ abi }); + + restore(); + + // console.log(rendered); + + expect(rendered).toEqual(factoryTemplate); }); }); diff --git a/packages/abi-typegen/src/templates/script/factory.ts b/packages/abi-typegen/src/templates/script/factory.ts index d26cf17499f..7782d935e32 100644 --- a/packages/abi-typegen/src/templates/script/factory.ts +++ b/packages/abi-typegen/src/templates/script/factory.ts @@ -12,8 +12,8 @@ export function renderFactoryTemplate(params: { abi: Abi }) { data: { capitalizedName, abiJsonString, - inputs: '{}', - output: '{}', + inputs: '{}', // TODO: fix place holder + output: '{}', // TODO: fix place holder }, }); diff --git a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs new file mode 100644 index 00000000000..703d066a90e --- /dev/null +++ b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs @@ -0,0 +1,36 @@ +/* Autogenerated file. Do not edit manually. */ + +/* tslint:disable */ +/* eslint-disable */ + +/* + Fuels version: 11.11.11 + Forc version: 22.22.22 + Fuel-Core version: 33.33.33 +*/ + +import type { IRawAbi, BigNumberish } from 'fuels'; +import { ScriptCaller } from 'fuels'; + +type MyContractAbiInputs = {} +type MyContractAbiOutput = {} + +export class MyContractAbi__factory { + + static readonly abi: IRawAbi = {} as IRawAbi; + static readonly bin: Uint8Array = {} as Uint8Array; + + static createInstance() { + + const { abi, bin } = MyContractAbi__factory + + const script = new ScriptCaller< + MyContractAbiInputs, + MyContractAbiOutput + >({ abi, bin }); + + return script; + + } + +} diff --git a/packages/abi-typegen/test/fixtures/templates/script/index.hbs b/packages/abi-typegen/test/fixtures/templates/script/index.hbs new file mode 100644 index 00000000000..68f3de2bec2 --- /dev/null +++ b/packages/abi-typegen/test/fixtures/templates/script/index.hbs @@ -0,0 +1,14 @@ +/* Autogenerated file. Do not edit manually. */ + +/* tslint:disable */ +/* eslint-disable */ + +/* + Fuels version: 11.11.11 + Forc version: 22.22.22 + Fuel-Core version: 33.33.33 +*/ + +export type { MyContractAbi } from './MyContractAbi'; + +export { MyContractAbi__factory } from './factories/MyContractAbi__factory'; From cad9eca46a1dbd19b2ab08fd19a28db99b21e35b Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 10:22:00 -0300 Subject: [PATCH 028/108] Remodeling function interfaces --- packages/abi-typegen/src/functions/Function.test.ts | 4 ++-- packages/abi-typegen/src/functions/Function.ts | 8 +++++--- packages/abi-typegen/src/interfaces/IFunction.ts | 2 +- packages/abi-typegen/src/templates/contract/dts.ts | 6 +++--- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/abi-typegen/src/functions/Function.test.ts b/packages/abi-typegen/src/functions/Function.test.ts index 1e56d9243d2..f4dc670596b 100644 --- a/packages/abi-typegen/src/functions/Function.test.ts +++ b/packages/abi-typegen/src/functions/Function.test.ts @@ -45,7 +45,7 @@ describe('Function.ts', () => { const func = new Function({ rawAbiFunction, types }); - expect(func.attributes.name).toEqual(rawAbiFunction.name); + expect(func.name).toEqual(rawAbiFunction.name); expect(func.attributes.inputs).toEqual('Vec'); expect(func.attributes.output).toEqual('Vec'); expect(func.attributes.prefixedInputs).toEqual('x: Vec'); @@ -64,7 +64,7 @@ describe('Function.ts', () => { const func = new Function({ rawAbiFunction, types }); - expect(func.attributes.name).toEqual(rawAbiFunction.name); + expect(func.name).toEqual(rawAbiFunction.name); expect(func.attributes.inputs).toEqual('Option'); expect(func.attributes.output).toEqual('Option'); expect(func.attributes.prefixedInputs).toEqual('x: Option'); diff --git a/packages/abi-typegen/src/functions/Function.ts b/packages/abi-typegen/src/functions/Function.ts index 851b6ba2951..00bfd1bc00b 100644 --- a/packages/abi-typegen/src/functions/Function.ts +++ b/packages/abi-typegen/src/functions/Function.ts @@ -6,16 +6,17 @@ import { findType } from '../utils/findType'; import { parseTypeArguments } from '../utils/parseTypeArguments'; export class Function implements IFunction { + public name: string; public types: IType[]; public rawAbiFunction: IRawAbiFunction; public attributes: IFunctionAttributes; constructor(params: { types: IType[]; rawAbiFunction: IRawAbiFunction }) { - this.types = params.types; this.rawAbiFunction = params.rawAbiFunction; + this.types = params.types; + this.name = params.rawAbiFunction.name; this.attributes = { - name: this.rawAbiFunction.name, inputs: this.bundleInputTypes(), output: this.bundleOutputTypes(), prefixedInputs: this.bundleInputTypes(true), @@ -66,7 +67,8 @@ export class Function implements IFunction { } getDeclaration() { - const { name, prefixedInputs, output } = this.attributes; + const { name } = this; + const { prefixedInputs, output } = this.attributes; const decl = `${name}: InvokeFunction<[${prefixedInputs}], ${output}>`; return decl; } diff --git a/packages/abi-typegen/src/interfaces/IFunction.ts b/packages/abi-typegen/src/interfaces/IFunction.ts index 2b6a3305c65..ad95898b885 100644 --- a/packages/abi-typegen/src/interfaces/IFunction.ts +++ b/packages/abi-typegen/src/interfaces/IFunction.ts @@ -2,7 +2,6 @@ import type { IRawAbiFunction } from './IRawAbiFunction'; import type { IType } from './IType'; export interface IFunctionAttributes { - name: string; inputs: string; output: string; prefixedInputs: string; @@ -10,6 +9,7 @@ export interface IFunctionAttributes { export interface IFunction { types: IType[]; + name: string; rawAbiFunction: IRawAbiFunction; attributes: IFunctionAttributes; getDeclaration(): string; diff --git a/packages/abi-typegen/src/templates/contract/dts.ts b/packages/abi-typegen/src/templates/contract/dts.ts index e56a41befee..95ed74d61d9 100644 --- a/packages/abi-typegen/src/templates/contract/dts.ts +++ b/packages/abi-typegen/src/templates/contract/dts.ts @@ -14,15 +14,15 @@ export function renderDtsTemplate(params: { abi: Abi }) { */ const functionsTypedefs = functions.map((f) => f.getDeclaration()); - const functionsFragments = functions.map((f) => f.attributes.name); + const functionsFragments = functions.map((f) => f.name); const encoders = functions.map((f) => ({ - functionName: f.attributes.name, + functionName: f.name, input: f.attributes.inputs, })); const decoders = functions.map((f) => ({ - functionName: f.attributes.name, + functionName: f.name, })); const structs = types From 0253092808265c8009b70a05c140ce6d0eaade95 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 10:22:15 -0300 Subject: [PATCH 029/108] Tyop --- packages/script/src/wrapper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/script/src/wrapper.ts b/packages/script/src/wrapper.ts index a3e7f6e7f8b..75e019d19a4 100644 --- a/packages/script/src/wrapper.ts +++ b/packages/script/src/wrapper.ts @@ -12,7 +12,7 @@ import type { BaseWalletLocked } from '@fuel-ts/wallet'; import type { ScriptResult } from './script'; import { Script } from './script'; -// Duplicating ingerface `TxParams` — can't import it normally +// Duplicating interface `TxParams` — can't import it normally // from `@fuel-ts/contract`, due to cyclic dependency problem export type TxParams = Partial<{ gasPrice: BigNumberish; From d998dc2e107adfce68377142323e2a17dd388e58 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 10:26:14 -0300 Subject: [PATCH 030/108] Fixing template --- packages/abi-typegen/src/templates/script/factory.hbs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/abi-typegen/src/templates/script/factory.hbs b/packages/abi-typegen/src/templates/script/factory.hbs index f3f04515024..cd4870abe9d 100644 --- a/packages/abi-typegen/src/templates/script/factory.hbs +++ b/packages/abi-typegen/src/templates/script/factory.hbs @@ -27,8 +27,8 @@ export type {{structName}}Output{{typeAnnotations}} = { {{outputValues}} }; {{/if}} {{/each}} -type {{capitalizedName}}Inputs = {{inputs}} -type {{capitalizedName}}Output = {{output}} +type {{capitalizedName}}Inputs = [{{inputs}}]; +type {{capitalizedName}}Output = {{output}}; export class {{capitalizedName}}__factory { From 50eee28f5e1893a6dc006a8ef48c80227d4c14e0 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 10:26:42 -0300 Subject: [PATCH 031/108] Adding placeholder code for reading script `bin` file from disk --- packages/abi-typegen/src/templates/script/factory.hbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/abi-typegen/src/templates/script/factory.hbs b/packages/abi-typegen/src/templates/script/factory.hbs index cd4870abe9d..ec3639249e8 100644 --- a/packages/abi-typegen/src/templates/script/factory.hbs +++ b/packages/abi-typegen/src/templates/script/factory.hbs @@ -34,7 +34,7 @@ type {{capitalizedName}}Output = {{output}}; export class {{capitalizedName}}__factory { static readonly abi: IRawAbi = {} as IRawAbi; - static readonly bin: Uint8Array = {} as Uint8Array; + static readonly bin: Uint8Array = readFileSync('path/to/bin/file'); static createInstance() { From bd52bb4da8b4c9e27e05b4d50d2eee8292d1dcf1 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 10:27:10 -0300 Subject: [PATCH 032/108] Switching variable class name to `ScriptFactory` --- packages/abi-typegen/src/templates/script/factory.hbs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/abi-typegen/src/templates/script/factory.hbs b/packages/abi-typegen/src/templates/script/factory.hbs index ec3639249e8..968b6145f99 100644 --- a/packages/abi-typegen/src/templates/script/factory.hbs +++ b/packages/abi-typegen/src/templates/script/factory.hbs @@ -1,7 +1,7 @@ {{header}} import type { IRawAbi, BigNumberish } from 'fuels'; -import { ScriptCaller } from 'fuels'; +import { ScriptFactory } from 'fuels'; {{#if commonTypesInUse}} import type { {{commonTypesInUse}} } from "./common"; @@ -40,7 +40,7 @@ export class {{capitalizedName}}__factory { const { abi, bin } = {{capitalizedName}}__factory - const script = new ScriptCaller< + const script = new ScriptFactory< {{capitalizedName}}Inputs, {{capitalizedName}}Output >({ abi, bin }); From 49b246e18e18ce2fab9af27198b85b18ed827619 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 10:30:11 -0300 Subject: [PATCH 033/108] Fixing script factory renderer method, gathering prop definitions --- .../abi-typegen/src/templates/script/factory.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/abi-typegen/src/templates/script/factory.ts b/packages/abi-typegen/src/templates/script/factory.ts index 7782d935e32..086667778b3 100644 --- a/packages/abi-typegen/src/templates/script/factory.ts +++ b/packages/abi-typegen/src/templates/script/factory.ts @@ -4,16 +4,26 @@ import { renderHbsTemplate } from '../utils/renderHbsTemplate'; import factoryTemplate from './factory.hbs'; export function renderFactoryTemplate(params: { abi: Abi }) { + const { abi } = params; const { name: capitalizedName, rawContents } = params.abi; + const abiJsonString = JSON.stringify(rawContents, null, 2); + const func = abi.functions.find((f) => f.name === 'main'); + + if (!func) { + throw new Error(`ABI doesn't have a 'main()' method.`); + } + + const { prefixedInputs: inputs, output } = func.attributes; + const text = renderHbsTemplate({ template: factoryTemplate, data: { - capitalizedName, + inputs, + output, abiJsonString, - inputs: '{}', // TODO: fix place holder - output: '{}', // TODO: fix place holder + capitalizedName, }, }); From fd5ba8c94e4c270c27c5979b41275e86058d353f Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 10:30:25 -0300 Subject: [PATCH 034/108] Adding missing import to template --- packages/abi-typegen/src/templates/script/factory.hbs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/abi-typegen/src/templates/script/factory.hbs b/packages/abi-typegen/src/templates/script/factory.hbs index 968b6145f99..e94d006028a 100644 --- a/packages/abi-typegen/src/templates/script/factory.hbs +++ b/packages/abi-typegen/src/templates/script/factory.hbs @@ -1,5 +1,7 @@ {{header}} +import { readFileSync } from 'fs'; + import type { IRawAbi, BigNumberish } from 'fuels'; import { ScriptFactory } from 'fuels'; From bae23f44d36c70152bb9de6565b72015d65ca111 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 10:33:13 -0300 Subject: [PATCH 035/108] Adjusting test fixture --- .../test/fixtures/templates/script/factory.hbs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs index 703d066a90e..2498f59fcf5 100644 --- a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs +++ b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs @@ -9,22 +9,24 @@ Fuel-Core version: 33.33.33 */ +import { readFileSync } from 'fs'; + import type { IRawAbi, BigNumberish } from 'fuels'; -import { ScriptCaller } from 'fuels'; +import { ScriptFactory } from 'fuels'; -type MyContractAbiInputs = {} -type MyContractAbiOutput = {} +type MyContractAbiInputs = [x: string, y: string]; +type MyContractAbiOutput = boolean; export class MyContractAbi__factory { static readonly abi: IRawAbi = {} as IRawAbi; - static readonly bin: Uint8Array = {} as Uint8Array; + static readonly bin: Uint8Array = readFileSync('path/to/bin/file'); static createInstance() { const { abi, bin } = MyContractAbi__factory - const script = new ScriptCaller< + const script = new ScriptFactory< MyContractAbiInputs, MyContractAbiOutput >({ abi, bin }); From d85b30c2241df8481f5b8e6a7a255f81b42b3371 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 12:19:19 -0300 Subject: [PATCH 036/108] Improving test helper, adding extra options --- .../test/utils/getNewAbiTypegen.ts | 71 +++++++++++++------ 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/packages/abi-typegen/test/utils/getNewAbiTypegen.ts b/packages/abi-typegen/test/utils/getNewAbiTypegen.ts index 19379e4896c..c8e9bd557cb 100644 --- a/packages/abi-typegen/test/utils/getNewAbiTypegen.ts +++ b/packages/abi-typegen/test/utils/getNewAbiTypegen.ts @@ -1,4 +1,4 @@ -import type { IFile } from '../../src/index'; +import type { IFile, IRawAbiTypeRoot } from '../../src/index'; import { AbiTypeGen } from '../../src/index'; import { CategoryEnum } from '../../src/interfaces/CategoryEnum'; @@ -6,39 +6,70 @@ export function getNewAbiTypegen( params: { category?: CategoryEnum; includeOptionType?: boolean; + includeMainFunction?: boolean; } = {} ) { - const { includeOptionType = false, category = CategoryEnum.CONTRACT } = params; + const { + includeOptionType = false, + category = CategoryEnum.CONTRACT, + includeMainFunction = false, + } = params; - const optionTypes = [ + const optionType: IRawAbiTypeRoot = { + typeId: 3, + type: 'enum Option', + components: [ + { + name: 'None', + type: 0, + typeArguments: null, + }, + { + name: 'Some', + type: 2, + typeArguments: null, + }, + ], + typeParameters: [2], + }; + + const types: IRawAbiTypeRoot[] = [ { typeId: 1, - type: 'enum Option', - components: [ - { - name: 'None', - type: 0, - typeArguments: null, - }, - { - name: 'Some', - type: 2, - typeArguments: null, - }, - ], - typeParameters: [2], + type: 'u8', + components: null, + typeParameters: null, }, { typeId: 2, - type: 'generic T', + type: 'u16', components: null, typeParameters: null, }, ]; - const types = includeOptionType ? optionTypes : []; + if (includeOptionType) { + types.push(optionType); + } + + const main = { + inputs: [ + { + name: 'params', + type: 1, + typeArguments: null, + }, + ], + name: 'main', + output: { + name: '', + type: 2, + typeArguments: null, + }, + }; + const functions = includeMainFunction ? [main] : []; - const stubAbi = JSON.stringify({ types, functions: [] }, null, 2); + const stubAbi = JSON.stringify({ types, functions }, null, 2); const abiFiles: IFile[] = [ { From c3d518fc1ab909798c538b583a5e4c096267e131 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 12:25:29 -0300 Subject: [PATCH 037/108] Adjusting missing test case --- .../src/templates/script/factory.test.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts index a82b54186ce..93249b41b79 100644 --- a/packages/abi-typegen/src/templates/script/factory.test.ts +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -1,5 +1,7 @@ import { contractPaths } from '../../../test/fixtures'; import factoryTemplate from '../../../test/fixtures/templates/script/factory.hbs'; +import { executeAndCatch } from '../../../test/utils/executeAndCatch'; +import { getNewAbiTypegen } from '../../../test/utils/getNewAbiTypegen'; import { mockVersions } from '../../../test/utils/mockVersions'; import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; import { Abi } from '../../Abi'; @@ -28,4 +30,26 @@ describe('factory.ts', () => { expect(rendered).toEqual(factoryTemplate); }); + + test('should throw for invalid Script ABI', async () => { + const { restore } = mockVersions(); + + const { rawContents } = getNewAbiTypegen({ + includeMainFunction: false, // friction here + }).typegen.abis[0]; + + const abi = new Abi({ + filepath: './my-contract-abi.json', + outputDir: 'stdout', + rawContents, + }); + + const { error } = await executeAndCatch(() => { + renderFactoryTemplate({ abi }); + }); + + expect(error?.message).toMatch(/ABI doesn't have a 'main\(\)' method/); + + restore(); + }); }); From e02a372a7175aa36d2201cb385790ef1e8dd56c8 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 12:25:34 -0300 Subject: [PATCH 038/108] Removing log call --- packages/abi-typegen/src/templates/script/factory.test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts index 93249b41b79..e67f342c908 100644 --- a/packages/abi-typegen/src/templates/script/factory.test.ts +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -26,8 +26,6 @@ describe('factory.ts', () => { restore(); - // console.log(rendered); - expect(rendered).toEqual(factoryTemplate); }); From 69ed22520757f73011941f998dcd9ecb01e3a894 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 12:31:07 -0300 Subject: [PATCH 039/108] =?UTF-8?q?Fixing=20test,=20dealing=20with=20mocke?= =?UTF-8?q?d=20instances=E2=80=99=20state?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/assemblers/assembleScripts.test.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/abi-typegen/src/assemblers/assembleScripts.test.ts b/packages/abi-typegen/src/assemblers/assembleScripts.test.ts index 5747e37f533..a3de08bde84 100644 --- a/packages/abi-typegen/src/assemblers/assembleScripts.test.ts +++ b/packages/abi-typegen/src/assemblers/assembleScripts.test.ts @@ -8,8 +8,6 @@ import { assembleScripts } from './assembleScripts'; describe('assembleScripts.ts', () => { function mockAllDeps() { - jest.resetAllMocks(); - const renderCommonTemplate = jest .spyOn(renderCommonTemplateMod, 'renderCommonTemplate') .mockImplementation(); @@ -29,15 +27,21 @@ describe('assembleScripts.ts', () => { }; } + beforeEach(jest.resetAllMocks); + afterEach(jest.restoreAllMocks); + test('should assemble all files from Script ABI ', () => { + const { renderCommonTemplate, renderFactoryTemplate, renderIndexTemplate } = mockAllDeps(); + const { typegen: { abis, outputDir }, } = getNewAbiTypegen({ category: CategoryEnum.SCRIPT, includeOptionType: false, // will prevent common template from being included + // includeMainFunction: true, // scripts must have at least one `main()` method }); - const { renderCommonTemplate, renderFactoryTemplate, renderIndexTemplate } = mockAllDeps(); + jest.resetAllMocks(); const files = assembleScripts({ abis, outputDir }); @@ -49,14 +53,17 @@ describe('assembleScripts.ts', () => { }); test('should assemble all files from Script ABI, including `common` file', () => { + const { renderCommonTemplate, renderFactoryTemplate, renderIndexTemplate } = mockAllDeps(); + const { typegen: { abis, outputDir }, } = getNewAbiTypegen({ category: CategoryEnum.SCRIPT, includeOptionType: true, // will cause common template to be included + // includeMainFunction: true, // scripts must have at least one `main()` method }); - const { renderCommonTemplate, renderFactoryTemplate, renderIndexTemplate } = mockAllDeps(); + jest.resetAllMocks(); const files = assembleScripts({ abis, outputDir }); From 3765c9a2e0dba53ff1f3428c7fda398297d70ff9 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 12:37:12 -0300 Subject: [PATCH 040/108] Removing duplicates, moving templates around --- .../src/assemblers/assembleContracts.test.ts | 2 +- .../src/assemblers/assembleContracts.ts | 2 +- .../src/assemblers/assembleScripts.test.ts | 2 +- .../abi-typegen/src/assemblers/assembleScripts.ts | 2 +- .../src/templates/{contract => common}/index.hbs | 0 .../templates/{contract => common}/index.test.ts | 2 +- .../src/templates/{contract => common}/index.ts | 0 .../abi-typegen/src/templates/script/index.hbs | 5 ----- .../src/templates/script/index.test.ts | 7 ------- .../abi-typegen/src/templates/script/index.ts | 15 --------------- 10 files changed, 5 insertions(+), 32 deletions(-) rename packages/abi-typegen/src/templates/{contract => common}/index.hbs (100%) rename packages/abi-typegen/src/templates/{contract => common}/index.test.ts (94%) rename packages/abi-typegen/src/templates/{contract => common}/index.ts (100%) delete mode 100644 packages/abi-typegen/src/templates/script/index.hbs delete mode 100644 packages/abi-typegen/src/templates/script/index.test.ts delete mode 100644 packages/abi-typegen/src/templates/script/index.ts diff --git a/packages/abi-typegen/src/assemblers/assembleContracts.test.ts b/packages/abi-typegen/src/assemblers/assembleContracts.test.ts index 633612b8a3d..3ddd93ace3d 100644 --- a/packages/abi-typegen/src/assemblers/assembleContracts.test.ts +++ b/packages/abi-typegen/src/assemblers/assembleContracts.test.ts @@ -2,7 +2,7 @@ import { getNewAbiTypegen } from '../../test/utils/getNewAbiTypegen'; import { CategoryEnum } from '../interfaces/CategoryEnum'; import * as renderCommonTemplateMod from '../templates/common/common'; import * as renderFactoryTemplateMod from '../templates/contract/factory'; -import * as renderIndexTemplateMod from '../templates/contract/index'; +import * as renderIndexTemplateMod from '../templates/common/index'; import { assembleContracts } from './assembleContracts'; diff --git a/packages/abi-typegen/src/assemblers/assembleContracts.ts b/packages/abi-typegen/src/assemblers/assembleContracts.ts index 64394de7459..97fff569be6 100644 --- a/packages/abi-typegen/src/assemblers/assembleContracts.ts +++ b/packages/abi-typegen/src/assemblers/assembleContracts.ts @@ -5,7 +5,7 @@ import type { IFile } from '../index'; import { renderCommonTemplate } from '../templates/common/common'; import { renderDtsTemplate } from '../templates/contract/dts'; import { renderFactoryTemplate } from '../templates/contract/factory'; -import { renderIndexTemplate } from '../templates/contract/index'; +import { renderIndexTemplate } from '../templates/common/index'; /** * Render all Contract-related templates and returns diff --git a/packages/abi-typegen/src/assemblers/assembleScripts.test.ts b/packages/abi-typegen/src/assemblers/assembleScripts.test.ts index a3de08bde84..09ad4550f7f 100644 --- a/packages/abi-typegen/src/assemblers/assembleScripts.test.ts +++ b/packages/abi-typegen/src/assemblers/assembleScripts.test.ts @@ -1,8 +1,8 @@ import { getNewAbiTypegen } from '../../test/utils/getNewAbiTypegen'; import { CategoryEnum } from '../interfaces/CategoryEnum'; import * as renderCommonTemplateMod from '../templates/common/common'; +import * as renderIndexTemplateMod from '../templates/common/index'; import * as renderFactoryTemplateMod from '../templates/script/factory'; -import * as renderIndexTemplateMod from '../templates/script/index'; import { assembleScripts } from './assembleScripts'; diff --git a/packages/abi-typegen/src/assemblers/assembleScripts.ts b/packages/abi-typegen/src/assemblers/assembleScripts.ts index 85eb02b70fb..77f27f1035e 100644 --- a/packages/abi-typegen/src/assemblers/assembleScripts.ts +++ b/packages/abi-typegen/src/assemblers/assembleScripts.ts @@ -3,8 +3,8 @@ import { join } from 'path'; import type { Abi } from '../Abi'; import type { IFile } from '../index'; import { renderCommonTemplate } from '../templates/common/common'; +import { renderIndexTemplate } from '../templates/common/index'; import { renderFactoryTemplate } from '../templates/script/factory'; -import { renderIndexTemplate } from '../templates/script/index'; /** * Render all Script-related templates and returns diff --git a/packages/abi-typegen/src/templates/contract/index.hbs b/packages/abi-typegen/src/templates/common/index.hbs similarity index 100% rename from packages/abi-typegen/src/templates/contract/index.hbs rename to packages/abi-typegen/src/templates/common/index.hbs diff --git a/packages/abi-typegen/src/templates/contract/index.test.ts b/packages/abi-typegen/src/templates/common/index.test.ts similarity index 94% rename from packages/abi-typegen/src/templates/contract/index.test.ts rename to packages/abi-typegen/src/templates/common/index.test.ts index a78780d7015..4615e17381b 100644 --- a/packages/abi-typegen/src/templates/contract/index.test.ts +++ b/packages/abi-typegen/src/templates/common/index.test.ts @@ -4,7 +4,7 @@ import { mockVersions } from '../../../test/utils/mockVersions'; import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; import { Abi } from '../../Abi'; -import { renderIndexTemplate } from '.'; +import { renderIndexTemplate } from './index'; describe('templates/index', () => { test('should render index template', () => { diff --git a/packages/abi-typegen/src/templates/contract/index.ts b/packages/abi-typegen/src/templates/common/index.ts similarity index 100% rename from packages/abi-typegen/src/templates/contract/index.ts rename to packages/abi-typegen/src/templates/common/index.ts diff --git a/packages/abi-typegen/src/templates/script/index.hbs b/packages/abi-typegen/src/templates/script/index.hbs deleted file mode 100644 index 94826241d48..00000000000 --- a/packages/abi-typegen/src/templates/script/index.hbs +++ /dev/null @@ -1,5 +0,0 @@ -{{header}} - -{{#each abiCapitalizedNames}} -export { {{this}}__factory } from './factories/{{this}}__factory'; -{{/each}} diff --git a/packages/abi-typegen/src/templates/script/index.test.ts b/packages/abi-typegen/src/templates/script/index.test.ts deleted file mode 100644 index 578d535c935..00000000000 --- a/packages/abi-typegen/src/templates/script/index.test.ts +++ /dev/null @@ -1,7 +0,0 @@ -// TODO: Implement test -describe('', () => { - test('', () => { - // TODO: implement test - expect(true).toBeTruthy(); - }); -}); diff --git a/packages/abi-typegen/src/templates/script/index.ts b/packages/abi-typegen/src/templates/script/index.ts deleted file mode 100644 index b0572a6a467..00000000000 --- a/packages/abi-typegen/src/templates/script/index.ts +++ /dev/null @@ -1,15 +0,0 @@ -import type { Abi } from '../../Abi'; -import { renderHbsTemplate } from '../utils/renderHbsTemplate'; - -import indexTemplate from './index.hbs'; - -export function renderIndexTemplate(params: { abis: Abi[] }) { - const abiCapitalizedNames = params.abis.map(({ name }) => name); - - const text = renderHbsTemplate({ - template: indexTemplate, - data: { abiCapitalizedNames }, - }); - - return text; -} From fe5d2bbd5eca71a38dfc22a9013a4706aa6b82f1 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 12:45:04 -0300 Subject: [PATCH 041/108] Lintfix --- packages/abi-typegen/src/assemblers/assembleContracts.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/abi-typegen/src/assemblers/assembleContracts.test.ts b/packages/abi-typegen/src/assemblers/assembleContracts.test.ts index 3ddd93ace3d..2fedfe70867 100644 --- a/packages/abi-typegen/src/assemblers/assembleContracts.test.ts +++ b/packages/abi-typegen/src/assemblers/assembleContracts.test.ts @@ -1,8 +1,8 @@ import { getNewAbiTypegen } from '../../test/utils/getNewAbiTypegen'; import { CategoryEnum } from '../interfaces/CategoryEnum'; import * as renderCommonTemplateMod from '../templates/common/common'; -import * as renderFactoryTemplateMod from '../templates/contract/factory'; import * as renderIndexTemplateMod from '../templates/common/index'; +import * as renderFactoryTemplateMod from '../templates/contract/factory'; import { assembleContracts } from './assembleContracts'; From 3871de19ebf79bfe3a9fb66f648ee1097a18afa9 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 13:15:28 -0300 Subject: [PATCH 042/108] Tidying up, lint fixing, spell checking, remodeling directory structure --- .vscode/settings.json | 11 ++++++- packages/abi-typegen/src/AbiTypeGen.test.ts | 8 ++--- packages/abi-typegen/src/AbiTypeGen.ts | 10 +++--- .../abi-typegen/src/{ => abi}/Abi.test.ts | 12 +++---- packages/abi-typegen/src/{ => abi}/Abi.ts | 12 +++---- .../src/{ => abi}/functions/Function.test.ts | 6 ++-- .../src/{ => abi}/functions/Function.ts | 11 +++---- .../abi-typegen/src/{ => abi}/types/AType.ts | 4 +-- .../src/{ => abi}/types/ArrayType.test.ts | 12 +++---- .../src/{ => abi}/types/ArrayType.ts | 8 ++--- .../src/{ => abi}/types/B256Type.test.ts | 0 .../src/{ => abi}/types/B256Type.ts | 0 .../src/{ => abi}/types/B512Type.test.ts | 0 .../src/{ => abi}/types/B512Type.ts | 0 .../src/{ => abi}/types/BoolType.test.ts | 0 .../src/{ => abi}/types/BoolType.ts | 2 +- .../src/{ => abi}/types/EnumType.test.ts | 12 +++---- .../src/{ => abi}/types/EnumType.ts | 10 +++--- .../src/{ => abi}/types/GenericType.test.ts | 0 .../src/{ => abi}/types/GenericType.ts | 4 +-- .../src/{ => abi}/types/OptionType.test.ts | 10 +++--- .../src/{ => abi}/types/OptionType.ts | 2 +- .../src/{ => abi}/types/RawUntypedPtr.test.ts | 0 .../src/{ => abi}/types/RawUntypedPtr.ts | 2 +- .../src/{ => abi}/types/StrType.test.ts | 0 .../src/{ => abi}/types/StrType.ts | 2 +- .../src/{ => abi}/types/StructType.test.ts | 14 ++++---- .../src/{ => abi}/types/StructType.ts | 12 +++---- .../src/{ => abi}/types/TupleType.test.ts | 12 +++---- .../src/{ => abi}/types/TupleType.ts | 8 ++--- .../src/{ => abi}/types/U16Type.test.ts | 0 .../src/{ => abi}/types/U16Type.ts | 2 +- .../src/{ => abi}/types/U32Type.test.ts | 0 .../src/{ => abi}/types/U32Type.ts | 2 +- .../src/{ => abi}/types/U64Type.test.ts | 0 .../src/{ => abi}/types/U64Type.ts | 2 +- .../src/{ => abi}/types/U8Type.test.ts | 0 .../abi-typegen/src/{ => abi}/types/U8Type.ts | 4 +-- .../src/{ => abi}/types/VectorType.test.ts | 0 .../src/{ => abi}/types/VectorType.ts | 2 +- packages/abi-typegen/src/cli.test.ts | 3 +- packages/abi-typegen/src/cli.ts | 2 +- packages/abi-typegen/src/index.ts | 12 +++---- packages/abi-typegen/src/runTypegen.test.ts | 2 +- packages/abi-typegen/src/runTypegen.ts | 4 +-- .../src/templates/common/common.ts | 2 +- .../src/templates/common/index.test.ts | 2 +- .../abi-typegen/src/templates/common/index.ts | 4 +-- .../src/templates/contract/dts.test.ts | 2 +- .../abi-typegen/src/templates/contract/dts.ts | 10 +++--- .../src/templates/contract/factory.test.ts | 2 +- .../src/templates/contract/factory.ts | 4 +-- .../{utils => }/renderHbsTemplate.test.ts | 2 +- .../{utils => }/renderHbsTemplate.ts | 2 +- .../src/templates/script/factory.test.ts | 2 +- .../src/templates/script/factory.ts | 4 +-- .../enums}/CategoryEnum.ts | 0 .../{interfaces => types/enums}/TargetEnum.ts | 0 .../src/{ => types}/interfaces/IFile.ts | 0 .../src/{ => types}/interfaces/IFunction.ts | 0 .../src/{ => types}/interfaces/IRawAbi.ts | 0 .../{ => types}/interfaces/IRawAbiFunction.ts | 0 .../interfaces/IRawAbiLoggedTypes.ts | 0 .../src/{ => types}/interfaces/IRawAbiType.ts | 0 .../src/{ => types}/interfaces/IType.ts | 0 .../assembleContracts.test.ts | 2 +- .../assembleContracts.ts | 4 +-- .../assembleScripts.test.ts | 2 +- .../{assemblers => utils}/assembleScripts.ts | 2 +- .../src/utils/extractStructName.test.ts | 8 ++--- .../src/utils/extractStructName.ts | 2 +- .../abi-typegen/src/utils/findType.test.ts | 4 +-- packages/abi-typegen/src/utils/findType.ts | 2 +- .../src/utils/makeFunction.test.ts | 6 ++-- .../abi-typegen/src/utils/makeFunction.ts | 6 ++-- .../abi-typegen/src/utils/makeType.test.ts | 2 +- packages/abi-typegen/src/utils/makeType.ts | 2 +- .../src/utils/parseFunctions.test.ts | 6 ++-- .../abi-typegen/src/utils/parseFunctions.ts | 6 ++-- .../src/utils/parseTypeArguments.test.ts | 4 +-- .../src/utils/parseTypeArguments.ts | 6 ++-- .../abi-typegen/src/utils/parseTypes.test.ts | 2 +- packages/abi-typegen/src/utils/parseTypes.ts | 4 +-- .../abi-typegen/src/utils/supportedTypes.ts | 32 +++++++++---------- .../test/utils/getNewAbiTypegen.ts | 2 +- .../test/utils/sway/compileSwayToJson.ts | 2 +- .../test/utils/sway/compileSwayToTs.ts | 9 ++---- packages/address/src/address.ts | 2 +- 88 files changed, 188 insertions(+), 184 deletions(-) rename packages/abi-typegen/src/{ => abi}/Abi.test.ts (87%) rename packages/abi-typegen/src/{ => abi}/Abi.ts (83%) rename packages/abi-typegen/src/{ => abi}/functions/Function.test.ts (91%) rename packages/abi-typegen/src/{ => abi}/functions/Function.ts (83%) rename packages/abi-typegen/src/{ => abi}/types/AType.ts (67%) rename packages/abi-typegen/src/{ => abi}/types/ArrayType.test.ts (84%) rename packages/abi-typegen/src/{ => abi}/types/ArrayType.ts (89%) rename packages/abi-typegen/src/{ => abi}/types/B256Type.test.ts (100%) rename packages/abi-typegen/src/{ => abi}/types/B256Type.ts (100%) rename packages/abi-typegen/src/{ => abi}/types/B512Type.test.ts (100%) rename packages/abi-typegen/src/{ => abi}/types/B512Type.ts (100%) rename packages/abi-typegen/src/{ => abi}/types/BoolType.test.ts (100%) rename packages/abi-typegen/src/{ => abi}/types/BoolType.ts (89%) rename packages/abi-typegen/src/{ => abi}/types/EnumType.test.ts (91%) rename packages/abi-typegen/src/{ => abi}/types/EnumType.ts (84%) rename packages/abi-typegen/src/{ => abi}/types/GenericType.test.ts (100%) rename packages/abi-typegen/src/{ => abi}/types/GenericType.ts (85%) rename packages/abi-typegen/src/{ => abi}/types/OptionType.test.ts (79%) rename packages/abi-typegen/src/{ => abi}/types/OptionType.ts (89%) rename packages/abi-typegen/src/{ => abi}/types/RawUntypedPtr.test.ts (100%) rename packages/abi-typegen/src/{ => abi}/types/RawUntypedPtr.ts (85%) rename packages/abi-typegen/src/{ => abi}/types/StrType.test.ts (100%) rename packages/abi-typegen/src/{ => abi}/types/StrType.ts (90%) rename packages/abi-typegen/src/{ => abi}/types/StructType.test.ts (85%) rename packages/abi-typegen/src/{ => abi}/types/StructType.ts (86%) rename packages/abi-typegen/src/{ => abi}/types/TupleType.test.ts (81%) rename packages/abi-typegen/src/{ => abi}/types/TupleType.ts (87%) rename packages/abi-typegen/src/{ => abi}/types/U16Type.test.ts (100%) rename packages/abi-typegen/src/{ => abi}/types/U16Type.ts (83%) rename packages/abi-typegen/src/{ => abi}/types/U32Type.test.ts (100%) rename packages/abi-typegen/src/{ => abi}/types/U32Type.ts (83%) rename packages/abi-typegen/src/{ => abi}/types/U64Type.test.ts (100%) rename packages/abi-typegen/src/{ => abi}/types/U64Type.ts (89%) rename packages/abi-typegen/src/{ => abi}/types/U8Type.test.ts (100%) rename packages/abi-typegen/src/{ => abi}/types/U8Type.ts (83%) rename packages/abi-typegen/src/{ => abi}/types/VectorType.test.ts (100%) rename packages/abi-typegen/src/{ => abi}/types/VectorType.ts (91%) rename packages/abi-typegen/src/templates/{utils => }/renderHbsTemplate.test.ts (93%) rename packages/abi-typegen/src/templates/{utils => }/renderHbsTemplate.ts (93%) rename packages/abi-typegen/src/{interfaces => types/enums}/CategoryEnum.ts (100%) rename packages/abi-typegen/src/{interfaces => types/enums}/TargetEnum.ts (100%) rename packages/abi-typegen/src/{ => types}/interfaces/IFile.ts (100%) rename packages/abi-typegen/src/{ => types}/interfaces/IFunction.ts (100%) rename packages/abi-typegen/src/{ => types}/interfaces/IRawAbi.ts (100%) rename packages/abi-typegen/src/{ => types}/interfaces/IRawAbiFunction.ts (100%) rename packages/abi-typegen/src/{ => types}/interfaces/IRawAbiLoggedTypes.ts (100%) rename packages/abi-typegen/src/{ => types}/interfaces/IRawAbiType.ts (100%) rename packages/abi-typegen/src/{ => types}/interfaces/IType.ts (100%) rename packages/abi-typegen/src/{assemblers => utils}/assembleContracts.test.ts (97%) rename packages/abi-typegen/src/{assemblers => utils}/assembleContracts.ts (97%) rename packages/abi-typegen/src/{assemblers => utils}/assembleScripts.test.ts (97%) rename packages/abi-typegen/src/{assemblers => utils}/assembleScripts.ts (97%) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1ed1ded9d5e..a1c30c94133 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -22,5 +22,14 @@ "[handlebars]": { "editor.formatOnSave": false, "editor.formatOnPaste": false - } + }, + "cSpell.words": [ + "arrayify", + "ethersproject", + "Numberish", + "struct", + "structs", + "Typedefs", + "Typegen" + ] } diff --git a/packages/abi-typegen/src/AbiTypeGen.test.ts b/packages/abi-typegen/src/AbiTypeGen.test.ts index 11dd58222b7..907871a6ab1 100644 --- a/packages/abi-typegen/src/AbiTypeGen.test.ts +++ b/packages/abi-typegen/src/AbiTypeGen.test.ts @@ -1,12 +1,12 @@ import { executeAndCatch } from '../test/utils/executeAndCatch'; import { getNewAbiTypegen } from '../test/utils/getNewAbiTypegen'; -import * as assembleContractsMod from './assemblers/assembleContracts'; -import * as assembleScriptsMod from './assemblers/assembleScripts'; -import { CategoryEnum } from './interfaces/CategoryEnum'; +import { CategoryEnum } from './types/enums/CategoryEnum'; +import * as assembleContractsMod from './utils/assembleContracts'; +import * as assembleScriptsMod from './utils/assembleScripts'; describe('AbiTypegen.ts', () => { - // Use as example of HORRIBLE auto-code-formatting + // Use as e sample of HORRIBLE auto-code-formatting function mockAllDeps() { const assembleContracts = jest .spyOn(assembleContractsMod, 'assembleContracts') diff --git a/packages/abi-typegen/src/AbiTypeGen.ts b/packages/abi-typegen/src/AbiTypeGen.ts index fa836b26d6c..45ec5fe5fe8 100644 --- a/packages/abi-typegen/src/AbiTypeGen.ts +++ b/packages/abi-typegen/src/AbiTypeGen.ts @@ -1,8 +1,8 @@ -import { Abi } from './Abi'; -import { assembleContracts } from './assemblers/assembleContracts'; -import { assembleScripts } from './assemblers/assembleScripts'; -import { CategoryEnum } from './interfaces/CategoryEnum'; -import type { IFile } from './interfaces/IFile'; +import { Abi } from './abi/Abi'; +import { CategoryEnum } from './types/enums/CategoryEnum'; +import type { IFile } from './types/interfaces/IFile'; +import { assembleContracts } from './utils/assembleContracts'; +import { assembleScripts } from './utils/assembleScripts'; /* Manages many instances of Abi diff --git a/packages/abi-typegen/src/Abi.test.ts b/packages/abi-typegen/src/abi/Abi.test.ts similarity index 87% rename from packages/abi-typegen/src/Abi.test.ts rename to packages/abi-typegen/src/abi/Abi.test.ts index b424c55ba08..2525f22c344 100644 --- a/packages/abi-typegen/src/Abi.test.ts +++ b/packages/abi-typegen/src/abi/Abi.test.ts @@ -1,14 +1,14 @@ -import { contractPaths } from '../test/fixtures/index'; -import { executeAndCatch } from '../test/utils/executeAndCatch'; -import { compileSwayToJson } from '../test/utils/sway/compileSwayToJson'; +import { contractPaths } from '../../test/fixtures/index'; +import { executeAndCatch } from '../../test/utils/executeAndCatch'; +import { compileSwayToJson } from '../../test/utils/sway/compileSwayToJson'; +import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType'; +import * as parseFunctionsMod from '../utils/parseFunctions'; +import * as parseTypesMod from '../utils/parseTypes'; import { Abi } from './Abi'; -import type { IRawAbiTypeRoot } from './interfaces/IRawAbiType'; import { EnumType } from './types/EnumType'; import { OptionType } from './types/OptionType'; import { VectorType } from './types/VectorType'; -import * as parseFunctionsMod from './utils/parseFunctions'; -import * as parseTypesMod from './utils/parseTypes'; describe('Abi.ts', () => { /* diff --git a/packages/abi-typegen/src/Abi.ts b/packages/abi-typegen/src/abi/Abi.ts similarity index 83% rename from packages/abi-typegen/src/Abi.ts rename to packages/abi-typegen/src/abi/Abi.ts index 5f607ceb9e6..c6a08faae14 100644 --- a/packages/abi-typegen/src/Abi.ts +++ b/packages/abi-typegen/src/abi/Abi.ts @@ -1,9 +1,9 @@ -import type { IFunction } from './interfaces/IFunction'; -import type { IRawAbi } from './interfaces/IRawAbi'; -import type { IType } from './interfaces/IType'; -import { normalizeName } from './utils/normalize'; -import { parseFunctions } from './utils/parseFunctions'; -import { parseTypes } from './utils/parseTypes'; +import type { IFunction } from '../types/interfaces/IFunction'; +import type { IRawAbi } from '../types/interfaces/IRawAbi'; +import type { IType } from '../types/interfaces/IType'; +import { normalizeName } from '../utils/normalize'; +import { parseFunctions } from '../utils/parseFunctions'; +import { parseTypes } from '../utils/parseTypes'; /* Manages many instances of Types and Functions diff --git a/packages/abi-typegen/src/functions/Function.test.ts b/packages/abi-typegen/src/abi/functions/Function.test.ts similarity index 91% rename from packages/abi-typegen/src/functions/Function.test.ts rename to packages/abi-typegen/src/abi/functions/Function.test.ts index f4dc670596b..cc58310c323 100644 --- a/packages/abi-typegen/src/functions/Function.test.ts +++ b/packages/abi-typegen/src/abi/functions/Function.test.ts @@ -1,6 +1,6 @@ -import { contractPaths } from '../../test/fixtures'; -import { compileSwayToJson } from '../../test/utils/sway/compileSwayToJson'; -import { parseTypes } from '../utils/parseTypes'; +import { contractPaths } from '../../../test/fixtures'; +import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import { parseTypes } from '../../utils/parseTypes'; import { Function } from './Function'; diff --git a/packages/abi-typegen/src/functions/Function.ts b/packages/abi-typegen/src/abi/functions/Function.ts similarity index 83% rename from packages/abi-typegen/src/functions/Function.ts rename to packages/abi-typegen/src/abi/functions/Function.ts index 00bfd1bc00b..f6ad8e87925 100644 --- a/packages/abi-typegen/src/functions/Function.ts +++ b/packages/abi-typegen/src/abi/functions/Function.ts @@ -1,9 +1,8 @@ -import type { IFunction, IFunctionAttributes } from '../interfaces/IFunction'; -import type { IRawAbiFunction } from '../interfaces/IRawAbiFunction'; -import type { IType } from '../interfaces/IType'; -import { TargetEnum } from '../interfaces/TargetEnum'; -import { findType } from '../utils/findType'; -import { parseTypeArguments } from '../utils/parseTypeArguments'; +import type { IFunction, IRawAbiFunction, IFunctionAttributes } from '../../index'; +import { TargetEnum } from '../../types/enums/TargetEnum'; +import type { IType } from '../../types/interfaces/IType'; +import { findType } from '../../utils/findType'; +import { parseTypeArguments } from '../../utils/parseTypeArguments'; export class Function implements IFunction { public name: string; diff --git a/packages/abi-typegen/src/types/AType.ts b/packages/abi-typegen/src/abi/types/AType.ts similarity index 67% rename from packages/abi-typegen/src/types/AType.ts rename to packages/abi-typegen/src/abi/types/AType.ts index ce536a982a6..eb5e74804d0 100644 --- a/packages/abi-typegen/src/types/AType.ts +++ b/packages/abi-typegen/src/abi/types/AType.ts @@ -1,5 +1,5 @@ -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; -import type { ITypeAttributes } from '../interfaces/IType'; +import type { IRawAbiTypeRoot } from '../../types/interfaces/IRawAbiType'; +import type { ITypeAttributes } from '../../types/interfaces/IType'; export class AType { public rawAbiType: IRawAbiTypeRoot; diff --git a/packages/abi-typegen/src/types/ArrayType.test.ts b/packages/abi-typegen/src/abi/types/ArrayType.test.ts similarity index 84% rename from packages/abi-typegen/src/types/ArrayType.test.ts rename to packages/abi-typegen/src/abi/types/ArrayType.test.ts index 0d2491518a7..bb1e4113fb5 100644 --- a/packages/abi-typegen/src/types/ArrayType.test.ts +++ b/packages/abi-typegen/src/abi/types/ArrayType.test.ts @@ -1,9 +1,9 @@ -import { contractPaths } from '../../test/fixtures'; -import { compileSwayToJson } from '../../test/utils/sway/compileSwayToJson'; -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; -import { findType } from '../utils/findType'; -import { makeType } from '../utils/makeType'; -import * as parseTypeArgumentsMod from '../utils/parseTypeArguments'; +import { contractPaths } from '../../../test/fixtures'; +import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import type { IRawAbiTypeRoot } from '../../types/interfaces/IRawAbiType'; +import { findType } from '../../utils/findType'; +import { makeType } from '../../utils/makeType'; +import * as parseTypeArgumentsMod from '../../utils/parseTypeArguments'; import { ArrayType } from './ArrayType'; import { TupleType } from './TupleType'; diff --git a/packages/abi-typegen/src/types/ArrayType.ts b/packages/abi-typegen/src/abi/types/ArrayType.ts similarity index 89% rename from packages/abi-typegen/src/types/ArrayType.ts rename to packages/abi-typegen/src/abi/types/ArrayType.ts index b8d33fdfd0f..fe67abd249e 100644 --- a/packages/abi-typegen/src/types/ArrayType.ts +++ b/packages/abi-typegen/src/abi/types/ArrayType.ts @@ -1,7 +1,7 @@ -import type { IType } from '../interfaces/IType'; -import { TargetEnum } from '../interfaces/TargetEnum'; -import { findType } from '../utils/findType'; -import { parseTypeArguments } from '../utils/parseTypeArguments'; +import { TargetEnum } from '../../types/enums/TargetEnum'; +import type { IType } from '../../types/interfaces/IType'; +import { findType } from '../../utils/findType'; +import { parseTypeArguments } from '../../utils/parseTypeArguments'; import { AType } from './AType'; diff --git a/packages/abi-typegen/src/types/B256Type.test.ts b/packages/abi-typegen/src/abi/types/B256Type.test.ts similarity index 100% rename from packages/abi-typegen/src/types/B256Type.test.ts rename to packages/abi-typegen/src/abi/types/B256Type.test.ts diff --git a/packages/abi-typegen/src/types/B256Type.ts b/packages/abi-typegen/src/abi/types/B256Type.ts similarity index 100% rename from packages/abi-typegen/src/types/B256Type.ts rename to packages/abi-typegen/src/abi/types/B256Type.ts diff --git a/packages/abi-typegen/src/types/B512Type.test.ts b/packages/abi-typegen/src/abi/types/B512Type.test.ts similarity index 100% rename from packages/abi-typegen/src/types/B512Type.test.ts rename to packages/abi-typegen/src/abi/types/B512Type.test.ts diff --git a/packages/abi-typegen/src/types/B512Type.ts b/packages/abi-typegen/src/abi/types/B512Type.ts similarity index 100% rename from packages/abi-typegen/src/types/B512Type.ts rename to packages/abi-typegen/src/abi/types/B512Type.ts diff --git a/packages/abi-typegen/src/types/BoolType.test.ts b/packages/abi-typegen/src/abi/types/BoolType.test.ts similarity index 100% rename from packages/abi-typegen/src/types/BoolType.test.ts rename to packages/abi-typegen/src/abi/types/BoolType.test.ts diff --git a/packages/abi-typegen/src/types/BoolType.ts b/packages/abi-typegen/src/abi/types/BoolType.ts similarity index 89% rename from packages/abi-typegen/src/types/BoolType.ts rename to packages/abi-typegen/src/abi/types/BoolType.ts index cc4b28e32c5..23cc7fb49fc 100644 --- a/packages/abi-typegen/src/types/BoolType.ts +++ b/packages/abi-typegen/src/abi/types/BoolType.ts @@ -1,4 +1,4 @@ -import type { IType } from '../interfaces/IType'; +import type { IType } from '../../types/interfaces/IType'; import { AType } from './AType'; diff --git a/packages/abi-typegen/src/types/EnumType.test.ts b/packages/abi-typegen/src/abi/types/EnumType.test.ts similarity index 91% rename from packages/abi-typegen/src/types/EnumType.test.ts rename to packages/abi-typegen/src/abi/types/EnumType.test.ts index c3e4a46232e..6bf9d4defb1 100644 --- a/packages/abi-typegen/src/types/EnumType.test.ts +++ b/packages/abi-typegen/src/abi/types/EnumType.test.ts @@ -1,9 +1,9 @@ -import { contractPaths } from '../../test/fixtures'; -import { compileSwayToJson } from '../../test/utils/sway/compileSwayToJson'; -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; -import { TargetEnum } from '../interfaces/TargetEnum'; -import { findType } from '../utils/findType'; -import { makeType } from '../utils/makeType'; +import { contractPaths } from '../../../test/fixtures'; +import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import { TargetEnum } from '../../types/enums/TargetEnum'; +import type { IRawAbiTypeRoot } from '../../types/interfaces/IRawAbiType'; +import { findType } from '../../utils/findType'; +import { makeType } from '../../utils/makeType'; import type { EnumType } from './EnumType'; import { StructType } from './StructType'; diff --git a/packages/abi-typegen/src/types/EnumType.ts b/packages/abi-typegen/src/abi/types/EnumType.ts similarity index 84% rename from packages/abi-typegen/src/types/EnumType.ts rename to packages/abi-typegen/src/abi/types/EnumType.ts index 38f459bce02..ced7619ff0e 100644 --- a/packages/abi-typegen/src/types/EnumType.ts +++ b/packages/abi-typegen/src/abi/types/EnumType.ts @@ -1,8 +1,8 @@ -import type { IRawAbiTypeComponent } from '../interfaces/IRawAbiType'; -import type { IType } from '../interfaces/IType'; -import type { TargetEnum } from '../interfaces/TargetEnum'; -import { extractStructName } from '../utils/extractStructName'; -import { findType } from '../utils/findType'; +import type { IRawAbiTypeComponent } from '../../index'; +import type { TargetEnum } from '../../types/enums/TargetEnum'; +import type { IType } from '../../types/interfaces/IType'; +import { extractStructName } from '../../utils/extractStructName'; +import { findType } from '../../utils/findType'; import { AType } from './AType'; diff --git a/packages/abi-typegen/src/types/GenericType.test.ts b/packages/abi-typegen/src/abi/types/GenericType.test.ts similarity index 100% rename from packages/abi-typegen/src/types/GenericType.test.ts rename to packages/abi-typegen/src/abi/types/GenericType.test.ts diff --git a/packages/abi-typegen/src/types/GenericType.ts b/packages/abi-typegen/src/abi/types/GenericType.ts similarity index 85% rename from packages/abi-typegen/src/types/GenericType.ts rename to packages/abi-typegen/src/abi/types/GenericType.ts index a4625c82c90..7dc5e64c43d 100644 --- a/packages/abi-typegen/src/types/GenericType.ts +++ b/packages/abi-typegen/src/abi/types/GenericType.ts @@ -1,5 +1,5 @@ -import type { IType } from '../interfaces/IType'; -import { extractStructName } from '../utils/extractStructName'; +import type { IType } from '../../types/interfaces/IType'; +import { extractStructName } from '../../utils/extractStructName'; import { AType } from './AType'; diff --git a/packages/abi-typegen/src/types/OptionType.test.ts b/packages/abi-typegen/src/abi/types/OptionType.test.ts similarity index 79% rename from packages/abi-typegen/src/types/OptionType.test.ts rename to packages/abi-typegen/src/abi/types/OptionType.test.ts index 062b1db2a3e..b5241cda138 100644 --- a/packages/abi-typegen/src/types/OptionType.test.ts +++ b/packages/abi-typegen/src/abi/types/OptionType.test.ts @@ -1,8 +1,8 @@ -import { contractPaths } from '../../test/fixtures'; -import { compileSwayToJson } from '../../test/utils/sway/compileSwayToJson'; -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; -import { findType } from '../utils/findType'; -import { makeType } from '../utils/makeType'; +import { contractPaths } from '../../../test/fixtures'; +import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import type { IRawAbiTypeRoot } from '../../index'; +import { findType } from '../../utils/findType'; +import { makeType } from '../../utils/makeType'; import { EnumType } from './EnumType'; import { OptionType } from './OptionType'; diff --git a/packages/abi-typegen/src/types/OptionType.ts b/packages/abi-typegen/src/abi/types/OptionType.ts similarity index 89% rename from packages/abi-typegen/src/types/OptionType.ts rename to packages/abi-typegen/src/abi/types/OptionType.ts index d3e9ba6c124..9a70c3f48d8 100644 --- a/packages/abi-typegen/src/types/OptionType.ts +++ b/packages/abi-typegen/src/abi/types/OptionType.ts @@ -1,4 +1,4 @@ -import type { IType } from '../interfaces/IType'; +import type { IType } from '../../types/interfaces/IType'; import { AType } from './AType'; diff --git a/packages/abi-typegen/src/types/RawUntypedPtr.test.ts b/packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts similarity index 100% rename from packages/abi-typegen/src/types/RawUntypedPtr.test.ts rename to packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts diff --git a/packages/abi-typegen/src/types/RawUntypedPtr.ts b/packages/abi-typegen/src/abi/types/RawUntypedPtr.ts similarity index 85% rename from packages/abi-typegen/src/types/RawUntypedPtr.ts rename to packages/abi-typegen/src/abi/types/RawUntypedPtr.ts index 62fc6bb0f44..884b8191ee0 100644 --- a/packages/abi-typegen/src/types/RawUntypedPtr.ts +++ b/packages/abi-typegen/src/abi/types/RawUntypedPtr.ts @@ -1,4 +1,4 @@ -import type { IType } from '../interfaces/IType'; +import type { IType } from '../../types/interfaces/IType'; import { U64Type } from './U64Type'; diff --git a/packages/abi-typegen/src/types/StrType.test.ts b/packages/abi-typegen/src/abi/types/StrType.test.ts similarity index 100% rename from packages/abi-typegen/src/types/StrType.test.ts rename to packages/abi-typegen/src/abi/types/StrType.test.ts diff --git a/packages/abi-typegen/src/types/StrType.ts b/packages/abi-typegen/src/abi/types/StrType.ts similarity index 90% rename from packages/abi-typegen/src/types/StrType.ts rename to packages/abi-typegen/src/abi/types/StrType.ts index 80db6380787..b574c1895c4 100644 --- a/packages/abi-typegen/src/types/StrType.ts +++ b/packages/abi-typegen/src/abi/types/StrType.ts @@ -1,4 +1,4 @@ -import type { IType } from '../interfaces/IType'; +import type { IType } from '../../types/interfaces/IType'; import { AType } from './AType'; diff --git a/packages/abi-typegen/src/types/StructType.test.ts b/packages/abi-typegen/src/abi/types/StructType.test.ts similarity index 85% rename from packages/abi-typegen/src/types/StructType.test.ts rename to packages/abi-typegen/src/abi/types/StructType.test.ts index 57f24459798..2fafc0f5c1a 100644 --- a/packages/abi-typegen/src/types/StructType.test.ts +++ b/packages/abi-typegen/src/abi/types/StructType.test.ts @@ -1,10 +1,10 @@ -import { contractPaths } from '../../test/fixtures'; -import { compileSwayToJson } from '../../test/utils/sway/compileSwayToJson'; -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; -import { TargetEnum } from '../interfaces/TargetEnum'; -import { findType } from '../utils/findType'; -import { makeType } from '../utils/makeType'; -import * as parseTypeArgumentsMod from '../utils/parseTypeArguments'; +import { contractPaths } from '../../../test/fixtures'; +import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import type { IRawAbiTypeRoot } from '../../index'; +import { TargetEnum } from '../../types/enums/TargetEnum'; +import { findType } from '../../utils/findType'; +import { makeType } from '../../utils/makeType'; +import * as parseTypeArgumentsMod from '../../utils/parseTypeArguments'; import { StructType } from './StructType'; import { U16Type } from './U16Type'; diff --git a/packages/abi-typegen/src/types/StructType.ts b/packages/abi-typegen/src/abi/types/StructType.ts similarity index 86% rename from packages/abi-typegen/src/types/StructType.ts rename to packages/abi-typegen/src/abi/types/StructType.ts index 78bef23618d..5426e2e6a87 100644 --- a/packages/abi-typegen/src/types/StructType.ts +++ b/packages/abi-typegen/src/abi/types/StructType.ts @@ -1,9 +1,9 @@ -import type { IRawAbiTypeComponent } from '../interfaces/IRawAbiType'; -import type { IType } from '../interfaces/IType'; -import type { TargetEnum } from '../interfaces/TargetEnum'; -import { extractStructName } from '../utils/extractStructName'; -import { findType } from '../utils/findType'; -import { parseTypeArguments } from '../utils/parseTypeArguments'; +import type { IRawAbiTypeComponent } from '../../index'; +import type { TargetEnum } from '../../types/enums/TargetEnum'; +import type { IType } from '../../types/interfaces/IType'; +import { extractStructName } from '../../utils/extractStructName'; +import { findType } from '../../utils/findType'; +import { parseTypeArguments } from '../../utils/parseTypeArguments'; import { AType } from './AType'; diff --git a/packages/abi-typegen/src/types/TupleType.test.ts b/packages/abi-typegen/src/abi/types/TupleType.test.ts similarity index 81% rename from packages/abi-typegen/src/types/TupleType.test.ts rename to packages/abi-typegen/src/abi/types/TupleType.test.ts index 1f6fa6e6759..6752d10ccc5 100644 --- a/packages/abi-typegen/src/types/TupleType.test.ts +++ b/packages/abi-typegen/src/abi/types/TupleType.test.ts @@ -1,9 +1,9 @@ -import { contractPaths } from '../../test/fixtures'; -import { compileSwayToJson } from '../../test/utils/sway/compileSwayToJson'; -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; -import { findType } from '../utils/findType'; -import { makeType } from '../utils/makeType'; -import * as parseTypeArgumentsMod from '../utils/parseTypeArguments'; +import { contractPaths } from '../../../test/fixtures'; +import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import type { IRawAbiTypeRoot } from '../../index'; +import { findType } from '../../utils/findType'; +import { makeType } from '../../utils/makeType'; +import * as parseTypeArgumentsMod from '../../utils/parseTypeArguments'; import { ArrayType } from './ArrayType'; import { TupleType } from './TupleType'; diff --git a/packages/abi-typegen/src/types/TupleType.ts b/packages/abi-typegen/src/abi/types/TupleType.ts similarity index 87% rename from packages/abi-typegen/src/types/TupleType.ts rename to packages/abi-typegen/src/abi/types/TupleType.ts index 601945590f6..13711a2c23e 100644 --- a/packages/abi-typegen/src/types/TupleType.ts +++ b/packages/abi-typegen/src/abi/types/TupleType.ts @@ -1,7 +1,7 @@ -import type { IType } from '../interfaces/IType'; -import { TargetEnum } from '../interfaces/TargetEnum'; -import { findType } from '../utils/findType'; -import { parseTypeArguments } from '../utils/parseTypeArguments'; +import { TargetEnum } from '../../types/enums/TargetEnum'; +import type { IType } from '../../types/interfaces/IType'; +import { findType } from '../../utils/findType'; +import { parseTypeArguments } from '../../utils/parseTypeArguments'; import { AType } from './AType'; diff --git a/packages/abi-typegen/src/types/U16Type.test.ts b/packages/abi-typegen/src/abi/types/U16Type.test.ts similarity index 100% rename from packages/abi-typegen/src/types/U16Type.test.ts rename to packages/abi-typegen/src/abi/types/U16Type.test.ts diff --git a/packages/abi-typegen/src/types/U16Type.ts b/packages/abi-typegen/src/abi/types/U16Type.ts similarity index 83% rename from packages/abi-typegen/src/types/U16Type.ts rename to packages/abi-typegen/src/abi/types/U16Type.ts index def679a399e..ed081083bf0 100644 --- a/packages/abi-typegen/src/types/U16Type.ts +++ b/packages/abi-typegen/src/abi/types/U16Type.ts @@ -1,4 +1,4 @@ -import type { IType } from '../interfaces/IType'; +import type { IType } from '../../types/interfaces/IType'; import { U8Type } from './U8Type'; diff --git a/packages/abi-typegen/src/types/U32Type.test.ts b/packages/abi-typegen/src/abi/types/U32Type.test.ts similarity index 100% rename from packages/abi-typegen/src/types/U32Type.test.ts rename to packages/abi-typegen/src/abi/types/U32Type.test.ts diff --git a/packages/abi-typegen/src/types/U32Type.ts b/packages/abi-typegen/src/abi/types/U32Type.ts similarity index 83% rename from packages/abi-typegen/src/types/U32Type.ts rename to packages/abi-typegen/src/abi/types/U32Type.ts index 30acd6795e5..eb2b9960be7 100644 --- a/packages/abi-typegen/src/types/U32Type.ts +++ b/packages/abi-typegen/src/abi/types/U32Type.ts @@ -1,4 +1,4 @@ -import type { IType } from '../interfaces/IType'; +import type { IType } from '../../types/interfaces/IType'; import { U8Type } from './U8Type'; diff --git a/packages/abi-typegen/src/types/U64Type.test.ts b/packages/abi-typegen/src/abi/types/U64Type.test.ts similarity index 100% rename from packages/abi-typegen/src/types/U64Type.test.ts rename to packages/abi-typegen/src/abi/types/U64Type.test.ts diff --git a/packages/abi-typegen/src/types/U64Type.ts b/packages/abi-typegen/src/abi/types/U64Type.ts similarity index 89% rename from packages/abi-typegen/src/types/U64Type.ts rename to packages/abi-typegen/src/abi/types/U64Type.ts index 3ea2115f56c..4167e993e51 100644 --- a/packages/abi-typegen/src/types/U64Type.ts +++ b/packages/abi-typegen/src/abi/types/U64Type.ts @@ -1,4 +1,4 @@ -import type { IType } from '../interfaces/IType'; +import type { IType } from '../../types/interfaces/IType'; import { U8Type } from './U8Type'; diff --git a/packages/abi-typegen/src/types/U8Type.test.ts b/packages/abi-typegen/src/abi/types/U8Type.test.ts similarity index 100% rename from packages/abi-typegen/src/types/U8Type.test.ts rename to packages/abi-typegen/src/abi/types/U8Type.test.ts diff --git a/packages/abi-typegen/src/types/U8Type.ts b/packages/abi-typegen/src/abi/types/U8Type.ts similarity index 83% rename from packages/abi-typegen/src/types/U8Type.ts rename to packages/abi-typegen/src/abi/types/U8Type.ts index 6786893ba93..b083c010aba 100644 --- a/packages/abi-typegen/src/types/U8Type.ts +++ b/packages/abi-typegen/src/abi/types/U8Type.ts @@ -1,5 +1,5 @@ -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; -import type { IType } from '../interfaces/IType'; +import type { IRawAbiTypeRoot } from '../../index'; +import type { IType } from '../../types/interfaces/IType'; import { AType } from './AType'; diff --git a/packages/abi-typegen/src/types/VectorType.test.ts b/packages/abi-typegen/src/abi/types/VectorType.test.ts similarity index 100% rename from packages/abi-typegen/src/types/VectorType.test.ts rename to packages/abi-typegen/src/abi/types/VectorType.test.ts diff --git a/packages/abi-typegen/src/types/VectorType.ts b/packages/abi-typegen/src/abi/types/VectorType.ts similarity index 91% rename from packages/abi-typegen/src/types/VectorType.ts rename to packages/abi-typegen/src/abi/types/VectorType.ts index d6ec9f23e89..6957833df24 100644 --- a/packages/abi-typegen/src/types/VectorType.ts +++ b/packages/abi-typegen/src/abi/types/VectorType.ts @@ -1,4 +1,4 @@ -import type { IType } from '../interfaces/IType'; +import type { IType } from '../../types/interfaces/IType'; import { ArrayType } from './ArrayType'; diff --git a/packages/abi-typegen/src/cli.test.ts b/packages/abi-typegen/src/cli.test.ts index f78a510e41e..9629d74979b 100644 --- a/packages/abi-typegen/src/cli.test.ts +++ b/packages/abi-typegen/src/cli.test.ts @@ -1,12 +1,11 @@ import { join } from 'path'; import { contractPaths } from '../test/fixtures'; -import { executeAndCatch } from '../test/utils/executeAndCatch'; import { createTempSwayProject } from '../test/utils/sway/createTempSwayProject'; import { run } from './cli'; -import { CategoryEnum } from './interfaces/CategoryEnum'; import * as runTypegenMod from './runTypegen'; +import { CategoryEnum } from './types/enums/CategoryEnum'; describe('cli.ts', () => { function mockDeps() { diff --git a/packages/abi-typegen/src/cli.ts b/packages/abi-typegen/src/cli.ts index 50bebb75901..6563e543a21 100644 --- a/packages/abi-typegen/src/cli.ts +++ b/packages/abi-typegen/src/cli.ts @@ -1,8 +1,8 @@ import { versions } from '@fuel-ts/versions'; import { Command, Option } from 'commander'; -import { CategoryEnum } from './interfaces/CategoryEnum'; import { runTypegen } from './runTypegen'; +import { CategoryEnum } from './types/enums/CategoryEnum'; export interface ICliParams { inputs: string[]; diff --git a/packages/abi-typegen/src/index.ts b/packages/abi-typegen/src/index.ts index 2eb578bdd1b..b6da0abaa43 100644 --- a/packages/abi-typegen/src/index.ts +++ b/packages/abi-typegen/src/index.ts @@ -1,8 +1,8 @@ export * from './AbiTypeGen'; -export * from './interfaces/IFile'; -export * from './interfaces/IFunction'; -export * from './interfaces/IRawAbi'; -export * from './interfaces/IRawAbiFunction'; -export * from './interfaces/IRawAbiType'; -export * from './interfaces/IRawAbiLoggedTypes'; +export * from './types/interfaces/IFile'; +export * from './types/interfaces/IFunction'; +export * from './types/interfaces/IRawAbi'; +export * from './types/interfaces/IRawAbiFunction'; +export * from './types/interfaces/IRawAbiType'; +export * from './types/interfaces/IRawAbiLoggedTypes'; diff --git a/packages/abi-typegen/src/runTypegen.test.ts b/packages/abi-typegen/src/runTypegen.test.ts index 36cbe21fb79..fbd5480d2d8 100644 --- a/packages/abi-typegen/src/runTypegen.test.ts +++ b/packages/abi-typegen/src/runTypegen.test.ts @@ -9,8 +9,8 @@ import { contractPaths } from '../test/fixtures'; import { executeAndCatch } from '../test/utils/executeAndCatch'; import { createTempSwayProject } from '../test/utils/sway/createTempSwayProject'; -import { CategoryEnum } from './interfaces/CategoryEnum'; import { runTypegen } from './runTypegen'; +import { CategoryEnum } from './types/enums/CategoryEnum'; describe('runTypegen.js', () => { test('should run typegen, using: globals', async () => { diff --git a/packages/abi-typegen/src/runTypegen.ts b/packages/abi-typegen/src/runTypegen.ts index 006e67db75f..8f5fd50c6f6 100644 --- a/packages/abi-typegen/src/runTypegen.ts +++ b/packages/abi-typegen/src/runTypegen.ts @@ -5,8 +5,8 @@ import { basename } from 'path'; import rimraf from 'rimraf'; import { AbiTypeGen } from './AbiTypeGen'; -import type { CategoryEnum } from './interfaces/CategoryEnum'; -import type { IFile } from './interfaces/IFile'; +import type { CategoryEnum } from './types/enums/CategoryEnum'; +import type { IFile } from './types/interfaces/IFile'; export interface IGenerateFilesParams { cwd: string; diff --git a/packages/abi-typegen/src/templates/common/common.ts b/packages/abi-typegen/src/templates/common/common.ts index 40ff263cd44..e6fa7400969 100644 --- a/packages/abi-typegen/src/templates/common/common.ts +++ b/packages/abi-typegen/src/templates/common/common.ts @@ -1,4 +1,4 @@ -import { renderHbsTemplate } from '../utils/renderHbsTemplate'; +import { renderHbsTemplate } from '../renderHbsTemplate'; import commonTemplate from './common.hbs'; diff --git a/packages/abi-typegen/src/templates/common/index.test.ts b/packages/abi-typegen/src/templates/common/index.test.ts index 4615e17381b..7785db81fd0 100644 --- a/packages/abi-typegen/src/templates/common/index.test.ts +++ b/packages/abi-typegen/src/templates/common/index.test.ts @@ -2,7 +2,7 @@ import { contractPaths } from '../../../test/fixtures'; import indexTemplate from '../../../test/fixtures/templates/contract/index.hbs'; import { mockVersions } from '../../../test/utils/mockVersions'; import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; -import { Abi } from '../../Abi'; +import { Abi } from '../../abi/Abi'; import { renderIndexTemplate } from './index'; diff --git a/packages/abi-typegen/src/templates/common/index.ts b/packages/abi-typegen/src/templates/common/index.ts index b0572a6a467..848345ceb87 100644 --- a/packages/abi-typegen/src/templates/common/index.ts +++ b/packages/abi-typegen/src/templates/common/index.ts @@ -1,5 +1,5 @@ -import type { Abi } from '../../Abi'; -import { renderHbsTemplate } from '../utils/renderHbsTemplate'; +import type { Abi } from '../../abi/Abi'; +import { renderHbsTemplate } from '../renderHbsTemplate'; import indexTemplate from './index.hbs'; diff --git a/packages/abi-typegen/src/templates/contract/dts.test.ts b/packages/abi-typegen/src/templates/contract/dts.test.ts index ff478329785..a77e5af5439 100644 --- a/packages/abi-typegen/src/templates/contract/dts.test.ts +++ b/packages/abi-typegen/src/templates/contract/dts.test.ts @@ -2,7 +2,7 @@ import { contractPaths } from '../../../test/fixtures'; import expectedDtsFullTemplate from '../../../test/fixtures/templates/contract/dts.hbs'; import { mockVersions } from '../../../test/utils/mockVersions'; import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; -import { Abi } from '../../Abi'; +import { Abi } from '../../abi/Abi'; import { renderDtsTemplate } from './dts'; diff --git a/packages/abi-typegen/src/templates/contract/dts.ts b/packages/abi-typegen/src/templates/contract/dts.ts index 95ed74d61d9..a06ca4d53ba 100644 --- a/packages/abi-typegen/src/templates/contract/dts.ts +++ b/packages/abi-typegen/src/templates/contract/dts.ts @@ -1,8 +1,8 @@ -import type { Abi } from '../../Abi'; -import { TargetEnum } from '../../interfaces/TargetEnum'; -import type { EnumType } from '../../types/EnumType'; -import type { StructType } from '../../types/StructType'; -import { renderHbsTemplate } from '../utils/renderHbsTemplate'; +import type { Abi } from '../../abi/Abi'; +import type { EnumType } from '../../abi/types/EnumType'; +import type { StructType } from '../../abi/types/StructType'; +import { TargetEnum } from '../../types/enums/TargetEnum'; +import { renderHbsTemplate } from '../renderHbsTemplate'; import dtsTemplate from './dts.hbs'; diff --git a/packages/abi-typegen/src/templates/contract/factory.test.ts b/packages/abi-typegen/src/templates/contract/factory.test.ts index c72bf1a1da1..45eae14951b 100644 --- a/packages/abi-typegen/src/templates/contract/factory.test.ts +++ b/packages/abi-typegen/src/templates/contract/factory.test.ts @@ -2,7 +2,7 @@ import { contractPaths } from '../../../test/fixtures'; import factoryTemplate from '../../../test/fixtures/templates/contract/factory.hbs'; import { mockVersions } from '../../../test/utils/mockVersions'; import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; -import { Abi } from '../../Abi'; +import { Abi } from '../../abi/Abi'; import { renderFactoryTemplate } from './factory'; diff --git a/packages/abi-typegen/src/templates/contract/factory.ts b/packages/abi-typegen/src/templates/contract/factory.ts index ff6d0e56393..e21469c2dbf 100644 --- a/packages/abi-typegen/src/templates/contract/factory.ts +++ b/packages/abi-typegen/src/templates/contract/factory.ts @@ -1,5 +1,5 @@ -import type { Abi } from '../../Abi'; -import { renderHbsTemplate } from '../utils/renderHbsTemplate'; +import type { Abi } from '../../abi/Abi'; +import { renderHbsTemplate } from '../renderHbsTemplate'; import factoryTemplate from './factory.hbs'; diff --git a/packages/abi-typegen/src/templates/utils/renderHbsTemplate.test.ts b/packages/abi-typegen/src/templates/renderHbsTemplate.test.ts similarity index 93% rename from packages/abi-typegen/src/templates/utils/renderHbsTemplate.test.ts rename to packages/abi-typegen/src/templates/renderHbsTemplate.test.ts index d598ac095a6..c9e2253eec7 100644 --- a/packages/abi-typegen/src/templates/utils/renderHbsTemplate.test.ts +++ b/packages/abi-typegen/src/templates/renderHbsTemplate.test.ts @@ -1,4 +1,4 @@ -import { mockVersions } from '../../../test/utils/mockVersions'; +import { mockVersions } from '../../test/utils/mockVersions'; import { renderHbsTemplate } from './renderHbsTemplate'; diff --git a/packages/abi-typegen/src/templates/utils/renderHbsTemplate.ts b/packages/abi-typegen/src/templates/renderHbsTemplate.ts similarity index 93% rename from packages/abi-typegen/src/templates/utils/renderHbsTemplate.ts rename to packages/abi-typegen/src/templates/renderHbsTemplate.ts index 65e138cee0d..9848c0e879b 100644 --- a/packages/abi-typegen/src/templates/utils/renderHbsTemplate.ts +++ b/packages/abi-typegen/src/templates/renderHbsTemplate.ts @@ -1,7 +1,7 @@ import { versions } from '@fuel-ts/versions'; import { compile } from 'handlebars'; -import headerTemplate from '../common/_header.hbs'; +import headerTemplate from './common/_header.hbs'; /* Renders the given template w/ the given data, while injecting common diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts index e67f342c908..3c1384b6160 100644 --- a/packages/abi-typegen/src/templates/script/factory.test.ts +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -4,7 +4,7 @@ import { executeAndCatch } from '../../../test/utils/executeAndCatch'; import { getNewAbiTypegen } from '../../../test/utils/getNewAbiTypegen'; import { mockVersions } from '../../../test/utils/mockVersions'; import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; -import { Abi } from '../../Abi'; +import { Abi } from '../../abi/Abi'; import { renderFactoryTemplate } from './factory'; diff --git a/packages/abi-typegen/src/templates/script/factory.ts b/packages/abi-typegen/src/templates/script/factory.ts index 086667778b3..84ddd9824a2 100644 --- a/packages/abi-typegen/src/templates/script/factory.ts +++ b/packages/abi-typegen/src/templates/script/factory.ts @@ -1,5 +1,5 @@ -import type { Abi } from '../../Abi'; -import { renderHbsTemplate } from '../utils/renderHbsTemplate'; +import type { Abi } from '../../abi/Abi'; +import { renderHbsTemplate } from '../renderHbsTemplate'; import factoryTemplate from './factory.hbs'; diff --git a/packages/abi-typegen/src/interfaces/CategoryEnum.ts b/packages/abi-typegen/src/types/enums/CategoryEnum.ts similarity index 100% rename from packages/abi-typegen/src/interfaces/CategoryEnum.ts rename to packages/abi-typegen/src/types/enums/CategoryEnum.ts diff --git a/packages/abi-typegen/src/interfaces/TargetEnum.ts b/packages/abi-typegen/src/types/enums/TargetEnum.ts similarity index 100% rename from packages/abi-typegen/src/interfaces/TargetEnum.ts rename to packages/abi-typegen/src/types/enums/TargetEnum.ts diff --git a/packages/abi-typegen/src/interfaces/IFile.ts b/packages/abi-typegen/src/types/interfaces/IFile.ts similarity index 100% rename from packages/abi-typegen/src/interfaces/IFile.ts rename to packages/abi-typegen/src/types/interfaces/IFile.ts diff --git a/packages/abi-typegen/src/interfaces/IFunction.ts b/packages/abi-typegen/src/types/interfaces/IFunction.ts similarity index 100% rename from packages/abi-typegen/src/interfaces/IFunction.ts rename to packages/abi-typegen/src/types/interfaces/IFunction.ts diff --git a/packages/abi-typegen/src/interfaces/IRawAbi.ts b/packages/abi-typegen/src/types/interfaces/IRawAbi.ts similarity index 100% rename from packages/abi-typegen/src/interfaces/IRawAbi.ts rename to packages/abi-typegen/src/types/interfaces/IRawAbi.ts diff --git a/packages/abi-typegen/src/interfaces/IRawAbiFunction.ts b/packages/abi-typegen/src/types/interfaces/IRawAbiFunction.ts similarity index 100% rename from packages/abi-typegen/src/interfaces/IRawAbiFunction.ts rename to packages/abi-typegen/src/types/interfaces/IRawAbiFunction.ts diff --git a/packages/abi-typegen/src/interfaces/IRawAbiLoggedTypes.ts b/packages/abi-typegen/src/types/interfaces/IRawAbiLoggedTypes.ts similarity index 100% rename from packages/abi-typegen/src/interfaces/IRawAbiLoggedTypes.ts rename to packages/abi-typegen/src/types/interfaces/IRawAbiLoggedTypes.ts diff --git a/packages/abi-typegen/src/interfaces/IRawAbiType.ts b/packages/abi-typegen/src/types/interfaces/IRawAbiType.ts similarity index 100% rename from packages/abi-typegen/src/interfaces/IRawAbiType.ts rename to packages/abi-typegen/src/types/interfaces/IRawAbiType.ts diff --git a/packages/abi-typegen/src/interfaces/IType.ts b/packages/abi-typegen/src/types/interfaces/IType.ts similarity index 100% rename from packages/abi-typegen/src/interfaces/IType.ts rename to packages/abi-typegen/src/types/interfaces/IType.ts diff --git a/packages/abi-typegen/src/assemblers/assembleContracts.test.ts b/packages/abi-typegen/src/utils/assembleContracts.test.ts similarity index 97% rename from packages/abi-typegen/src/assemblers/assembleContracts.test.ts rename to packages/abi-typegen/src/utils/assembleContracts.test.ts index 2fedfe70867..87c2899767c 100644 --- a/packages/abi-typegen/src/assemblers/assembleContracts.test.ts +++ b/packages/abi-typegen/src/utils/assembleContracts.test.ts @@ -1,8 +1,8 @@ import { getNewAbiTypegen } from '../../test/utils/getNewAbiTypegen'; -import { CategoryEnum } from '../interfaces/CategoryEnum'; import * as renderCommonTemplateMod from '../templates/common/common'; import * as renderIndexTemplateMod from '../templates/common/index'; import * as renderFactoryTemplateMod from '../templates/contract/factory'; +import { CategoryEnum } from '../types/enums/CategoryEnum'; import { assembleContracts } from './assembleContracts'; diff --git a/packages/abi-typegen/src/assemblers/assembleContracts.ts b/packages/abi-typegen/src/utils/assembleContracts.ts similarity index 97% rename from packages/abi-typegen/src/assemblers/assembleContracts.ts rename to packages/abi-typegen/src/utils/assembleContracts.ts index 97fff569be6..8d5bf08314b 100644 --- a/packages/abi-typegen/src/assemblers/assembleContracts.ts +++ b/packages/abi-typegen/src/utils/assembleContracts.ts @@ -1,11 +1,11 @@ import { join } from 'path'; -import type { Abi } from '../Abi'; +import type { Abi } from '../abi/Abi'; import type { IFile } from '../index'; import { renderCommonTemplate } from '../templates/common/common'; +import { renderIndexTemplate } from '../templates/common/index'; import { renderDtsTemplate } from '../templates/contract/dts'; import { renderFactoryTemplate } from '../templates/contract/factory'; -import { renderIndexTemplate } from '../templates/common/index'; /** * Render all Contract-related templates and returns diff --git a/packages/abi-typegen/src/assemblers/assembleScripts.test.ts b/packages/abi-typegen/src/utils/assembleScripts.test.ts similarity index 97% rename from packages/abi-typegen/src/assemblers/assembleScripts.test.ts rename to packages/abi-typegen/src/utils/assembleScripts.test.ts index 09ad4550f7f..f669129ee18 100644 --- a/packages/abi-typegen/src/assemblers/assembleScripts.test.ts +++ b/packages/abi-typegen/src/utils/assembleScripts.test.ts @@ -1,8 +1,8 @@ import { getNewAbiTypegen } from '../../test/utils/getNewAbiTypegen'; -import { CategoryEnum } from '../interfaces/CategoryEnum'; import * as renderCommonTemplateMod from '../templates/common/common'; import * as renderIndexTemplateMod from '../templates/common/index'; import * as renderFactoryTemplateMod from '../templates/script/factory'; +import { CategoryEnum } from '../types/enums/CategoryEnum'; import { assembleScripts } from './assembleScripts'; diff --git a/packages/abi-typegen/src/assemblers/assembleScripts.ts b/packages/abi-typegen/src/utils/assembleScripts.ts similarity index 97% rename from packages/abi-typegen/src/assemblers/assembleScripts.ts rename to packages/abi-typegen/src/utils/assembleScripts.ts index 77f27f1035e..69fd3f6f26f 100644 --- a/packages/abi-typegen/src/assemblers/assembleScripts.ts +++ b/packages/abi-typegen/src/utils/assembleScripts.ts @@ -1,6 +1,6 @@ import { join } from 'path'; -import type { Abi } from '../Abi'; +import type { Abi } from '../abi/Abi'; import type { IFile } from '../index'; import { renderCommonTemplate } from '../templates/common/common'; import { renderIndexTemplate } from '../templates/common/index'; diff --git a/packages/abi-typegen/src/utils/extractStructName.test.ts b/packages/abi-typegen/src/utils/extractStructName.test.ts index ecff2c5349a..b5df918687e 100644 --- a/packages/abi-typegen/src/utils/extractStructName.test.ts +++ b/packages/abi-typegen/src/utils/extractStructName.test.ts @@ -1,7 +1,7 @@ -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; -import { EnumType } from '../types/EnumType'; -import { GenericType } from '../types/GenericType'; -import { StructType } from '../types/StructType'; +import { EnumType } from '../abi/types/EnumType'; +import { GenericType } from '../abi/types/GenericType'; +import { StructType } from '../abi/types/StructType'; +import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType'; import { extractStructName } from './extractStructName'; diff --git a/packages/abi-typegen/src/utils/extractStructName.ts b/packages/abi-typegen/src/utils/extractStructName.ts index feb9cf9d526..21be1ce17d0 100644 --- a/packages/abi-typegen/src/utils/extractStructName.ts +++ b/packages/abi-typegen/src/utils/extractStructName.ts @@ -1,4 +1,4 @@ -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; +import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType'; export function extractStructName(params: { rawAbiType: IRawAbiTypeRoot; regex: RegExp }) { const { rawAbiType, regex } = params; diff --git a/packages/abi-typegen/src/utils/findType.test.ts b/packages/abi-typegen/src/utils/findType.test.ts index fc7344f622d..1d04c5e6c71 100644 --- a/packages/abi-typegen/src/utils/findType.test.ts +++ b/packages/abi-typegen/src/utils/findType.test.ts @@ -1,6 +1,6 @@ import { executeAndCatch } from '../../test/utils/executeAndCatch'; -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; -import type { IType } from '../interfaces/IType'; +import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType'; +import type { IType } from '../types/interfaces/IType'; import { findType } from './findType'; import { makeType } from './makeType'; diff --git a/packages/abi-typegen/src/utils/findType.ts b/packages/abi-typegen/src/utils/findType.ts index de9176d0af8..0545df117d8 100644 --- a/packages/abi-typegen/src/utils/findType.ts +++ b/packages/abi-typegen/src/utils/findType.ts @@ -1,4 +1,4 @@ -import type { IType } from '../interfaces/IType'; +import type { IType } from '../types/interfaces/IType'; export function findType(params: { types: IType[]; typeId: number }) { const { types, typeId } = params; diff --git a/packages/abi-typegen/src/utils/makeFunction.test.ts b/packages/abi-typegen/src/utils/makeFunction.test.ts index 6e143dd1c67..302c6cc2a25 100644 --- a/packages/abi-typegen/src/utils/makeFunction.test.ts +++ b/packages/abi-typegen/src/utils/makeFunction.test.ts @@ -1,6 +1,6 @@ -import type { IRawAbiFunction } from '../interfaces/IRawAbiFunction'; -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; -import type { IType } from '../interfaces/IType'; +import type { IRawAbiFunction } from '../types/interfaces/IRawAbiFunction'; +import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType'; +import type { IType } from '../types/interfaces/IType'; import { makeFunction } from './makeFunction'; import { makeType } from './makeType'; diff --git a/packages/abi-typegen/src/utils/makeFunction.ts b/packages/abi-typegen/src/utils/makeFunction.ts index 1eaa179eb3f..97b263c946d 100644 --- a/packages/abi-typegen/src/utils/makeFunction.ts +++ b/packages/abi-typegen/src/utils/makeFunction.ts @@ -1,6 +1,6 @@ -import { Function } from '../functions/Function'; -import type { IRawAbiFunction } from '../interfaces/IRawAbiFunction'; -import type { IType } from '../interfaces/IType'; +import { Function } from '../abi/functions/Function'; +import type { IRawAbiFunction } from '../types/interfaces/IRawAbiFunction'; +import type { IType } from '../types/interfaces/IType'; export function makeFunction(params: { types: IType[]; rawAbiFunction: IRawAbiFunction }) { const { types, rawAbiFunction } = params; diff --git a/packages/abi-typegen/src/utils/makeType.test.ts b/packages/abi-typegen/src/utils/makeType.test.ts index 715f6c22405..69ccbf11995 100644 --- a/packages/abi-typegen/src/utils/makeType.test.ts +++ b/packages/abi-typegen/src/utils/makeType.test.ts @@ -1,5 +1,5 @@ import { executeAndCatch } from '../../test/utils/executeAndCatch'; -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; +import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType'; import { makeType } from './makeType'; diff --git a/packages/abi-typegen/src/utils/makeType.ts b/packages/abi-typegen/src/utils/makeType.ts index f0e8ef5a2e9..77dd3f82ee5 100644 --- a/packages/abi-typegen/src/utils/makeType.ts +++ b/packages/abi-typegen/src/utils/makeType.ts @@ -1,4 +1,4 @@ -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; +import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType'; import { supportedTypes } from './supportedTypes'; diff --git a/packages/abi-typegen/src/utils/parseFunctions.test.ts b/packages/abi-typegen/src/utils/parseFunctions.test.ts index f65612fedfa..e0771c1d78d 100644 --- a/packages/abi-typegen/src/utils/parseFunctions.test.ts +++ b/packages/abi-typegen/src/utils/parseFunctions.test.ts @@ -1,6 +1,6 @@ -import type { IRawAbiFunction } from '../interfaces/IRawAbiFunction'; -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; -import type { IType } from '../interfaces/IType'; +import type { IRawAbiFunction } from '../types/interfaces/IRawAbiFunction'; +import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType'; +import type { IType } from '../types/interfaces/IType'; import { makeType } from './makeType'; import { parseFunctions } from './parseFunctions'; diff --git a/packages/abi-typegen/src/utils/parseFunctions.ts b/packages/abi-typegen/src/utils/parseFunctions.ts index 6b840010ae0..3fce38cb90c 100644 --- a/packages/abi-typegen/src/utils/parseFunctions.ts +++ b/packages/abi-typegen/src/utils/parseFunctions.ts @@ -1,6 +1,6 @@ -import type { IFunction } from '../interfaces/IFunction'; -import type { IRawAbiFunction } from '../interfaces/IRawAbiFunction'; -import type { IType } from '../interfaces/IType'; +import type { IFunction } from '../types/interfaces/IFunction'; +import type { IRawAbiFunction } from '../types/interfaces/IRawAbiFunction'; +import type { IType } from '../types/interfaces/IType'; import { makeFunction } from './makeFunction'; diff --git a/packages/abi-typegen/src/utils/parseTypeArguments.test.ts b/packages/abi-typegen/src/utils/parseTypeArguments.test.ts index 751ca73f61c..7337a0e9752 100644 --- a/packages/abi-typegen/src/utils/parseTypeArguments.test.ts +++ b/packages/abi-typegen/src/utils/parseTypeArguments.test.ts @@ -1,7 +1,7 @@ import { contractPaths } from '../../test/fixtures/index'; import { compileSwayToJson } from '../../test/utils/sway/compileSwayToJson'; -import type { IRawAbiTypeRoot, IRawAbiTypeComponent } from '../interfaces/IRawAbiType'; -import { TargetEnum } from '../interfaces/TargetEnum'; +import { TargetEnum } from '../types/enums/TargetEnum'; +import type { IRawAbiTypeRoot, IRawAbiTypeComponent } from '../types/interfaces/IRawAbiType'; import { makeType } from './makeType'; import { parseTypeArguments } from './parseTypeArguments'; diff --git a/packages/abi-typegen/src/utils/parseTypeArguments.ts b/packages/abi-typegen/src/utils/parseTypeArguments.ts index db35589656f..d64bfc4c029 100644 --- a/packages/abi-typegen/src/utils/parseTypeArguments.ts +++ b/packages/abi-typegen/src/utils/parseTypeArguments.ts @@ -1,6 +1,6 @@ -import type { IRawAbiTypeComponent } from '../interfaces/IRawAbiType'; -import type { IType } from '../interfaces/IType'; -import type { TargetEnum } from '../interfaces/TargetEnum'; +import type { TargetEnum } from '../types/enums/TargetEnum'; +import type { IRawAbiTypeComponent } from '../types/interfaces/IRawAbiType'; +import type { IType } from '../types/interfaces/IType'; import { findType } from './findType'; diff --git a/packages/abi-typegen/src/utils/parseTypes.test.ts b/packages/abi-typegen/src/utils/parseTypes.test.ts index 3dea497f7ab..6984a1d352d 100644 --- a/packages/abi-typegen/src/utils/parseTypes.test.ts +++ b/packages/abi-typegen/src/utils/parseTypes.test.ts @@ -1,4 +1,4 @@ -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; +import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType'; import { parseTypes } from './parseTypes'; diff --git a/packages/abi-typegen/src/utils/parseTypes.ts b/packages/abi-typegen/src/utils/parseTypes.ts index 251ff1f1509..24f022a3aab 100644 --- a/packages/abi-typegen/src/utils/parseTypes.ts +++ b/packages/abi-typegen/src/utils/parseTypes.ts @@ -1,5 +1,5 @@ -import type { IRawAbiTypeRoot } from '../interfaces/IRawAbiType'; -import type { IType } from '../interfaces/IType'; +import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType'; +import type { IType } from '../types/interfaces/IType'; import { makeType } from './makeType'; import { shouldSkipAbiType } from './shouldSkipAbiType'; diff --git a/packages/abi-typegen/src/utils/supportedTypes.ts b/packages/abi-typegen/src/utils/supportedTypes.ts index 4d68c96caa5..faa8e435337 100644 --- a/packages/abi-typegen/src/utils/supportedTypes.ts +++ b/packages/abi-typegen/src/utils/supportedTypes.ts @@ -1,19 +1,19 @@ -import { ArrayType } from '../types/ArrayType'; -import { B256Type } from '../types/B256Type'; -import { B512Type } from '../types/B512Type'; -import { BoolType } from '../types/BoolType'; -import { EnumType } from '../types/EnumType'; -import { GenericType } from '../types/GenericType'; -import { OptionType } from '../types/OptionType'; -import { RawUntypedPtr } from '../types/RawUntypedPtr'; -import { StrType } from '../types/StrType'; -import { StructType } from '../types/StructType'; -import { TupleType } from '../types/TupleType'; -import { U16Type } from '../types/U16Type'; -import { U32Type } from '../types/U32Type'; -import { U64Type } from '../types/U64Type'; -import { U8Type } from '../types/U8Type'; -import { VectorType } from '../types/VectorType'; +import { ArrayType } from '../abi/types/ArrayType'; +import { B256Type } from '../abi/types/B256Type'; +import { B512Type } from '../abi/types/B512Type'; +import { BoolType } from '../abi/types/BoolType'; +import { EnumType } from '../abi/types/EnumType'; +import { GenericType } from '../abi/types/GenericType'; +import { OptionType } from '../abi/types/OptionType'; +import { RawUntypedPtr } from '../abi/types/RawUntypedPtr'; +import { StrType } from '../abi/types/StrType'; +import { StructType } from '../abi/types/StructType'; +import { TupleType } from '../abi/types/TupleType'; +import { U16Type } from '../abi/types/U16Type'; +import { U32Type } from '../abi/types/U32Type'; +import { U64Type } from '../abi/types/U64Type'; +import { U8Type } from '../abi/types/U8Type'; +import { VectorType } from '../abi/types/VectorType'; export const supportedTypes = [ ArrayType, diff --git a/packages/abi-typegen/test/utils/getNewAbiTypegen.ts b/packages/abi-typegen/test/utils/getNewAbiTypegen.ts index c8e9bd557cb..a856f041092 100644 --- a/packages/abi-typegen/test/utils/getNewAbiTypegen.ts +++ b/packages/abi-typegen/test/utils/getNewAbiTypegen.ts @@ -1,6 +1,6 @@ import type { IFile, IRawAbiTypeRoot } from '../../src/index'; import { AbiTypeGen } from '../../src/index'; -import { CategoryEnum } from '../../src/interfaces/CategoryEnum'; +import { CategoryEnum } from '../../src/types/enums/CategoryEnum'; export function getNewAbiTypegen( params: { diff --git a/packages/abi-typegen/test/utils/sway/compileSwayToJson.ts b/packages/abi-typegen/test/utils/sway/compileSwayToJson.ts index f2b5888104d..6f69fa72de5 100644 --- a/packages/abi-typegen/test/utils/sway/compileSwayToJson.ts +++ b/packages/abi-typegen/test/utils/sway/compileSwayToJson.ts @@ -3,7 +3,7 @@ import mkdirp from 'mkdirp'; import { basename, dirname, join } from 'path'; import rimraf from 'rimraf'; -import type { IRawAbi } from '../../../src/interfaces/IRawAbi'; +import type { IRawAbi } from '../../../src/types/interfaces/IRawAbi'; import type { ISwayParams } from './ISwayUtilParams'; import { createTempSwayProject } from './createTempSwayProject'; diff --git a/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts b/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts index 2ce08dbd836..e4d5892d81d 100644 --- a/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts +++ b/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts @@ -2,7 +2,8 @@ import { writeFileSync } from 'fs'; import mkdirp from 'mkdirp'; import { dirname } from 'path'; -import { AbiTypeGen } from '../../../src'; +import { AbiTypeGen } from '../../../src/AbiTypeGen'; +import { CategoryEnum } from '../../../src/types/enums/CategoryEnum'; import type { ISwayParams } from './ISwayUtilParams'; import { compileSwayToJson } from './compileSwayToJson'; @@ -25,14 +26,12 @@ export function compileSwayToTs(params: ISwayParams) { contents: JSON.stringify(rawContents, null, 2), }, ], + category: CategoryEnum.CONTRACT, }); // create handy shortcuts for common definitions const [abi] = typegen.abis; - const dts = abi.getDtsDeclaration(); - const factory = abi.getFactoryDeclaration(); - if (params.inPlace) { typegen.files.forEach((f) => { mkdirp.sync(dirname(f.path)); @@ -43,7 +42,5 @@ export function compileSwayToTs(params: ISwayParams) { // bundle and shoot return { abi, - dts, - factory, }; } diff --git a/packages/address/src/address.ts b/packages/address/src/address.ts index e1dfd9f6466..610ae6683c2 100644 --- a/packages/address/src/address.ts +++ b/packages/address/src/address.ts @@ -138,7 +138,7 @@ export default class Address extends AbstractAddress { * Takes an optional string and returns back an Address * * @param addressId - Can be a string containing Bech32, B256, or Public Key - * @throws {Error} + * @throws Error * thrown if the input string is not nilsy and cannot be resolved to a valid address format * @returns a new `Address` instance */ From f91127f05907fd08ec970d66e7e3a54084d13803 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 14:40:37 -0300 Subject: [PATCH 043/108] Improving test validations, checking for parameters incompatibility --- packages/abi-typegen/src/cli.test.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/abi-typegen/src/cli.test.ts b/packages/abi-typegen/src/cli.test.ts index 9629d74979b..21a146cde1e 100644 --- a/packages/abi-typegen/src/cli.test.ts +++ b/packages/abi-typegen/src/cli.test.ts @@ -1,4 +1,5 @@ import { join } from 'path'; +import { stderr } from 'process'; import { contractPaths } from '../test/fixtures'; import { createTempSwayProject } from '../test/utils/sway/createTempSwayProject'; @@ -11,8 +12,9 @@ describe('cli.ts', () => { function mockDeps() { const runTypegen = jest.spyOn(runTypegenMod, 'runTypegen').mockImplementation(); const exit = jest.spyOn(process, 'exit').mockImplementation(); + const err = jest.spyOn(stderr, 'write').mockImplementation(); - return { exit, runTypegen }; + return { exit, err, runTypegen }; } function setupTestSwayProject() { @@ -70,8 +72,8 @@ describe('cli.ts', () => { expect(exit).toHaveBeenCalledTimes(0); }); - test('should error if called with improper parameters', async () => { - const { exit } = mockDeps(); + test('should error if called with incompatible parameters', async () => { + const { exit, err } = mockDeps(); const { inputs, output } = await setupTestSwayProject(); const argv = ['node', 'fuels-typegen', '-i', inputs.join(' '), '-o', output, '-s', '-c']; @@ -79,5 +81,12 @@ describe('cli.ts', () => { await run({ argv, programName: 'cli.js:test' }); expect(exit).toHaveBeenNthCalledWith(1, 1); + expect(err).toHaveBeenCalledTimes(2); + + const err1 = /error: option '-c, --contract' cannot be used with option '-s, --script/; + expect(err.mock.calls[0][0].toString()).toMatch(err1); + + const err2 = /error: option '-s, --script' cannot be used with option '-c, --contract/m; + expect(err.mock.calls[1][0].toString()).toMatch(err2); }); }); From b7ddc50b8efa73f91e1e194c58b546265237cec0 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 17:39:03 -0300 Subject: [PATCH 044/108] Docs --- README.md | 2 +- .../generate-contract-types-from-abi.md | 22 ------- .../abi-typegen/generating-types-from-abi.md | 64 +++++++++++++++++++ docs/_guide/abi-typegen/index.md | 13 ++-- .../abi-typegen/using-generated-types.md | 29 ++++++++- packages/abi-typegen/src/cli.ts | 4 +- 6 files changed, 103 insertions(+), 31 deletions(-) delete mode 100644 docs/_guide/abi-typegen/generate-contract-types-from-abi.md create mode 100644 docs/_guide/abi-typegen/generating-types-from-abi.md diff --git a/README.md b/README.md index f74134dd25d..1984d6dfe7f 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ - [Install](#install) - [Import](#import) - [Interacting with Contracts](https://fuellabs.github.io/fuels-ts/guide/contracts/interacting-with-contracts.html) -- [Generate Contract Types from ABI](https://fuellabs.github.io/fuels-ts/guide/abi-typegen/generate-contract-types-from-abi.html) +- [Generate Types from ABI](https://fuellabs.github.io/fuels-ts/guide/abi-typegen/generating-types-from-abi.html) - [Using Generated Types](https://fuellabs.github.io/fuels-ts/guide/abi-typegen/using-generated-types.html) - [Deploying Contracts](https://fuellabs.github.io/fuels-ts/guide/contracts/deploying-contracts) - [CLI](#cli) diff --git a/docs/_guide/abi-typegen/generate-contract-types-from-abi.md b/docs/_guide/abi-typegen/generate-contract-types-from-abi.md deleted file mode 100644 index 7d78dc8733f..00000000000 --- a/docs/_guide/abi-typegen/generate-contract-types-from-abi.md +++ /dev/null @@ -1,22 +0,0 @@ -[nav_order: 0] - -# Generate Contract Types from ABI - -See also [types](../types/). - -#### Dependencies - -```sh -yarn add fuels -``` - -#### Generate Types - -```sh -yarn exec fuels -i ./abis/*-abi.json -o ./types -``` - -Note: - -- `-i`: the relative path/global to the ABI JSON file(s) -- `-o`: the output directory for the generated types diff --git a/docs/_guide/abi-typegen/generating-types-from-abi.md b/docs/_guide/abi-typegen/generating-types-from-abi.md new file mode 100644 index 00000000000..b4f7ba6d7e8 --- /dev/null +++ b/docs/_guide/abi-typegen/generating-types-from-abi.md @@ -0,0 +1,64 @@ +[nav_order: 0] + +# Generating Types from ABI + +## Installation + +First we install `fuels` to our project: + +```sh +yarn add fuels +``` + +## Help + +A first glance at the docs: + +```console +$ yarn exec fuels typegen -h + +Usage: fuels typegen [options] + +generate typescript from contract abi json files + +Options: + -i, --inputs directory path for generated files + -c, --contract generate code for contracts [default] + -s, --script generate code for scripts + --silent omit output messages + -h, --help display help for command +``` + +## Generating Types for Contracts + +We can omit the `--contract` option here; its the default: + +```console +yarn exec fuels -i ./abis/*-abi.json -o ./types +``` + +**Notes** + +- `-i`: the relative path/global to the ABI JSON file(s) +- `-o`: the output directory for the generated types +- `-c, --contract`: tells we want to generate types for contracts _(default, can be omitted)_ + +## Generating Types for Scripts + +Note how we make use of the option `--script` in this case: + +```console +yarn exec fuels -i ./abis/*-abi.json -o ./types --script +``` + +**Notes** + +- `-s, --script`: Tells we want to generate types for scripts + +--- + +See also: + +- [Using Generated Contract Types](./using-generated-types.md#using-generated-contract-types) +- [Using Generated Script Types](./using-generated-types.md#using-generated-script-types) diff --git a/docs/_guide/abi-typegen/index.md b/docs/_guide/abi-typegen/index.md index 45601a85448..a5b6f2d315e 100644 --- a/docs/_guide/abi-typegen/index.md +++ b/docs/_guide/abi-typegen/index.md @@ -4,9 +4,11 @@ ## The JSON ABI file -Whether you want to deploy or connect to a pre-existing smart contract, the JSON ABI file is extremely important: it's what tells the SDK about the [ABI methods](https://fuellabs.github.io/sway/v{{site.data.versions.sway}}/book/introduction/sway_quickstart.html?highlight=abi#abi) in your smart contracts. +Whether you want to deploy or connect to a pre-existing smart contract, the [JSON ABI](https://fuellabs.github.io/sway/v{{site.data.versions.sway}}/book/introduction/sway_quickstart.html?highlight=abi#abi) file is what makes it possible. -For the same example Sway code as above: +It tells the SDK about the [ABI methods](https://fuellabs.github.io/sway/v{{site.data.versions.sway}}/book/introduction/sway_quickstart.html?highlight=abi#abi) in your [Smart Contracts](https://fuellabs.github.io/sway/vv{{site.data.versions.sway}}/book/sway-program-types/smart_contracts.html) and [Scripts](https://fuellabs.github.io/sway/vv{{site.data.versions.sway}}/book/sway-program-types/smart_contracts.html). + +Given the following Sway smart contract: ```rust contract; @@ -22,7 +24,7 @@ impl MyContract for Contract { } ``` -The JSON ABI file looks like this: +The JSON ABI file would look something like this: ```json $ cat out/debug/my-test-abi.json @@ -42,4 +44,7 @@ $ cat out/debug/my-test-abi.json ] ``` -The Fuel TypeScript SDK will take this file as input and generate equivalent methods (and custom types if applicable) that you can call from your TypeScript code. +See also: + +- [Generating Contract Types](./generating-types-from-abi.md) +- [Generating Contract Types](./generating-types-from-abi.md) diff --git a/docs/_guide/abi-typegen/using-generated-types.md b/docs/_guide/abi-typegen/using-generated-types.md index aeec743ce37..c65994fb4c7 100644 --- a/docs/_guide/abi-typegen/using-generated-types.md +++ b/docs/_guide/abi-typegen/using-generated-types.md @@ -1,8 +1,14 @@ [nav_order: 1] -# Using Generated Types +# Using Generated Contracts Types -See also [Generate Contract Types from ABI](./generate-contract-types-from-abi.md). +After generating types via: + +```console +yarn exec fuels -i ./abis/*-abi.json -o ./types +``` + +We can use these files like so: ```ts import { Wallet } from "fuels"; @@ -16,3 +22,22 @@ const contract = MyContract__factory.connect(contractId, wallet); const { transactionId, value } = await contract.functions.my_fn(1n).call(); console.log(transactionId, value); ``` + +# Using Generated Script Types + +After generating types via: + +```console +yarn exec fuels -i ./abis/*-abi.json -o ./types --script +``` + +We can use these files like so: + +```ts +// TODO: Add usage +``` + +See also: + +- [Generating Types for Contracts](./generating-types-from-abi.md#generating-types-for-contracts) +- [Generating Types for Scripts](./generating-types-from-abi.md#generating-types-for-scripts) diff --git a/packages/abi-typegen/src/cli.ts b/packages/abi-typegen/src/cli.ts index 6563e543a21..65aaba7ccd5 100644 --- a/packages/abi-typegen/src/cli.ts +++ b/packages/abi-typegen/src/cli.ts @@ -44,12 +44,12 @@ export function configureCliOptions(program: Command) { .requiredOption('-i, --inputs ', 'input paths/globals to your abi json files') .requiredOption('-o, --output ', 'directory path for generated files') .addOption( - new Option('-c, --contract', 'generate code for contracts [default]') + new Option('-c, --contract', 'generate types for contracts [default]') .conflicts('script') .implies({ script: undefined }) ) .addOption( - new Option('-s, --script', 'generate code for scripts') + new Option('-s, --script', 'generate types for scripts') .conflicts('contract') .implies({ contract: undefined }) ) From d5bb2dfe41577e27fdfe87b7b3d2e7255956e813 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 17:52:39 -0300 Subject: [PATCH 045/108] Fixing scripts URL for Sway docs --- docs/_guide/abi-typegen/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_guide/abi-typegen/index.md b/docs/_guide/abi-typegen/index.md index a5b6f2d315e..8e67ac975b8 100644 --- a/docs/_guide/abi-typegen/index.md +++ b/docs/_guide/abi-typegen/index.md @@ -6,7 +6,7 @@ Whether you want to deploy or connect to a pre-existing smart contract, the [JSON ABI](https://fuellabs.github.io/sway/v{{site.data.versions.sway}}/book/introduction/sway_quickstart.html?highlight=abi#abi) file is what makes it possible. -It tells the SDK about the [ABI methods](https://fuellabs.github.io/sway/v{{site.data.versions.sway}}/book/introduction/sway_quickstart.html?highlight=abi#abi) in your [Smart Contracts](https://fuellabs.github.io/sway/vv{{site.data.versions.sway}}/book/sway-program-types/smart_contracts.html) and [Scripts](https://fuellabs.github.io/sway/vv{{site.data.versions.sway}}/book/sway-program-types/smart_contracts.html). +It tells the SDK about the [ABI methods](https://fuellabs.github.io/sway/v{{site.data.versions.sway}}/book/introduction/sway_quickstart.html?highlight=abi#abi) in your [Smart Contracts](https://fuellabs.github.io/sway/vv{{site.data.versions.sway}}/book/sway-program-types/smart_contracts.html) and [Scripts](https://fuellabs.github.io/sway/vv{{site.data.versions.sway}}/book/sway-program-types/scripts.html). Given the following Sway smart contract: From ebf77f7b475afbc9a6514e8a595d8ae3243d21f8 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 17:54:21 -0300 Subject: [PATCH 046/108] Removing extraneous configs --- .vscode/settings.json | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index a1c30c94133..1ed1ded9d5e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -22,14 +22,5 @@ "[handlebars]": { "editor.formatOnSave": false, "editor.formatOnPaste": false - }, - "cSpell.words": [ - "arrayify", - "ethersproject", - "Numberish", - "struct", - "structs", - "Typedefs", - "Typegen" - ] + } } From 515cb99bca2978f0d5070a1b9a6d81067d7ed1ea Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 18:01:12 -0300 Subject: [PATCH 047/108] Updating help usage, fixing tyop --- packages/abi-typegen/README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/abi-typegen/README.md b/packages/abi-typegen/README.md index adecbeac69b..22d60e98eb2 100644 --- a/packages/abi-typegen/README.md +++ b/packages/abi-typegen/README.md @@ -31,13 +31,16 @@ npm add @fuel-ts/abi-typegen ```console $ fuels-typegen -h -Usage: fuels-typegen -i ../out/*-abi.json -o ./generated/ +Usage: fuels typegen [options] + +generate typescript from contract abi json files Options: - -V, --version output the version number -i, --inputs input paths/globals to your abi json files -o, --output directory path for generated files - -s, --silent omit output messages + -c, --contract generate types for contracts [default] + -s, --script generate types for scripts + --silent omit output messages -h, --help display help for command ``` @@ -49,7 +52,7 @@ When using the package in a standalone fashion, its bin is prefixed with `fuels- npx fuels-typegen -i ./out/debug/*-abi.json -o ./src/contracts ``` -## Programatic API +## Programmatic API ```ts import { runTypegen } from "@fuel-ts/abi-typegen"; From 06f51c3174f346c656752f21365102dd52f749f1 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 18:04:24 -0300 Subject: [PATCH 048/108] Removing useless options --- packages/abi-typegen/src/utils/assembleScripts.test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/abi-typegen/src/utils/assembleScripts.test.ts b/packages/abi-typegen/src/utils/assembleScripts.test.ts index f669129ee18..d6611d4ccf5 100644 --- a/packages/abi-typegen/src/utils/assembleScripts.test.ts +++ b/packages/abi-typegen/src/utils/assembleScripts.test.ts @@ -38,7 +38,6 @@ describe('assembleScripts.ts', () => { } = getNewAbiTypegen({ category: CategoryEnum.SCRIPT, includeOptionType: false, // will prevent common template from being included - // includeMainFunction: true, // scripts must have at least one `main()` method }); jest.resetAllMocks(); @@ -60,7 +59,6 @@ describe('assembleScripts.ts', () => { } = getNewAbiTypegen({ category: CategoryEnum.SCRIPT, includeOptionType: true, // will cause common template to be included - // includeMainFunction: true, // scripts must have at least one `main()` method }); jest.resetAllMocks(); From ee6b41bb611ada12257feaccd86af3dbf233f451 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 18:06:35 -0300 Subject: [PATCH 049/108] Purging dead code --- .../test/fixtures/templates/script/index.hbs | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 packages/abi-typegen/test/fixtures/templates/script/index.hbs diff --git a/packages/abi-typegen/test/fixtures/templates/script/index.hbs b/packages/abi-typegen/test/fixtures/templates/script/index.hbs deleted file mode 100644 index 68f3de2bec2..00000000000 --- a/packages/abi-typegen/test/fixtures/templates/script/index.hbs +++ /dev/null @@ -1,14 +0,0 @@ -/* Autogenerated file. Do not edit manually. */ - -/* tslint:disable */ -/* eslint-disable */ - -/* - Fuels version: 11.11.11 - Forc version: 22.22.22 - Fuel-Core version: 33.33.33 -*/ - -export type { MyContractAbi } from './MyContractAbi'; - -export { MyContractAbi__factory } from './factories/MyContractAbi__factory'; From debc9d3928c0b1788746f7e93206fb284defb5b1 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 18:13:00 -0300 Subject: [PATCH 050/108] Removing temporary draft / placeholder --- packages/script/src/wrapper.ts | 98 ---------------------------------- 1 file changed, 98 deletions(-) delete mode 100644 packages/script/src/wrapper.ts diff --git a/packages/script/src/wrapper.ts b/packages/script/src/wrapper.ts deleted file mode 100644 index 75e019d19a4..00000000000 --- a/packages/script/src/wrapper.ts +++ /dev/null @@ -1,98 +0,0 @@ -import { arrayify } from '@ethersproject/bytes'; -import type { InputValue } from '@fuel-ts/abi-coder'; -import { AbiCoder } from '@fuel-ts/abi-coder'; -import type { IRawAbi } from '@fuel-ts/abi-typegen'; -import type { BigNumberish } from '@fuel-ts/math'; -import { bn } from '@fuel-ts/math'; -import type { CoinQuantityLike } from '@fuel-ts/providers'; -import { ScriptTransactionRequest } from '@fuel-ts/providers'; -import { ReceiptType } from '@fuel-ts/transactions'; -import type { BaseWalletLocked } from '@fuel-ts/wallet'; - -import type { ScriptResult } from './script'; -import { Script } from './script'; - -// Duplicating interface `TxParams` — can't import it normally -// from `@fuel-ts/contract`, due to cyclic dependency problem -export type TxParams = Partial<{ - gasPrice: BigNumberish; - gasLimit: BigNumberish; - variableOutput: number; -}>; - -/** - * This class provides a facade on top of the Script class, - * providing a more fluent API for common script calls. - * - * It draws inspiration from the Contract class, however it - * does not uses an `Interface`, but instead follows the - * `call-script` examples present in the unit tests. - */ -export class ScriptWrapper { - public readonly coder: AbiCoder; - public readonly script: Script; - - public readonly txParameters?: TxParams; - public readonly transactionRequest = new ScriptTransactionRequest(); - - constructor(abi: IRawAbi, bin: Uint8Array) { - this.coder = new AbiCoder(); - - this.script = new Script( - bin, - (data: Inputs) => { - // TODO: Review mismatch: TYPEGEN IRawAbiFunctionIO[] vs CODER 'JsonAbiFragmentType[]' - const encoded = this.coder.encode(abi.functions[0].inputs as any, [data]); - return arrayify(encoded); - }, - (scriptResult: ScriptResult): Output => { - if (scriptResult.returnReceipt.type === ReceiptType.Revert) { - throw new Error('Script call reverted'); - } - if (scriptResult.returnReceipt.type !== ReceiptType.ReturnData) { - throw new Error('Script call failed'); - } - const decoded = this.coder.decode( - // TODO: Review mismatch: TYPEGEN 'IRawAbiFunctionIO' vs CODER 'JsonAbiFragmentType'. - [abi.functions[0].output as any], - scriptResult.returnReceipt.data - ); - return decoded?.[0] as Output; - } - ); - } - - txParams(txParams: TxParams) { - const request = this.transactionRequest; - - request.gasLimit = bn(txParams.gasLimit || request.gasLimit); - request.gasPrice = bn(txParams.gasPrice || request.gasPrice); - - request.addVariableOutputs(this.txParameters?.variableOutput || 0); - - return this; - } - - async call(params: { data: Inputs; wallet: BaseWalletLocked }) { - const request = this.transactionRequest; - - const { script } = this; - const { data, wallet } = params; - - request.setScript(script, data); - - const requiredCoinQuantities: CoinQuantityLike[] = []; - requiredCoinQuantities.push(request.calculateFee()); - - if (requiredCoinQuantities.length) { - const resources = await wallet.getResourcesToSpend(requiredCoinQuantities); - request.addResources(resources); - } - - const response = await wallet.sendTransaction(request); - const transactionResult = await response.waitForResult(); - const result = script.decodeCallResult(transactionResult); - - return { transactionResult, result, response }; - } -} From 5840c8c9a7717e0a28d85ab722e5bbce97f0d633 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 24 Jan 2023 18:22:52 -0300 Subject: [PATCH 051/108] Fixing template; updating test fixture to match --- .../src/templates/script/factory.hbs | 3 +- .../fixtures/templates/script/factory.hbs | 43 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/abi-typegen/src/templates/script/factory.hbs b/packages/abi-typegen/src/templates/script/factory.hbs index e94d006028a..594a80af698 100644 --- a/packages/abi-typegen/src/templates/script/factory.hbs +++ b/packages/abi-typegen/src/templates/script/factory.hbs @@ -32,10 +32,11 @@ export type {{structName}}Output{{typeAnnotations}} = { {{outputValues}} }; type {{capitalizedName}}Inputs = [{{inputs}}]; type {{capitalizedName}}Output = {{output}}; +const _abi: IRawAbi = {{abiJsonString}} export class {{capitalizedName}}__factory { - static readonly abi: IRawAbi = {} as IRawAbi; + static readonly abi: IRawAbi = _abi static readonly bin: Uint8Array = readFileSync('path/to/bin/file'); static createInstance() { diff --git a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs index 2498f59fcf5..46441bcc535 100644 --- a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs +++ b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs @@ -17,9 +17,50 @@ import { ScriptFactory } from 'fuels'; type MyContractAbiInputs = [x: string, y: string]; type MyContractAbiOutput = boolean; +const _abi: IRawAbi = { + "types": [ + { + "typeId": 0, + "type": "bool", + "components": null, + "typeParameters": null + }, + { + "typeId": 1, + "type": "str[10]", + "components": null, + "typeParameters": null + } + ], + "functions": [ + { + "inputs": [ + { + "name": "x", + "type": 1, + "typeArguments": null + }, + { + "name": "y", + "type": 1, + "typeArguments": null + } + ], + "name": "main", + "output": { + "name": "", + "type": 0, + "typeArguments": null + } + } + ], + "loggedTypes": [], + "messagesTypes": [] +} + export class MyContractAbi__factory { - static readonly abi: IRawAbi = {} as IRawAbi; + static readonly abi: IRawAbi = _abi static readonly bin: Uint8Array = readFileSync('path/to/bin/file'); static createInstance() { From d5cb1b7ec16246f902e89de9ea626dca1e006006 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 14:24:05 -0300 Subject: [PATCH 052/108] Extracting code chunks into separate utilities --- .../abi-typegen/src/templates/contract/dts.ts | 38 ++----------------- .../src/templates/utils/assembleEnums.ts | 24 ++++++++++++ .../src/templates/utils/assembleStructs.ts | 26 +++++++++++++ 3 files changed, 54 insertions(+), 34 deletions(-) create mode 100644 packages/abi-typegen/src/templates/utils/assembleEnums.ts create mode 100644 packages/abi-typegen/src/templates/utils/assembleStructs.ts diff --git a/packages/abi-typegen/src/templates/contract/dts.ts b/packages/abi-typegen/src/templates/contract/dts.ts index a06ca4d53ba..95be6be4b7c 100644 --- a/packages/abi-typegen/src/templates/contract/dts.ts +++ b/packages/abi-typegen/src/templates/contract/dts.ts @@ -1,8 +1,7 @@ import type { Abi } from '../../abi/Abi'; -import type { EnumType } from '../../abi/types/EnumType'; -import type { StructType } from '../../abi/types/StructType'; -import { TargetEnum } from '../../types/enums/TargetEnum'; import { renderHbsTemplate } from '../renderHbsTemplate'; +import { assembleEnums } from '../utils/assembleEnums'; +import { assembleStructs } from '../utils/assembleStructs'; import dtsTemplate from './dts.hbs'; @@ -25,37 +24,8 @@ export function renderDtsTemplate(params: { abi: Abi }) { functionName: f.name, })); - const structs = types - .filter((t) => t.name === 'struct') - .map((t) => { - const st = t as StructType; // only structs here - const structName = st.getStructName(); - const inputValues = st.getStructContents({ types, target: TargetEnum.INPUT }); - const outputValues = st.getStructContents({ types, target: TargetEnum.OUTPUT }); - const typeAnnotations = st.getStructDeclaration({ types }); - return { - structName, - typeAnnotations, - inputValues, - outputValues, - recycleRef: inputValues === outputValues, // reduces duplication - }; - }); - - const enums = types - .filter((t) => t.name === 'enum') - .map((t) => { - const et = t as EnumType; // only enums here - const structName = et.getStructName(); - const inputValues = et.getStructContents({ types, target: TargetEnum.INPUT }); - const outputValues = et.getStructContents({ types, target: TargetEnum.OUTPUT }); - return { - structName, - inputValues, - outputValues, - recycleRef: inputValues === outputValues, // reduces duplication - }; - }); + const enums = assembleEnums({ types }); + const structs = assembleStructs({ types }); /* And finally render template diff --git a/packages/abi-typegen/src/templates/utils/assembleEnums.ts b/packages/abi-typegen/src/templates/utils/assembleEnums.ts new file mode 100644 index 00000000000..85c33212475 --- /dev/null +++ b/packages/abi-typegen/src/templates/utils/assembleEnums.ts @@ -0,0 +1,24 @@ +import type { EnumType } from '../../abi/types/EnumType'; +import { TargetEnum } from '../../types/enums/TargetEnum'; +import type { IType } from '../../types/interfaces/IType'; + +export function assembleEnums(params: { types: IType[] }) { + const { types } = params; + + const enums = types + .filter((t) => t.name === 'enum') + .map((t) => { + const et = t as EnumType; // only enums here + const structName = et.getStructName(); + const inputValues = et.getStructContents({ types, target: TargetEnum.INPUT }); + const outputValues = et.getStructContents({ types, target: TargetEnum.OUTPUT }); + return { + structName, + inputValues, + outputValues, + recycleRef: inputValues === outputValues, // reduces duplication + }; + }); + + return enums; +} diff --git a/packages/abi-typegen/src/templates/utils/assembleStructs.ts b/packages/abi-typegen/src/templates/utils/assembleStructs.ts new file mode 100644 index 00000000000..c26664fb6c0 --- /dev/null +++ b/packages/abi-typegen/src/templates/utils/assembleStructs.ts @@ -0,0 +1,26 @@ +import type { StructType } from '../../abi/types/StructType'; +import { TargetEnum } from '../../types/enums/TargetEnum'; +import type { IType } from '../../types/interfaces/IType'; + +export function assembleStructs(params: { types: IType[] }) { + const { types } = params; + + const structs = types + .filter((t) => t.name === 'struct') + .map((t) => { + const st = t as StructType; // only structs here + const structName = st.getStructName(); + const inputValues = st.getStructContents({ types, target: TargetEnum.INPUT }); + const outputValues = st.getStructContents({ types, target: TargetEnum.OUTPUT }); + const typeAnnotations = st.getStructDeclaration({ types }); + return { + structName, + typeAnnotations, + inputValues, + outputValues, + recycleRef: inputValues === outputValues, // reduces duplication + }; + }); + + return structs; +} From d3e5cf35814e15bcf66fce1e278ae17b60c0b93c Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 14:24:44 -0300 Subject: [PATCH 053/108] Adding `@ethersproject/bytes` to the mix --- packages/abi-typegen/package.json | 1 + pnpm-lock.yaml | 2 ++ 2 files changed, 3 insertions(+) diff --git a/packages/abi-typegen/package.json b/packages/abi-typegen/package.json index cef8823c2b2..de79295eafa 100644 --- a/packages/abi-typegen/package.json +++ b/packages/abi-typegen/package.json @@ -33,6 +33,7 @@ ], "license": "Apache-2.0", "dependencies": { + "@ethersproject/bytes": "^5.7.0", "@fuel-ts/versions": "workspace:*", "commander": "^9.4.1", "glob": "^8.0.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b87c7ba90b0..e6cf0a939ec 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -124,6 +124,7 @@ importers: packages/abi-typegen: specifiers: + '@ethersproject/bytes': ^5.7.0 '@fuel-ts/versions': workspace:* '@types/glob': ^8.0.0 '@types/lodash.upperfirst': ^4.3.7 @@ -137,6 +138,7 @@ importers: mkdirp: ^1.0.4 rimraf: ^3.0.2 dependencies: + '@ethersproject/bytes': 5.7.0 '@fuel-ts/versions': link:../versions commander: 9.4.1 glob: 8.0.3 From 6f5627cdee401d7894c25d9c78990fd07f790eaa Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 14:29:50 -0300 Subject: [PATCH 054/108] Updating script/factory template, adjusting data format and rendering --- .../src/templates/script/factory.hbs | 17 +++++++++-------- .../abi-typegen/src/templates/script/factory.ts | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/packages/abi-typegen/src/templates/script/factory.hbs b/packages/abi-typegen/src/templates/script/factory.hbs index 594a80af698..364b9521e9d 100644 --- a/packages/abi-typegen/src/templates/script/factory.hbs +++ b/packages/abi-typegen/src/templates/script/factory.hbs @@ -2,8 +2,7 @@ import { readFileSync } from 'fs'; -import type { IRawAbi, BigNumberish } from 'fuels'; -import { ScriptFactory } from 'fuels'; +import type { BigNumberish, BaseWalletLocked, Provider, ScriptFactory } from 'fuels'; {{#if commonTypesInUse}} import type { {{commonTypesInUse}} } from "./common"; @@ -32,21 +31,23 @@ export type {{structName}}Output{{typeAnnotations}} = { {{outputValues}} }; type {{capitalizedName}}Inputs = [{{inputs}}]; type {{capitalizedName}}Output = {{output}}; -const _abi: IRawAbi = {{abiJsonString}} +const _abi = {{abiJsonString}} + +const _bin = '{{hexlifiedBinString}}' export class {{capitalizedName}}__factory { - static readonly abi: IRawAbi = _abi - static readonly bin: Uint8Array = readFileSync('path/to/bin/file'); + static readonly abi = _abi + static readonly bin = _bin - static createInstance() { + static createInstance(walletOrProvider: BaseWalletLocked | Provider) { const { abi, bin } = {{capitalizedName}}__factory const script = new ScriptFactory< {{capitalizedName}}Inputs, - {{capitalizedName}}Output - >({ abi, bin }); + {{capitalizedName}}Output, + >(bin, abi, walletOrProvider); return script; diff --git a/packages/abi-typegen/src/templates/script/factory.ts b/packages/abi-typegen/src/templates/script/factory.ts index 84ddd9824a2..0123acb2e7d 100644 --- a/packages/abi-typegen/src/templates/script/factory.ts +++ b/packages/abi-typegen/src/templates/script/factory.ts @@ -1,11 +1,20 @@ import type { Abi } from '../../abi/Abi'; import { renderHbsTemplate } from '../renderHbsTemplate'; +import { assembleEnums } from '../utils/assembleEnums'; +import { assembleStructs } from '../utils/assembleStructs'; import factoryTemplate from './factory.hbs'; export function renderFactoryTemplate(params: { abi: Abi }) { const { abi } = params; - const { name: capitalizedName, rawContents } = params.abi; + + const { types } = abi; + + const { + rawContents, + name: capitalizedName, + hexlifiedBinContents: hexlifiedBinString, + } = params.abi; const abiJsonString = JSON.stringify(rawContents, null, 2); @@ -15,6 +24,9 @@ export function renderFactoryTemplate(params: { abi: Abi }) { throw new Error(`ABI doesn't have a 'main()' method.`); } + const enums = assembleEnums({ types }); + const structs = assembleStructs({ types }); + const { prefixedInputs: inputs, output } = func.attributes; const text = renderHbsTemplate({ @@ -22,7 +34,10 @@ export function renderFactoryTemplate(params: { abi: Abi }) { data: { inputs, output, + structs, + enums, abiJsonString, + hexlifiedBinString, capitalizedName, }, }); From 2359d18ab27a85df03bc33f4d4101fe012c43d0c Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 14:31:09 -0300 Subject: [PATCH 055/108] Adding new validation utility --- .../abi-typegen/src/utils/validateBinFile.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 packages/abi-typegen/src/utils/validateBinFile.ts diff --git a/packages/abi-typegen/src/utils/validateBinFile.ts b/packages/abi-typegen/src/utils/validateBinFile.ts new file mode 100644 index 00000000000..136809a306d --- /dev/null +++ b/packages/abi-typegen/src/utils/validateBinFile.ts @@ -0,0 +1,23 @@ +import { CategoryEnum } from '../types/enums/CategoryEnum'; + +export function validateBinFile(params: { + abiFilepath: string; + binFilepath: string; + binExists: boolean; + category: CategoryEnum; +}) { + const { abiFilepath, binFilepath, binExists, category } = params; + + const isNotAContract = category !== CategoryEnum.CONTRACT; + + if (!binExists && isNotAContract) { + throw new Error( + [ + 'Could not find [required] BIN file for given ABI.', + `- ABI: ${abiFilepath}`, + `- BIN: ${binFilepath}`, + category, + ].join('\n') + ); + } +} From 6f8535adad5caf91de383ebd900095502658342f Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 14:32:07 -0300 Subject: [PATCH 056/108] Start handling BIN files alongside their ABI counterparts --- packages/abi-typegen/src/AbiTypeGen.ts | 29 +++++++++++++++++++++--- packages/abi-typegen/src/abi/Abi.ts | 13 +++++++++-- packages/abi-typegen/src/runTypegen.ts | 31 ++++++++++++++++++++++---- 3 files changed, 64 insertions(+), 9 deletions(-) diff --git a/packages/abi-typegen/src/AbiTypeGen.ts b/packages/abi-typegen/src/AbiTypeGen.ts index 45ec5fe5fe8..5913cd37bbd 100644 --- a/packages/abi-typegen/src/AbiTypeGen.ts +++ b/packages/abi-typegen/src/AbiTypeGen.ts @@ -3,6 +3,7 @@ import { CategoryEnum } from './types/enums/CategoryEnum'; import type { IFile } from './types/interfaces/IFile'; import { assembleContracts } from './utils/assembleContracts'; import { assembleScripts } from './utils/assembleScripts'; +import { validateBinFile } from './utils/validateBinFile'; /* Manages many instances of Abi @@ -10,23 +11,45 @@ import { assembleScripts } from './utils/assembleScripts'; export class AbiTypeGen { public readonly abis: Abi[]; public readonly abiFiles: IFile[]; + public readonly binFiles: IFile[]; public readonly outputDir: string; public readonly files: IFile[]; - constructor(params: { abiFiles: IFile[]; outputDir: string; category: CategoryEnum }) { - const { abiFiles, outputDir, category } = params; + constructor(params: { + abiFiles: IFile[]; + binFiles?: IFile[]; + outputDir: string; + category: CategoryEnum; + }) { + const { abiFiles, binFiles = [], outputDir, category } = params; this.outputDir = outputDir; + this.abiFiles = abiFiles; + this.binFiles = binFiles; // Creates a `Abi` for each abi file this.abis = this.abiFiles.map((abiFile) => { + const binFilepath = abiFile.path.replace('-abi.json', '.bin'); + const relatedBinFile = this.binFiles.find(({ path }) => path === binFilepath); + + if (!relatedBinFile) { + validateBinFile({ + abiFilepath: abiFile.path, + binExists: !!relatedBinFile, + binFilepath, + category, + }); + } + const abi = new Abi({ filepath: abiFile.path, - rawContents: JSON.parse(abiFile.contents), + rawContents: JSON.parse(abiFile.contents as string), + hexlifiedBinContents: relatedBinFile?.contents, outputDir, }); + return abi; }); diff --git a/packages/abi-typegen/src/abi/Abi.ts b/packages/abi-typegen/src/abi/Abi.ts index c6a08faae14..af49aaa358b 100644 --- a/packages/abi-typegen/src/abi/Abi.ts +++ b/packages/abi-typegen/src/abi/Abi.ts @@ -16,11 +16,18 @@ export class Abi { public commonTypesInUse: string[] = []; public rawContents: IRawAbi; + public hexlifiedBinContents?: string; + public types: IType[]; public functions: IFunction[]; - constructor(params: { filepath: string; outputDir: string; rawContents: IRawAbi }) { - const { filepath, outputDir, rawContents } = params; + constructor(params: { + filepath: string; + outputDir: string; + rawContents: IRawAbi; + hexlifiedBinContents?: string; + }) { + const { filepath, outputDir, rawContents, hexlifiedBinContents } = params; const abiNameRegex = /([^/]+)-abi\.json$/m; const abiName = filepath.match(abiNameRegex); @@ -34,8 +41,10 @@ export class Abi { const name = `${normalizeName(abiName[1])}Abi`; this.name = name; + this.filepath = filepath; this.rawContents = rawContents; + this.hexlifiedBinContents = hexlifiedBinContents; this.outputDir = outputDir; const { types, functions } = this.parse(); diff --git a/packages/abi-typegen/src/runTypegen.ts b/packages/abi-typegen/src/runTypegen.ts index 8f5fd50c6f6..223d271ec9a 100644 --- a/packages/abi-typegen/src/runTypegen.ts +++ b/packages/abi-typegen/src/runTypegen.ts @@ -1,12 +1,14 @@ -import { readFileSync, writeFileSync } from 'fs'; +import { hexlify } from '@ethersproject/bytes'; +import { existsSync, readFileSync, writeFileSync } from 'fs'; import { sync as globSync } from 'glob'; import mkdirp from 'mkdirp'; import { basename } from 'path'; import rimraf from 'rimraf'; import { AbiTypeGen } from './AbiTypeGen'; -import type { CategoryEnum } from './types/enums/CategoryEnum'; +import { CategoryEnum } from './types/enums/CategoryEnum'; import type { IFile } from './types/interfaces/IFile'; +import { validateBinFile } from './utils/validateBinFile'; export interface IGenerateFilesParams { cwd: string; @@ -44,19 +46,40 @@ export function runTypegen(params: IGenerateFilesParams) { Assembling file paths x contents */ const abiFiles = filepaths.map((filepath) => { - const file: IFile = { + const abi: IFile = { path: filepath, contents: readFileSync(filepath, 'utf-8'), }; - return file; + return abi; }); + const isContract = category === CategoryEnum.CONTRACT; + + const binFiles = isContract + ? [] + : filepaths.map((abiFilepath) => { + const binFilepath = abiFilepath.replace('-abi.json', '.bin'); + const binExists = existsSync(binFilepath); + + if (!binExists) { + validateBinFile({ abiFilepath, binFilepath, binExists, category }); + } + + const bin: IFile = { + path: binFilepath, + contents: hexlify(readFileSync(binFilepath)), + }; + + return bin; + }); + /* Starting the engine */ const abiTypeGen = new AbiTypeGen({ outputDir: output, abiFiles, + binFiles, category, }); From 7ec043d88cc650b742adc33eb3b9612c41b60599 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 14:38:15 -0300 Subject: [PATCH 057/108] Adding test stubs/placeholders --- .../abi-typegen/src/templates/utils/assembleEnums.test.ts | 7 +++++++ .../src/templates/utils/assembleStructs.test.ts | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 packages/abi-typegen/src/templates/utils/assembleEnums.test.ts create mode 100644 packages/abi-typegen/src/templates/utils/assembleStructs.test.ts diff --git a/packages/abi-typegen/src/templates/utils/assembleEnums.test.ts b/packages/abi-typegen/src/templates/utils/assembleEnums.test.ts new file mode 100644 index 00000000000..3283fce1097 --- /dev/null +++ b/packages/abi-typegen/src/templates/utils/assembleEnums.test.ts @@ -0,0 +1,7 @@ +import { assembleEnums } from './assembleEnums'; + +describe('assembleEnums.ts', () => { + test('should assemble enums just fine', () => { + expect(assembleEnums).toBeTruthy(); + }); +}); diff --git a/packages/abi-typegen/src/templates/utils/assembleStructs.test.ts b/packages/abi-typegen/src/templates/utils/assembleStructs.test.ts new file mode 100644 index 00000000000..b7d97ad934a --- /dev/null +++ b/packages/abi-typegen/src/templates/utils/assembleStructs.test.ts @@ -0,0 +1,7 @@ +import { assembleStructs } from './assembleStructs'; + +describe('assembleStructs.ts', () => { + test('should assemble structs just fine', () => { + expect(assembleStructs).toBeTruthy(); + }); +}); From 4a5399d59f18351fa0238665a6456f3155598334 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 15:22:45 -0300 Subject: [PATCH 058/108] Adjusting virtual filename for Script in tests --- packages/abi-typegen/src/templates/script/factory.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts index 3c1384b6160..81452652b69 100644 --- a/packages/abi-typegen/src/templates/script/factory.test.ts +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -17,7 +17,7 @@ describe('factory.ts', () => { const { rawContents } = compileSwayToJson({ contractPath }); const abi = new Abi({ - filepath: './my-contract-abi.json', + filepath: './my-script-abi.json', outputDir: 'stdout', rawContents, }); @@ -37,7 +37,7 @@ describe('factory.ts', () => { }).typegen.abis[0]; const abi = new Abi({ - filepath: './my-contract-abi.json', + filepath: './my-script-abi.json', outputDir: 'stdout', rawContents, }); From 7568660b98ad68f75e6fd39039cab8ea8299f217 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 15:24:35 -0300 Subject: [PATCH 059/108] Updating template fixture --- .../fixtures/templates/script/factory.hbs | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs index 46441bcc535..098c434bca2 100644 --- a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs +++ b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs @@ -11,13 +11,12 @@ import { readFileSync } from 'fs'; -import type { IRawAbi, BigNumberish } from 'fuels'; -import { ScriptFactory } from 'fuels'; +import type { BigNumberish, BaseWalletLocked, Provider, ScriptFactory } from 'fuels'; -type MyContractAbiInputs = [x: string, y: string]; -type MyContractAbiOutput = boolean; +type MyScriptAbiInputs = [x: string, y: string]; +type MyScriptAbiOutput = boolean; -const _abi: IRawAbi = { +const _abi = { "types": [ { "typeId": 0, @@ -58,19 +57,21 @@ const _abi: IRawAbi = { "messagesTypes": [] } -export class MyContractAbi__factory { +const _bin = '' - static readonly abi: IRawAbi = _abi - static readonly bin: Uint8Array = readFileSync('path/to/bin/file'); +export class MyScriptAbi__factory { - static createInstance() { + static readonly abi = _abi + static readonly bin = _bin - const { abi, bin } = MyContractAbi__factory + static createInstance(walletOrProvider: BaseWalletLocked | Provider) { + + const { abi, bin } = MyScriptAbi__factory const script = new ScriptFactory< - MyContractAbiInputs, - MyContractAbiOutput - >({ abi, bin }); + MyScriptAbiInputs, + MyScriptAbiOutput, + >(bin, abi, walletOrProvider); return script; From f74f65e0b94c533704684fb8f8f2c8cde8394f6b Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 15:24:50 -0300 Subject: [PATCH 060/108] Temporarily skipping broken tests, adding TODOs --- packages/abi-typegen/src/AbiTypeGen.test.ts | 6 ++++-- packages/abi-typegen/src/templates/script/factory.test.ts | 1 + packages/abi-typegen/src/utils/assembleScripts.test.ts | 6 ++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/abi-typegen/src/AbiTypeGen.test.ts b/packages/abi-typegen/src/AbiTypeGen.test.ts index 907871a6ab1..d1018ba2c12 100644 --- a/packages/abi-typegen/src/AbiTypeGen.test.ts +++ b/packages/abi-typegen/src/AbiTypeGen.test.ts @@ -36,7 +36,8 @@ describe('AbiTypegen.ts', () => { expect(assembleScripts).toHaveBeenCalledTimes(0); }); - test('should create multiple ABI instances for: scripts', async () => { + // TODO: fix and un-skip test + test.skip('should create multiple ABI instances for: scripts', async () => { const { assembleContracts, assembleScripts } = mockAllDeps(); const category = CategoryEnum.SCRIPT; @@ -49,7 +50,8 @@ describe('AbiTypegen.ts', () => { expect(assembleScripts).toHaveBeenCalledTimes(1); }); - test('should throw for unknown category', async () => { + // TODO: fix and un-skip test + test.skip('should throw for unknown category', async () => { const { assembleContracts, assembleScripts } = mockAllDeps(); const category = 'nope' as any; diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts index 81452652b69..450047f999d 100644 --- a/packages/abi-typegen/src/templates/script/factory.test.ts +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -14,6 +14,7 @@ describe('factory.ts', () => { const contractPath = contractPaths.minimal; + // TODO: Use a Sway Script instead of a Sway Contract const { rawContents } = compileSwayToJson({ contractPath }); const abi = new Abi({ diff --git a/packages/abi-typegen/src/utils/assembleScripts.test.ts b/packages/abi-typegen/src/utils/assembleScripts.test.ts index d6611d4ccf5..79e127e1be9 100644 --- a/packages/abi-typegen/src/utils/assembleScripts.test.ts +++ b/packages/abi-typegen/src/utils/assembleScripts.test.ts @@ -30,7 +30,8 @@ describe('assembleScripts.ts', () => { beforeEach(jest.resetAllMocks); afterEach(jest.restoreAllMocks); - test('should assemble all files from Script ABI ', () => { + // TODO: fix and un-skip test + test.skip('should assemble all files from Script ABI ', () => { const { renderCommonTemplate, renderFactoryTemplate, renderIndexTemplate } = mockAllDeps(); const { @@ -51,7 +52,8 @@ describe('assembleScripts.ts', () => { expect(renderIndexTemplate).toHaveBeenCalledTimes(1); }); - test('should assemble all files from Script ABI, including `common` file', () => { + // TODO: fix and un-skip test + test.skip('should assemble all files from Script ABI, including `common` file', () => { const { renderCommonTemplate, renderFactoryTemplate, renderIndexTemplate } = mockAllDeps(); const { From 980e7410ad3ad447814e01c2a9f1c1faf4fcfb99 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 17:43:39 -0300 Subject: [PATCH 061/108] Improving test utility, adding options required for dealing w/ Scripts --- .../test/utils/getNewAbiTypegen.ts | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/abi-typegen/test/utils/getNewAbiTypegen.ts b/packages/abi-typegen/test/utils/getNewAbiTypegen.ts index a856f041092..8c328b719fb 100644 --- a/packages/abi-typegen/test/utils/getNewAbiTypegen.ts +++ b/packages/abi-typegen/test/utils/getNewAbiTypegen.ts @@ -7,12 +7,14 @@ export function getNewAbiTypegen( category?: CategoryEnum; includeOptionType?: boolean; includeMainFunction?: boolean; + includeBinFiles?: boolean; } = {} ) { const { includeOptionType = false, category = CategoryEnum.CONTRACT, includeMainFunction = false, + includeBinFiles = false, } = params; const optionType: IRawAbiTypeRoot = { @@ -67,9 +69,11 @@ export function getNewAbiTypegen( typeArguments: null, }, }; + const functions = includeMainFunction ? [main] : []; const stubAbi = JSON.stringify({ types, functions }, null, 2); + const stubBin = '0x000'; const abiFiles: IFile[] = [ { @@ -82,9 +86,25 @@ export function getNewAbiTypegen( }, ]; + const binFiles: IFile[] = [ + { + path: './first.bin', + contents: stubBin, + }, + { + path: './second.bin', + contents: stubBin, + }, + ]; + const outputDir = './contracts'; - const typegen = new AbiTypeGen({ abiFiles, outputDir, category }); + const typegen = new AbiTypeGen({ + binFiles: includeBinFiles ? binFiles : [], + abiFiles, + outputDir, + category, + }); return { typegen }; } From 7baa1f6385dcf13922d9434ef7ad68622523d5fe Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 17:45:09 -0300 Subject: [PATCH 062/108] Resolving TODOs, fixing and un-skipping broken tests --- packages/abi-typegen/src/AbiTypeGen.test.ts | 10 ++++------ packages/abi-typegen/src/utils/assembleScripts.test.ts | 10 ++++++---- packages/abi-typegen/src/utils/validateBinFile.ts | 6 +++--- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/abi-typegen/src/AbiTypeGen.test.ts b/packages/abi-typegen/src/AbiTypeGen.test.ts index d1018ba2c12..5e09e39fce2 100644 --- a/packages/abi-typegen/src/AbiTypeGen.test.ts +++ b/packages/abi-typegen/src/AbiTypeGen.test.ts @@ -36,12 +36,11 @@ describe('AbiTypegen.ts', () => { expect(assembleScripts).toHaveBeenCalledTimes(0); }); - // TODO: fix and un-skip test - test.skip('should create multiple ABI instances for: scripts', async () => { + test('should create multiple ABI instances for: scripts', async () => { const { assembleContracts, assembleScripts } = mockAllDeps(); const category = CategoryEnum.SCRIPT; - const { typegen } = getNewAbiTypegen({ category }); + const { typegen } = getNewAbiTypegen({ category, includeBinFiles: true }); expect(typegen).toBeTruthy(); expect(typegen.abis.length).toEqual(2); @@ -50,14 +49,13 @@ describe('AbiTypegen.ts', () => { expect(assembleScripts).toHaveBeenCalledTimes(1); }); - // TODO: fix and un-skip test - test.skip('should throw for unknown category', async () => { + test('should throw for unknown category', async () => { const { assembleContracts, assembleScripts } = mockAllDeps(); const category = 'nope' as any; const { error } = await executeAndCatch(() => { - getNewAbiTypegen({ category }); + getNewAbiTypegen({ category, includeBinFiles: true }); }); expect(error?.message).toMatch(/Invalid Typegen category: nope/); diff --git a/packages/abi-typegen/src/utils/assembleScripts.test.ts b/packages/abi-typegen/src/utils/assembleScripts.test.ts index 79e127e1be9..d818b4ba75f 100644 --- a/packages/abi-typegen/src/utils/assembleScripts.test.ts +++ b/packages/abi-typegen/src/utils/assembleScripts.test.ts @@ -30,8 +30,7 @@ describe('assembleScripts.ts', () => { beforeEach(jest.resetAllMocks); afterEach(jest.restoreAllMocks); - // TODO: fix and un-skip test - test.skip('should assemble all files from Script ABI ', () => { + test('should assemble all files from Script ABI ', () => { const { renderCommonTemplate, renderFactoryTemplate, renderIndexTemplate } = mockAllDeps(); const { @@ -39,6 +38,8 @@ describe('assembleScripts.ts', () => { } = getNewAbiTypegen({ category: CategoryEnum.SCRIPT, includeOptionType: false, // will prevent common template from being included + includeMainFunction: true, + includeBinFiles: true, }); jest.resetAllMocks(); @@ -52,8 +53,7 @@ describe('assembleScripts.ts', () => { expect(renderIndexTemplate).toHaveBeenCalledTimes(1); }); - // TODO: fix and un-skip test - test.skip('should assemble all files from Script ABI, including `common` file', () => { + test('should assemble all files from Script ABI, including `common` file', () => { const { renderCommonTemplate, renderFactoryTemplate, renderIndexTemplate } = mockAllDeps(); const { @@ -61,6 +61,8 @@ describe('assembleScripts.ts', () => { } = getNewAbiTypegen({ category: CategoryEnum.SCRIPT, includeOptionType: true, // will cause common template to be included + includeMainFunction: true, + includeBinFiles: true, }); jest.resetAllMocks(); diff --git a/packages/abi-typegen/src/utils/validateBinFile.ts b/packages/abi-typegen/src/utils/validateBinFile.ts index 136809a306d..ce385a76a1d 100644 --- a/packages/abi-typegen/src/utils/validateBinFile.ts +++ b/packages/abi-typegen/src/utils/validateBinFile.ts @@ -8,12 +8,12 @@ export function validateBinFile(params: { }) { const { abiFilepath, binFilepath, binExists, category } = params; - const isNotAContract = category !== CategoryEnum.CONTRACT; + const isScript = !!~[CategoryEnum.SCRIPT].indexOf(category); - if (!binExists && isNotAContract) { + if (!binExists && isScript) { throw new Error( [ - 'Could not find [required] BIN file for given ABI.', + `Could not find [required] BIN file for — '${category}' ABI.`, `- ABI: ${abiFilepath}`, `- BIN: ${binFilepath}`, category, From c64545d60979c63ec08f2d7d292b8d5dea6da789 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 18:12:43 -0300 Subject: [PATCH 063/108] Adding new Sway Script test fixture --- packages/abi-typegen/test/fixtures/index.ts | 2 ++ packages/abi-typegen/test/fixtures/script.sw | 9 +++++++++ 2 files changed, 11 insertions(+) create mode 100644 packages/abi-typegen/test/fixtures/script.sw diff --git a/packages/abi-typegen/test/fixtures/index.ts b/packages/abi-typegen/test/fixtures/index.ts index 3206b552825..2dd97dc0faf 100644 --- a/packages/abi-typegen/test/fixtures/index.ts +++ b/packages/abi-typegen/test/fixtures/index.ts @@ -11,6 +11,7 @@ const fnVoid = join(fixturesDir, 'fn-void.sw'); const full = join(fixturesDir, 'full.sw'); const minimal = join(fixturesDir, 'minimal.sw'); const optionSimple = join(fixturesDir, 'option-simple.sw'); +const script = join(fixturesDir, 'script.sw'); const structNested = join(fixturesDir, 'struct-nested.sw'); const structSimple = join(fixturesDir, 'struct-simple.sw'); const structWithArray = join(fixturesDir, 'struct-with-array.sw'); @@ -27,6 +28,7 @@ export const contractPaths: { [key: string]: string } = { full, minimal, optionSimple, + script, structNested, structSimple, structWithArray, diff --git a/packages/abi-typegen/test/fixtures/script.sw b/packages/abi-typegen/test/fixtures/script.sw new file mode 100644 index 00000000000..d329e15f619 --- /dev/null +++ b/packages/abi-typegen/test/fixtures/script.sw @@ -0,0 +1,9 @@ +script; + +struct Score { + points: u8 +} + +fn main(points: u8) -> Score { + return Score { points }; +} From 7d70f862a9829b3559b83971be55652c274669a4 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 18:13:30 -0300 Subject: [PATCH 064/108] Narrowing Script conditional --- packages/abi-typegen/src/runTypegen.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/abi-typegen/src/runTypegen.ts b/packages/abi-typegen/src/runTypegen.ts index 223d271ec9a..3c1d9daebd0 100644 --- a/packages/abi-typegen/src/runTypegen.ts +++ b/packages/abi-typegen/src/runTypegen.ts @@ -53,9 +53,9 @@ export function runTypegen(params: IGenerateFilesParams) { return abi; }); - const isContract = category === CategoryEnum.CONTRACT; + const isScript = category === CategoryEnum.SCRIPT; - const binFiles = isContract + const binFiles = !isScript ? [] : filepaths.map((abiFilepath) => { const binFilepath = abiFilepath.replace('-abi.json', '.bin'); From b1fe166dac42aa433d130cb80d8a94c712db176d Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 18:14:24 -0300 Subject: [PATCH 065/108] Updating error message --- packages/abi-typegen/src/utils/validateBinFile.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/abi-typegen/src/utils/validateBinFile.ts b/packages/abi-typegen/src/utils/validateBinFile.ts index ce385a76a1d..cfcb7ac3abb 100644 --- a/packages/abi-typegen/src/utils/validateBinFile.ts +++ b/packages/abi-typegen/src/utils/validateBinFile.ts @@ -1,3 +1,5 @@ +import upperFirst from 'lodash.upperfirst'; + import { CategoryEnum } from '../types/enums/CategoryEnum'; export function validateBinFile(params: { @@ -13,7 +15,7 @@ export function validateBinFile(params: { if (!binExists && isScript) { throw new Error( [ - `Could not find [required] BIN file for — '${category}' ABI.`, + `Could not find BIN file for counterpart ${upperFirst(category)} ABI.`, `- ABI: ${abiFilepath}`, `- BIN: ${binFilepath}`, category, From ad0a8c92e7b246714dbd2b910d3f28e02b55adeb Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 18:15:45 -0300 Subject: [PATCH 066/108] Making parameter required --- packages/abi-typegen/src/AbiTypeGen.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/abi-typegen/src/AbiTypeGen.ts b/packages/abi-typegen/src/AbiTypeGen.ts index 5913cd37bbd..078d06ca190 100644 --- a/packages/abi-typegen/src/AbiTypeGen.ts +++ b/packages/abi-typegen/src/AbiTypeGen.ts @@ -18,11 +18,11 @@ export class AbiTypeGen { constructor(params: { abiFiles: IFile[]; - binFiles?: IFile[]; + binFiles: IFile[]; outputDir: string; category: CategoryEnum; }) { - const { abiFiles, binFiles = [], outputDir, category } = params; + const { abiFiles, binFiles, outputDir, category } = params; this.outputDir = outputDir; From 7cfe83251e294e6f0ea3ea717604a704e8a296b3 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 18:17:01 -0300 Subject: [PATCH 067/108] Adding missing test cases; stressing errors related to Scripts --- packages/abi-typegen/src/runTypegen.test.ts | 93 ++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/packages/abi-typegen/src/runTypegen.test.ts b/packages/abi-typegen/src/runTypegen.test.ts index fbd5480d2d8..e2a40c364dc 100644 --- a/packages/abi-typegen/src/runTypegen.test.ts +++ b/packages/abi-typegen/src/runTypegen.test.ts @@ -1,4 +1,4 @@ -import { existsSync } from 'fs'; +import { existsSync, rmSync } from 'fs'; import { sync as globSync } from 'glob'; import { join } from 'path'; import rimraf from 'rimraf'; @@ -120,6 +120,97 @@ describe('runTypegen.js', () => { rimraf.sync(tempDir); }); + test('should run typegen for Scripts, using: filepaths', async () => { + // setup temp sway project + const contractPath = contractPaths.script; + const autoBuild = true; + + const { tempDir, normalizedContractName } = createTempSwayProject({ + contractPath, + autoBuild, + }); + + // compute filepaths + const cwd = process.cwd(); + const input = join(tempDir, '/out/debug/*-abi.json'); + const output = join(tempDir, 'generated'); + const category = CategoryEnum.SCRIPT; + const silent = true; + + const filepaths = globSync(input, { cwd }); + + // executes program + const fn = () => + runTypegen({ + cwd, + filepaths, + output, + category, + silent, + }); + + const { error } = await executeAndCatch(fn); + + // validates execution was ok + expect(error).toBeFalsy(); + + // check if all files were created + const files = [ + join(output, 'index.ts'), + join(output, 'factories', `${normalizedContractName}Abi__factory.ts`), + ]; + + expect(files.length).toEqual(2); + + files.forEach((f) => { + expect(existsSync(f)).toEqual(true); + }); + + rimraf.sync(tempDir); + }); + + test('should raise error for non-existent Script BIN file', async () => { + // setup temp sway project + const contractPath = contractPaths.script; + const autoBuild = true; + + const { tempDir } = createTempSwayProject({ + contractPath, + autoBuild, + }); + + // IMPORTANT: deletes bin file from disk to yield error + const binFilepath = join(tempDir, 'out', 'debug', 'script.bin'); + rmSync(binFilepath); + + // compute filepaths + const cwd = process.cwd(); + const input = join(tempDir, '/out/debug/*-abi.json'); + const output = join(tempDir, 'generated'); + const category = CategoryEnum.SCRIPT; + const silent = true; + + const filepaths = globSync(input, { cwd }); + + // executes program via wrapped function + const fn = () => { + runTypegen({ + cwd, + filepaths, + output, + category, + silent, + }); + }; + + const { error } = await executeAndCatch(fn); + + // validates execution was ok + expect(error?.message).toMatch(/Could not find BIN file for counterpart Script ABI\./gm); + + rimraf.sync(tempDir); + }); + test('should warn about minimum parameters', async () => { // setup temp sway project const contractPath = contractPaths.full; From 58170511acb4112691214e76f129ac328de2a0f0 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 18:38:31 -0300 Subject: [PATCH 068/108] Resolving TODO, using Sway script sample, fixing ABI instantiation --- packages/abi-typegen/src/templates/script/factory.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts index 450047f999d..16e5b354947 100644 --- a/packages/abi-typegen/src/templates/script/factory.test.ts +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -12,13 +12,13 @@ describe('factory.ts', () => { test('should render factory template', () => { const { restore } = mockVersions(); - const contractPath = contractPaths.minimal; + const contractPath = contractPaths.script; - // TODO: Use a Sway Script instead of a Sway Contract const { rawContents } = compileSwayToJson({ contractPath }); const abi = new Abi({ filepath: './my-script-abi.json', + hexlifiedBinContents: '0x000', outputDir: 'stdout', rawContents, }); From d7631e0e15479f15b8a572fa9f89f2c5aed2bcee Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 2 Feb 2023 18:39:02 -0300 Subject: [PATCH 069/108] Updating rendered template fixture --- .../fixtures/templates/script/factory.hbs | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs index 098c434bca2..ac769a9f279 100644 --- a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs +++ b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs @@ -13,20 +13,29 @@ import { readFileSync } from 'fs'; import type { BigNumberish, BaseWalletLocked, Provider, ScriptFactory } from 'fuels'; -type MyScriptAbiInputs = [x: string, y: string]; -type MyScriptAbiOutput = boolean; +export type ScoreInput = { points: BigNumberish }; +export type ScoreOutput = { points: number }; + +type MyScriptAbiInputs = [points: BigNumberish]; +type MyScriptAbiOutput = ScoreOutput; const _abi = { "types": [ { "typeId": 0, - "type": "bool", - "components": null, + "type": "struct Score", + "components": [ + { + "name": "points", + "type": 1, + "typeArguments": null + } + ], "typeParameters": null }, { "typeId": 1, - "type": "str[10]", + "type": "u8", "components": null, "typeParameters": null } @@ -35,12 +44,7 @@ const _abi = { { "inputs": [ { - "name": "x", - "type": 1, - "typeArguments": null - }, - { - "name": "y", + "name": "points", "type": 1, "typeArguments": null } @@ -57,7 +61,7 @@ const _abi = { "messagesTypes": [] } -const _bin = '' +const _bin = '0x000' export class MyScriptAbi__factory { From dcc38c7c220204e56b9354ab54f9385c797205c0 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 3 Feb 2023 05:56:36 -0300 Subject: [PATCH 070/108] Removing useless conditional --- packages/abi-typegen/src/runTypegen.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/abi-typegen/src/runTypegen.ts b/packages/abi-typegen/src/runTypegen.ts index 3c1d9daebd0..498c7ed934c 100644 --- a/packages/abi-typegen/src/runTypegen.ts +++ b/packages/abi-typegen/src/runTypegen.ts @@ -61,9 +61,7 @@ export function runTypegen(params: IGenerateFilesParams) { const binFilepath = abiFilepath.replace('-abi.json', '.bin'); const binExists = existsSync(binFilepath); - if (!binExists) { - validateBinFile({ abiFilepath, binFilepath, binExists, category }); - } + validateBinFile({ abiFilepath, binFilepath, binExists, category }); const bin: IFile = { path: binFilepath, From 4c086d354fb1fc193b2127b8c90827ca09c40108 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 3 Feb 2023 05:56:52 -0300 Subject: [PATCH 071/108] Fine-tuning new utility, adding tests --- .../src/utils/validateBinFile.test.ts | 40 +++++++++++++++++++ .../abi-typegen/src/utils/validateBinFile.ts | 6 +-- 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 packages/abi-typegen/src/utils/validateBinFile.test.ts diff --git a/packages/abi-typegen/src/utils/validateBinFile.test.ts b/packages/abi-typegen/src/utils/validateBinFile.test.ts new file mode 100644 index 00000000000..9504be19997 --- /dev/null +++ b/packages/abi-typegen/src/utils/validateBinFile.test.ts @@ -0,0 +1,40 @@ +import { CategoryEnum } from '../types/enums/CategoryEnum'; + +import { validateBinFile } from './validateBinFile'; + +describe('validateBinFile.ts', () => { + test('should not throw for existent Script BIN file', () => { + const params = { + abiFilepath: 'script-abi.json', + binExists: true, + binFilepath: 'script.bin', + category: CategoryEnum.SCRIPT, + }; + + expect(() => validateBinFile(params)).not.toThrow(); + }); + + test('should not throw for non-existent Contract BIN file', () => { + const params = { + abiFilepath: './contract-abi.json', + binExists: false, + binFilepath: './contract.bin', + category: CategoryEnum.CONTRACT, + }; + + expect(() => validateBinFile(params)).not.toThrow(); + }); + + test('should throw for non-existent Script BIN file', () => { + const params = { + abiFilepath: './script-abi.json', + binExists: false, + binFilepath: './script.bin', + category: CategoryEnum.SCRIPT, + }; + + expect(() => validateBinFile(params)).toThrowError( + /Could not find BIN file for counterpart Script ABI./ + ); + }); +}); diff --git a/packages/abi-typegen/src/utils/validateBinFile.ts b/packages/abi-typegen/src/utils/validateBinFile.ts index cfcb7ac3abb..0b54804006d 100644 --- a/packages/abi-typegen/src/utils/validateBinFile.ts +++ b/packages/abi-typegen/src/utils/validateBinFile.ts @@ -10,14 +10,14 @@ export function validateBinFile(params: { }) { const { abiFilepath, binFilepath, binExists, category } = params; - const isScript = !!~[CategoryEnum.SCRIPT].indexOf(category); + const isScript = category === CategoryEnum.SCRIPT; if (!binExists && isScript) { throw new Error( [ `Could not find BIN file for counterpart ${upperFirst(category)} ABI.`, - `- ABI: ${abiFilepath}`, - `- BIN: ${binFilepath}`, + ` - ABI: ${abiFilepath}`, + ` - BIN: ${binFilepath}`, category, ].join('\n') ); From d0f85d6c7fb1c9c5551c4d34ad069f58fea28291 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 3 Feb 2023 05:57:44 -0300 Subject: [PATCH 072/108] Removing needless type definitions; improving auto-complete --- packages/abi-typegen/test/fixtures/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/abi-typegen/test/fixtures/index.ts b/packages/abi-typegen/test/fixtures/index.ts index 2dd97dc0faf..5e50d701c48 100644 --- a/packages/abi-typegen/test/fixtures/index.ts +++ b/packages/abi-typegen/test/fixtures/index.ts @@ -18,7 +18,7 @@ const structWithArray = join(fixturesDir, 'struct-with-array.sw'); const tupleSimple = join(fixturesDir, 'tuple-simple.sw'); const vectorSimple = join(fixturesDir, 'vector-simple.sw'); -export const contractPaths: { [key: string]: string } = { +export const contractPaths = { arrayOfEnums, arrayWithGenerics, enumOfEnums, From 4894ee97e9ce6ce43285d18d8a8aa7d47434333c Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 3 Feb 2023 06:50:12 -0300 Subject: [PATCH 073/108] Implementing tests for new template utilities --- .../src/templates/utils/assembleEnums.test.ts | 30 +++++++++++++- .../templates/utils/assembleStructs.test.ts | 39 ++++++++++++++++++- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/packages/abi-typegen/src/templates/utils/assembleEnums.test.ts b/packages/abi-typegen/src/templates/utils/assembleEnums.test.ts index 3283fce1097..22c470e30ac 100644 --- a/packages/abi-typegen/src/templates/utils/assembleEnums.test.ts +++ b/packages/abi-typegen/src/templates/utils/assembleEnums.test.ts @@ -1,7 +1,33 @@ +import enumOfEnumsAbiJson from '../../../test/fixtures/out/abis/enum-of-enums-abi.json'; +import { Abi } from '../../abi/Abi'; + import { assembleEnums } from './assembleEnums'; describe('assembleEnums.ts', () => { - test('should assemble enums just fine', () => { - expect(assembleEnums).toBeTruthy(); + test('should assemble enums just fine', async () => { + const abi = new Abi({ + filepath: './enum-simple-abi.json', + outputDir: './contracts', + rawContents: enumOfEnumsAbiJson, + }); + + // executing + const enums = assembleEnums({ types: abi.types }); + + // validating + expect(enums).toStrictEqual([ + { + structName: 'LetterEnum', + inputValues: 'a: [], b: [], c: []', + outputValues: 'a: [], b: [], c: []', + recycleRef: true, + }, + { + structName: 'MyEnum', + inputValues: 'letter: LetterEnumInput', + outputValues: 'letter: LetterEnumOutput', + recycleRef: false, + }, + ]); }); }); diff --git a/packages/abi-typegen/src/templates/utils/assembleStructs.test.ts b/packages/abi-typegen/src/templates/utils/assembleStructs.test.ts index b7d97ad934a..a4e745256c1 100644 --- a/packages/abi-typegen/src/templates/utils/assembleStructs.test.ts +++ b/packages/abi-typegen/src/templates/utils/assembleStructs.test.ts @@ -1,7 +1,42 @@ +import structSimpleAbiJson from '../../../test/fixtures/out/abis/struct-simple-abi.json'; +import { Abi } from '../../abi/Abi'; + import { assembleStructs } from './assembleStructs'; describe('assembleStructs.ts', () => { - test('should assemble structs just fine', () => { - expect(assembleStructs).toBeTruthy(); + test('should assemble structs just fine', async () => { + const abi = new Abi({ + filepath: './struct-simple-abi.json', + outputDir: './contracts', + rawContents: structSimpleAbiJson, + }); + + // executing + const structs = assembleStructs({ types: abi.types }); + + // validating + expect(structs).toStrictEqual([ + { + structName: 'StructA', + typeAnnotations: '', + inputValues: 'propA1: T, propA2: U', + outputValues: 'propA1: T, propA2: U', + recycleRef: true, + }, + { + structName: 'StructB', + typeAnnotations: '', + inputValues: 'propB1: T', + outputValues: 'propB1: T', + recycleRef: true, + }, + { + structName: 'StructC', + typeAnnotations: '', + inputValues: 'propC1: StructAInput, BigNumberish>', + outputValues: 'propC1: StructAOutput, number>', + recycleRef: false, + }, + ]); }); }); From 49dd990598e3f9651f89c30211a4101e42b0c934 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 3 Feb 2023 06:56:04 -0300 Subject: [PATCH 074/108] Renaming files and updating their references --- packages/abi-typegen/src/templates/contract/dts.ts | 8 ++++---- packages/abi-typegen/src/templates/script/factory.ts | 8 ++++---- .../utils/{assembleEnums.test.ts => parseEnums.test.ts} | 8 ++++---- .../templates/utils/{assembleEnums.ts => parseEnums.ts} | 4 ++-- .../{assembleStructs.test.ts => parseStructs.test.ts} | 8 ++++---- .../utils/{assembleStructs.ts => parseStructs.ts} | 4 ++-- 6 files changed, 20 insertions(+), 20 deletions(-) rename packages/abi-typegen/src/templates/utils/{assembleEnums.test.ts => parseEnums.test.ts} (78%) rename packages/abi-typegen/src/templates/utils/{assembleEnums.ts => parseEnums.ts} (90%) rename packages/abi-typegen/src/templates/utils/{assembleStructs.test.ts => parseStructs.test.ts} (82%) rename packages/abi-typegen/src/templates/utils/{assembleStructs.ts => parseStructs.ts} (91%) diff --git a/packages/abi-typegen/src/templates/contract/dts.ts b/packages/abi-typegen/src/templates/contract/dts.ts index 95be6be4b7c..2f68232e7b3 100644 --- a/packages/abi-typegen/src/templates/contract/dts.ts +++ b/packages/abi-typegen/src/templates/contract/dts.ts @@ -1,7 +1,7 @@ import type { Abi } from '../../abi/Abi'; import { renderHbsTemplate } from '../renderHbsTemplate'; -import { assembleEnums } from '../utils/assembleEnums'; -import { assembleStructs } from '../utils/assembleStructs'; +import { parseEnums } from '../utils/parseEnums'; +import { parseStructs } from '../utils/parseStructs'; import dtsTemplate from './dts.hbs'; @@ -24,8 +24,8 @@ export function renderDtsTemplate(params: { abi: Abi }) { functionName: f.name, })); - const enums = assembleEnums({ types }); - const structs = assembleStructs({ types }); + const { enums } = parseEnums({ types }); + const { structs } = parseStructs({ types }); /* And finally render template diff --git a/packages/abi-typegen/src/templates/script/factory.ts b/packages/abi-typegen/src/templates/script/factory.ts index 0123acb2e7d..52ebf120cc6 100644 --- a/packages/abi-typegen/src/templates/script/factory.ts +++ b/packages/abi-typegen/src/templates/script/factory.ts @@ -1,7 +1,7 @@ import type { Abi } from '../../abi/Abi'; import { renderHbsTemplate } from '../renderHbsTemplate'; -import { assembleEnums } from '../utils/assembleEnums'; -import { assembleStructs } from '../utils/assembleStructs'; +import { parseEnums } from '../utils/parseEnums'; +import { parseStructs } from '../utils/parseStructs'; import factoryTemplate from './factory.hbs'; @@ -24,8 +24,8 @@ export function renderFactoryTemplate(params: { abi: Abi }) { throw new Error(`ABI doesn't have a 'main()' method.`); } - const enums = assembleEnums({ types }); - const structs = assembleStructs({ types }); + const { enums } = parseEnums({ types }); + const { structs } = parseStructs({ types }); const { prefixedInputs: inputs, output } = func.attributes; diff --git a/packages/abi-typegen/src/templates/utils/assembleEnums.test.ts b/packages/abi-typegen/src/templates/utils/parseEnums.test.ts similarity index 78% rename from packages/abi-typegen/src/templates/utils/assembleEnums.test.ts rename to packages/abi-typegen/src/templates/utils/parseEnums.test.ts index 22c470e30ac..0df86a8c53a 100644 --- a/packages/abi-typegen/src/templates/utils/assembleEnums.test.ts +++ b/packages/abi-typegen/src/templates/utils/parseEnums.test.ts @@ -1,10 +1,10 @@ import enumOfEnumsAbiJson from '../../../test/fixtures/out/abis/enum-of-enums-abi.json'; import { Abi } from '../../abi/Abi'; -import { assembleEnums } from './assembleEnums'; +import { parseEnums } from './parseEnums'; -describe('assembleEnums.ts', () => { - test('should assemble enums just fine', async () => { +describe('parseEnums.ts', () => { + test('should parse enums just fine', async () => { const abi = new Abi({ filepath: './enum-simple-abi.json', outputDir: './contracts', @@ -12,7 +12,7 @@ describe('assembleEnums.ts', () => { }); // executing - const enums = assembleEnums({ types: abi.types }); + const { enums } = parseEnums({ types: abi.types }); // validating expect(enums).toStrictEqual([ diff --git a/packages/abi-typegen/src/templates/utils/assembleEnums.ts b/packages/abi-typegen/src/templates/utils/parseEnums.ts similarity index 90% rename from packages/abi-typegen/src/templates/utils/assembleEnums.ts rename to packages/abi-typegen/src/templates/utils/parseEnums.ts index 85c33212475..ae6fb7e7b65 100644 --- a/packages/abi-typegen/src/templates/utils/assembleEnums.ts +++ b/packages/abi-typegen/src/templates/utils/parseEnums.ts @@ -2,7 +2,7 @@ import type { EnumType } from '../../abi/types/EnumType'; import { TargetEnum } from '../../types/enums/TargetEnum'; import type { IType } from '../../types/interfaces/IType'; -export function assembleEnums(params: { types: IType[] }) { +export function parseEnums(params: { types: IType[] }) { const { types } = params; const enums = types @@ -20,5 +20,5 @@ export function assembleEnums(params: { types: IType[] }) { }; }); - return enums; + return { enums }; } diff --git a/packages/abi-typegen/src/templates/utils/assembleStructs.test.ts b/packages/abi-typegen/src/templates/utils/parseStructs.test.ts similarity index 82% rename from packages/abi-typegen/src/templates/utils/assembleStructs.test.ts rename to packages/abi-typegen/src/templates/utils/parseStructs.test.ts index a4e745256c1..de0a2f8d785 100644 --- a/packages/abi-typegen/src/templates/utils/assembleStructs.test.ts +++ b/packages/abi-typegen/src/templates/utils/parseStructs.test.ts @@ -1,10 +1,10 @@ import structSimpleAbiJson from '../../../test/fixtures/out/abis/struct-simple-abi.json'; import { Abi } from '../../abi/Abi'; -import { assembleStructs } from './assembleStructs'; +import { parseStructs } from './parseStructs'; -describe('assembleStructs.ts', () => { - test('should assemble structs just fine', async () => { +describe('parseStructs.ts', () => { + test('should parse structs just fine', async () => { const abi = new Abi({ filepath: './struct-simple-abi.json', outputDir: './contracts', @@ -12,7 +12,7 @@ describe('assembleStructs.ts', () => { }); // executing - const structs = assembleStructs({ types: abi.types }); + const { structs } = parseStructs({ types: abi.types }); // validating expect(structs).toStrictEqual([ diff --git a/packages/abi-typegen/src/templates/utils/assembleStructs.ts b/packages/abi-typegen/src/templates/utils/parseStructs.ts similarity index 91% rename from packages/abi-typegen/src/templates/utils/assembleStructs.ts rename to packages/abi-typegen/src/templates/utils/parseStructs.ts index c26664fb6c0..07d25a3c4ac 100644 --- a/packages/abi-typegen/src/templates/utils/assembleStructs.ts +++ b/packages/abi-typegen/src/templates/utils/parseStructs.ts @@ -2,7 +2,7 @@ import type { StructType } from '../../abi/types/StructType'; import { TargetEnum } from '../../types/enums/TargetEnum'; import type { IType } from '../../types/interfaces/IType'; -export function assembleStructs(params: { types: IType[] }) { +export function parseStructs(params: { types: IType[] }) { const { types } = params; const structs = types @@ -22,5 +22,5 @@ export function assembleStructs(params: { types: IType[] }) { }; }); - return structs; + return { structs }; } From ce81949c12eb6356730008b1adb8be15f3c2f28d Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 3 Feb 2023 07:01:23 -0300 Subject: [PATCH 075/108] Renaming files and updating their references, again --- packages/abi-typegen/src/templates/contract/dts.ts | 8 ++++---- packages/abi-typegen/src/templates/script/factory.ts | 8 ++++---- .../utils/{parseEnums.test.ts => formatEnums.test.ts} | 8 ++++---- .../src/templates/utils/{parseEnums.ts => formatEnums.ts} | 2 +- .../utils/{parseStructs.test.ts => formatStructs.test.ts} | 8 ++++---- .../templates/utils/{parseStructs.ts => formatStructs.ts} | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) rename packages/abi-typegen/src/templates/utils/{parseEnums.test.ts => formatEnums.test.ts} (78%) rename packages/abi-typegen/src/templates/utils/{parseEnums.ts => formatEnums.ts} (92%) rename packages/abi-typegen/src/templates/utils/{parseStructs.test.ts => formatStructs.test.ts} (83%) rename packages/abi-typegen/src/templates/utils/{parseStructs.ts => formatStructs.ts} (93%) diff --git a/packages/abi-typegen/src/templates/contract/dts.ts b/packages/abi-typegen/src/templates/contract/dts.ts index 2f68232e7b3..38e89ad98a2 100644 --- a/packages/abi-typegen/src/templates/contract/dts.ts +++ b/packages/abi-typegen/src/templates/contract/dts.ts @@ -1,7 +1,7 @@ import type { Abi } from '../../abi/Abi'; import { renderHbsTemplate } from '../renderHbsTemplate'; -import { parseEnums } from '../utils/parseEnums'; -import { parseStructs } from '../utils/parseStructs'; +import { formatEnums } from '../utils/formatEnums'; +import { formatStructs } from '../utils/formatStructs'; import dtsTemplate from './dts.hbs'; @@ -24,8 +24,8 @@ export function renderDtsTemplate(params: { abi: Abi }) { functionName: f.name, })); - const { enums } = parseEnums({ types }); - const { structs } = parseStructs({ types }); + const { enums } = formatEnums({ types }); + const { structs } = formatStructs({ types }); /* And finally render template diff --git a/packages/abi-typegen/src/templates/script/factory.ts b/packages/abi-typegen/src/templates/script/factory.ts index 52ebf120cc6..8268dc24d5f 100644 --- a/packages/abi-typegen/src/templates/script/factory.ts +++ b/packages/abi-typegen/src/templates/script/factory.ts @@ -1,7 +1,7 @@ import type { Abi } from '../../abi/Abi'; import { renderHbsTemplate } from '../renderHbsTemplate'; -import { parseEnums } from '../utils/parseEnums'; -import { parseStructs } from '../utils/parseStructs'; +import { formatEnums } from '../utils/formatEnums'; +import { formatStructs } from '../utils/formatStructs'; import factoryTemplate from './factory.hbs'; @@ -24,8 +24,8 @@ export function renderFactoryTemplate(params: { abi: Abi }) { throw new Error(`ABI doesn't have a 'main()' method.`); } - const { enums } = parseEnums({ types }); - const { structs } = parseStructs({ types }); + const { enums } = formatEnums({ types }); + const { structs } = formatStructs({ types }); const { prefixedInputs: inputs, output } = func.attributes; diff --git a/packages/abi-typegen/src/templates/utils/parseEnums.test.ts b/packages/abi-typegen/src/templates/utils/formatEnums.test.ts similarity index 78% rename from packages/abi-typegen/src/templates/utils/parseEnums.test.ts rename to packages/abi-typegen/src/templates/utils/formatEnums.test.ts index 0df86a8c53a..6706db8baae 100644 --- a/packages/abi-typegen/src/templates/utils/parseEnums.test.ts +++ b/packages/abi-typegen/src/templates/utils/formatEnums.test.ts @@ -1,10 +1,10 @@ import enumOfEnumsAbiJson from '../../../test/fixtures/out/abis/enum-of-enums-abi.json'; import { Abi } from '../../abi/Abi'; -import { parseEnums } from './parseEnums'; +import { formatEnums } from './formatEnums'; -describe('parseEnums.ts', () => { - test('should parse enums just fine', async () => { +describe('formatEnums.ts', () => { + test('should format enums just fine', async () => { const abi = new Abi({ filepath: './enum-simple-abi.json', outputDir: './contracts', @@ -12,7 +12,7 @@ describe('parseEnums.ts', () => { }); // executing - const { enums } = parseEnums({ types: abi.types }); + const { enums } = formatEnums({ types: abi.types }); // validating expect(enums).toStrictEqual([ diff --git a/packages/abi-typegen/src/templates/utils/parseEnums.ts b/packages/abi-typegen/src/templates/utils/formatEnums.ts similarity index 92% rename from packages/abi-typegen/src/templates/utils/parseEnums.ts rename to packages/abi-typegen/src/templates/utils/formatEnums.ts index ae6fb7e7b65..6a11b83c2e9 100644 --- a/packages/abi-typegen/src/templates/utils/parseEnums.ts +++ b/packages/abi-typegen/src/templates/utils/formatEnums.ts @@ -2,7 +2,7 @@ import type { EnumType } from '../../abi/types/EnumType'; import { TargetEnum } from '../../types/enums/TargetEnum'; import type { IType } from '../../types/interfaces/IType'; -export function parseEnums(params: { types: IType[] }) { +export function formatEnums(params: { types: IType[] }) { const { types } = params; const enums = types diff --git a/packages/abi-typegen/src/templates/utils/parseStructs.test.ts b/packages/abi-typegen/src/templates/utils/formatStructs.test.ts similarity index 83% rename from packages/abi-typegen/src/templates/utils/parseStructs.test.ts rename to packages/abi-typegen/src/templates/utils/formatStructs.test.ts index de0a2f8d785..1a1c04e07ba 100644 --- a/packages/abi-typegen/src/templates/utils/parseStructs.test.ts +++ b/packages/abi-typegen/src/templates/utils/formatStructs.test.ts @@ -1,10 +1,10 @@ import structSimpleAbiJson from '../../../test/fixtures/out/abis/struct-simple-abi.json'; import { Abi } from '../../abi/Abi'; -import { parseStructs } from './parseStructs'; +import { formatStructs } from './formatStructs'; -describe('parseStructs.ts', () => { - test('should parse structs just fine', async () => { +describe('formatStructs.ts', () => { + test('should format structs just fine', async () => { const abi = new Abi({ filepath: './struct-simple-abi.json', outputDir: './contracts', @@ -12,7 +12,7 @@ describe('parseStructs.ts', () => { }); // executing - const { structs } = parseStructs({ types: abi.types }); + const { structs } = formatStructs({ types: abi.types }); // validating expect(structs).toStrictEqual([ diff --git a/packages/abi-typegen/src/templates/utils/parseStructs.ts b/packages/abi-typegen/src/templates/utils/formatStructs.ts similarity index 93% rename from packages/abi-typegen/src/templates/utils/parseStructs.ts rename to packages/abi-typegen/src/templates/utils/formatStructs.ts index 07d25a3c4ac..2f7486cc7ca 100644 --- a/packages/abi-typegen/src/templates/utils/parseStructs.ts +++ b/packages/abi-typegen/src/templates/utils/formatStructs.ts @@ -2,7 +2,7 @@ import type { StructType } from '../../abi/types/StructType'; import { TargetEnum } from '../../types/enums/TargetEnum'; import type { IType } from '../../types/interfaces/IType'; -export function parseStructs(params: { types: IType[] }) { +export function formatStructs(params: { types: IType[] }) { const { types } = params; const structs = types From 710cc80bb34343af7c9822c5d828ad6064eef7cd Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 3 Feb 2023 08:02:59 -0300 Subject: [PATCH 076/108] Adding new property to Abi classes --- packages/abi-typegen/src/AbiTypeGen.ts | 1 + packages/abi-typegen/src/abi/Abi.ts | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/abi-typegen/src/AbiTypeGen.ts b/packages/abi-typegen/src/AbiTypeGen.ts index 078d06ca190..a3a225368a5 100644 --- a/packages/abi-typegen/src/AbiTypeGen.ts +++ b/packages/abi-typegen/src/AbiTypeGen.ts @@ -48,6 +48,7 @@ export class AbiTypeGen { rawContents: JSON.parse(abiFile.contents as string), hexlifiedBinContents: relatedBinFile?.contents, outputDir, + category, }); return abi; diff --git a/packages/abi-typegen/src/abi/Abi.ts b/packages/abi-typegen/src/abi/Abi.ts index af49aaa358b..26a059d4074 100644 --- a/packages/abi-typegen/src/abi/Abi.ts +++ b/packages/abi-typegen/src/abi/Abi.ts @@ -1,3 +1,4 @@ +import type { CategoryEnum } from '../types/enums/CategoryEnum'; import type { IFunction } from '../types/interfaces/IFunction'; import type { IRawAbi } from '../types/interfaces/IRawAbi'; import type { IType } from '../types/interfaces/IType'; @@ -10,6 +11,8 @@ import { parseTypes } from '../utils/parseTypes'; */ export class Abi { public name: string; + public category: CategoryEnum; + public filepath: string; public outputDir: string; @@ -23,11 +26,12 @@ export class Abi { constructor(params: { filepath: string; - outputDir: string; + category: CategoryEnum; rawContents: IRawAbi; hexlifiedBinContents?: string; + outputDir: string; }) { - const { filepath, outputDir, rawContents, hexlifiedBinContents } = params; + const { filepath, outputDir, rawContents, hexlifiedBinContents, category } = params; const abiNameRegex = /([^/]+)-abi\.json$/m; const abiName = filepath.match(abiNameRegex); @@ -41,6 +45,7 @@ export class Abi { const name = `${normalizeName(abiName[1])}Abi`; this.name = name; + this.category = category; this.filepath = filepath; this.rawContents = rawContents; From 922d93f451fb06f9103c5a08e3d8268425adec2b Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 3 Feb 2023 08:03:28 -0300 Subject: [PATCH 077/108] Adjusting index template, adding exception for Contracts --- packages/abi-typegen/src/templates/common/index.hbs | 10 ++++++---- packages/abi-typegen/src/templates/common/index.ts | 7 +++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/abi-typegen/src/templates/common/index.hbs b/packages/abi-typegen/src/templates/common/index.hbs index d86a8ffabeb..a8e0b8fc135 100644 --- a/packages/abi-typegen/src/templates/common/index.hbs +++ b/packages/abi-typegen/src/templates/common/index.hbs @@ -1,9 +1,11 @@ {{header}} -{{#each abiCapitalizedNames}} -export type { {{this}} } from './{{this}}'; +{{#if isGeneratingContracts}} +{{#each abis}} +export type { {{name}} } from './{{name}}'; {{/each}} +{{/if}} -{{#each abiCapitalizedNames}} -export { {{this}}__factory } from './factories/{{this}}__factory'; +{{#each abis}} +export { {{name}}__factory } from './factories/{{name}}__factory'; {{/each}} diff --git a/packages/abi-typegen/src/templates/common/index.ts b/packages/abi-typegen/src/templates/common/index.ts index 848345ceb87..17947b06a5b 100644 --- a/packages/abi-typegen/src/templates/common/index.ts +++ b/packages/abi-typegen/src/templates/common/index.ts @@ -1,14 +1,17 @@ import type { Abi } from '../../abi/Abi'; +import { CategoryEnum } from '../../types/enums/CategoryEnum'; import { renderHbsTemplate } from '../renderHbsTemplate'; import indexTemplate from './index.hbs'; export function renderIndexTemplate(params: { abis: Abi[] }) { - const abiCapitalizedNames = params.abis.map(({ name }) => name); + const { abis } = params; + + const isGeneratingContracts = abis[0].category === CategoryEnum.CONTRACT; const text = renderHbsTemplate({ template: indexTemplate, - data: { abiCapitalizedNames }, + data: { abis, isGeneratingContracts }, }); return text; From dc51d049b4a31bf0738d4c74b90cf83751028dbd Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 3 Feb 2023 08:24:19 -0300 Subject: [PATCH 078/108] Adjusting Script factory template --- packages/abi-typegen/src/templates/script/factory.hbs | 6 ++---- .../abi-typegen/test/fixtures/templates/script/factory.hbs | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/abi-typegen/src/templates/script/factory.hbs b/packages/abi-typegen/src/templates/script/factory.hbs index 364b9521e9d..55272a5d84e 100644 --- a/packages/abi-typegen/src/templates/script/factory.hbs +++ b/packages/abi-typegen/src/templates/script/factory.hbs @@ -1,8 +1,6 @@ {{header}} -import { readFileSync } from 'fs'; - -import type { BigNumberish, BaseWalletLocked, Provider, ScriptFactory } from 'fuels'; +import { BigNumberish, BaseWalletLocked, Provider, ScriptFactory } from 'fuels'; {{#if commonTypesInUse}} import type { {{commonTypesInUse}} } from "./common"; @@ -46,7 +44,7 @@ export class {{capitalizedName}}__factory { const script = new ScriptFactory< {{capitalizedName}}Inputs, - {{capitalizedName}}Output, + {{capitalizedName}}Output >(bin, abi, walletOrProvider); return script; diff --git a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs index ac769a9f279..923388d3f65 100644 --- a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs +++ b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs @@ -9,9 +9,7 @@ Fuel-Core version: 33.33.33 */ -import { readFileSync } from 'fs'; - -import type { BigNumberish, BaseWalletLocked, Provider, ScriptFactory } from 'fuels'; +import { BigNumberish, BaseWalletLocked, Provider, ScriptFactory } from 'fuels'; export type ScoreInput = { points: BigNumberish }; export type ScoreOutput = { points: number }; @@ -74,7 +72,7 @@ export class MyScriptAbi__factory { const script = new ScriptFactory< MyScriptAbiInputs, - MyScriptAbiOutput, + MyScriptAbiOutput >(bin, abi, walletOrProvider); return script; From 6b1690333ec73f1c05db1606fd6d2f73709dd9e5 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 3 Feb 2023 08:26:18 -0300 Subject: [PATCH 079/108] Fixing usages of Abi class; updating constructor signature --- packages/abi-typegen/src/abi/Abi.test.ts | 2 ++ packages/abi-typegen/src/templates/common/index.test.ts | 2 ++ packages/abi-typegen/src/templates/contract/dts.test.ts | 5 +++++ packages/abi-typegen/src/templates/contract/factory.test.ts | 2 ++ packages/abi-typegen/src/templates/script/factory.test.ts | 3 +++ packages/abi-typegen/src/templates/utils/formatEnums.test.ts | 2 ++ .../abi-typegen/src/templates/utils/formatStructs.test.ts | 2 ++ 7 files changed, 18 insertions(+) diff --git a/packages/abi-typegen/src/abi/Abi.test.ts b/packages/abi-typegen/src/abi/Abi.test.ts index 2525f22c344..f57bdf81f7c 100644 --- a/packages/abi-typegen/src/abi/Abi.test.ts +++ b/packages/abi-typegen/src/abi/Abi.test.ts @@ -1,6 +1,7 @@ import { contractPaths } from '../../test/fixtures/index'; import { executeAndCatch } from '../../test/utils/executeAndCatch'; import { compileSwayToJson } from '../../test/utils/sway/compileSwayToJson'; +import { CategoryEnum } from '../types/enums/CategoryEnum'; import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType'; import * as parseFunctionsMod from '../utils/parseFunctions'; import * as parseTypesMod from '../utils/parseTypes'; @@ -40,6 +41,7 @@ describe('Abi.ts', () => { filepath: inputPath, outputDir, rawContents, + category: CategoryEnum.CONTRACT, }); return { diff --git a/packages/abi-typegen/src/templates/common/index.test.ts b/packages/abi-typegen/src/templates/common/index.test.ts index 7785db81fd0..f424f5b7af0 100644 --- a/packages/abi-typegen/src/templates/common/index.test.ts +++ b/packages/abi-typegen/src/templates/common/index.test.ts @@ -3,6 +3,7 @@ import indexTemplate from '../../../test/fixtures/templates/contract/index.hbs'; import { mockVersions } from '../../../test/utils/mockVersions'; import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; import { Abi } from '../../abi/Abi'; +import { CategoryEnum } from '../../types/enums/CategoryEnum'; import { renderIndexTemplate } from './index'; @@ -19,6 +20,7 @@ describe('templates/index', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, + category: CategoryEnum.CONTRACT, }); const rendered = renderIndexTemplate({ abis: [abi] }); diff --git a/packages/abi-typegen/src/templates/contract/dts.test.ts b/packages/abi-typegen/src/templates/contract/dts.test.ts index a77e5af5439..41fb3d28904 100644 --- a/packages/abi-typegen/src/templates/contract/dts.test.ts +++ b/packages/abi-typegen/src/templates/contract/dts.test.ts @@ -3,6 +3,7 @@ import expectedDtsFullTemplate from '../../../test/fixtures/templates/contract/d import { mockVersions } from '../../../test/utils/mockVersions'; import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; import { Abi } from '../../abi/Abi'; +import { CategoryEnum } from '../../types/enums/CategoryEnum'; import { renderDtsTemplate } from './dts'; @@ -19,6 +20,7 @@ describe('templates/dts', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, + category: CategoryEnum.CONTRACT, }); const rendered = renderDtsTemplate({ abi }); @@ -36,6 +38,7 @@ describe('templates/dts', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, + category: CategoryEnum.CONTRACT, }); const rendered = renderDtsTemplate({ abi }); @@ -49,6 +52,7 @@ describe('templates/dts', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, + category: CategoryEnum.CONTRACT, }); const rendered = renderDtsTemplate({ abi }); @@ -62,6 +66,7 @@ describe('templates/dts', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, + category: CategoryEnum.CONTRACT, }); const rendered = renderDtsTemplate({ abi }); diff --git a/packages/abi-typegen/src/templates/contract/factory.test.ts b/packages/abi-typegen/src/templates/contract/factory.test.ts index 45eae14951b..3fb271bc6d7 100644 --- a/packages/abi-typegen/src/templates/contract/factory.test.ts +++ b/packages/abi-typegen/src/templates/contract/factory.test.ts @@ -3,6 +3,7 @@ import factoryTemplate from '../../../test/fixtures/templates/contract/factory.h import { mockVersions } from '../../../test/utils/mockVersions'; import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; import { Abi } from '../../abi/Abi'; +import { CategoryEnum } from '../../types/enums/CategoryEnum'; import { renderFactoryTemplate } from './factory'; @@ -20,6 +21,7 @@ describe('templates/factory', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, + category: CategoryEnum.CONTRACT, }); const rendered = renderFactoryTemplate({ abi }); diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts index 16e5b354947..b061ad8d73e 100644 --- a/packages/abi-typegen/src/templates/script/factory.test.ts +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -5,6 +5,7 @@ import { getNewAbiTypegen } from '../../../test/utils/getNewAbiTypegen'; import { mockVersions } from '../../../test/utils/mockVersions'; import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; import { Abi } from '../../abi/Abi'; +import { CategoryEnum } from '../../types/enums/CategoryEnum'; import { renderFactoryTemplate } from './factory'; @@ -21,6 +22,7 @@ describe('factory.ts', () => { hexlifiedBinContents: '0x000', outputDir: 'stdout', rawContents, + category: CategoryEnum.SCRIPT, }); const rendered = renderFactoryTemplate({ abi }); @@ -41,6 +43,7 @@ describe('factory.ts', () => { filepath: './my-script-abi.json', outputDir: 'stdout', rawContents, + category: CategoryEnum.SCRIPT, }); const { error } = await executeAndCatch(() => { diff --git a/packages/abi-typegen/src/templates/utils/formatEnums.test.ts b/packages/abi-typegen/src/templates/utils/formatEnums.test.ts index 6706db8baae..18be7d98ad0 100644 --- a/packages/abi-typegen/src/templates/utils/formatEnums.test.ts +++ b/packages/abi-typegen/src/templates/utils/formatEnums.test.ts @@ -1,5 +1,6 @@ import enumOfEnumsAbiJson from '../../../test/fixtures/out/abis/enum-of-enums-abi.json'; import { Abi } from '../../abi/Abi'; +import { CategoryEnum } from '../../types/enums/CategoryEnum'; import { formatEnums } from './formatEnums'; @@ -9,6 +10,7 @@ describe('formatEnums.ts', () => { filepath: './enum-simple-abi.json', outputDir: './contracts', rawContents: enumOfEnumsAbiJson, + category: CategoryEnum.CONTRACT, }); // executing diff --git a/packages/abi-typegen/src/templates/utils/formatStructs.test.ts b/packages/abi-typegen/src/templates/utils/formatStructs.test.ts index 1a1c04e07ba..b6bcd1c3dda 100644 --- a/packages/abi-typegen/src/templates/utils/formatStructs.test.ts +++ b/packages/abi-typegen/src/templates/utils/formatStructs.test.ts @@ -1,5 +1,6 @@ import structSimpleAbiJson from '../../../test/fixtures/out/abis/struct-simple-abi.json'; import { Abi } from '../../abi/Abi'; +import { CategoryEnum } from '../../types/enums/CategoryEnum'; import { formatStructs } from './formatStructs'; @@ -9,6 +10,7 @@ describe('formatStructs.ts', () => { filepath: './struct-simple-abi.json', outputDir: './contracts', rawContents: structSimpleAbiJson, + category: CategoryEnum.CONTRACT, }); // executing From 508b41a6dea4ef7e782d8be5c59da9ef1ea7800a Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 9 Feb 2023 06:09:55 -0300 Subject: [PATCH 080/108] Improving test for Script factory template --- .../abi-typegen/src/templates/script/factory.test.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts index b061ad8d73e..1f3d16a2c2d 100644 --- a/packages/abi-typegen/src/templates/script/factory.test.ts +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -1,7 +1,6 @@ import { contractPaths } from '../../../test/fixtures'; import factoryTemplate from '../../../test/fixtures/templates/script/factory.hbs'; import { executeAndCatch } from '../../../test/utils/executeAndCatch'; -import { getNewAbiTypegen } from '../../../test/utils/getNewAbiTypegen'; import { mockVersions } from '../../../test/utils/mockVersions'; import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; import { Abi } from '../../abi/Abi'; @@ -35,12 +34,16 @@ describe('factory.ts', () => { test('should throw for invalid Script ABI', async () => { const { restore } = mockVersions(); - const { rawContents } = getNewAbiTypegen({ - includeMainFunction: false, // friction here - }).typegen.abis[0]; + const contractPath = contractPaths.script; + + const { rawContents } = compileSwayToJson({ contractPath }); + + // friction here (deletes 'main' function by emptying the functions array) + rawContents.functions = []; const abi = new Abi({ filepath: './my-script-abi.json', + hexlifiedBinContents: '0x000', outputDir: 'stdout', rawContents, category: CategoryEnum.SCRIPT, From 5f33bcbb876b31a83e13ac466945dde1faf5eb24 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 23 Feb 2023 16:48:44 -0300 Subject: [PATCH 081/108] Adding new flag to denote items that need to be imported from `fuels` --- packages/abi-typegen/src/abi/types/AType.ts | 2 ++ packages/abi-typegen/src/abi/types/U8Type.ts | 1 + packages/abi-typegen/src/types/interfaces/IType.ts | 2 ++ 3 files changed, 5 insertions(+) diff --git a/packages/abi-typegen/src/abi/types/AType.ts b/packages/abi-typegen/src/abi/types/AType.ts index eb5e74804d0..ef2ee55a192 100644 --- a/packages/abi-typegen/src/abi/types/AType.ts +++ b/packages/abi-typegen/src/abi/types/AType.ts @@ -4,6 +4,7 @@ import type { ITypeAttributes } from '../../types/interfaces/IType'; export class AType { public rawAbiType: IRawAbiTypeRoot; public attributes: ITypeAttributes; + public requireImportFromFuels?: boolean; constructor(params: { rawAbiType: IRawAbiTypeRoot }) { this.rawAbiType = params.rawAbiType; @@ -11,5 +12,6 @@ export class AType { inputLabel: 'unknown', outputLabel: 'unknown', }; + this.requireImportFromFuels = false; } } diff --git a/packages/abi-typegen/src/abi/types/U8Type.ts b/packages/abi-typegen/src/abi/types/U8Type.ts index b083c010aba..d10626bce2a 100644 --- a/packages/abi-typegen/src/abi/types/U8Type.ts +++ b/packages/abi-typegen/src/abi/types/U8Type.ts @@ -16,6 +16,7 @@ export class U8Type extends AType implements IType { inputLabel: `BigNumberish`, outputLabel: `number`, }; + this.requireImportFromFuels = true; } static isSuitableFor(params: { type: string }) { diff --git a/packages/abi-typegen/src/types/interfaces/IType.ts b/packages/abi-typegen/src/types/interfaces/IType.ts index fc78a03e754..b747d7afc30 100644 --- a/packages/abi-typegen/src/types/interfaces/IType.ts +++ b/packages/abi-typegen/src/types/interfaces/IType.ts @@ -13,6 +13,8 @@ export interface IType { name: string; attributes: ITypeAttributes; rawAbiType: IRawAbiTypeRoot; + requireImportFromFuels?: boolean; + parseComponentsAttributes(params: { types: IType[] }): ITypeAttributes; // Methods only present in `EnumType` and `StructType` classes From 56748d1967706927e32279b5617284b4ea7b6248 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 23 Feb 2023 16:48:53 -0300 Subject: [PATCH 082/108] Updating tests for all types --- packages/abi-typegen/src/abi/types/ArrayType.test.ts | 1 + packages/abi-typegen/src/abi/types/B256Type.test.ts | 1 + packages/abi-typegen/src/abi/types/B512Type.test.ts | 1 + packages/abi-typegen/src/abi/types/BoolType.test.ts | 1 + packages/abi-typegen/src/abi/types/EnumType.test.ts | 1 + packages/abi-typegen/src/abi/types/GenericType.test.ts | 1 + packages/abi-typegen/src/abi/types/OptionType.test.ts | 1 + packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts | 1 + packages/abi-typegen/src/abi/types/StrType.test.ts | 1 + packages/abi-typegen/src/abi/types/StructType.test.ts | 2 ++ packages/abi-typegen/src/abi/types/TupleType.test.ts | 2 ++ packages/abi-typegen/src/abi/types/U16Type.test.ts | 1 + packages/abi-typegen/src/abi/types/U32Type.test.ts | 1 + packages/abi-typegen/src/abi/types/U64Type.test.ts | 1 + packages/abi-typegen/src/abi/types/U8Type.test.ts | 1 + packages/abi-typegen/src/abi/types/VectorType.test.ts | 1 + 16 files changed, 18 insertions(+) diff --git a/packages/abi-typegen/src/abi/types/ArrayType.test.ts b/packages/abi-typegen/src/abi/types/ArrayType.test.ts index bb1e4113fb5..9ede6e330e3 100644 --- a/packages/abi-typegen/src/abi/types/ArrayType.test.ts +++ b/packages/abi-typegen/src/abi/types/ArrayType.test.ts @@ -31,6 +31,7 @@ describe('ArrayType.ts', () => { expect(b.attributes.inputLabel).toEqual('[BigNumberish, BigNumberish]'); expect(b.attributes.outputLabel).toEqual('[number, number]'); + expect(b.requireImportFromFuels).toEqual(false); expect(parseTypeArguments).toHaveBeenCalledTimes(0); // never called }); diff --git a/packages/abi-typegen/src/abi/types/B256Type.test.ts b/packages/abi-typegen/src/abi/types/B256Type.test.ts index 5fae49bcb82..a0e9a3d5539 100644 --- a/packages/abi-typegen/src/abi/types/B256Type.test.ts +++ b/packages/abi-typegen/src/abi/types/B256Type.test.ts @@ -22,5 +22,6 @@ describe('B256Type.ts', () => { expect(b256.attributes.inputLabel).toEqual('string'); expect(b256.attributes.outputLabel).toEqual('string'); + expect(b256.requireImportFromFuels).toEqual(false); }); }); diff --git a/packages/abi-typegen/src/abi/types/B512Type.test.ts b/packages/abi-typegen/src/abi/types/B512Type.test.ts index dd5721154d1..94324313877 100644 --- a/packages/abi-typegen/src/abi/types/B512Type.test.ts +++ b/packages/abi-typegen/src/abi/types/B512Type.test.ts @@ -22,5 +22,6 @@ describe('B512Type.ts', () => { expect(b512.attributes.inputLabel).toEqual('string'); expect(b512.attributes.outputLabel).toEqual('string'); + expect(b512.requireImportFromFuels).toEqual(false); }); }); diff --git a/packages/abi-typegen/src/abi/types/BoolType.test.ts b/packages/abi-typegen/src/abi/types/BoolType.test.ts index af346ee08aa..f9a73a3c2e5 100644 --- a/packages/abi-typegen/src/abi/types/BoolType.test.ts +++ b/packages/abi-typegen/src/abi/types/BoolType.test.ts @@ -22,5 +22,6 @@ describe('BoolType.ts', () => { expect(bool.attributes.inputLabel).toEqual('boolean'); expect(bool.attributes.outputLabel).toEqual('boolean'); + expect(bool.requireImportFromFuels).toEqual(false); }); }); diff --git a/packages/abi-typegen/src/abi/types/EnumType.test.ts b/packages/abi-typegen/src/abi/types/EnumType.test.ts index 6bf9d4defb1..36cfd27af24 100644 --- a/packages/abi-typegen/src/abi/types/EnumType.test.ts +++ b/packages/abi-typegen/src/abi/types/EnumType.test.ts @@ -30,6 +30,7 @@ describe('EnumType.ts', () => { expect(params.enum.attributes.inputLabel).toEqual('MyEnumInput'); expect(params.enum.attributes.outputLabel).toEqual('MyEnumOutput'); expect(params.enum.getStructName()).toEqual('MyEnum'); + expect(params.enum.requireImportFromFuels).toEqual(false); } /* diff --git a/packages/abi-typegen/src/abi/types/GenericType.test.ts b/packages/abi-typegen/src/abi/types/GenericType.test.ts index 7379f6f3827..61403eb10b9 100644 --- a/packages/abi-typegen/src/abi/types/GenericType.test.ts +++ b/packages/abi-typegen/src/abi/types/GenericType.test.ts @@ -22,5 +22,6 @@ describe('GenericType.ts', () => { expect(generic.attributes.inputLabel).toEqual('T'); expect(generic.attributes.outputLabel).toEqual('T'); + expect(generic.requireImportFromFuels).toEqual(false); }); }); diff --git a/packages/abi-typegen/src/abi/types/OptionType.test.ts b/packages/abi-typegen/src/abi/types/OptionType.test.ts index b5241cda138..006ff0d12df 100644 --- a/packages/abi-typegen/src/abi/types/OptionType.test.ts +++ b/packages/abi-typegen/src/abi/types/OptionType.test.ts @@ -39,5 +39,6 @@ describe('OptionType.ts', () => { expect(b.attributes.inputLabel).toEqual('Option'); expect(b.attributes.outputLabel).toEqual('Option'); + expect(b.requireImportFromFuels).toEqual(false); }); }); diff --git a/packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts b/packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts index 6da6cfab606..ceb658c9d2d 100644 --- a/packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts +++ b/packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts @@ -24,5 +24,6 @@ describe('RawUntypedPtrType.ts', () => { expect(rawUntypedPtr.attributes.inputLabel).toEqual('BigNumberish'); expect(rawUntypedPtr.attributes.outputLabel).toEqual('BN'); + expect(rawUntypedPtr.requireImportFromFuels).toEqual(false); }); }); diff --git a/packages/abi-typegen/src/abi/types/StrType.test.ts b/packages/abi-typegen/src/abi/types/StrType.test.ts index 1e2cbea5a8a..f7b8e3884a2 100644 --- a/packages/abi-typegen/src/abi/types/StrType.test.ts +++ b/packages/abi-typegen/src/abi/types/StrType.test.ts @@ -22,5 +22,6 @@ describe('StrType.ts', () => { expect(str.attributes.inputLabel).toEqual('string'); expect(str.attributes.outputLabel).toEqual('string'); + expect(str.requireImportFromFuels).toEqual(false); }); }); diff --git a/packages/abi-typegen/src/abi/types/StructType.test.ts b/packages/abi-typegen/src/abi/types/StructType.test.ts index 2fafc0f5c1a..404d6efe089 100644 --- a/packages/abi-typegen/src/abi/types/StructType.test.ts +++ b/packages/abi-typegen/src/abi/types/StructType.test.ts @@ -32,6 +32,7 @@ describe('StructType.ts', () => { expect(c.attributes.structName).toEqual('StructC'); expect(c.attributes.inputLabel).toEqual('StructCInput'); expect(c.attributes.outputLabel).toEqual('StructCOutput'); + expect(c.requireImportFromFuels).toEqual(false); // inputs and outputs with nested `typeArguments` let inputs = c.getStructContents({ types, target: TargetEnum.INPUT }); @@ -51,6 +52,7 @@ describe('StructType.ts', () => { expect(a.attributes.structName).toEqual('StructA'); expect(a.attributes.inputLabel).toEqual('StructAInput'); expect(a.attributes.outputLabel).toEqual('StructAOutput'); + expect(a.requireImportFromFuels).toEqual(false); inputs = a.getStructContents({ types, target: TargetEnum.INPUT }); expect(inputs).toEqual('propA1: T, propA2: U'); diff --git a/packages/abi-typegen/src/abi/types/TupleType.test.ts b/packages/abi-typegen/src/abi/types/TupleType.test.ts index 6752d10ccc5..3b86aaced4f 100644 --- a/packages/abi-typegen/src/abi/types/TupleType.test.ts +++ b/packages/abi-typegen/src/abi/types/TupleType.test.ts @@ -28,6 +28,7 @@ describe('TupleType.ts', () => { expect(b.attributes.inputLabel).toEqual('[boolean, BigNumberish]'); expect(b.attributes.outputLabel).toEqual('[boolean, BN]'); + expect(b.requireImportFromFuels).toEqual(false); expect(parseTypeArguments).toHaveBeenCalledTimes(0); // never called @@ -39,6 +40,7 @@ describe('TupleType.ts', () => { '[BigNumberish, StructAInput, string>]' ); expect(c.attributes.outputLabel).toEqual('[number, StructAOutput, string>]'); + expect(c.requireImportFromFuels).toEqual(false); expect(parseTypeArguments).toHaveBeenCalledTimes(2); // called 2x times }); diff --git a/packages/abi-typegen/src/abi/types/U16Type.test.ts b/packages/abi-typegen/src/abi/types/U16Type.test.ts index d9e5fc5d976..30e054cf6b9 100644 --- a/packages/abi-typegen/src/abi/types/U16Type.test.ts +++ b/packages/abi-typegen/src/abi/types/U16Type.test.ts @@ -22,5 +22,6 @@ describe('U16Type.ts', () => { expect(u16.attributes.inputLabel).toEqual('BigNumberish'); expect(u16.attributes.outputLabel).toEqual('number'); + expect(u16.requireImportFromFuels).toEqual(true); }); }); diff --git a/packages/abi-typegen/src/abi/types/U32Type.test.ts b/packages/abi-typegen/src/abi/types/U32Type.test.ts index 9043653cd87..b43f0d2f3f1 100644 --- a/packages/abi-typegen/src/abi/types/U32Type.test.ts +++ b/packages/abi-typegen/src/abi/types/U32Type.test.ts @@ -22,5 +22,6 @@ describe('U32Type.ts', () => { expect(u32.attributes.inputLabel).toEqual('BigNumberish'); expect(u32.attributes.outputLabel).toEqual('number'); + expect(u32.requireImportFromFuels).toEqual(true); }); }); diff --git a/packages/abi-typegen/src/abi/types/U64Type.test.ts b/packages/abi-typegen/src/abi/types/U64Type.test.ts index 9ba90fab151..9bf70ab3494 100644 --- a/packages/abi-typegen/src/abi/types/U64Type.test.ts +++ b/packages/abi-typegen/src/abi/types/U64Type.test.ts @@ -22,5 +22,6 @@ describe('U64Type.ts', () => { expect(u64.attributes.inputLabel).toEqual('BigNumberish'); expect(u64.attributes.outputLabel).toEqual('BN'); + expect(u64.requireImportFromFuels).toEqual(true); }); }); diff --git a/packages/abi-typegen/src/abi/types/U8Type.test.ts b/packages/abi-typegen/src/abi/types/U8Type.test.ts index 6c4e6037cd7..4d61134ff15 100644 --- a/packages/abi-typegen/src/abi/types/U8Type.test.ts +++ b/packages/abi-typegen/src/abi/types/U8Type.test.ts @@ -22,5 +22,6 @@ describe('U8Type.ts', () => { expect(u8.attributes.inputLabel).toEqual('BigNumberish'); expect(u8.attributes.outputLabel).toEqual('number'); + expect(u8.requireImportFromFuels).toEqual(true); }); }); diff --git a/packages/abi-typegen/src/abi/types/VectorType.test.ts b/packages/abi-typegen/src/abi/types/VectorType.test.ts index 5a391dda9f2..f5bdad5f746 100644 --- a/packages/abi-typegen/src/abi/types/VectorType.test.ts +++ b/packages/abi-typegen/src/abi/types/VectorType.test.ts @@ -24,5 +24,6 @@ describe('VectorType.ts', () => { expect(vector.attributes.inputLabel).toEqual('Vec'); expect(vector.attributes.outputLabel).toEqual('Vec'); + expect(vector.requireImportFromFuels).toEqual(false); }); }); From ddc8a44dcfbf7bab5c4f63401d58ad3baf90cffb Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 23 Feb 2023 17:10:29 -0300 Subject: [PATCH 083/108] Adding new template utility --- .../src/templates/utils/formatImports.test.ts | 55 +++++++++++++++++++ .../src/templates/utils/formatImports.ts | 15 +++++ 2 files changed, 70 insertions(+) create mode 100644 packages/abi-typegen/src/templates/utils/formatImports.test.ts create mode 100644 packages/abi-typegen/src/templates/utils/formatImports.ts diff --git a/packages/abi-typegen/src/templates/utils/formatImports.test.ts b/packages/abi-typegen/src/templates/utils/formatImports.test.ts new file mode 100644 index 00000000000..7dd07055c40 --- /dev/null +++ b/packages/abi-typegen/src/templates/utils/formatImports.test.ts @@ -0,0 +1,55 @@ +import { BoolType } from '../../abi/types/BoolType'; +import { U8Type } from '../../abi/types/U8Type'; + +import { formatImports } from './formatImports'; + +describe('formatImports.ts', () => { + const baseMembers = ['Contract']; + + const u8 = new U8Type({ + rawAbiType: { + typeId: 1, + type: 'u8', + components: null, + typeParameters: null, + }, + }); + + const u16 = new U8Type({ + rawAbiType: { + typeId: 2, + type: 'u16', + components: null, + typeParameters: null, + }, + }); + + const bool = new BoolType({ + rawAbiType: { + typeId: 3, + type: 'bool', + components: null, + typeParameters: null, + }, + }); + + test('should format imports just fine', async () => { + const { imports } = formatImports({ types: [u8, u16, bool] }); + expect(imports).toStrictEqual(['BigNumberish']); + }); + + test('should format imports, preserving base members', async () => { + const { imports } = formatImports({ types: [u8, u16, bool], baseMembers }); + expect(imports).toStrictEqual(['BigNumberish'].concat(baseMembers)); + }); + + test('should return undefined when no imports are evaluated', async () => { + const { imports } = formatImports({ types: [bool] }); + expect(imports).toEqual(undefined); + }); + + test('should return only base members, when nothing else is required', async () => { + const { imports } = formatImports({ types: [bool], baseMembers }); + expect(imports).toStrictEqual(baseMembers); + }); +}); diff --git a/packages/abi-typegen/src/templates/utils/formatImports.ts b/packages/abi-typegen/src/templates/utils/formatImports.ts new file mode 100644 index 00000000000..5a8bc4d593d --- /dev/null +++ b/packages/abi-typegen/src/templates/utils/formatImports.ts @@ -0,0 +1,15 @@ +import uniq from 'lodash.uniq'; + +import type { IType } from '../../types/interfaces/IType'; + +export function formatImports(params: { types: IType[]; baseMembers?: string[] }) { + const { types, baseMembers = [] } = params; + + const fuelTypes = types.filter((t) => t.requireImportFromFuels); + const members = fuelTypes.map((t) => t.attributes.inputLabel); + const imports = uniq(baseMembers.concat(members).sort()); + + return { + imports: imports.length ? imports : undefined, + }; +} From b4e34478f8c9cca002cafec272120697a5a9ad3c Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 23 Feb 2023 17:24:21 -0300 Subject: [PATCH 084/108] Improving array sorting by running it in a case-insensitive manner --- packages/abi-typegen/src/templates/utils/formatImports.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/abi-typegen/src/templates/utils/formatImports.ts b/packages/abi-typegen/src/templates/utils/formatImports.ts index 5a8bc4d593d..472537afca0 100644 --- a/packages/abi-typegen/src/templates/utils/formatImports.ts +++ b/packages/abi-typegen/src/templates/utils/formatImports.ts @@ -2,12 +2,15 @@ import uniq from 'lodash.uniq'; import type { IType } from '../../types/interfaces/IType'; +const caseInsensitiveSort = (a: string, b: string) => + a.toLowerCase().localeCompare(b.toLowerCase()); + export function formatImports(params: { types: IType[]; baseMembers?: string[] }) { const { types, baseMembers = [] } = params; const fuelTypes = types.filter((t) => t.requireImportFromFuels); const members = fuelTypes.map((t) => t.attributes.inputLabel); - const imports = uniq(baseMembers.concat(members).sort()); + const imports = uniq(baseMembers.concat(members).sort(caseInsensitiveSort)); return { imports: imports.length ? imports : undefined, From 1cba7680594289c14e4f63b929b1dc0320420c40 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 23 Feb 2023 17:25:16 -0300 Subject: [PATCH 085/108] Adding `lodash.uniq` to the mix --- packages/abi-typegen/package.json | 2 ++ pnpm-lock.yaml | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/packages/abi-typegen/package.json b/packages/abi-typegen/package.json index e3c98b5a23a..adfa9eca9b4 100644 --- a/packages/abi-typegen/package.json +++ b/packages/abi-typegen/package.json @@ -38,12 +38,14 @@ "commander": "^9.4.1", "glob": "^8.0.3", "handlebars": "^4.7.7", + "lodash.uniq": "^4.5.0", "lodash.upperfirst": "^4.3.1", "mkdirp": "^1.0.4", "rimraf": "^3.0.2" }, "devDependencies": { "@types/glob": "^8.0.0", + "@types/lodash.uniq": "^4.5.7", "@types/lodash.upperfirst": "^4.3.7", "@types/mkdirp": "^1.0.2", "@types/rimraf": "^3.0.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d32783c5724..f4d885d7f2c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -127,6 +127,7 @@ importers: '@ethersproject/bytes': ^5.7.0 '@fuel-ts/versions': workspace:* '@types/glob': ^8.0.0 + '@types/lodash.uniq': ^4.5.7 '@types/lodash.upperfirst': ^4.3.7 '@types/mkdirp': ^1.0.2 '@types/rimraf': ^3.0.2 @@ -134,6 +135,7 @@ importers: forc-bin: workspace:* glob: ^8.0.3 handlebars: ^4.7.7 + lodash.uniq: ^4.5.0 lodash.upperfirst: ^4.3.1 mkdirp: ^1.0.4 rimraf: ^3.0.2 @@ -143,11 +145,13 @@ importers: commander: 9.4.1 glob: 8.0.3 handlebars: 4.7.7 + lodash.uniq: 4.5.0 lodash.upperfirst: 4.3.1 mkdirp: 1.0.4 rimraf: 3.0.2 devDependencies: '@types/glob': 8.0.0 + '@types/lodash.uniq': 4.5.7 '@types/lodash.upperfirst': 4.3.7 '@types/mkdirp': 1.0.2 '@types/rimraf': 3.0.2 @@ -3043,6 +3047,12 @@ packages: '@types/lodash': 4.14.190 dev: true + /@types/lodash.uniq/4.5.7: + resolution: {integrity: sha512-qg7DeAbdZMi6DGvCxThlJycykLLhETrJrQZ6F2KaZ+o0sNK1qRHz46lgNA+nHHjwrmA2a91DyiZTp3ey3m1rEw==} + dependencies: + '@types/lodash': 4.14.190 + dev: true + /@types/lodash.upperfirst/4.3.7: resolution: {integrity: sha512-CrBjoB4lO6h7tXNMBUl1eh/w0KdMosiEOXOoD5DMECsA/kDWo/WQfOt1KyGKVvgwK3I6cKAY6z8LymKiMazLFg==} dependencies: @@ -6844,6 +6854,10 @@ packages: resolution: {integrity: sha512-+WKqsK294HMSc2jEbNgpHpd0JfIBhp7rEV4aqXWqFr6AlXov+SlcgB1Fv01y2kGe3Gc8nMW7VA0SrGuSkRfIEg==} dev: true + /lodash.uniq/4.5.0: + resolution: {integrity: sha512-xfBaXQd9ryd9dlSDvnvI0lvxfLJlYAZzXomUYzLKtUeOQvOP5piqAWuGtrhWeqaXK9hhoM/iyJc5AV+XfsX3HQ==} + dev: false + /lodash.upperfirst/4.3.1: resolution: {integrity: sha512-sReKOYJIJf74dhJONhU4e0/shzi1trVbSWDOhKYE5XV2O+H7Sb2Dihwuc7xWxVl+DgFPyTqIN3zMfT9cq5iWDg==} dev: false From 03e0940d439094a2c845d2437a2ea54485c26502 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 23 Feb 2023 17:25:50 -0300 Subject: [PATCH 086/108] Updating Contract `dts` template, render, and tests --- .../abi-typegen/src/templates/contract/dts.hbs | 14 +++++--------- packages/abi-typegen/src/templates/contract/dts.ts | 14 ++++++++++++++ .../test/fixtures/templates/contract/dts.hbs | 12 ++++++------ 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/abi-typegen/src/templates/contract/dts.hbs b/packages/abi-typegen/src/templates/contract/dts.hbs index b5e68cc88ac..cae59076949 100644 --- a/packages/abi-typegen/src/templates/contract/dts.hbs +++ b/packages/abi-typegen/src/templates/contract/dts.hbs @@ -1,21 +1,17 @@ {{header}} +{{#if imports}} import type { - Interface, - FunctionFragment, - DecodedValue, - Contract, - BytesLike, - BigNumberish, - InvokeFunction, - BN, +{{#each imports}} + {{this}}, +{{/each}} } from 'fuels'; +{{/if}} {{#if commonTypesInUse}} import type { {{commonTypesInUse}} } from "./common"; {{/if}} - {{#each enums}} export type {{structName}}Input = Enum<{ {{inputValues}} }>; {{#if recycleRef}} diff --git a/packages/abi-typegen/src/templates/contract/dts.ts b/packages/abi-typegen/src/templates/contract/dts.ts index 38e89ad98a2..209f97acf58 100644 --- a/packages/abi-typegen/src/templates/contract/dts.ts +++ b/packages/abi-typegen/src/templates/contract/dts.ts @@ -1,6 +1,7 @@ import type { Abi } from '../../abi/Abi'; import { renderHbsTemplate } from '../renderHbsTemplate'; import { formatEnums } from '../utils/formatEnums'; +import { formatImports } from '../utils/formatImports'; import { formatStructs } from '../utils/formatStructs'; import dtsTemplate from './dts.hbs'; @@ -26,6 +27,18 @@ export function renderDtsTemplate(params: { abi: Abi }) { const { enums } = formatEnums({ types }); const { structs } = formatStructs({ types }); + const { imports } = formatImports({ + types, + baseMembers: [ + 'Interface', + 'FunctionFragment', + 'DecodedValue', + 'Contract', + 'BytesLike', + 'InvokeFunction', + 'BN', + ], + }); /* And finally render template @@ -41,6 +54,7 @@ export function renderDtsTemplate(params: { abi: Abi }) { decoders, structs, enums, + imports, }, }); diff --git a/packages/abi-typegen/test/fixtures/templates/contract/dts.hbs b/packages/abi-typegen/test/fixtures/templates/contract/dts.hbs index 229b9c299c7..442f760bef8 100644 --- a/packages/abi-typegen/test/fixtures/templates/contract/dts.hbs +++ b/packages/abi-typegen/test/fixtures/templates/contract/dts.hbs @@ -10,14 +10,14 @@ */ import type { - Interface, - FunctionFragment, - DecodedValue, - Contract, - BytesLike, BigNumberish, - InvokeFunction, BN, + BytesLike, + Contract, + DecodedValue, + FunctionFragment, + Interface, + InvokeFunction, } from 'fuels'; import type { Option, Enum, Vec } from "./common"; From a1980eaf05fe552df07dbc17c3e06d2e9701d84e Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 23 Feb 2023 17:26:12 -0300 Subject: [PATCH 087/108] Updating Script `factory` template, render, and tests --- .../abi-typegen/src/templates/script/factory.hbs | 14 ++++++++++---- .../abi-typegen/src/templates/script/factory.ts | 3 +++ .../test/fixtures/templates/script/factory.hbs | 12 ++++++++---- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/packages/abi-typegen/src/templates/script/factory.hbs b/packages/abi-typegen/src/templates/script/factory.hbs index 55272a5d84e..a94534038e1 100644 --- a/packages/abi-typegen/src/templates/script/factory.hbs +++ b/packages/abi-typegen/src/templates/script/factory.hbs @@ -1,6 +1,12 @@ {{header}} -import { BigNumberish, BaseWalletLocked, Provider, ScriptFactory } from 'fuels'; +{{#if imports}} +import { +{{#each imports}} + {{this}}, +{{/each}} +} from 'fuels'; +{{/if}} {{#if commonTypesInUse}} import type { {{commonTypesInUse}} } from "./common"; @@ -38,14 +44,14 @@ export class {{capitalizedName}}__factory { static readonly abi = _abi static readonly bin = _bin - static createInstance(walletOrProvider: BaseWalletLocked | Provider) { + static createInstance(wallet: Account) { const { abi, bin } = {{capitalizedName}}__factory - const script = new ScriptFactory< + const script = new Script< {{capitalizedName}}Inputs, {{capitalizedName}}Output - >(bin, abi, walletOrProvider); + >(bin, abi, wallet); return script; diff --git a/packages/abi-typegen/src/templates/script/factory.ts b/packages/abi-typegen/src/templates/script/factory.ts index 8268dc24d5f..252af09e7be 100644 --- a/packages/abi-typegen/src/templates/script/factory.ts +++ b/packages/abi-typegen/src/templates/script/factory.ts @@ -1,6 +1,7 @@ import type { Abi } from '../../abi/Abi'; import { renderHbsTemplate } from '../renderHbsTemplate'; import { formatEnums } from '../utils/formatEnums'; +import { formatImports } from '../utils/formatImports'; import { formatStructs } from '../utils/formatStructs'; import factoryTemplate from './factory.hbs'; @@ -26,6 +27,7 @@ export function renderFactoryTemplate(params: { abi: Abi }) { const { enums } = formatEnums({ types }); const { structs } = formatStructs({ types }); + const { imports } = formatImports({ types, baseMembers: ['Script', 'Account'] }); const { prefixedInputs: inputs, output } = func.attributes; @@ -39,6 +41,7 @@ export function renderFactoryTemplate(params: { abi: Abi }) { abiJsonString, hexlifiedBinString, capitalizedName, + imports, }, }); diff --git a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs index 923388d3f65..9fa1593ec6d 100644 --- a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs +++ b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs @@ -9,7 +9,11 @@ Fuel-Core version: 33.33.33 */ -import { BigNumberish, BaseWalletLocked, Provider, ScriptFactory } from 'fuels'; +import { + Account, + BigNumberish, + Script, +} from 'fuels'; export type ScoreInput = { points: BigNumberish }; export type ScoreOutput = { points: number }; @@ -66,14 +70,14 @@ export class MyScriptAbi__factory { static readonly abi = _abi static readonly bin = _bin - static createInstance(walletOrProvider: BaseWalletLocked | Provider) { + static createInstance(wallet: Account) { const { abi, bin } = MyScriptAbi__factory - const script = new ScriptFactory< + const script = new Script< MyScriptAbiInputs, MyScriptAbiOutput - >(bin, abi, walletOrProvider); + >(bin, abi, wallet); return script; From a86dfa9403d78d7138130409ca0ee2a62007aa16 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 23 Feb 2023 17:27:13 -0300 Subject: [PATCH 088/108] Fixing broken test --- packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts b/packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts index ceb658c9d2d..b02eb0a9e37 100644 --- a/packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts +++ b/packages/abi-typegen/src/abi/types/RawUntypedPtr.test.ts @@ -24,6 +24,6 @@ describe('RawUntypedPtrType.ts', () => { expect(rawUntypedPtr.attributes.inputLabel).toEqual('BigNumberish'); expect(rawUntypedPtr.attributes.outputLabel).toEqual('BN'); - expect(rawUntypedPtr.requireImportFromFuels).toEqual(false); + expect(rawUntypedPtr.requireImportFromFuels).toEqual(true); }); }); From 7245b92249ae70568dce2dcd6740b76da7ec5f03 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 23 Feb 2023 17:31:32 -0300 Subject: [PATCH 089/108] Tyop --- packages/abi-typegen/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/abi-typegen/README.md b/packages/abi-typegen/README.md index 22d60e98eb2..6c5363ba5d7 100644 --- a/packages/abi-typegen/README.md +++ b/packages/abi-typegen/README.md @@ -31,7 +31,7 @@ npm add @fuel-ts/abi-typegen ```console $ fuels-typegen -h -Usage: fuels typegen [options] +Usage: fuels-typegen [options] generate typescript from contract abi json files From 77bf91f964fd7fbb9f5359c194c02a91ee4c7512 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 23 Feb 2023 17:31:42 -0300 Subject: [PATCH 090/108] Updating docs on programmatic usage --- packages/abi-typegen/README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/abi-typegen/README.md b/packages/abi-typegen/README.md index 6c5363ba5d7..b839106f0d9 100644 --- a/packages/abi-typegen/README.md +++ b/packages/abi-typegen/README.md @@ -55,18 +55,19 @@ npx fuels-typegen -i ./out/debug/*-abi.json -o ./src/contracts ## Programmatic API ```ts -import { runTypegen } from "@fuel-ts/abi-typegen"; +import { CategoryEnum, runTypegen } from "@fuel-ts/abi-typegen"; const cwd = process.cwd(); const input = './abis/**-abi.json' const output = './types' const filepaths = [ './abis/a-abi.json', './abis/b-abi.json' ] + const category = CategoryEnum.contract; // using input global - await runTypegen({ cwd, input, output }); + await runTypegen({ cwd, input, output, category }); // using filepaths' array - await runTypegen({ cwd, filepaths, output }); + await runTypegen({ cwd, filepaths, output, category }); } ``` From f7ff31151a8ad9be7fcba86a634e52aef3c6f884 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 23 Feb 2023 17:34:04 -0300 Subject: [PATCH 091/108] Adjusting enum CASE --- packages/abi-typegen/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/abi-typegen/README.md b/packages/abi-typegen/README.md index b839106f0d9..9ea028bc759 100644 --- a/packages/abi-typegen/README.md +++ b/packages/abi-typegen/README.md @@ -61,7 +61,7 @@ import { CategoryEnum, runTypegen } from "@fuel-ts/abi-typegen"; const input = './abis/**-abi.json' const output = './types' const filepaths = [ './abis/a-abi.json', './abis/b-abi.json' ] - const category = CategoryEnum.contract; + const category = CategoryEnum.CONTRACT; // using input global await runTypegen({ cwd, input, output, category }); From 9fb30770c98924005c2e4ac6e8d2093d8087048a Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 23 Feb 2023 18:37:39 -0300 Subject: [PATCH 092/108] Docs --- .../_guide/abi-typegen/using-generated-types.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/docs/_guide/abi-typegen/using-generated-types.md b/docs/_guide/abi-typegen/using-generated-types.md index c65994fb4c7..16500d5e074 100644 --- a/docs/_guide/abi-typegen/using-generated-types.md +++ b/docs/_guide/abi-typegen/using-generated-types.md @@ -1,6 +1,6 @@ [nav_order: 1] -# Using Generated Contracts Types +# Using Generated Types After generating types via: @@ -15,11 +15,12 @@ import { Wallet } from "fuels"; import { MyContract__factory } from "./types"; const contractId = "0x..."; -const wallet = new Wallet("0x..."); +const wallet = Wallet.fromAddress("..."); const contract = MyContract__factory.connect(contractId, wallet); // All contract methods are available under functions with the correct types -const { transactionId, value } = await contract.functions.my_fn(1n).call(); +const { transactionId, value } = await contract.functions.my_fn(1).call(); + console.log(transactionId, value); ``` @@ -34,7 +35,15 @@ yarn exec fuels -i ./abis/*-abi.json -o ./types --script We can use these files like so: ```ts -// TODO: Add usage +import { Wallet } from "fuels"; +import { MyScript__factory } from "./types"; + +const wallet = Wallet.fromAddress("..."); +const script = ScriptAbi__factory.createInstance(wallet); + +const { value, logs } = await script.functions.main(1).call(); + +console.log({ value, logs }); ``` See also: From c4075fda774d81dc6246d787873b81b8dd721280 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 24 Feb 2023 08:45:17 -0300 Subject: [PATCH 093/108] Updating fixture utility --- packages/abi-typegen/test/fixtures/buildAll.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/abi-typegen/test/fixtures/buildAll.ts b/packages/abi-typegen/test/fixtures/buildAll.ts index 01a90cbfcfd..f1f5e1a842c 100644 --- a/packages/abi-typegen/test/fixtures/buildAll.ts +++ b/packages/abi-typegen/test/fixtures/buildAll.ts @@ -4,13 +4,24 @@ import { contractPaths } from '.'; const { log } = console; +function get(source: { [k: string]: string }, key: string) { + return source[key]; +} + Object.keys(contractPaths).forEach((key) => { - const contractPath: string = contractPaths[key]; + const contractPath: string = get(contractPaths, key); + log('——————————————————————————————————————————————————————'); log(contractPath); log('——————————————————————————————————————————————————————'); const ts = compileSwayToTs({ contractPath, inPlace: true }); - log(ts.dts); + + ts.typegen.files.forEach((f) => { + log('----------'); + log(f.path); + log(f.contents); + }); + log('\n\n'); }); From a3dabc1f2d9f928cffb7890bf1b77a2c8b37d21b Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 24 Feb 2023 08:45:29 -0300 Subject: [PATCH 094/108] Restoring mocks before assertions --- packages/abi-typegen/src/templates/script/factory.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts index 1f3d16a2c2d..5fb4e8f97be 100644 --- a/packages/abi-typegen/src/templates/script/factory.test.ts +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -53,8 +53,8 @@ describe('factory.ts', () => { renderFactoryTemplate({ abi }); }); - expect(error?.message).toMatch(/ABI doesn't have a 'main\(\)' method/); - restore(); + + expect(error?.message).toMatch(/ABI doesn't have a 'main\(\)' method/); }); }); From 1f220ef970b82daf8a2eae3c183ca35b58e02f60 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 24 Feb 2023 08:46:13 -0300 Subject: [PATCH 095/108] Updating Sway script fixture --- packages/abi-typegen/test/fixtures/script.sw | 5 +-- .../fixtures/templates/script/factory.hbs | 31 +++++++++++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/packages/abi-typegen/test/fixtures/script.sw b/packages/abi-typegen/test/fixtures/script.sw index d329e15f619..6aa0d54ebd4 100644 --- a/packages/abi-typegen/test/fixtures/script.sw +++ b/packages/abi-typegen/test/fixtures/script.sw @@ -1,9 +1,10 @@ script; struct Score { + user: u8, points: u8 } -fn main(points: u8) -> Score { - return Score { points }; +fn main(score: Score) -> bool { + return true } diff --git a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs index 9fa1593ec6d..0fb0d7c0e1b 100644 --- a/packages/abi-typegen/test/fixtures/templates/script/factory.hbs +++ b/packages/abi-typegen/test/fixtures/templates/script/factory.hbs @@ -15,28 +15,39 @@ import { Script, } from 'fuels'; -export type ScoreInput = { points: BigNumberish }; -export type ScoreOutput = { points: number }; +export type ScoreInput = { user: BigNumberish, points: BigNumberish }; +export type ScoreOutput = { user: number, points: number }; -type MyScriptAbiInputs = [points: BigNumberish]; -type MyScriptAbiOutput = ScoreOutput; +type MyScriptAbiInputs = [score: ScoreInput]; +type MyScriptAbiOutput = boolean; const _abi = { "types": [ { "typeId": 0, + "type": "bool", + "components": null, + "typeParameters": null + }, + { + "typeId": 1, "type": "struct Score", "components": [ + { + "name": "user", + "type": 2, + "typeArguments": null + }, { "name": "points", - "type": 1, + "type": 2, "typeArguments": null } ], "typeParameters": null }, { - "typeId": 1, + "typeId": 2, "type": "u8", "components": null, "typeParameters": null @@ -46,7 +57,7 @@ const _abi = { { "inputs": [ { - "name": "points", + "name": "score", "type": 1, "typeArguments": null } @@ -56,11 +67,13 @@ const _abi = { "name": "", "type": 0, "typeArguments": null - } + }, + "attributes": null } ], "loggedTypes": [], - "messagesTypes": [] + "messagesTypes": [], + "configurables": [] } const _bin = '0x000' From 8ef07b93bcb6cf944447ab6dac8a2f516e4e307e Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 24 Feb 2023 08:51:39 -0300 Subject: [PATCH 096/108] Returning additional property. --- .../abi-typegen/test/utils/sway/createTempSwayProject.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/abi-typegen/test/utils/sway/createTempSwayProject.ts b/packages/abi-typegen/test/utils/sway/createTempSwayProject.ts index ddde6d123bb..8e4d43b0699 100644 --- a/packages/abi-typegen/test/utils/sway/createTempSwayProject.ts +++ b/packages/abi-typegen/test/utils/sway/createTempSwayProject.ts @@ -72,9 +72,10 @@ export function createTempSwayProject(params: ISwayParams) { const destinationDebugDir = join(tempDir, 'out/debug/'); const destinationAbiJsonPath = join(destinationDebugDir, `${contractName}-abi.json`); + const destinationBinPath = join(destinationDebugDir, `${contractName}.bin`); // voilà - return { + const bundle = { contractFilename, contractName, destinationContractPath, @@ -85,5 +86,8 @@ export function createTempSwayProject(params: ISwayParams) { tomlContents, destinationDebugDir, destinationAbiJsonPath, + destinationBinPath, }; + + return bundle; } From 171f230967b02b1bbdb9e91da44119eed51d7d6f Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 24 Feb 2023 08:52:15 -0300 Subject: [PATCH 097/108] DRY; considering [hexlified] bin files --- .../test/utils/sway/compileSwayToJson.ts | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/packages/abi-typegen/test/utils/sway/compileSwayToJson.ts b/packages/abi-typegen/test/utils/sway/compileSwayToJson.ts index 6f69fa72de5..f645cbd8807 100644 --- a/packages/abi-typegen/test/utils/sway/compileSwayToJson.ts +++ b/packages/abi-typegen/test/utils/sway/compileSwayToJson.ts @@ -1,3 +1,4 @@ +import { hexlify } from '@ethersproject/bytes'; import { copyFileSync, statSync, readFileSync, existsSync } from 'fs'; import mkdirp from 'mkdirp'; import { basename, dirname, join } from 'path'; @@ -8,6 +9,24 @@ import type { IRawAbi } from '../../../src/types/interfaces/IRawAbi'; import type { ISwayParams } from './ISwayUtilParams'; import { createTempSwayProject } from './createTempSwayProject'; +/* + Reads files from disk and return them ready to be used + */ +const getBundle = (abiFilepath: string) => { + const abiContents: IRawAbi = JSON.parse(readFileSync(abiFilepath, 'utf-8')); + + const binFilepath = abiFilepath.replace('-abi.json', '.bin'); + const binContents = hexlify(readFileSync(binFilepath)); + + return { + abiFilepath, + abiContents, + rawContents: abiContents, // TODO: remove a lias, refactor refs + binFilepath, + binContents, + }; +}; + /* Compile Sway contract to JSON ABI */ @@ -15,6 +34,7 @@ export function compileSwayToJson(params: ISwayParams) { const { inPlace = true, contractPath } = params; let inPlaceJsonAbiPath: string | undefined; + let inPlaceBinPath: string | undefined; // if `inPlace` is on, validates the need to re-compile contract if (inPlace && contractPath) { @@ -39,10 +59,7 @@ export function compileSwayToJson(params: ISwayParams) { // if file is fresh, return it at the speed of light if (isFresh) { - const abiContents = readFileSync(inPlaceJsonAbiPath, 'utf-8'); - const rawContents: IRawAbi = JSON.parse(abiContents); - const filepath = inPlaceJsonAbiPath; - return { filepath, rawContents }; + return getBundle(inPlaceJsonAbiPath); } } @@ -50,23 +67,18 @@ export function compileSwayToJson(params: ISwayParams) { const paramsWithAutoBuild = { ...params, autoBuild: true }; const project = createTempSwayProject(paramsWithAutoBuild); - // read compiled json abi - const abiContents = readFileSync(project.destinationAbiJsonPath, 'utf-8'); - const abiJson: IRawAbi = JSON.parse(abiContents); - - // format output to our needs - const output = { - filepath: project.destinationAbiJsonPath, - rawContents: abiJson, - }; - // if `inPlace` is enabled, we save a `abi.json` file // side-by-side with its origin contract if (inPlace && inPlaceJsonAbiPath) { + inPlaceBinPath = inPlaceJsonAbiPath.replace('-abi.json', '.bin'); mkdirp.sync(dirname(inPlaceJsonAbiPath)); + copyFileSync(project.destinationAbiJsonPath, inPlaceJsonAbiPath); + copyFileSync(project.destinationBinPath, inPlaceBinPath); } + const output = getBundle(project.destinationAbiJsonPath); + // delete temp project rimraf.sync(project.tempDir); From 2feed029edb6b3c163705f2f7dd2a65eedf391ad Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 24 Feb 2023 08:54:27 -0300 Subject: [PATCH 098/108] Updating sub-method signature; expanding outputs --- .../test/utils/sway/compileSwayToTs.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts b/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts index e4d5892d81d..d080dfca319 100644 --- a/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts +++ b/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts @@ -16,14 +16,20 @@ export function compileSwayToTs(params: ISwayParams) { const json = compileSwayToJson(params); // than creates a new Abi instance - const { filepath, rawContents } = json; + const { abiFilepath, abiContents, binFilepath, binContents } = json; const typegen = new AbiTypeGen({ - outputDir: dirname(filepath).replace('abis', 'contracts'), + outputDir: dirname(abiFilepath).replace('abis', 'contracts'), abiFiles: [ { - path: filepath, - contents: JSON.stringify(rawContents, null, 2), + path: abiFilepath, + contents: JSON.stringify(abiContents, null, 2), + }, + ], + binFiles: [ + { + path: binFilepath, + contents: binContents, }, ], category: CategoryEnum.CONTRACT, @@ -42,5 +48,6 @@ export function compileSwayToTs(params: ISwayParams) { // bundle and shoot return { abi, + typegen, }; } From 672f91d77346eb6d6defadd90e8d350259514c4d Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 24 Feb 2023 08:55:49 -0300 Subject: [PATCH 099/108] Renaming method --- packages/abi-typegen/src/abi/Abi.test.ts | 4 ++-- .../abi-typegen/src/abi/functions/Function.test.ts | 8 ++++---- packages/abi-typegen/src/abi/types/ArrayType.test.ts | 6 +++--- packages/abi-typegen/src/abi/types/EnumType.test.ts | 4 ++-- packages/abi-typegen/src/abi/types/OptionType.test.ts | 4 ++-- packages/abi-typegen/src/abi/types/StructType.test.ts | 4 ++-- packages/abi-typegen/src/abi/types/TupleType.test.ts | 4 ++-- .../abi-typegen/src/templates/common/index.test.ts | 4 ++-- .../abi-typegen/src/templates/contract/dts.test.ts | 10 +++++----- .../abi-typegen/src/templates/contract/factory.test.ts | 4 ++-- .../abi-typegen/src/templates/script/factory.test.ts | 6 +++--- .../abi-typegen/src/utils/parseTypeArguments.test.ts | 4 ++-- .../abi-typegen/test/utils/sway/compileSwayToJson.ts | 2 +- .../abi-typegen/test/utils/sway/compileSwayToTs.ts | 4 ++-- 14 files changed, 34 insertions(+), 34 deletions(-) diff --git a/packages/abi-typegen/src/abi/Abi.test.ts b/packages/abi-typegen/src/abi/Abi.test.ts index f57bdf81f7c..cf3b9bc1117 100644 --- a/packages/abi-typegen/src/abi/Abi.test.ts +++ b/packages/abi-typegen/src/abi/Abi.test.ts @@ -1,6 +1,6 @@ import { contractPaths } from '../../test/fixtures/index'; import { executeAndCatch } from '../../test/utils/executeAndCatch'; -import { compileSwayToJson } from '../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../test/utils/sway/compileSwayToJson'; import { CategoryEnum } from '../types/enums/CategoryEnum'; import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType'; import * as parseFunctionsMod from '../utils/parseFunctions'; @@ -35,7 +35,7 @@ describe('Abi.ts', () => { const outputDir = './out'; const contractPath = contractPaths.minimal; - const { rawContents } = compileSwayToJson({ contractPath }); + const { rawContents } = buildSway({ contractPath }); const abi = new Abi({ filepath: inputPath, diff --git a/packages/abi-typegen/src/abi/functions/Function.test.ts b/packages/abi-typegen/src/abi/functions/Function.test.ts index cc58310c323..caf2ba7d235 100644 --- a/packages/abi-typegen/src/abi/functions/Function.test.ts +++ b/packages/abi-typegen/src/abi/functions/Function.test.ts @@ -1,5 +1,5 @@ import { contractPaths } from '../../../test/fixtures'; -import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; import { parseTypes } from '../../utils/parseTypes'; import { Function } from './Function'; @@ -9,7 +9,7 @@ describe('Function.ts', () => { Method: `getDeclaration` */ test('should properly get function declaration', () => { - const { rawContents } = compileSwayToJson({ + const { rawContents } = buildSway({ contractPath: contractPaths.minimal, inPlace: true, }); @@ -33,7 +33,7 @@ describe('Function.ts', () => { Inputs / Output */ test('should compute i/o types for Vector', () => { - const { rawContents } = compileSwayToJson({ + const { rawContents } = buildSway({ contractPath: contractPaths.vectorSimple, inPlace: true, }); @@ -52,7 +52,7 @@ describe('Function.ts', () => { }); test('should build i/o types for Option', () => { - const { rawContents } = compileSwayToJson({ + const { rawContents } = buildSway({ contractPath: contractPaths.optionSimple, inPlace: true, }); diff --git a/packages/abi-typegen/src/abi/types/ArrayType.test.ts b/packages/abi-typegen/src/abi/types/ArrayType.test.ts index 9ede6e330e3..1f31adda507 100644 --- a/packages/abi-typegen/src/abi/types/ArrayType.test.ts +++ b/packages/abi-typegen/src/abi/types/ArrayType.test.ts @@ -1,5 +1,5 @@ import { contractPaths } from '../../../test/fixtures'; -import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; import type { IRawAbiTypeRoot } from '../../types/interfaces/IRawAbiType'; import { findType } from '../../utils/findType'; import { makeType } from '../../utils/makeType'; @@ -23,7 +23,7 @@ describe('ArrayType.ts', () => { const parseTypeArguments = jest.spyOn(parseTypeArgumentsMod, 'parseTypeArguments'); const contractPath = contractPaths.structWithArray; - const rawTypes = compileSwayToJson({ contractPath }).rawContents.types; + const rawTypes = buildSway({ contractPath }).rawContents.types; const types = rawTypes.map((rawAbiType: IRawAbiTypeRoot) => makeType({ rawAbiType })); // validating `struct B`, with simple tuples on property `x` @@ -40,7 +40,7 @@ describe('ArrayType.ts', () => { const parseTypeArguments = jest.spyOn(parseTypeArgumentsMod, 'parseTypeArguments'); const contractPath = contractPaths.arrayWithGenerics; - const rawTypes = compileSwayToJson({ contractPath }).rawContents.types; + const rawTypes = buildSway({ contractPath }).rawContents.types; const types = rawTypes.map((rawAbiType: IRawAbiTypeRoot) => makeType({ rawAbiType })); const a = findType({ types, typeId: 1 }) as ArrayType; diff --git a/packages/abi-typegen/src/abi/types/EnumType.test.ts b/packages/abi-typegen/src/abi/types/EnumType.test.ts index 36cfd27af24..c026e1e6de5 100644 --- a/packages/abi-typegen/src/abi/types/EnumType.test.ts +++ b/packages/abi-typegen/src/abi/types/EnumType.test.ts @@ -1,5 +1,5 @@ import { contractPaths } from '../../../test/fixtures'; -import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; import { TargetEnum } from '../../types/enums/TargetEnum'; import type { IRawAbiTypeRoot } from '../../types/interfaces/IRawAbiType'; import { findType } from '../../utils/findType'; @@ -16,7 +16,7 @@ describe('EnumType.ts', () => { function getTypesForContract(params: { contractPath: string }) { const { contractPath } = params; - const rawTypes = compileSwayToJson({ contractPath }).rawContents.types; + const rawTypes = buildSway({ contractPath }).rawContents.types; const types = rawTypes .filter((t) => t.type !== '()') diff --git a/packages/abi-typegen/src/abi/types/OptionType.test.ts b/packages/abi-typegen/src/abi/types/OptionType.test.ts index 006ff0d12df..c376f377d7f 100644 --- a/packages/abi-typegen/src/abi/types/OptionType.test.ts +++ b/packages/abi-typegen/src/abi/types/OptionType.test.ts @@ -1,5 +1,5 @@ import { contractPaths } from '../../../test/fixtures'; -import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; import type { IRawAbiTypeRoot } from '../../index'; import { findType } from '../../utils/findType'; import { makeType } from '../../utils/makeType'; @@ -14,7 +14,7 @@ describe('OptionType.ts', () => { function getTypesForContract() { const contractPath = contractPaths.optionSimple; - const rawTypes = compileSwayToJson({ contractPath }).rawContents.types; + const rawTypes = buildSway({ contractPath }).rawContents.types; const types = rawTypes .filter((t) => t.type !== '()') diff --git a/packages/abi-typegen/src/abi/types/StructType.test.ts b/packages/abi-typegen/src/abi/types/StructType.test.ts index 404d6efe089..cb13990d653 100644 --- a/packages/abi-typegen/src/abi/types/StructType.test.ts +++ b/packages/abi-typegen/src/abi/types/StructType.test.ts @@ -1,5 +1,5 @@ import { contractPaths } from '../../../test/fixtures'; -import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; import type { IRawAbiTypeRoot } from '../../index'; import { TargetEnum } from '../../types/enums/TargetEnum'; import { findType } from '../../utils/findType'; @@ -14,7 +14,7 @@ describe('StructType.ts', () => { const parseTypeArguments = jest.spyOn(parseTypeArgumentsMod, 'parseTypeArguments'); const contractPath = contractPaths.structSimple; - const rawTypes = compileSwayToJson({ contractPath }).rawContents.types; + const rawTypes = buildSway({ contractPath }).rawContents.types; const types = rawTypes.map((rawAbiType: IRawAbiTypeRoot) => makeType({ rawAbiType })); const suitableForStruct = StructType.isSuitableFor({ type: StructType.swayType }); diff --git a/packages/abi-typegen/src/abi/types/TupleType.test.ts b/packages/abi-typegen/src/abi/types/TupleType.test.ts index 3b86aaced4f..0231d05be37 100644 --- a/packages/abi-typegen/src/abi/types/TupleType.test.ts +++ b/packages/abi-typegen/src/abi/types/TupleType.test.ts @@ -1,5 +1,5 @@ import { contractPaths } from '../../../test/fixtures'; -import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; import type { IRawAbiTypeRoot } from '../../index'; import { findType } from '../../utils/findType'; import { makeType } from '../../utils/makeType'; @@ -13,7 +13,7 @@ describe('TupleType.ts', () => { const parseTypeArguments = jest.spyOn(parseTypeArgumentsMod, 'parseTypeArguments'); const contractPath = contractPaths.tupleSimple; - const rawTypes = compileSwayToJson({ contractPath }).rawContents.types; + const rawTypes = buildSway({ contractPath }).rawContents.types; const types = rawTypes.map((rawAbiType: IRawAbiTypeRoot) => makeType({ rawAbiType })); const suitableForTuple = TupleType.isSuitableFor({ type: TupleType.swayType }); diff --git a/packages/abi-typegen/src/templates/common/index.test.ts b/packages/abi-typegen/src/templates/common/index.test.ts index f424f5b7af0..dce36abe76d 100644 --- a/packages/abi-typegen/src/templates/common/index.test.ts +++ b/packages/abi-typegen/src/templates/common/index.test.ts @@ -1,7 +1,7 @@ import { contractPaths } from '../../../test/fixtures'; import indexTemplate from '../../../test/fixtures/templates/contract/index.hbs'; import { mockVersions } from '../../../test/utils/mockVersions'; -import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; import { Abi } from '../../abi/Abi'; import { CategoryEnum } from '../../types/enums/CategoryEnum'; @@ -14,7 +14,7 @@ describe('templates/index', () => { // executing const contractPath = contractPaths.minimal; - const { rawContents } = compileSwayToJson({ contractPath }); + const { rawContents } = buildSway({ contractPath }); const abi = new Abi({ filepath: './my-contract-abi.json', diff --git a/packages/abi-typegen/src/templates/contract/dts.test.ts b/packages/abi-typegen/src/templates/contract/dts.test.ts index 41fb3d28904..847d5cdb2e1 100644 --- a/packages/abi-typegen/src/templates/contract/dts.test.ts +++ b/packages/abi-typegen/src/templates/contract/dts.test.ts @@ -1,7 +1,7 @@ import { contractPaths } from '../../../test/fixtures'; import expectedDtsFullTemplate from '../../../test/fixtures/templates/contract/dts.hbs'; import { mockVersions } from '../../../test/utils/mockVersions'; -import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; import { Abi } from '../../abi/Abi'; import { CategoryEnum } from '../../types/enums/CategoryEnum'; @@ -14,7 +14,7 @@ describe('templates/dts', () => { // executing const contractPath = contractPaths.full; - const { rawContents } = compileSwayToJson({ contractPath }); + const { rawContents } = buildSway({ contractPath }); const abi = new Abi({ filepath: './my-contract-abi.json', @@ -33,7 +33,7 @@ describe('templates/dts', () => { test('should render dts template w/ custom common types', () => { const contractPath = contractPaths.vectorSimple; - const { rawContents } = compileSwayToJson({ contractPath }); + const { rawContents } = buildSway({ contractPath }); const abi = new Abi({ filepath: './my-contract-abi.json', outputDir: 'stdout', @@ -47,7 +47,7 @@ describe('templates/dts', () => { test('should render dts cross-referencing for identical structs', () => { const contractPath = contractPaths.structSimple; - const { rawContents } = compileSwayToJson({ contractPath }); + const { rawContents } = buildSway({ contractPath }); const abi = new Abi({ filepath: './my-contract-abi.json', outputDir: 'stdout', @@ -61,7 +61,7 @@ describe('templates/dts', () => { test('should render dts cross-referencing for identical enums', () => { const contractPath = contractPaths.enumSimple; - const { rawContents } = compileSwayToJson({ contractPath }); + const { rawContents } = buildSway({ contractPath }); const abi = new Abi({ filepath: './my-contract-abi.json', outputDir: 'stdout', diff --git a/packages/abi-typegen/src/templates/contract/factory.test.ts b/packages/abi-typegen/src/templates/contract/factory.test.ts index 3fb271bc6d7..b0fda835a6f 100644 --- a/packages/abi-typegen/src/templates/contract/factory.test.ts +++ b/packages/abi-typegen/src/templates/contract/factory.test.ts @@ -1,7 +1,7 @@ import { contractPaths } from '../../../test/fixtures'; import factoryTemplate from '../../../test/fixtures/templates/contract/factory.hbs'; import { mockVersions } from '../../../test/utils/mockVersions'; -import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; import { Abi } from '../../abi/Abi'; import { CategoryEnum } from '../../types/enums/CategoryEnum'; @@ -14,7 +14,7 @@ describe('templates/factory', () => { // executing const contractPath = contractPaths.minimal; - const { rawContents } = compileSwayToJson({ contractPath }); + const { rawContents } = buildSway({ contractPath }); // executing const abi = new Abi({ diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts index 5fb4e8f97be..e480d4d191d 100644 --- a/packages/abi-typegen/src/templates/script/factory.test.ts +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -2,7 +2,7 @@ import { contractPaths } from '../../../test/fixtures'; import factoryTemplate from '../../../test/fixtures/templates/script/factory.hbs'; import { executeAndCatch } from '../../../test/utils/executeAndCatch'; import { mockVersions } from '../../../test/utils/mockVersions'; -import { compileSwayToJson } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; import { Abi } from '../../abi/Abi'; import { CategoryEnum } from '../../types/enums/CategoryEnum'; @@ -14,7 +14,7 @@ describe('factory.ts', () => { const contractPath = contractPaths.script; - const { rawContents } = compileSwayToJson({ contractPath }); + const { rawContents } = buildSway({ contractPath }); const abi = new Abi({ filepath: './my-script-abi.json', @@ -36,7 +36,7 @@ describe('factory.ts', () => { const contractPath = contractPaths.script; - const { rawContents } = compileSwayToJson({ contractPath }); + const { rawContents } = buildSway({ contractPath }); // friction here (deletes 'main' function by emptying the functions array) rawContents.functions = []; diff --git a/packages/abi-typegen/src/utils/parseTypeArguments.test.ts b/packages/abi-typegen/src/utils/parseTypeArguments.test.ts index 7337a0e9752..3fe52a239fe 100644 --- a/packages/abi-typegen/src/utils/parseTypeArguments.test.ts +++ b/packages/abi-typegen/src/utils/parseTypeArguments.test.ts @@ -1,5 +1,5 @@ import { contractPaths } from '../../test/fixtures/index'; -import { compileSwayToJson } from '../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../test/utils/sway/compileSwayToJson'; import { TargetEnum } from '../types/enums/TargetEnum'; import type { IRawAbiTypeRoot, IRawAbiTypeComponent } from '../types/interfaces/IRawAbiType'; @@ -102,7 +102,7 @@ describe('parseTypeArguments.ts', () => { test('should fallback to void for null outputs', async () => { const contractPath = contractPaths.fnVoid; - const jsonAbi = await compileSwayToJson({ contractPath }); + const jsonAbi = await buildSway({ contractPath }); const types = bundleTypes([]); const typeArguments = [jsonAbi.rawContents.functions[0].output]; diff --git a/packages/abi-typegen/test/utils/sway/compileSwayToJson.ts b/packages/abi-typegen/test/utils/sway/compileSwayToJson.ts index f645cbd8807..92a3cdb6036 100644 --- a/packages/abi-typegen/test/utils/sway/compileSwayToJson.ts +++ b/packages/abi-typegen/test/utils/sway/compileSwayToJson.ts @@ -30,7 +30,7 @@ const getBundle = (abiFilepath: string) => { /* Compile Sway contract to JSON ABI */ -export function compileSwayToJson(params: ISwayParams) { +export function buildSway(params: ISwayParams) { const { inPlace = true, contractPath } = params; let inPlaceJsonAbiPath: string | undefined; diff --git a/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts b/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts index d080dfca319..ea6161c0e80 100644 --- a/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts +++ b/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts @@ -6,14 +6,14 @@ import { AbiTypeGen } from '../../../src/AbiTypeGen'; import { CategoryEnum } from '../../../src/types/enums/CategoryEnum'; import type { ISwayParams } from './ISwayUtilParams'; -import { compileSwayToJson } from './compileSwayToJson'; +import { buildSway } from './compileSwayToJson'; /* Compile Sway contract to Typescript */ export function compileSwayToTs(params: ISwayParams) { // first get the json abi for it - const json = compileSwayToJson(params); + const json = buildSway(params); // than creates a new Abi instance const { abiFilepath, abiContents, binFilepath, binContents } = json; From e7a6970bc8e578854aeb35bcf248db5298ff5e71 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 24 Feb 2023 08:57:11 -0300 Subject: [PATCH 100/108] Renaming file to match inner method --- packages/abi-typegen/src/abi/Abi.test.ts | 2 +- packages/abi-typegen/src/abi/functions/Function.test.ts | 2 +- packages/abi-typegen/src/abi/types/ArrayType.test.ts | 2 +- packages/abi-typegen/src/abi/types/EnumType.test.ts | 2 +- packages/abi-typegen/src/abi/types/OptionType.test.ts | 2 +- packages/abi-typegen/src/abi/types/StructType.test.ts | 2 +- packages/abi-typegen/src/abi/types/TupleType.test.ts | 2 +- packages/abi-typegen/src/templates/common/index.test.ts | 2 +- packages/abi-typegen/src/templates/contract/dts.test.ts | 2 +- packages/abi-typegen/src/templates/contract/factory.test.ts | 2 +- packages/abi-typegen/src/templates/script/factory.test.ts | 2 +- packages/abi-typegen/src/utils/parseTypeArguments.test.ts | 2 +- .../test/utils/sway/{compileSwayToJson.ts => buildSway.ts} | 0 packages/abi-typegen/test/utils/sway/compileSwayToTs.ts | 2 +- 14 files changed, 13 insertions(+), 13 deletions(-) rename packages/abi-typegen/test/utils/sway/{compileSwayToJson.ts => buildSway.ts} (100%) diff --git a/packages/abi-typegen/src/abi/Abi.test.ts b/packages/abi-typegen/src/abi/Abi.test.ts index cf3b9bc1117..5185083dd38 100644 --- a/packages/abi-typegen/src/abi/Abi.test.ts +++ b/packages/abi-typegen/src/abi/Abi.test.ts @@ -1,6 +1,6 @@ import { contractPaths } from '../../test/fixtures/index'; import { executeAndCatch } from '../../test/utils/executeAndCatch'; -import { buildSway } from '../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../test/utils/sway/buildSway'; import { CategoryEnum } from '../types/enums/CategoryEnum'; import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType'; import * as parseFunctionsMod from '../utils/parseFunctions'; diff --git a/packages/abi-typegen/src/abi/functions/Function.test.ts b/packages/abi-typegen/src/abi/functions/Function.test.ts index caf2ba7d235..9249364c44d 100644 --- a/packages/abi-typegen/src/abi/functions/Function.test.ts +++ b/packages/abi-typegen/src/abi/functions/Function.test.ts @@ -1,5 +1,5 @@ import { contractPaths } from '../../../test/fixtures'; -import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/buildSway'; import { parseTypes } from '../../utils/parseTypes'; import { Function } from './Function'; diff --git a/packages/abi-typegen/src/abi/types/ArrayType.test.ts b/packages/abi-typegen/src/abi/types/ArrayType.test.ts index 1f31adda507..7da3f7c8721 100644 --- a/packages/abi-typegen/src/abi/types/ArrayType.test.ts +++ b/packages/abi-typegen/src/abi/types/ArrayType.test.ts @@ -1,5 +1,5 @@ import { contractPaths } from '../../../test/fixtures'; -import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/buildSway'; import type { IRawAbiTypeRoot } from '../../types/interfaces/IRawAbiType'; import { findType } from '../../utils/findType'; import { makeType } from '../../utils/makeType'; diff --git a/packages/abi-typegen/src/abi/types/EnumType.test.ts b/packages/abi-typegen/src/abi/types/EnumType.test.ts index c026e1e6de5..13de45c1ef9 100644 --- a/packages/abi-typegen/src/abi/types/EnumType.test.ts +++ b/packages/abi-typegen/src/abi/types/EnumType.test.ts @@ -1,5 +1,5 @@ import { contractPaths } from '../../../test/fixtures'; -import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/buildSway'; import { TargetEnum } from '../../types/enums/TargetEnum'; import type { IRawAbiTypeRoot } from '../../types/interfaces/IRawAbiType'; import { findType } from '../../utils/findType'; diff --git a/packages/abi-typegen/src/abi/types/OptionType.test.ts b/packages/abi-typegen/src/abi/types/OptionType.test.ts index c376f377d7f..e33711d4caf 100644 --- a/packages/abi-typegen/src/abi/types/OptionType.test.ts +++ b/packages/abi-typegen/src/abi/types/OptionType.test.ts @@ -1,5 +1,5 @@ import { contractPaths } from '../../../test/fixtures'; -import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/buildSway'; import type { IRawAbiTypeRoot } from '../../index'; import { findType } from '../../utils/findType'; import { makeType } from '../../utils/makeType'; diff --git a/packages/abi-typegen/src/abi/types/StructType.test.ts b/packages/abi-typegen/src/abi/types/StructType.test.ts index cb13990d653..cb9f088cb5b 100644 --- a/packages/abi-typegen/src/abi/types/StructType.test.ts +++ b/packages/abi-typegen/src/abi/types/StructType.test.ts @@ -1,5 +1,5 @@ import { contractPaths } from '../../../test/fixtures'; -import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/buildSway'; import type { IRawAbiTypeRoot } from '../../index'; import { TargetEnum } from '../../types/enums/TargetEnum'; import { findType } from '../../utils/findType'; diff --git a/packages/abi-typegen/src/abi/types/TupleType.test.ts b/packages/abi-typegen/src/abi/types/TupleType.test.ts index 0231d05be37..5c27621c07d 100644 --- a/packages/abi-typegen/src/abi/types/TupleType.test.ts +++ b/packages/abi-typegen/src/abi/types/TupleType.test.ts @@ -1,5 +1,5 @@ import { contractPaths } from '../../../test/fixtures'; -import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/buildSway'; import type { IRawAbiTypeRoot } from '../../index'; import { findType } from '../../utils/findType'; import { makeType } from '../../utils/makeType'; diff --git a/packages/abi-typegen/src/templates/common/index.test.ts b/packages/abi-typegen/src/templates/common/index.test.ts index dce36abe76d..c71f8e2d2fc 100644 --- a/packages/abi-typegen/src/templates/common/index.test.ts +++ b/packages/abi-typegen/src/templates/common/index.test.ts @@ -1,7 +1,7 @@ import { contractPaths } from '../../../test/fixtures'; import indexTemplate from '../../../test/fixtures/templates/contract/index.hbs'; import { mockVersions } from '../../../test/utils/mockVersions'; -import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/buildSway'; import { Abi } from '../../abi/Abi'; import { CategoryEnum } from '../../types/enums/CategoryEnum'; diff --git a/packages/abi-typegen/src/templates/contract/dts.test.ts b/packages/abi-typegen/src/templates/contract/dts.test.ts index 847d5cdb2e1..1f29ebe20b6 100644 --- a/packages/abi-typegen/src/templates/contract/dts.test.ts +++ b/packages/abi-typegen/src/templates/contract/dts.test.ts @@ -1,7 +1,7 @@ import { contractPaths } from '../../../test/fixtures'; import expectedDtsFullTemplate from '../../../test/fixtures/templates/contract/dts.hbs'; import { mockVersions } from '../../../test/utils/mockVersions'; -import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/buildSway'; import { Abi } from '../../abi/Abi'; import { CategoryEnum } from '../../types/enums/CategoryEnum'; diff --git a/packages/abi-typegen/src/templates/contract/factory.test.ts b/packages/abi-typegen/src/templates/contract/factory.test.ts index b0fda835a6f..c011d07735f 100644 --- a/packages/abi-typegen/src/templates/contract/factory.test.ts +++ b/packages/abi-typegen/src/templates/contract/factory.test.ts @@ -1,7 +1,7 @@ import { contractPaths } from '../../../test/fixtures'; import factoryTemplate from '../../../test/fixtures/templates/contract/factory.hbs'; import { mockVersions } from '../../../test/utils/mockVersions'; -import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/buildSway'; import { Abi } from '../../abi/Abi'; import { CategoryEnum } from '../../types/enums/CategoryEnum'; diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts index e480d4d191d..1209ad98c7b 100644 --- a/packages/abi-typegen/src/templates/script/factory.test.ts +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -2,7 +2,7 @@ import { contractPaths } from '../../../test/fixtures'; import factoryTemplate from '../../../test/fixtures/templates/script/factory.hbs'; import { executeAndCatch } from '../../../test/utils/executeAndCatch'; import { mockVersions } from '../../../test/utils/mockVersions'; -import { buildSway } from '../../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../../test/utils/sway/buildSway'; import { Abi } from '../../abi/Abi'; import { CategoryEnum } from '../../types/enums/CategoryEnum'; diff --git a/packages/abi-typegen/src/utils/parseTypeArguments.test.ts b/packages/abi-typegen/src/utils/parseTypeArguments.test.ts index 3fe52a239fe..f7f44b58d1a 100644 --- a/packages/abi-typegen/src/utils/parseTypeArguments.test.ts +++ b/packages/abi-typegen/src/utils/parseTypeArguments.test.ts @@ -1,5 +1,5 @@ import { contractPaths } from '../../test/fixtures/index'; -import { buildSway } from '../../test/utils/sway/compileSwayToJson'; +import { buildSway } from '../../test/utils/sway/buildSway'; import { TargetEnum } from '../types/enums/TargetEnum'; import type { IRawAbiTypeRoot, IRawAbiTypeComponent } from '../types/interfaces/IRawAbiType'; diff --git a/packages/abi-typegen/test/utils/sway/compileSwayToJson.ts b/packages/abi-typegen/test/utils/sway/buildSway.ts similarity index 100% rename from packages/abi-typegen/test/utils/sway/compileSwayToJson.ts rename to packages/abi-typegen/test/utils/sway/buildSway.ts diff --git a/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts b/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts index ea6161c0e80..1a7be77cff8 100644 --- a/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts +++ b/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts @@ -6,7 +6,7 @@ import { AbiTypeGen } from '../../../src/AbiTypeGen'; import { CategoryEnum } from '../../../src/types/enums/CategoryEnum'; import type { ISwayParams } from './ISwayUtilParams'; -import { buildSway } from './compileSwayToJson'; +import { buildSway } from './buildSway'; /* Compile Sway contract to Typescript From 5ebe8862f56f271f3e8323d5798867728751f2c3 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 24 Feb 2023 09:00:12 -0300 Subject: [PATCH 101/108] Lintfix --- packages/abi-typegen/src/AbiTypeGen.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/abi-typegen/src/AbiTypeGen.test.ts b/packages/abi-typegen/src/AbiTypeGen.test.ts index 5e09e39fce2..fe6c42970c9 100644 --- a/packages/abi-typegen/src/AbiTypeGen.test.ts +++ b/packages/abi-typegen/src/AbiTypeGen.test.ts @@ -52,7 +52,7 @@ describe('AbiTypegen.ts', () => { test('should throw for unknown category', async () => { const { assembleContracts, assembleScripts } = mockAllDeps(); - const category = 'nope' as any; + const category = 'nope' as CategoryEnum; // forced cast to cause error const { error } = await executeAndCatch(() => { getNewAbiTypegen({ category, includeBinFiles: true }); From fc55b6124428651568b7f9af239db5319f0e0391 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 7 Mar 2023 08:15:33 -0300 Subject: [PATCH 102/108] Renaming enum, updating refs --- packages/abi-typegen/src/AbiTypeGen.test.ts | 8 ++++---- packages/abi-typegen/src/AbiTypeGen.ts | 10 +++++----- packages/abi-typegen/src/abi/Abi.test.ts | 4 ++-- packages/abi-typegen/src/abi/Abi.ts | 6 +++--- packages/abi-typegen/src/cli.test.ts | 6 +++--- packages/abi-typegen/src/cli.ts | 6 +++--- packages/abi-typegen/src/runTypegen.test.ts | 12 ++++++------ packages/abi-typegen/src/runTypegen.ts | 6 +++--- .../abi-typegen/src/templates/common/index.test.ts | 4 ++-- packages/abi-typegen/src/templates/common/index.ts | 4 ++-- .../abi-typegen/src/templates/contract/dts.test.ts | 10 +++++----- .../src/templates/contract/factory.test.ts | 4 ++-- .../abi-typegen/src/templates/script/factory.test.ts | 6 +++--- .../src/templates/utils/formatEnums.test.ts | 4 ++-- .../src/templates/utils/formatStructs.test.ts | 4 ++-- .../enums/{CategoryEnum.ts => ProgramTypeEnum.ts} | 2 +- .../abi-typegen/src/utils/assembleContracts.test.ts | 6 +++--- .../abi-typegen/src/utils/assembleScripts.test.ts | 6 +++--- .../abi-typegen/src/utils/validateBinFile.test.ts | 8 ++++---- packages/abi-typegen/src/utils/validateBinFile.ts | 6 +++--- packages/abi-typegen/test/utils/getNewAbiTypegen.ts | 6 +++--- .../abi-typegen/test/utils/sway/compileSwayToTs.ts | 4 ++-- 22 files changed, 66 insertions(+), 66 deletions(-) rename packages/abi-typegen/src/types/enums/{CategoryEnum.ts => ProgramTypeEnum.ts} (61%) diff --git a/packages/abi-typegen/src/AbiTypeGen.test.ts b/packages/abi-typegen/src/AbiTypeGen.test.ts index fe6c42970c9..e28b69053d8 100644 --- a/packages/abi-typegen/src/AbiTypeGen.test.ts +++ b/packages/abi-typegen/src/AbiTypeGen.test.ts @@ -1,7 +1,7 @@ import { executeAndCatch } from '../test/utils/executeAndCatch'; import { getNewAbiTypegen } from '../test/utils/getNewAbiTypegen'; -import { CategoryEnum } from './types/enums/CategoryEnum'; +import { ProgramTypeEnum } from './types/enums/ProgramTypeEnum'; import * as assembleContractsMod from './utils/assembleContracts'; import * as assembleScriptsMod from './utils/assembleScripts'; @@ -26,7 +26,7 @@ describe('AbiTypegen.ts', () => { test('should create multiple ABI instances for: contracts', async () => { const { assembleContracts, assembleScripts } = mockAllDeps(); - const category = CategoryEnum.CONTRACT; + const category = ProgramTypeEnum.CONTRACT; const { typegen } = getNewAbiTypegen({ category }); expect(typegen).toBeTruthy(); @@ -39,7 +39,7 @@ describe('AbiTypegen.ts', () => { test('should create multiple ABI instances for: scripts', async () => { const { assembleContracts, assembleScripts } = mockAllDeps(); - const category = CategoryEnum.SCRIPT; + const category = ProgramTypeEnum.SCRIPT; const { typegen } = getNewAbiTypegen({ category, includeBinFiles: true }); expect(typegen).toBeTruthy(); @@ -52,7 +52,7 @@ describe('AbiTypegen.ts', () => { test('should throw for unknown category', async () => { const { assembleContracts, assembleScripts } = mockAllDeps(); - const category = 'nope' as CategoryEnum; // forced cast to cause error + const category = 'nope' as ProgramTypeEnum; // forced cast to cause error const { error } = await executeAndCatch(() => { getNewAbiTypegen({ category, includeBinFiles: true }); diff --git a/packages/abi-typegen/src/AbiTypeGen.ts b/packages/abi-typegen/src/AbiTypeGen.ts index a3a225368a5..df4713629fa 100644 --- a/packages/abi-typegen/src/AbiTypeGen.ts +++ b/packages/abi-typegen/src/AbiTypeGen.ts @@ -1,5 +1,5 @@ import { Abi } from './abi/Abi'; -import { CategoryEnum } from './types/enums/CategoryEnum'; +import { ProgramTypeEnum } from './types/enums/ProgramTypeEnum'; import type { IFile } from './types/interfaces/IFile'; import { assembleContracts } from './utils/assembleContracts'; import { assembleScripts } from './utils/assembleScripts'; @@ -20,7 +20,7 @@ export class AbiTypeGen { abiFiles: IFile[]; binFiles: IFile[]; outputDir: string; - category: CategoryEnum; + category: ProgramTypeEnum; }) { const { abiFiles, binFiles, outputDir, category } = params; @@ -58,14 +58,14 @@ export class AbiTypeGen { this.files = this.getAssembledFiles({ category }); } - private getAssembledFiles(params: { category: CategoryEnum }): IFile[] { + private getAssembledFiles(params: { category: ProgramTypeEnum }): IFile[] { const { abis, outputDir } = this; const { category } = params; switch (category) { - case CategoryEnum.CONTRACT: + case ProgramTypeEnum.CONTRACT: return assembleContracts({ abis, outputDir }); - case CategoryEnum.SCRIPT: + case ProgramTypeEnum.SCRIPT: return assembleScripts({ abis, outputDir }); default: throw new Error(`Invalid Typegen category: ${category}`); diff --git a/packages/abi-typegen/src/abi/Abi.test.ts b/packages/abi-typegen/src/abi/Abi.test.ts index 5185083dd38..7b618032f9c 100644 --- a/packages/abi-typegen/src/abi/Abi.test.ts +++ b/packages/abi-typegen/src/abi/Abi.test.ts @@ -1,7 +1,7 @@ import { contractPaths } from '../../test/fixtures/index'; import { executeAndCatch } from '../../test/utils/executeAndCatch'; import { buildSway } from '../../test/utils/sway/buildSway'; -import { CategoryEnum } from '../types/enums/CategoryEnum'; +import { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum'; import type { IRawAbiTypeRoot } from '../types/interfaces/IRawAbiType'; import * as parseFunctionsMod from '../utils/parseFunctions'; import * as parseTypesMod from '../utils/parseTypes'; @@ -41,7 +41,7 @@ describe('Abi.ts', () => { filepath: inputPath, outputDir, rawContents, - category: CategoryEnum.CONTRACT, + category: ProgramTypeEnum.CONTRACT, }); return { diff --git a/packages/abi-typegen/src/abi/Abi.ts b/packages/abi-typegen/src/abi/Abi.ts index 26a059d4074..828e23310c9 100644 --- a/packages/abi-typegen/src/abi/Abi.ts +++ b/packages/abi-typegen/src/abi/Abi.ts @@ -1,4 +1,4 @@ -import type { CategoryEnum } from '../types/enums/CategoryEnum'; +import type { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum'; import type { IFunction } from '../types/interfaces/IFunction'; import type { IRawAbi } from '../types/interfaces/IRawAbi'; import type { IType } from '../types/interfaces/IType'; @@ -11,7 +11,7 @@ import { parseTypes } from '../utils/parseTypes'; */ export class Abi { public name: string; - public category: CategoryEnum; + public category: ProgramTypeEnum; public filepath: string; public outputDir: string; @@ -26,7 +26,7 @@ export class Abi { constructor(params: { filepath: string; - category: CategoryEnum; + category: ProgramTypeEnum; rawContents: IRawAbi; hexlifiedBinContents?: string; outputDir: string; diff --git a/packages/abi-typegen/src/cli.test.ts b/packages/abi-typegen/src/cli.test.ts index 21a146cde1e..2d8258eefdb 100644 --- a/packages/abi-typegen/src/cli.test.ts +++ b/packages/abi-typegen/src/cli.test.ts @@ -6,7 +6,7 @@ import { createTempSwayProject } from '../test/utils/sway/createTempSwayProject' import { run } from './cli'; import * as runTypegenMod from './runTypegen'; -import { CategoryEnum } from './types/enums/CategoryEnum'; +import { ProgramTypeEnum } from './types/enums/ProgramTypeEnum'; describe('cli.ts', () => { function mockDeps() { @@ -48,7 +48,7 @@ describe('cli.ts', () => { cwd: process.cwd(), inputs, output, - category: CategoryEnum.CONTRACT, + category: ProgramTypeEnum.CONTRACT, silent: false, }); }); @@ -65,7 +65,7 @@ describe('cli.ts', () => { cwd: process.cwd(), inputs, output, - category: CategoryEnum.SCRIPT, + category: ProgramTypeEnum.SCRIPT, silent: false, }); diff --git a/packages/abi-typegen/src/cli.ts b/packages/abi-typegen/src/cli.ts index 65aaba7ccd5..f731d077c99 100644 --- a/packages/abi-typegen/src/cli.ts +++ b/packages/abi-typegen/src/cli.ts @@ -2,7 +2,7 @@ import { versions } from '@fuel-ts/versions'; import { Command, Option } from 'commander'; import { runTypegen } from './runTypegen'; -import { CategoryEnum } from './types/enums/CategoryEnum'; +import { ProgramTypeEnum } from './types/enums/ProgramTypeEnum'; export interface ICliParams { inputs: string[]; @@ -18,10 +18,10 @@ export function resolveCategory(params: { contract: boolean; script: boolean }) const noneSpecified = !contract && !script; if (contract || noneSpecified) { - return CategoryEnum.CONTRACT; + return ProgramTypeEnum.CONTRACT; } - return CategoryEnum.SCRIPT; + return ProgramTypeEnum.SCRIPT; } export function runCliAction(options: ICliParams) { diff --git a/packages/abi-typegen/src/runTypegen.test.ts b/packages/abi-typegen/src/runTypegen.test.ts index e2a40c364dc..adbcb567991 100644 --- a/packages/abi-typegen/src/runTypegen.test.ts +++ b/packages/abi-typegen/src/runTypegen.test.ts @@ -10,7 +10,7 @@ import { executeAndCatch } from '../test/utils/executeAndCatch'; import { createTempSwayProject } from '../test/utils/sway/createTempSwayProject'; import { runTypegen } from './runTypegen'; -import { CategoryEnum } from './types/enums/CategoryEnum'; +import { ProgramTypeEnum } from './types/enums/ProgramTypeEnum'; describe('runTypegen.js', () => { test('should run typegen, using: globals', async () => { @@ -33,7 +33,7 @@ describe('runTypegen.js', () => { const cwd = process.cwd(); const inputs = [join(tempDir, '/out/debug/*-abi.json')]; const output = join(tempDir, 'generated'); - const category = CategoryEnum.CONTRACT; + const category = ProgramTypeEnum.CONTRACT; const silent = true; // executes program @@ -83,7 +83,7 @@ describe('runTypegen.js', () => { const cwd = process.cwd(); const input = join(tempDir, '/out/debug/*-abi.json'); const output = join(tempDir, 'generated'); - const category = CategoryEnum.CONTRACT; + const category = ProgramTypeEnum.CONTRACT; const silent = true; const filepaths = globSync(input, { cwd }); @@ -134,7 +134,7 @@ describe('runTypegen.js', () => { const cwd = process.cwd(); const input = join(tempDir, '/out/debug/*-abi.json'); const output = join(tempDir, 'generated'); - const category = CategoryEnum.SCRIPT; + const category = ProgramTypeEnum.SCRIPT; const silent = true; const filepaths = globSync(input, { cwd }); @@ -187,7 +187,7 @@ describe('runTypegen.js', () => { const cwd = process.cwd(); const input = join(tempDir, '/out/debug/*-abi.json'); const output = join(tempDir, 'generated'); - const category = CategoryEnum.SCRIPT; + const category = ProgramTypeEnum.SCRIPT; const silent = true; const filepaths = globSync(input, { cwd }); @@ -224,7 +224,7 @@ describe('runTypegen.js', () => { // compute filepaths const cwd = process.cwd(); const output = join(tempDir, 'generated'); - const category = CategoryEnum.CONTRACT; + const category = ProgramTypeEnum.CONTRACT; const silent = true; // executes program diff --git a/packages/abi-typegen/src/runTypegen.ts b/packages/abi-typegen/src/runTypegen.ts index 498c7ed934c..0d29695c185 100644 --- a/packages/abi-typegen/src/runTypegen.ts +++ b/packages/abi-typegen/src/runTypegen.ts @@ -6,7 +6,7 @@ import { basename } from 'path'; import rimraf from 'rimraf'; import { AbiTypeGen } from './AbiTypeGen'; -import { CategoryEnum } from './types/enums/CategoryEnum'; +import { ProgramTypeEnum } from './types/enums/ProgramTypeEnum'; import type { IFile } from './types/interfaces/IFile'; import { validateBinFile } from './utils/validateBinFile'; @@ -16,7 +16,7 @@ export interface IGenerateFilesParams { inputs?: string[]; output: string; silent?: boolean; - category: CategoryEnum; + category: ProgramTypeEnum; } export function runTypegen(params: IGenerateFilesParams) { @@ -53,7 +53,7 @@ export function runTypegen(params: IGenerateFilesParams) { return abi; }); - const isScript = category === CategoryEnum.SCRIPT; + const isScript = category === ProgramTypeEnum.SCRIPT; const binFiles = !isScript ? [] diff --git a/packages/abi-typegen/src/templates/common/index.test.ts b/packages/abi-typegen/src/templates/common/index.test.ts index c71f8e2d2fc..26b6047e438 100644 --- a/packages/abi-typegen/src/templates/common/index.test.ts +++ b/packages/abi-typegen/src/templates/common/index.test.ts @@ -3,7 +3,7 @@ import indexTemplate from '../../../test/fixtures/templates/contract/index.hbs'; import { mockVersions } from '../../../test/utils/mockVersions'; import { buildSway } from '../../../test/utils/sway/buildSway'; import { Abi } from '../../abi/Abi'; -import { CategoryEnum } from '../../types/enums/CategoryEnum'; +import { ProgramTypeEnum } from '../../types/enums/ProgramTypeEnum'; import { renderIndexTemplate } from './index'; @@ -20,7 +20,7 @@ describe('templates/index', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, - category: CategoryEnum.CONTRACT, + category: ProgramTypeEnum.CONTRACT, }); const rendered = renderIndexTemplate({ abis: [abi] }); diff --git a/packages/abi-typegen/src/templates/common/index.ts b/packages/abi-typegen/src/templates/common/index.ts index 17947b06a5b..8aa50e5efca 100644 --- a/packages/abi-typegen/src/templates/common/index.ts +++ b/packages/abi-typegen/src/templates/common/index.ts @@ -1,5 +1,5 @@ import type { Abi } from '../../abi/Abi'; -import { CategoryEnum } from '../../types/enums/CategoryEnum'; +import { ProgramTypeEnum } from '../../types/enums/ProgramTypeEnum'; import { renderHbsTemplate } from '../renderHbsTemplate'; import indexTemplate from './index.hbs'; @@ -7,7 +7,7 @@ import indexTemplate from './index.hbs'; export function renderIndexTemplate(params: { abis: Abi[] }) { const { abis } = params; - const isGeneratingContracts = abis[0].category === CategoryEnum.CONTRACT; + const isGeneratingContracts = abis[0].category === ProgramTypeEnum.CONTRACT; const text = renderHbsTemplate({ template: indexTemplate, diff --git a/packages/abi-typegen/src/templates/contract/dts.test.ts b/packages/abi-typegen/src/templates/contract/dts.test.ts index 1f29ebe20b6..84c511d0b49 100644 --- a/packages/abi-typegen/src/templates/contract/dts.test.ts +++ b/packages/abi-typegen/src/templates/contract/dts.test.ts @@ -3,7 +3,7 @@ import expectedDtsFullTemplate from '../../../test/fixtures/templates/contract/d import { mockVersions } from '../../../test/utils/mockVersions'; import { buildSway } from '../../../test/utils/sway/buildSway'; import { Abi } from '../../abi/Abi'; -import { CategoryEnum } from '../../types/enums/CategoryEnum'; +import { ProgramTypeEnum } from '../../types/enums/ProgramTypeEnum'; import { renderDtsTemplate } from './dts'; @@ -20,7 +20,7 @@ describe('templates/dts', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, - category: CategoryEnum.CONTRACT, + category: ProgramTypeEnum.CONTRACT, }); const rendered = renderDtsTemplate({ abi }); @@ -38,7 +38,7 @@ describe('templates/dts', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, - category: CategoryEnum.CONTRACT, + category: ProgramTypeEnum.CONTRACT, }); const rendered = renderDtsTemplate({ abi }); @@ -52,7 +52,7 @@ describe('templates/dts', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, - category: CategoryEnum.CONTRACT, + category: ProgramTypeEnum.CONTRACT, }); const rendered = renderDtsTemplate({ abi }); @@ -66,7 +66,7 @@ describe('templates/dts', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, - category: CategoryEnum.CONTRACT, + category: ProgramTypeEnum.CONTRACT, }); const rendered = renderDtsTemplate({ abi }); diff --git a/packages/abi-typegen/src/templates/contract/factory.test.ts b/packages/abi-typegen/src/templates/contract/factory.test.ts index c011d07735f..23c7f4e419a 100644 --- a/packages/abi-typegen/src/templates/contract/factory.test.ts +++ b/packages/abi-typegen/src/templates/contract/factory.test.ts @@ -3,7 +3,7 @@ import factoryTemplate from '../../../test/fixtures/templates/contract/factory.h import { mockVersions } from '../../../test/utils/mockVersions'; import { buildSway } from '../../../test/utils/sway/buildSway'; import { Abi } from '../../abi/Abi'; -import { CategoryEnum } from '../../types/enums/CategoryEnum'; +import { ProgramTypeEnum } from '../../types/enums/ProgramTypeEnum'; import { renderFactoryTemplate } from './factory'; @@ -21,7 +21,7 @@ describe('templates/factory', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, - category: CategoryEnum.CONTRACT, + category: ProgramTypeEnum.CONTRACT, }); const rendered = renderFactoryTemplate({ abi }); diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts index 1209ad98c7b..bcd7b7cf8a2 100644 --- a/packages/abi-typegen/src/templates/script/factory.test.ts +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -4,7 +4,7 @@ import { executeAndCatch } from '../../../test/utils/executeAndCatch'; import { mockVersions } from '../../../test/utils/mockVersions'; import { buildSway } from '../../../test/utils/sway/buildSway'; import { Abi } from '../../abi/Abi'; -import { CategoryEnum } from '../../types/enums/CategoryEnum'; +import { ProgramTypeEnum } from '../../types/enums/ProgramTypeEnum'; import { renderFactoryTemplate } from './factory'; @@ -21,7 +21,7 @@ describe('factory.ts', () => { hexlifiedBinContents: '0x000', outputDir: 'stdout', rawContents, - category: CategoryEnum.SCRIPT, + category: ProgramTypeEnum.SCRIPT, }); const rendered = renderFactoryTemplate({ abi }); @@ -46,7 +46,7 @@ describe('factory.ts', () => { hexlifiedBinContents: '0x000', outputDir: 'stdout', rawContents, - category: CategoryEnum.SCRIPT, + category: ProgramTypeEnum.SCRIPT, }); const { error } = await executeAndCatch(() => { diff --git a/packages/abi-typegen/src/templates/utils/formatEnums.test.ts b/packages/abi-typegen/src/templates/utils/formatEnums.test.ts index 18be7d98ad0..20bd0a8138c 100644 --- a/packages/abi-typegen/src/templates/utils/formatEnums.test.ts +++ b/packages/abi-typegen/src/templates/utils/formatEnums.test.ts @@ -1,6 +1,6 @@ import enumOfEnumsAbiJson from '../../../test/fixtures/out/abis/enum-of-enums-abi.json'; import { Abi } from '../../abi/Abi'; -import { CategoryEnum } from '../../types/enums/CategoryEnum'; +import { ProgramTypeEnum } from '../../types/enums/ProgramTypeEnum'; import { formatEnums } from './formatEnums'; @@ -10,7 +10,7 @@ describe('formatEnums.ts', () => { filepath: './enum-simple-abi.json', outputDir: './contracts', rawContents: enumOfEnumsAbiJson, - category: CategoryEnum.CONTRACT, + category: ProgramTypeEnum.CONTRACT, }); // executing diff --git a/packages/abi-typegen/src/templates/utils/formatStructs.test.ts b/packages/abi-typegen/src/templates/utils/formatStructs.test.ts index b6bcd1c3dda..8ff55c5738a 100644 --- a/packages/abi-typegen/src/templates/utils/formatStructs.test.ts +++ b/packages/abi-typegen/src/templates/utils/formatStructs.test.ts @@ -1,6 +1,6 @@ import structSimpleAbiJson from '../../../test/fixtures/out/abis/struct-simple-abi.json'; import { Abi } from '../../abi/Abi'; -import { CategoryEnum } from '../../types/enums/CategoryEnum'; +import { ProgramTypeEnum } from '../../types/enums/ProgramTypeEnum'; import { formatStructs } from './formatStructs'; @@ -10,7 +10,7 @@ describe('formatStructs.ts', () => { filepath: './struct-simple-abi.json', outputDir: './contracts', rawContents: structSimpleAbiJson, - category: CategoryEnum.CONTRACT, + category: ProgramTypeEnum.CONTRACT, }); // executing diff --git a/packages/abi-typegen/src/types/enums/CategoryEnum.ts b/packages/abi-typegen/src/types/enums/ProgramTypeEnum.ts similarity index 61% rename from packages/abi-typegen/src/types/enums/CategoryEnum.ts rename to packages/abi-typegen/src/types/enums/ProgramTypeEnum.ts index 37d304266de..0299b8f9fd6 100644 --- a/packages/abi-typegen/src/types/enums/CategoryEnum.ts +++ b/packages/abi-typegen/src/types/enums/ProgramTypeEnum.ts @@ -1,4 +1,4 @@ -export enum CategoryEnum { +export enum ProgramTypeEnum { CONTRACT = 'contract', SCRIPT = 'script', } diff --git a/packages/abi-typegen/src/utils/assembleContracts.test.ts b/packages/abi-typegen/src/utils/assembleContracts.test.ts index 87c2899767c..1cab93467e8 100644 --- a/packages/abi-typegen/src/utils/assembleContracts.test.ts +++ b/packages/abi-typegen/src/utils/assembleContracts.test.ts @@ -2,7 +2,7 @@ import { getNewAbiTypegen } from '../../test/utils/getNewAbiTypegen'; import * as renderCommonTemplateMod from '../templates/common/common'; import * as renderIndexTemplateMod from '../templates/common/index'; import * as renderFactoryTemplateMod from '../templates/contract/factory'; -import { CategoryEnum } from '../types/enums/CategoryEnum'; +import { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum'; import { assembleContracts } from './assembleContracts'; @@ -33,7 +33,7 @@ describe('assembleContracts.ts', () => { const { typegen: { abis, outputDir }, } = getNewAbiTypegen({ - category: CategoryEnum.CONTRACT, + category: ProgramTypeEnum.CONTRACT, includeOptionType: false, // will prevent `common` template from being included }); @@ -52,7 +52,7 @@ describe('assembleContracts.ts', () => { const { typegen: { abis, outputDir }, } = getNewAbiTypegen({ - category: CategoryEnum.CONTRACT, + category: ProgramTypeEnum.CONTRACT, includeOptionType: true, // will cause `common` template to be included }); diff --git a/packages/abi-typegen/src/utils/assembleScripts.test.ts b/packages/abi-typegen/src/utils/assembleScripts.test.ts index d818b4ba75f..86504f3b36c 100644 --- a/packages/abi-typegen/src/utils/assembleScripts.test.ts +++ b/packages/abi-typegen/src/utils/assembleScripts.test.ts @@ -2,7 +2,7 @@ import { getNewAbiTypegen } from '../../test/utils/getNewAbiTypegen'; import * as renderCommonTemplateMod from '../templates/common/common'; import * as renderIndexTemplateMod from '../templates/common/index'; import * as renderFactoryTemplateMod from '../templates/script/factory'; -import { CategoryEnum } from '../types/enums/CategoryEnum'; +import { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum'; import { assembleScripts } from './assembleScripts'; @@ -36,7 +36,7 @@ describe('assembleScripts.ts', () => { const { typegen: { abis, outputDir }, } = getNewAbiTypegen({ - category: CategoryEnum.SCRIPT, + category: ProgramTypeEnum.SCRIPT, includeOptionType: false, // will prevent common template from being included includeMainFunction: true, includeBinFiles: true, @@ -59,7 +59,7 @@ describe('assembleScripts.ts', () => { const { typegen: { abis, outputDir }, } = getNewAbiTypegen({ - category: CategoryEnum.SCRIPT, + category: ProgramTypeEnum.SCRIPT, includeOptionType: true, // will cause common template to be included includeMainFunction: true, includeBinFiles: true, diff --git a/packages/abi-typegen/src/utils/validateBinFile.test.ts b/packages/abi-typegen/src/utils/validateBinFile.test.ts index 9504be19997..03ce562bf11 100644 --- a/packages/abi-typegen/src/utils/validateBinFile.test.ts +++ b/packages/abi-typegen/src/utils/validateBinFile.test.ts @@ -1,4 +1,4 @@ -import { CategoryEnum } from '../types/enums/CategoryEnum'; +import { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum'; import { validateBinFile } from './validateBinFile'; @@ -8,7 +8,7 @@ describe('validateBinFile.ts', () => { abiFilepath: 'script-abi.json', binExists: true, binFilepath: 'script.bin', - category: CategoryEnum.SCRIPT, + category: ProgramTypeEnum.SCRIPT, }; expect(() => validateBinFile(params)).not.toThrow(); @@ -19,7 +19,7 @@ describe('validateBinFile.ts', () => { abiFilepath: './contract-abi.json', binExists: false, binFilepath: './contract.bin', - category: CategoryEnum.CONTRACT, + category: ProgramTypeEnum.CONTRACT, }; expect(() => validateBinFile(params)).not.toThrow(); @@ -30,7 +30,7 @@ describe('validateBinFile.ts', () => { abiFilepath: './script-abi.json', binExists: false, binFilepath: './script.bin', - category: CategoryEnum.SCRIPT, + category: ProgramTypeEnum.SCRIPT, }; expect(() => validateBinFile(params)).toThrowError( diff --git a/packages/abi-typegen/src/utils/validateBinFile.ts b/packages/abi-typegen/src/utils/validateBinFile.ts index 0b54804006d..2780f630bdb 100644 --- a/packages/abi-typegen/src/utils/validateBinFile.ts +++ b/packages/abi-typegen/src/utils/validateBinFile.ts @@ -1,16 +1,16 @@ import upperFirst from 'lodash.upperfirst'; -import { CategoryEnum } from '../types/enums/CategoryEnum'; +import { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum'; export function validateBinFile(params: { abiFilepath: string; binFilepath: string; binExists: boolean; - category: CategoryEnum; + category: ProgramTypeEnum; }) { const { abiFilepath, binFilepath, binExists, category } = params; - const isScript = category === CategoryEnum.SCRIPT; + const isScript = category === ProgramTypeEnum.SCRIPT; if (!binExists && isScript) { throw new Error( diff --git a/packages/abi-typegen/test/utils/getNewAbiTypegen.ts b/packages/abi-typegen/test/utils/getNewAbiTypegen.ts index 8c328b719fb..52c5324804e 100644 --- a/packages/abi-typegen/test/utils/getNewAbiTypegen.ts +++ b/packages/abi-typegen/test/utils/getNewAbiTypegen.ts @@ -1,10 +1,10 @@ import type { IFile, IRawAbiTypeRoot } from '../../src/index'; import { AbiTypeGen } from '../../src/index'; -import { CategoryEnum } from '../../src/types/enums/CategoryEnum'; +import { ProgramTypeEnum } from '../../src/types/enums/ProgramTypeEnum'; export function getNewAbiTypegen( params: { - category?: CategoryEnum; + category?: ProgramTypeEnum; includeOptionType?: boolean; includeMainFunction?: boolean; includeBinFiles?: boolean; @@ -12,7 +12,7 @@ export function getNewAbiTypegen( ) { const { includeOptionType = false, - category = CategoryEnum.CONTRACT, + category = ProgramTypeEnum.CONTRACT, includeMainFunction = false, includeBinFiles = false, } = params; diff --git a/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts b/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts index 1a7be77cff8..6f9fb32a1f7 100644 --- a/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts +++ b/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts @@ -3,7 +3,7 @@ import mkdirp from 'mkdirp'; import { dirname } from 'path'; import { AbiTypeGen } from '../../../src/AbiTypeGen'; -import { CategoryEnum } from '../../../src/types/enums/CategoryEnum'; +import { ProgramTypeEnum } from '../../../src/types/enums/ProgramTypeEnum'; import type { ISwayParams } from './ISwayUtilParams'; import { buildSway } from './buildSway'; @@ -32,7 +32,7 @@ export function compileSwayToTs(params: ISwayParams) { contents: binContents, }, ], - category: CategoryEnum.CONTRACT, + category: ProgramTypeEnum.CONTRACT, }); // create handy shortcuts for common definitions From d6aaa044c21faab5272d71681a08f8205994fe1b Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 7 Mar 2023 08:21:01 -0300 Subject: [PATCH 103/108] Adjusting semantics of category x programType in more places --- packages/abi-typegen/src/AbiTypeGen.test.ts | 16 +++++++-------- packages/abi-typegen/src/AbiTypeGen.ts | 18 ++++++++--------- packages/abi-typegen/src/abi/Abi.test.ts | 2 +- packages/abi-typegen/src/abi/Abi.ts | 8 ++++---- packages/abi-typegen/src/cli.test.ts | 4 ++-- packages/abi-typegen/src/cli.ts | 6 +++--- packages/abi-typegen/src/runTypegen.test.ts | 20 +++++++++---------- packages/abi-typegen/src/runTypegen.ts | 10 +++++----- .../src/templates/common/index.test.ts | 2 +- .../abi-typegen/src/templates/common/index.ts | 2 +- .../src/templates/contract/dts.test.ts | 8 ++++---- .../src/templates/contract/factory.test.ts | 2 +- .../src/templates/script/factory.test.ts | 4 ++-- .../src/templates/utils/formatEnums.test.ts | 2 +- .../src/templates/utils/formatStructs.test.ts | 2 +- .../src/utils/assembleContracts.test.ts | 4 ++-- .../src/utils/assembleScripts.test.ts | 4 ++-- .../src/utils/validateBinFile.test.ts | 6 +++--- .../abi-typegen/src/utils/validateBinFile.ts | 10 +++++----- .../test/utils/getNewAbiTypegen.ts | 6 +++--- .../test/utils/sway/compileSwayToTs.ts | 2 +- 21 files changed, 69 insertions(+), 69 deletions(-) diff --git a/packages/abi-typegen/src/AbiTypeGen.test.ts b/packages/abi-typegen/src/AbiTypeGen.test.ts index e28b69053d8..47f4801d7c6 100644 --- a/packages/abi-typegen/src/AbiTypeGen.test.ts +++ b/packages/abi-typegen/src/AbiTypeGen.test.ts @@ -26,8 +26,8 @@ describe('AbiTypegen.ts', () => { test('should create multiple ABI instances for: contracts', async () => { const { assembleContracts, assembleScripts } = mockAllDeps(); - const category = ProgramTypeEnum.CONTRACT; - const { typegen } = getNewAbiTypegen({ category }); + const programType = ProgramTypeEnum.CONTRACT; + const { typegen } = getNewAbiTypegen({ programType }); expect(typegen).toBeTruthy(); expect(typegen.abis.length).toEqual(2); @@ -39,8 +39,8 @@ describe('AbiTypegen.ts', () => { test('should create multiple ABI instances for: scripts', async () => { const { assembleContracts, assembleScripts } = mockAllDeps(); - const category = ProgramTypeEnum.SCRIPT; - const { typegen } = getNewAbiTypegen({ category, includeBinFiles: true }); + const programType = ProgramTypeEnum.SCRIPT; + const { typegen } = getNewAbiTypegen({ programType, includeBinFiles: true }); expect(typegen).toBeTruthy(); expect(typegen.abis.length).toEqual(2); @@ -49,16 +49,16 @@ describe('AbiTypegen.ts', () => { expect(assembleScripts).toHaveBeenCalledTimes(1); }); - test('should throw for unknown category', async () => { + test('should throw for unknown programType', async () => { const { assembleContracts, assembleScripts } = mockAllDeps(); - const category = 'nope' as ProgramTypeEnum; // forced cast to cause error + const programType = 'nope' as ProgramTypeEnum; // forced cast to cause error const { error } = await executeAndCatch(() => { - getNewAbiTypegen({ category, includeBinFiles: true }); + getNewAbiTypegen({ programType, includeBinFiles: true }); }); - expect(error?.message).toMatch(/Invalid Typegen category: nope/); + expect(error?.message).toMatch(/Invalid Typegen programType: nope/); expect(assembleContracts).toHaveBeenCalledTimes(0); expect(assembleScripts).toHaveBeenCalledTimes(0); diff --git a/packages/abi-typegen/src/AbiTypeGen.ts b/packages/abi-typegen/src/AbiTypeGen.ts index df4713629fa..8c1df252114 100644 --- a/packages/abi-typegen/src/AbiTypeGen.ts +++ b/packages/abi-typegen/src/AbiTypeGen.ts @@ -20,9 +20,9 @@ export class AbiTypeGen { abiFiles: IFile[]; binFiles: IFile[]; outputDir: string; - category: ProgramTypeEnum; + programType: ProgramTypeEnum; }) { - const { abiFiles, binFiles, outputDir, category } = params; + const { abiFiles, binFiles, outputDir, programType } = params; this.outputDir = outputDir; @@ -39,7 +39,7 @@ export class AbiTypeGen { abiFilepath: abiFile.path, binExists: !!relatedBinFile, binFilepath, - category, + programType, }); } @@ -48,27 +48,27 @@ export class AbiTypeGen { rawContents: JSON.parse(abiFile.contents as string), hexlifiedBinContents: relatedBinFile?.contents, outputDir, - category, + programType, }); return abi; }); // Assemble list of files to be written to disk - this.files = this.getAssembledFiles({ category }); + this.files = this.getAssembledFiles({ programType }); } - private getAssembledFiles(params: { category: ProgramTypeEnum }): IFile[] { + private getAssembledFiles(params: { programType: ProgramTypeEnum }): IFile[] { const { abis, outputDir } = this; - const { category } = params; + const { programType } = params; - switch (category) { + switch (programType) { case ProgramTypeEnum.CONTRACT: return assembleContracts({ abis, outputDir }); case ProgramTypeEnum.SCRIPT: return assembleScripts({ abis, outputDir }); default: - throw new Error(`Invalid Typegen category: ${category}`); + throw new Error(`Invalid Typegen programType: ${programType}`); } } } diff --git a/packages/abi-typegen/src/abi/Abi.test.ts b/packages/abi-typegen/src/abi/Abi.test.ts index 7b618032f9c..b44ed68ccba 100644 --- a/packages/abi-typegen/src/abi/Abi.test.ts +++ b/packages/abi-typegen/src/abi/Abi.test.ts @@ -41,7 +41,7 @@ describe('Abi.ts', () => { filepath: inputPath, outputDir, rawContents, - category: ProgramTypeEnum.CONTRACT, + programType: ProgramTypeEnum.CONTRACT, }); return { diff --git a/packages/abi-typegen/src/abi/Abi.ts b/packages/abi-typegen/src/abi/Abi.ts index 828e23310c9..0cc0601f552 100644 --- a/packages/abi-typegen/src/abi/Abi.ts +++ b/packages/abi-typegen/src/abi/Abi.ts @@ -11,7 +11,7 @@ import { parseTypes } from '../utils/parseTypes'; */ export class Abi { public name: string; - public category: ProgramTypeEnum; + public programType: ProgramTypeEnum; public filepath: string; public outputDir: string; @@ -26,12 +26,12 @@ export class Abi { constructor(params: { filepath: string; - category: ProgramTypeEnum; + programType: ProgramTypeEnum; rawContents: IRawAbi; hexlifiedBinContents?: string; outputDir: string; }) { - const { filepath, outputDir, rawContents, hexlifiedBinContents, category } = params; + const { filepath, outputDir, rawContents, hexlifiedBinContents, programType } = params; const abiNameRegex = /([^/]+)-abi\.json$/m; const abiName = filepath.match(abiNameRegex); @@ -45,7 +45,7 @@ export class Abi { const name = `${normalizeName(abiName[1])}Abi`; this.name = name; - this.category = category; + this.programType = programType; this.filepath = filepath; this.rawContents = rawContents; diff --git a/packages/abi-typegen/src/cli.test.ts b/packages/abi-typegen/src/cli.test.ts index 2d8258eefdb..046f9106917 100644 --- a/packages/abi-typegen/src/cli.test.ts +++ b/packages/abi-typegen/src/cli.test.ts @@ -48,7 +48,7 @@ describe('cli.ts', () => { cwd: process.cwd(), inputs, output, - category: ProgramTypeEnum.CONTRACT, + programType: ProgramTypeEnum.CONTRACT, silent: false, }); }); @@ -65,7 +65,7 @@ describe('cli.ts', () => { cwd: process.cwd(), inputs, output, - category: ProgramTypeEnum.SCRIPT, + programType: ProgramTypeEnum.SCRIPT, silent: false, }); diff --git a/packages/abi-typegen/src/cli.ts b/packages/abi-typegen/src/cli.ts index f731d077c99..82c9fc2a863 100644 --- a/packages/abi-typegen/src/cli.ts +++ b/packages/abi-typegen/src/cli.ts @@ -12,7 +12,7 @@ export interface ICliParams { script: boolean; } -export function resolveCategory(params: { contract: boolean; script: boolean }) { +export function resolveProgramType(params: { contract: boolean; script: boolean }) { const { contract, script } = params; const noneSpecified = !contract && !script; @@ -28,13 +28,13 @@ export function runCliAction(options: ICliParams) { const { inputs, output, silent, contract, script } = options; const cwd = process.cwd(); - const category = resolveCategory({ contract, script }); + const programType = resolveProgramType({ contract, script }); runTypegen({ cwd, inputs, output, - category, + programType, silent: !!silent, }); } diff --git a/packages/abi-typegen/src/runTypegen.test.ts b/packages/abi-typegen/src/runTypegen.test.ts index adbcb567991..262b09bdbea 100644 --- a/packages/abi-typegen/src/runTypegen.test.ts +++ b/packages/abi-typegen/src/runTypegen.test.ts @@ -33,7 +33,7 @@ describe('runTypegen.js', () => { const cwd = process.cwd(); const inputs = [join(tempDir, '/out/debug/*-abi.json')]; const output = join(tempDir, 'generated'); - const category = ProgramTypeEnum.CONTRACT; + const programType = ProgramTypeEnum.CONTRACT; const silent = true; // executes program @@ -42,7 +42,7 @@ describe('runTypegen.js', () => { cwd, inputs, output, - category, + programType, silent, }); @@ -83,7 +83,7 @@ describe('runTypegen.js', () => { const cwd = process.cwd(); const input = join(tempDir, '/out/debug/*-abi.json'); const output = join(tempDir, 'generated'); - const category = ProgramTypeEnum.CONTRACT; + const programType = ProgramTypeEnum.CONTRACT; const silent = true; const filepaths = globSync(input, { cwd }); @@ -94,7 +94,7 @@ describe('runTypegen.js', () => { cwd, filepaths, output, - category, + programType, silent, }); @@ -134,7 +134,7 @@ describe('runTypegen.js', () => { const cwd = process.cwd(); const input = join(tempDir, '/out/debug/*-abi.json'); const output = join(tempDir, 'generated'); - const category = ProgramTypeEnum.SCRIPT; + const programType = ProgramTypeEnum.SCRIPT; const silent = true; const filepaths = globSync(input, { cwd }); @@ -145,7 +145,7 @@ describe('runTypegen.js', () => { cwd, filepaths, output, - category, + programType, silent, }); @@ -187,7 +187,7 @@ describe('runTypegen.js', () => { const cwd = process.cwd(); const input = join(tempDir, '/out/debug/*-abi.json'); const output = join(tempDir, 'generated'); - const category = ProgramTypeEnum.SCRIPT; + const programType = ProgramTypeEnum.SCRIPT; const silent = true; const filepaths = globSync(input, { cwd }); @@ -198,7 +198,7 @@ describe('runTypegen.js', () => { cwd, filepaths, output, - category, + programType, silent, }); }; @@ -224,7 +224,7 @@ describe('runTypegen.js', () => { // compute filepaths const cwd = process.cwd(); const output = join(tempDir, 'generated'); - const category = ProgramTypeEnum.CONTRACT; + const programType = ProgramTypeEnum.CONTRACT; const silent = true; // executes program @@ -232,7 +232,7 @@ describe('runTypegen.js', () => { runTypegen({ cwd, output, - category, + programType, silent, }); diff --git a/packages/abi-typegen/src/runTypegen.ts b/packages/abi-typegen/src/runTypegen.ts index 0d29695c185..0ab2ce3f852 100644 --- a/packages/abi-typegen/src/runTypegen.ts +++ b/packages/abi-typegen/src/runTypegen.ts @@ -16,11 +16,11 @@ export interface IGenerateFilesParams { inputs?: string[]; output: string; silent?: boolean; - category: ProgramTypeEnum; + programType: ProgramTypeEnum; } export function runTypegen(params: IGenerateFilesParams) { - const { cwd, inputs, output, silent, category, filepaths: originalFilepaths } = params; + const { cwd, inputs, output, silent, programType, filepaths: originalFilepaths } = params; const cwdBasename = basename(cwd); @@ -53,7 +53,7 @@ export function runTypegen(params: IGenerateFilesParams) { return abi; }); - const isScript = category === ProgramTypeEnum.SCRIPT; + const isScript = programType === ProgramTypeEnum.SCRIPT; const binFiles = !isScript ? [] @@ -61,7 +61,7 @@ export function runTypegen(params: IGenerateFilesParams) { const binFilepath = abiFilepath.replace('-abi.json', '.bin'); const binExists = existsSync(binFilepath); - validateBinFile({ abiFilepath, binFilepath, binExists, category }); + validateBinFile({ abiFilepath, binFilepath, binExists, programType }); const bin: IFile = { path: binFilepath, @@ -78,7 +78,7 @@ export function runTypegen(params: IGenerateFilesParams) { outputDir: output, abiFiles, binFiles, - category, + programType, }); /* diff --git a/packages/abi-typegen/src/templates/common/index.test.ts b/packages/abi-typegen/src/templates/common/index.test.ts index 26b6047e438..f7f87cecf8d 100644 --- a/packages/abi-typegen/src/templates/common/index.test.ts +++ b/packages/abi-typegen/src/templates/common/index.test.ts @@ -20,7 +20,7 @@ describe('templates/index', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, - category: ProgramTypeEnum.CONTRACT, + programType: ProgramTypeEnum.CONTRACT, }); const rendered = renderIndexTemplate({ abis: [abi] }); diff --git a/packages/abi-typegen/src/templates/common/index.ts b/packages/abi-typegen/src/templates/common/index.ts index 8aa50e5efca..e900ba32b24 100644 --- a/packages/abi-typegen/src/templates/common/index.ts +++ b/packages/abi-typegen/src/templates/common/index.ts @@ -7,7 +7,7 @@ import indexTemplate from './index.hbs'; export function renderIndexTemplate(params: { abis: Abi[] }) { const { abis } = params; - const isGeneratingContracts = abis[0].category === ProgramTypeEnum.CONTRACT; + const isGeneratingContracts = abis[0].programType === ProgramTypeEnum.CONTRACT; const text = renderHbsTemplate({ template: indexTemplate, diff --git a/packages/abi-typegen/src/templates/contract/dts.test.ts b/packages/abi-typegen/src/templates/contract/dts.test.ts index 84c511d0b49..7bb2f08f6a8 100644 --- a/packages/abi-typegen/src/templates/contract/dts.test.ts +++ b/packages/abi-typegen/src/templates/contract/dts.test.ts @@ -20,7 +20,7 @@ describe('templates/dts', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, - category: ProgramTypeEnum.CONTRACT, + programType: ProgramTypeEnum.CONTRACT, }); const rendered = renderDtsTemplate({ abi }); @@ -38,7 +38,7 @@ describe('templates/dts', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, - category: ProgramTypeEnum.CONTRACT, + programType: ProgramTypeEnum.CONTRACT, }); const rendered = renderDtsTemplate({ abi }); @@ -52,7 +52,7 @@ describe('templates/dts', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, - category: ProgramTypeEnum.CONTRACT, + programType: ProgramTypeEnum.CONTRACT, }); const rendered = renderDtsTemplate({ abi }); @@ -66,7 +66,7 @@ describe('templates/dts', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, - category: ProgramTypeEnum.CONTRACT, + programType: ProgramTypeEnum.CONTRACT, }); const rendered = renderDtsTemplate({ abi }); diff --git a/packages/abi-typegen/src/templates/contract/factory.test.ts b/packages/abi-typegen/src/templates/contract/factory.test.ts index 23c7f4e419a..19c7fb9e2c7 100644 --- a/packages/abi-typegen/src/templates/contract/factory.test.ts +++ b/packages/abi-typegen/src/templates/contract/factory.test.ts @@ -21,7 +21,7 @@ describe('templates/factory', () => { filepath: './my-contract-abi.json', outputDir: 'stdout', rawContents, - category: ProgramTypeEnum.CONTRACT, + programType: ProgramTypeEnum.CONTRACT, }); const rendered = renderFactoryTemplate({ abi }); diff --git a/packages/abi-typegen/src/templates/script/factory.test.ts b/packages/abi-typegen/src/templates/script/factory.test.ts index bcd7b7cf8a2..51b6ea6ef66 100644 --- a/packages/abi-typegen/src/templates/script/factory.test.ts +++ b/packages/abi-typegen/src/templates/script/factory.test.ts @@ -21,7 +21,7 @@ describe('factory.ts', () => { hexlifiedBinContents: '0x000', outputDir: 'stdout', rawContents, - category: ProgramTypeEnum.SCRIPT, + programType: ProgramTypeEnum.SCRIPT, }); const rendered = renderFactoryTemplate({ abi }); @@ -46,7 +46,7 @@ describe('factory.ts', () => { hexlifiedBinContents: '0x000', outputDir: 'stdout', rawContents, - category: ProgramTypeEnum.SCRIPT, + programType: ProgramTypeEnum.SCRIPT, }); const { error } = await executeAndCatch(() => { diff --git a/packages/abi-typegen/src/templates/utils/formatEnums.test.ts b/packages/abi-typegen/src/templates/utils/formatEnums.test.ts index 20bd0a8138c..bb6035f4f2f 100644 --- a/packages/abi-typegen/src/templates/utils/formatEnums.test.ts +++ b/packages/abi-typegen/src/templates/utils/formatEnums.test.ts @@ -10,7 +10,7 @@ describe('formatEnums.ts', () => { filepath: './enum-simple-abi.json', outputDir: './contracts', rawContents: enumOfEnumsAbiJson, - category: ProgramTypeEnum.CONTRACT, + programType: ProgramTypeEnum.CONTRACT, }); // executing diff --git a/packages/abi-typegen/src/templates/utils/formatStructs.test.ts b/packages/abi-typegen/src/templates/utils/formatStructs.test.ts index 8ff55c5738a..18c7b7a5026 100644 --- a/packages/abi-typegen/src/templates/utils/formatStructs.test.ts +++ b/packages/abi-typegen/src/templates/utils/formatStructs.test.ts @@ -10,7 +10,7 @@ describe('formatStructs.ts', () => { filepath: './struct-simple-abi.json', outputDir: './contracts', rawContents: structSimpleAbiJson, - category: ProgramTypeEnum.CONTRACT, + programType: ProgramTypeEnum.CONTRACT, }); // executing diff --git a/packages/abi-typegen/src/utils/assembleContracts.test.ts b/packages/abi-typegen/src/utils/assembleContracts.test.ts index 1cab93467e8..0f16c0c99ad 100644 --- a/packages/abi-typegen/src/utils/assembleContracts.test.ts +++ b/packages/abi-typegen/src/utils/assembleContracts.test.ts @@ -33,7 +33,7 @@ describe('assembleContracts.ts', () => { const { typegen: { abis, outputDir }, } = getNewAbiTypegen({ - category: ProgramTypeEnum.CONTRACT, + programType: ProgramTypeEnum.CONTRACT, includeOptionType: false, // will prevent `common` template from being included }); @@ -52,7 +52,7 @@ describe('assembleContracts.ts', () => { const { typegen: { abis, outputDir }, } = getNewAbiTypegen({ - category: ProgramTypeEnum.CONTRACT, + programType: ProgramTypeEnum.CONTRACT, includeOptionType: true, // will cause `common` template to be included }); diff --git a/packages/abi-typegen/src/utils/assembleScripts.test.ts b/packages/abi-typegen/src/utils/assembleScripts.test.ts index 86504f3b36c..0be690ba72b 100644 --- a/packages/abi-typegen/src/utils/assembleScripts.test.ts +++ b/packages/abi-typegen/src/utils/assembleScripts.test.ts @@ -36,7 +36,7 @@ describe('assembleScripts.ts', () => { const { typegen: { abis, outputDir }, } = getNewAbiTypegen({ - category: ProgramTypeEnum.SCRIPT, + programType: ProgramTypeEnum.SCRIPT, includeOptionType: false, // will prevent common template from being included includeMainFunction: true, includeBinFiles: true, @@ -59,7 +59,7 @@ describe('assembleScripts.ts', () => { const { typegen: { abis, outputDir }, } = getNewAbiTypegen({ - category: ProgramTypeEnum.SCRIPT, + programType: ProgramTypeEnum.SCRIPT, includeOptionType: true, // will cause common template to be included includeMainFunction: true, includeBinFiles: true, diff --git a/packages/abi-typegen/src/utils/validateBinFile.test.ts b/packages/abi-typegen/src/utils/validateBinFile.test.ts index 03ce562bf11..35bc8639622 100644 --- a/packages/abi-typegen/src/utils/validateBinFile.test.ts +++ b/packages/abi-typegen/src/utils/validateBinFile.test.ts @@ -8,7 +8,7 @@ describe('validateBinFile.ts', () => { abiFilepath: 'script-abi.json', binExists: true, binFilepath: 'script.bin', - category: ProgramTypeEnum.SCRIPT, + programType: ProgramTypeEnum.SCRIPT, }; expect(() => validateBinFile(params)).not.toThrow(); @@ -19,7 +19,7 @@ describe('validateBinFile.ts', () => { abiFilepath: './contract-abi.json', binExists: false, binFilepath: './contract.bin', - category: ProgramTypeEnum.CONTRACT, + programType: ProgramTypeEnum.CONTRACT, }; expect(() => validateBinFile(params)).not.toThrow(); @@ -30,7 +30,7 @@ describe('validateBinFile.ts', () => { abiFilepath: './script-abi.json', binExists: false, binFilepath: './script.bin', - category: ProgramTypeEnum.SCRIPT, + programType: ProgramTypeEnum.SCRIPT, }; expect(() => validateBinFile(params)).toThrowError( diff --git a/packages/abi-typegen/src/utils/validateBinFile.ts b/packages/abi-typegen/src/utils/validateBinFile.ts index 2780f630bdb..a229bfaa394 100644 --- a/packages/abi-typegen/src/utils/validateBinFile.ts +++ b/packages/abi-typegen/src/utils/validateBinFile.ts @@ -6,19 +6,19 @@ export function validateBinFile(params: { abiFilepath: string; binFilepath: string; binExists: boolean; - category: ProgramTypeEnum; + programType: ProgramTypeEnum; }) { - const { abiFilepath, binFilepath, binExists, category } = params; + const { abiFilepath, binFilepath, binExists, programType } = params; - const isScript = category === ProgramTypeEnum.SCRIPT; + const isScript = programType === ProgramTypeEnum.SCRIPT; if (!binExists && isScript) { throw new Error( [ - `Could not find BIN file for counterpart ${upperFirst(category)} ABI.`, + `Could not find BIN file for counterpart ${upperFirst(programType)} ABI.`, ` - ABI: ${abiFilepath}`, ` - BIN: ${binFilepath}`, - category, + programType, ].join('\n') ); } diff --git a/packages/abi-typegen/test/utils/getNewAbiTypegen.ts b/packages/abi-typegen/test/utils/getNewAbiTypegen.ts index 52c5324804e..c72b7f290d3 100644 --- a/packages/abi-typegen/test/utils/getNewAbiTypegen.ts +++ b/packages/abi-typegen/test/utils/getNewAbiTypegen.ts @@ -4,7 +4,7 @@ import { ProgramTypeEnum } from '../../src/types/enums/ProgramTypeEnum'; export function getNewAbiTypegen( params: { - category?: ProgramTypeEnum; + programType?: ProgramTypeEnum; includeOptionType?: boolean; includeMainFunction?: boolean; includeBinFiles?: boolean; @@ -12,7 +12,7 @@ export function getNewAbiTypegen( ) { const { includeOptionType = false, - category = ProgramTypeEnum.CONTRACT, + programType = ProgramTypeEnum.CONTRACT, includeMainFunction = false, includeBinFiles = false, } = params; @@ -103,7 +103,7 @@ export function getNewAbiTypegen( binFiles: includeBinFiles ? binFiles : [], abiFiles, outputDir, - category, + programType, }); return { typegen }; diff --git a/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts b/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts index 6f9fb32a1f7..bf89b18584e 100644 --- a/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts +++ b/packages/abi-typegen/test/utils/sway/compileSwayToTs.ts @@ -32,7 +32,7 @@ export function compileSwayToTs(params: ISwayParams) { contents: binContents, }, ], - category: ProgramTypeEnum.CONTRACT, + programType: ProgramTypeEnum.CONTRACT, }); // create handy shortcuts for common definitions From 6cd300d00f14829d30534aaae953a374681994d8 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 7 Mar 2023 08:27:54 -0300 Subject: [PATCH 104/108] Dedupe; adding correct link to related doc section --- docs/_guide/abi-typegen/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_guide/abi-typegen/index.md b/docs/_guide/abi-typegen/index.md index 8e67ac975b8..8a743c9b789 100644 --- a/docs/_guide/abi-typegen/index.md +++ b/docs/_guide/abi-typegen/index.md @@ -47,4 +47,4 @@ $ cat out/debug/my-test-abi.json See also: - [Generating Contract Types](./generating-types-from-abi.md) -- [Generating Contract Types](./generating-types-from-abi.md) +- [Using Contract Types](./using-generated-types.md) From 8abe9c1db22cfb92a1f47f01918c24dfef2c7091 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 7 Mar 2023 08:29:15 -0300 Subject: [PATCH 105/108] Adjusting link --- docs/_guide/abi-typegen/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_guide/abi-typegen/index.md b/docs/_guide/abi-typegen/index.md index 8a743c9b789..c4973fdb763 100644 --- a/docs/_guide/abi-typegen/index.md +++ b/docs/_guide/abi-typegen/index.md @@ -47,4 +47,4 @@ $ cat out/debug/my-test-abi.json See also: - [Generating Contract Types](./generating-types-from-abi.md) -- [Using Contract Types](./using-generated-types.md) +- [Using Generated Types](./using-generated-types.md) From 695f221fd025464040639ed18964408e5131b192 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 7 Mar 2023 08:33:38 -0300 Subject: [PATCH 106/108] Docs; updating nomenclatures on `packages/abi-typegen` README --- packages/abi-typegen/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/abi-typegen/README.md b/packages/abi-typegen/README.md index 9ea028bc759..b672a659999 100644 --- a/packages/abi-typegen/README.md +++ b/packages/abi-typegen/README.md @@ -55,19 +55,19 @@ npx fuels-typegen -i ./out/debug/*-abi.json -o ./src/contracts ## Programmatic API ```ts -import { CategoryEnum, runTypegen } from "@fuel-ts/abi-typegen"; +import { ProgramTypeEnum, runTypegen } from "@fuel-ts/abi-typegen"; const cwd = process.cwd(); const input = './abis/**-abi.json' const output = './types' const filepaths = [ './abis/a-abi.json', './abis/b-abi.json' ] - const category = CategoryEnum.CONTRACT; + const programType = ProgramTypeEnum.CONTRACT; // using input global - await runTypegen({ cwd, input, output, category }); + await runTypegen({ cwd, input, output, programType }); // using filepaths' array - await runTypegen({ cwd, filepaths, output, category }); + await runTypegen({ cwd, filepaths, output, programType }); } ``` From 342b43ccdd2c01f46d622aa1e8932651e215b85b Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 7 Mar 2023 09:03:32 -0300 Subject: [PATCH 107/108] Docs, adjusting links again --- docs/_guide/abi-typegen/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_guide/abi-typegen/index.md b/docs/_guide/abi-typegen/index.md index c4973fdb763..405a2859b90 100644 --- a/docs/_guide/abi-typegen/index.md +++ b/docs/_guide/abi-typegen/index.md @@ -46,5 +46,5 @@ $ cat out/debug/my-test-abi.json See also: -- [Generating Contract Types](./generating-types-from-abi.md) +- [Generating Types](./generating-types-from-abi.md) - [Using Generated Types](./using-generated-types.md) From d1dac3d99baa25ae6899a6dc536b1f6154ffb614 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Tue, 7 Mar 2023 09:43:05 -0300 Subject: [PATCH 108/108] Reality check, exorcizing code smells --- packages/abi-typegen/test/fixtures/buildAll.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/abi-typegen/test/fixtures/buildAll.ts b/packages/abi-typegen/test/fixtures/buildAll.ts index f1f5e1a842c..15d477ea419 100644 --- a/packages/abi-typegen/test/fixtures/buildAll.ts +++ b/packages/abi-typegen/test/fixtures/buildAll.ts @@ -4,13 +4,7 @@ import { contractPaths } from '.'; const { log } = console; -function get(source: { [k: string]: string }, key: string) { - return source[key]; -} - -Object.keys(contractPaths).forEach((key) => { - const contractPath: string = get(contractPaths, key); - +Object.entries(contractPaths).forEach(([_contractName, contractPath]) => { log('——————————————————————————————————————————————————————'); log(contractPath); log('——————————————————————————————————————————————————————');