Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support forc build --json-abi-with-callpaths #2162

Open
nedsalk opened this issue Apr 25, 2024 · 0 comments
Open

Support forc build --json-abi-with-callpaths #2162

nedsalk opened this issue Apr 25, 2024 · 0 comments
Assignees
Labels

Comments

@nedsalk
Copy link
Contributor

nedsalk commented Apr 25, 2024

From this rust-sdk issue:

Currently, type-paths are feature-gated behind the forc flag --json-abi-with-callpaths.
Once the TS SDK implements support for type paths, forc will make this behavior default.

Running forc build with the flag --json-abi-with-callpaths "outputs json abi with callpaths instead of names for struct and enums". We can take the sway program below as an example:

contract;

use std::address::Address;

struct CustomAddress {
    value: b256,
}

abi MyContract {
    fn std_address(address: Address) -> b256;
    fn custom_address(address: CustomAddress) -> b256;
}

Running forc build produces these types in the abi:

[
  {
    "typeId": 0,
    "type": "b256",
    "components": null,
    "typeParameters": null
  },
  {
    "typeId": 1,
    "type": "struct Address",
    "components": [
      {
        "name": "bits",
        "type": 0,
        "typeArguments": null
      }
    ],
    "typeParameters": null
  },
  {
    "typeId": 2,
    "type": "struct CustomAddress",
    "components": [
      {
        "name": "value",
        "type": 0,
        "typeArguments": null
      }
    ],
    "typeParameters": null
  }
]

Whereas building it with --json-abi-with-callpaths produces this:

[
  {
    "typeId": 0,
    "type": "b256",
    "components": null,
    "typeParameters": null
  },
  {
    "typeId": 1,
    "type": "struct CustomAddress",
    "components": [
      {
        "name": "value",
        "type": 0,
        "typeArguments": null
      }
    ],
    "typeParameters": null
  },
  {
    "typeId": 2,
    "type": "struct std::address::Address",
    "components": [
      {
        "name": "bits",
        "type": 0,
        "typeArguments": null
      }
    ],
    "typeParameters": null
  }
]

The second abi names the struct from the std library with its callpath. This is very beneficial because we can use it to differentiate between sway standard library constructs and custom constructs and custom-tailor our typegen and abi-coder for a better developer experience.

For example, the generated types and functions based on the first abi would be

export type AddressInput = { bits: string };
export type AddressOutput = AddressInput;
export type CustomAddressInput = { value: string };
export type CustomAddressOutput = CustomAddressInput;

export class MyProjectAbi extends Contract {
  functions: {
    custom_address: InvokeFunction<[address: CustomAddressInput], string>;
    std_address: InvokeFunction<[address: AddressInput], string>;
  };
}

Whereas what we could support with this flag is the following:

export type CustomAddressInput = { value: string };
export type CustomAddressOutput = CustomAddressInput;

export class MyProjectAbi extends Contract {
  functions: {
    custom_address: InvokeFunction<[address: CustomAddressInput], string>;
    std_address: InvokeFunction<[address: string], string>; // <--------------- Not object but string
  };
}

This way we can custom-tailor other std types like EvmAddress, Identity, ContractId, etc.

Our typegen currently doesn't support this build flag because it generates the corresponding types like this:

export type std::address::AddressInput = { bits: string };
export type std::address::AddressOutput = std::address::AddressInput;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant