FuelLabs / fuels-ts

Fuel Network Typescript SDK
https://docs.fuel.network/docs/fuels-ts/
Apache License 2.0
44.22k stars 1.34k forks source link

Typegen fails when handling Sway type aliases #3073

Closed petertonysmith94 closed 3 weeks ago

petertonysmith94 commented 3 weeks ago

Summary

We can define alias types in Sway. These are getting incorrectly transpiled and therefore causing the following error while running typegen:

FuelError: Type not supported: (struct std::asset_id::AssetId, struct std::asset_id::AssetId, bool)

Incoming from the forum.

Example

Consider the following contract:

type TupleWithNativeAssets = (AssetId, AssetId, bool);

abi MyContract {
    fn alias_types_tuple_with_native_types(x: TupleWithNativeAssets) -> TupleWithNativeAssets;
}

The following transpile ABI is produced:

{
  "types": [
    {
      "typeId": 1,
      // This type is incorrect
      "type": "(struct std::asset_id::AssetId, struct std::asset_id::AssetId, bool)",
      "components": [
        {
          "name": "__tuple_element",
          "type": 22,
          "typeArguments": null
        },
        {
          "name": "__tuple_element",
          "type": 22,
          "typeArguments": null
        },
        {
          "name": "__tuple_element",
          "type": 32,
          "typeArguments": null
        }
      ],
      "typeParameters": null
    },
  ],
  "functions": [
    {
      "inputs": [
        {
          "name": "x",
          "type": 1,
          "typeArguments": null
        }
      ],
      "name": "alias_types_tuple_with_native_types",
      // ...
    },
  ]
  // ...
}

Expected

We expect to generate a type with signature: (_, _, _)

Actual

We actually generate a type with signature: (struct std::asset_id::AssetId, struct std::asset_id::AssetId, bool)

petertonysmith94 commented 3 weeks ago

It appears this is potentially an issue with forc rather than the transpiler - more to follow.

From my investigation, I've created the following issue in Sway:

petertonysmith94 commented 3 weeks ago

Closed as this appears to be an issue in Sway:

arboleya commented 3 weeks ago

@petertonysmith94 Should we add some tests to validate this? They will fail at first, but they'll pass once I merge this:

The issue (https://github.com/FuelLabs/sway/pull/6494) was fixed in the latest forc:

petertonysmith94 commented 3 weeks ago

@arboleya We have tests here for an aliased type.

arboleya commented 3 weeks ago

Great! Can we merge these tests, then?

petertonysmith94 commented 3 weeks ago

I've based off of:

This is ready to go once we merge the above:

arboleya commented 3 weeks ago

Thank you! 🫡