Skip to content

Commit

Permalink
feat: improve Typegen support for StdString, RawSlice and Bytes (
Browse files Browse the repository at this point in the history
…#1412)

* feat: improve typegen support for std string

* feat: improve typegen support for raw slice

* feat: improve typegen support for bytes

* chore: changeset

* fix: correct doc snippet key
  • Loading branch information
danielbate authored Nov 7, 2023
1 parent 475d450 commit 119f83c
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 25 deletions.
7 changes: 7 additions & 0 deletions .changeset/tasty-bananas-impress.md
Original file line number Diff line number Diff line change
@@ -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
8 changes: 5 additions & 3 deletions apps/docs-snippets/src/guide/types/bytes.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Contract } from 'fuels';
import type { Contract, Bytes } from 'fuels';

import { SnippetProjectEnum } from '../../../projects';
import { createAndDeployContractFromProject } from '../../utils';
Expand All @@ -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();

Expand All @@ -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();

Expand Down
8 changes: 5 additions & 3 deletions apps/docs-snippets/src/guide/types/raw-slice.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();

Expand All @@ -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();

Expand Down
8 changes: 5 additions & 3 deletions apps/docs-snippets/src/guide/types/std-string.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Contract } from 'fuels';
import type { Contract, StdString } from 'fuels';

import { SnippetProjectEnum } from '../../../projects';
import { createAndDeployContractFromProject } from '../../utils';
Expand All @@ -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();

Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion packages/abi-typegen/src/abi/types/BytesType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});
9 changes: 7 additions & 2 deletions packages/abi-typegen/src/abi/types/BytesType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 3 additions & 3 deletions packages/abi-typegen/src/abi/types/RawUntypedSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});
9 changes: 7 additions & 2 deletions packages/abi-typegen/src/abi/types/RawUntypedSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion packages/abi-typegen/src/abi/types/StdStringType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});
13 changes: 9 additions & 4 deletions packages/abi-typegen/src/abi/types/StdStringType.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
}
}
7 changes: 5 additions & 2 deletions packages/abi-typegen/test/fixtures/templates/contract/dts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
import type {
BigNumberish,
BN,
Bytes,
BytesLike,
Contract,
DecodedValue,
EvmAddress,
FunctionFragment,
Interface,
InvokeFunction,
RawSlice,
StdString,
} from 'fuels';

import type { Option, Enum, Vec } from "./common";
Expand Down Expand Up @@ -71,7 +74,7 @@ interface MyContractAbiInterface extends Interface {
encodeFunctionData(functionFragment: 'types_evm_address', values: [EvmAddress]): Uint8Array;
encodeFunctionData(functionFragment: 'types_option', values: [Option<BigNumberish>]): Uint8Array;
encodeFunctionData(functionFragment: 'types_option_geo', values: [Option<MyStructInput>]): 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;
Expand Down Expand Up @@ -121,7 +124,7 @@ export class MyContractAbi extends Contract {
types_evm_address: InvokeFunction<[x: EvmAddress], EvmAddress>;
types_option: InvokeFunction<[x: Option<BigNumberish>], Option<number>>;
types_option_geo: InvokeFunction<[x: Option<MyStructInput>], Option<MyStructOutput>>;
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>;
Expand Down
6 changes: 5 additions & 1 deletion packages/interfaces/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,6 +25,8 @@ export type EvmAddress = {
value: B256AddressEvm;
};

export type StdString = string;

/**
* @hidden
*/
Expand Down

0 comments on commit 119f83c

Please sign in to comment.