diff --git a/.changeset/tasty-bananas-impress.md b/.changeset/tasty-bananas-impress.md new file mode 100644 index 00000000000..62c0a098aa6 --- /dev/null +++ b/.changeset/tasty-bananas-impress.md @@ -0,0 +1,7 @@ +--- +"@fuel-ts/docs-snippets": patch +"@fuel-ts/abi-typegen": patch +"@fuel-ts/interfaces": patch +--- + +Improve typegen support for String, RawSlice and Bytes diff --git a/apps/docs-snippets/src/guide/types/bytes.test.ts b/apps/docs-snippets/src/guide/types/bytes.test.ts index c599da17775..eb054d9361f 100644 --- a/apps/docs-snippets/src/guide/types/bytes.test.ts +++ b/apps/docs-snippets/src/guide/types/bytes.test.ts @@ -1,4 +1,4 @@ -import type { Contract } from 'fuels'; +import type { Contract, Bytes } from 'fuels'; import { SnippetProjectEnum } from '../../../projects'; import { createAndDeployContractFromProject } from '../../utils'; @@ -12,8 +12,9 @@ describe('Bytes', () => { it('should pass bytes to a contract', async () => { // #region bytes-1 + // #context import type { Bytes } from 'fuels'; - const bytes = [40, 41, 42]; + const bytes: Bytes = [40, 41, 42]; const { value } = await contract.functions.bytes_comparison(bytes).simulate(); @@ -23,8 +24,9 @@ describe('Bytes', () => { it('should retrieve bytes from a contract', async () => { // #region bytes-2 + // #context import type { Bytes } from 'fuels'; - const bytes = [8, 42, 77]; + const bytes: Bytes = [8, 42, 77]; const { value } = await contract.functions.echo_bytes(bytes).simulate(); diff --git a/apps/docs-snippets/src/guide/types/raw-slice.test.ts b/apps/docs-snippets/src/guide/types/raw-slice.test.ts index d9325d826c0..a6afefcb19d 100644 --- a/apps/docs-snippets/src/guide/types/raw-slice.test.ts +++ b/apps/docs-snippets/src/guide/types/raw-slice.test.ts @@ -1,4 +1,4 @@ -import type { Contract, BN } from 'fuels'; +import type { Contract, BN, RawSlice } from 'fuels'; import { SnippetProjectEnum } from '../../../projects'; import { createAndDeployContractFromProject } from '../../utils'; @@ -12,8 +12,9 @@ describe('RawSlice', () => { it('should pass a raw slice to a contract', async () => { // #region raw-slice-1 + // #context import type { RawSlice } from 'fuels'; - const rawSlice = [40, 41, 42]; + const rawSlice: RawSlice = [40, 41, 42]; const { value } = await contract.functions.raw_slice_comparison(rawSlice).simulate(); @@ -23,8 +24,9 @@ describe('RawSlice', () => { it('should retrieve a raw slice from a contract', async () => { // #region raw-slice-2 + // #context import type { RawSlice } from 'fuels'; - const rawSlice = [8, 42, 77]; + const rawSlice: RawSlice = [8, 42, 77]; const { value } = await contract.functions.echo_raw_slice(rawSlice).simulate(); diff --git a/apps/docs-snippets/src/guide/types/std-string.test.ts b/apps/docs-snippets/src/guide/types/std-string.test.ts index 1956da0d9e4..13f1d5f7da2 100644 --- a/apps/docs-snippets/src/guide/types/std-string.test.ts +++ b/apps/docs-snippets/src/guide/types/std-string.test.ts @@ -1,4 +1,4 @@ -import type { Contract } from 'fuels'; +import type { Contract, StdString } from 'fuels'; import { SnippetProjectEnum } from '../../../projects'; import { createAndDeployContractFromProject } from '../../utils'; @@ -12,8 +12,9 @@ describe('StdString', () => { it('should pass a std string to a contract', async () => { // #region std-string-1 + // #context import type { StdString } from 'fuels'; - const stdString = 'Hello World'; + const stdString: StdString = 'Hello World'; const { value } = await contract.functions.string_comparison(stdString).simulate(); @@ -23,8 +24,9 @@ describe('StdString', () => { it('should retrieve a std string from a contract', async () => { // #region std-string-2 + // #context import type { StdString } from 'fuels'; - const stdString = 'Hello Fuel'; + const stdString: StdString = 'Hello Fuel'; const { value } = await contract.functions.echo_string(stdString).simulate(); diff --git a/packages/abi-typegen/src/abi/types/BytesType.test.ts b/packages/abi-typegen/src/abi/types/BytesType.test.ts index 53bdc0bf724..7d081332d1a 100644 --- a/packages/abi-typegen/src/abi/types/BytesType.test.ts +++ b/packages/abi-typegen/src/abi/types/BytesType.test.ts @@ -22,6 +22,6 @@ describe('BytesType.ts', () => { expect(bytes.attributes.inputLabel).toEqual('Bytes'); expect(bytes.attributes.outputLabel).toEqual('Bytes'); - expect(bytes.requiredFuelsMembersImports).toStrictEqual([]); + expect(bytes.requiredFuelsMembersImports).toStrictEqual(['Bytes']); }); }); diff --git a/packages/abi-typegen/src/abi/types/BytesType.ts b/packages/abi-typegen/src/abi/types/BytesType.ts index 3f01708b7e9..92e22b219db 100644 --- a/packages/abi-typegen/src/abi/types/BytesType.ts +++ b/packages/abi-typegen/src/abi/types/BytesType.ts @@ -14,10 +14,15 @@ export class BytesType extends ArrayType { } public parseComponentsAttributes(_params: { types: IType[] }) { + const capitalizedName = 'Bytes'; + this.attributes = { - inputLabel: `Bytes`, - outputLabel: `Bytes`, + inputLabel: capitalizedName, + outputLabel: capitalizedName, }; + + this.requiredFuelsMembersImports = [capitalizedName]; + return this.attributes; } } diff --git a/packages/abi-typegen/src/abi/types/RawUntypedSlice.test.ts b/packages/abi-typegen/src/abi/types/RawUntypedSlice.test.ts index 24ca40d3bcc..dff7d7121cf 100644 --- a/packages/abi-typegen/src/abi/types/RawUntypedSlice.test.ts +++ b/packages/abi-typegen/src/abi/types/RawUntypedSlice.test.ts @@ -20,8 +20,8 @@ describe('RawUntypedSlice.ts', () => { expect(suitableForRawUntyped).toEqual(true); expect(suitableForStruct).toEqual(false); - expect(rawSlice.attributes.inputLabel).toEqual('RawUntypedSlice'); - expect(rawSlice.attributes.outputLabel).toEqual('RawUntypedSlice'); - expect(rawSlice.requiredFuelsMembersImports).toStrictEqual([]); + expect(rawSlice.attributes.inputLabel).toEqual('RawSlice'); + expect(rawSlice.attributes.outputLabel).toEqual('RawSlice'); + expect(rawSlice.requiredFuelsMembersImports).toStrictEqual(['RawSlice']); }); }); diff --git a/packages/abi-typegen/src/abi/types/RawUntypedSlice.ts b/packages/abi-typegen/src/abi/types/RawUntypedSlice.ts index a3b2e666e35..3ec754c4e68 100644 --- a/packages/abi-typegen/src/abi/types/RawUntypedSlice.ts +++ b/packages/abi-typegen/src/abi/types/RawUntypedSlice.ts @@ -14,10 +14,15 @@ export class RawUntypedSlice extends ArrayType { } public parseComponentsAttributes(_params: { types: IType[] }) { + const capitalizedName = 'RawSlice'; + this.attributes = { - inputLabel: `RawUntypedSlice`, - outputLabel: `RawUntypedSlice`, + inputLabel: capitalizedName, + outputLabel: capitalizedName, }; + + this.requiredFuelsMembersImports = [capitalizedName]; + return this.attributes; } } diff --git a/packages/abi-typegen/src/abi/types/StdStringType.test.ts b/packages/abi-typegen/src/abi/types/StdStringType.test.ts index 2efd8164b87..8d87fc6974e 100644 --- a/packages/abi-typegen/src/abi/types/StdStringType.test.ts +++ b/packages/abi-typegen/src/abi/types/StdStringType.test.ts @@ -22,6 +22,6 @@ describe('StdStringType.ts', () => { expect(stdString.attributes.inputLabel).toEqual('StdString'); expect(stdString.attributes.outputLabel).toEqual('StdString'); - expect(stdString.requiredFuelsMembersImports).toStrictEqual([]); + expect(stdString.requiredFuelsMembersImports).toStrictEqual(['StdString']); }); }); diff --git a/packages/abi-typegen/src/abi/types/StdStringType.ts b/packages/abi-typegen/src/abi/types/StdStringType.ts index d7c0b103318..6272bc03fca 100644 --- a/packages/abi-typegen/src/abi/types/StdStringType.ts +++ b/packages/abi-typegen/src/abi/types/StdStringType.ts @@ -1,8 +1,8 @@ import type { IType } from '../../types/interfaces/IType'; -import { ArrayType } from './ArrayType'; +import { AType } from './AType'; -export class StdStringType extends ArrayType { +export class StdStringType extends AType implements IType { public static swayType = 'struct String'; public name = 'stdString'; @@ -14,10 +14,15 @@ export class StdStringType extends ArrayType { } public parseComponentsAttributes(_params: { types: IType[] }) { + const capitalizedName = 'StdString'; + this.attributes = { - inputLabel: `StdString`, - outputLabel: `StdString`, + inputLabel: capitalizedName, + outputLabel: capitalizedName, }; + + this.requiredFuelsMembersImports = [capitalizedName]; + return this.attributes; } } diff --git a/packages/abi-typegen/test/fixtures/templates/contract/dts.hbs b/packages/abi-typegen/test/fixtures/templates/contract/dts.hbs index 7c4f05404db..b2ae11e14de 100644 --- a/packages/abi-typegen/test/fixtures/templates/contract/dts.hbs +++ b/packages/abi-typegen/test/fixtures/templates/contract/dts.hbs @@ -12,6 +12,7 @@ import type { BigNumberish, BN, + Bytes, BytesLike, Contract, DecodedValue, @@ -19,6 +20,8 @@ import type { FunctionFragment, Interface, InvokeFunction, + RawSlice, + StdString, } from 'fuels'; import type { Option, Enum, Vec } from "./common"; @@ -71,7 +74,7 @@ interface MyContractAbiInterface extends Interface { encodeFunctionData(functionFragment: 'types_evm_address', values: [EvmAddress]): Uint8Array; encodeFunctionData(functionFragment: 'types_option', values: [Option]): Uint8Array; encodeFunctionData(functionFragment: 'types_option_geo', values: [Option]): Uint8Array; - encodeFunctionData(functionFragment: 'types_raw_slice', values: [RawUntypedSlice]): Uint8Array; + encodeFunctionData(functionFragment: 'types_raw_slice', values: [RawSlice]): Uint8Array; encodeFunctionData(functionFragment: 'types_std_string', values: [StdString]): Uint8Array; encodeFunctionData(functionFragment: 'types_str', values: [string]): Uint8Array; encodeFunctionData(functionFragment: 'types_struct', values: [MyStructInput]): Uint8Array; @@ -121,7 +124,7 @@ export class MyContractAbi extends Contract { types_evm_address: InvokeFunction<[x: EvmAddress], EvmAddress>; types_option: InvokeFunction<[x: Option], Option>; types_option_geo: InvokeFunction<[x: Option], Option>; - types_raw_slice: InvokeFunction<[x: RawUntypedSlice], RawUntypedSlice>; + types_raw_slice: InvokeFunction<[x: RawSlice], RawSlice>; types_std_string: InvokeFunction<[x: StdString], StdString>; types_str: InvokeFunction<[x: string], string>; types_struct: InvokeFunction<[x: MyStructInput], MyStructOutput>; diff --git a/packages/interfaces/src/index.ts b/packages/interfaces/src/index.ts index 5112e973baf..a91be53076a 100644 --- a/packages/interfaces/src/index.ts +++ b/packages/interfaces/src/index.ts @@ -14,7 +14,9 @@ export type B256Address = string; export type B256AddressEvm = `0x000000000000000000000000${string}`; -export type Bytes = Uint8Array; +export type Bytes = Uint8Array | number[]; + +export type RawSlice = Uint8Array | number[]; /** * @prop value - A 256 bit hash string with the first 12 bytes cleared @@ -23,6 +25,8 @@ export type EvmAddress = { value: B256AddressEvm; }; +export type StdString = string; + /** * @hidden */