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

Add doc-comment attribute support for typegen outputs #2750

Open
nedsalk opened this issue Jul 11, 2024 · 1 comment
Open

Add doc-comment attribute support for typegen outputs #2750

nedsalk opened this issue Jul 11, 2024 · 1 comment
Labels
feat Issue is a feature

Comments

@nedsalk
Copy link
Contributor

nedsalk commented Jul 11, 2024

forc build adds doc-comment attributes to the generated abi whenever a function has doc comments on it. We can add those comments on typegen-generated functions in the form of multi-line comments.

Below is an example of a sway contract function and its related abi.

    /// Verifies that a newly deployed contract is the child of a contract factory and registers it.
    ///
    /// # Additional Information
    ///
    /// This example does not check whether a contract has already been registered and will overwrite any values.
    ///
    /// # Arguments
    ///
    /// * `child_contract`: [ContractId] - The deployed factory child contract of which to verify the bytecode root.
    /// * `configurables`: [Option<ContractConfigurables>] - The configurables value set for the `child_contract`.
    ///
    /// # Returns
    ///
    /// * [Result<BytecodeRoot, str>] - Either the bytecode root of the newly registered contract or a `str` error message.
    ///
    /// # Number of Storage Accesses
    ///
    /// * Writes: `2`
    ///
    /// # Examples
    ///
    /// ```sway
    /// use src12::SRC12;
    ///
    /// fn foo(my_src_12_contract: ContractId, my_deployed_contract: ContractId, my_configurables: Option<ContractConfigurables>) {
    ///     let src_12_contract_abi = abi(SRC12, my_src_12_contract.bits());
    ///     src_12_contract_abi.register_contract(my_deployed_contract, my_configurables);
    ///     assert(src_12_contract_abi.is_valid(my_deployed_contract));
    /// }
    /// ```
    #[storage(read, write)]
    fn register_contract(
        child_contract: ContractId,
        configurables: Option<ContractConfigurables>,
    ) -> Result<BytecodeRoot, str> { ...

The generated abi will be:

{
      "inputs": [
        {
          "name": "child_contract",
          "type": 10,
          "typeArguments": null
        },
        {
          "name": "configurables",
          "type": 4,
          "typeArguments": [
            {
              "name": "",
              "type": 13,
              "typeArguments": null
            }
          ]
        }
      ],
      "name": "register_contract",
      "output": {
        "name": "",
        "type": 5,
        "typeArguments": [
          {
            "name": "",
            "type": 2,
            "typeArguments": null
          },
          {
            "name": "",
            "type": 9,
            "typeArguments": null
          }
        ]
      },
      "attributes": [
        {
          "name": "doc-comment",
          "arguments": [
            " Verifies that a newly deployed contract is the child of a contract factory and registers it."
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            ""
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " # Additional Information"
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            ""
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " This example does not check whether a contract has already been registered and will overwrite any values."
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            ""
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " # Arguments"
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            ""
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " * `child_contract`: [ContractId] - The deployed factory child contract of which to verify the bytecode root."
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " * `configurables`: [Option<ContractConfigurables>] - The configurables value set for the `child_contract`."
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            ""
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " # Returns"
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            ""
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " * [Result<BytecodeRoot, str>] - Either the bytecode root of the newly registered contract or a `str` error message."
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            ""
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " # Number of Storage Accesses"
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            ""
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " * Writes: `2`"
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            ""
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " # Examples"
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            ""
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " ```sway"
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " use src12::SRC12;"
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            ""
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " fn foo(my_src_12_contract: ContractId, my_deployed_contract: ContractId, my_configurables: Option<ContractConfigurables>) {"
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            "     let src_12_contract_abi = abi(SRC12, my_src_12_contract.bits());"
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            "     src_12_contract_abi.register_contract(my_deployed_contract, my_configurables);"
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            "     assert(src_12_contract_abi.is_valid(my_deployed_contract));"
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " }"
          ]
        },
        {
          "name": "doc-comment",
          "arguments": [
            " ```"
          ]
        },
        {
          "name": "storage",
          "arguments": [
            "read",
            "write"
          ]
        }
      ]
    }
@nedsalk nedsalk added feat Issue is a feature p2 labels Jul 11, 2024
@nedsalk nedsalk added this to the 0.x post-launch milestone Jul 11, 2024
@nedsalk nedsalk changed the title Add doc-comment attribute to typegen functions Add doc-comment attribute support for typegen outputs Jul 11, 2024
@arboleya arboleya removed this from the 0.x post-launch milestone Jul 19, 2024
@arboleya arboleya removed the p2 label Aug 2, 2024
@arboleya arboleya modified the milestone: Caterpillar v1 Aug 2, 2024
@arboleya arboleya added this to the Caterpillar v1 milestone Aug 16, 2024
@arboleya arboleya removed this from the Caterpillar v1 milestone Sep 7, 2024
@arboleya arboleya added the temp:notion label Sep 8, 2024 — with Linear
@arboleya arboleya added the temp-linear label Sep 8, 2024 — with Linear
Copy link
Contributor

We will address this after the refactor of TS-600

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
None yet
Development

No branches or pull requests

3 participants