metaplex-foundation / solita

Genrates an SDK API from solana contract IDL.
Apache License 2.0
140 stars 32 forks source link

solita should handle all Rust data enums #64

Closed thlorenz closed 2 years ago

thlorenz commented 2 years ago

Currently solita fails to render the below:

pub enum CollectionDetails {
    V1 { size: u64 },
}

The generated IDL for this type is the below:

{
  "name": "CollectionDetails",
  "type": {
    "kind": "enum",
    "variants": [
      {
        "name": "V1",
        "fields": [
          "u64"
        ]
      }
    ]
  }
}

I reproduced this error locally to verify that it is related to mapping the CollectionDetails data enum:

  ✖ TypeError: Cannot read properties of undefined (reading 'option')
  --------------------------------------------------------------------
    operator: error
    stack: |-
TypeError: Cannot read properties of undefined (reading 'option')
at isIdlTypeOption (/solita/test/integration/src/types.ts:193:32)
at _TypeMapper.map (/solita/test/integration/src/type-mapper.ts:166:9)
at /solita/test/integration/src/render-data-enum.ts:84:41
at Array.map (<anonymous>)
at /solita/test/integration/src/render-data-enum.ts:83:35
thlorenz commented 2 years ago

Looking at this closer I realize that the problem is actually the way that shank generates the IDL for data enums. As indicated in this issue the IDL for such enums generated by anchor does include the name of each data field:

{
  "name": "CollectionInfo",
  "type": {
    "kind": "enum",
    "variants": [
      {
        "name": "V1",
        "fields": [
          {
            "name": "symbol",
            "type": "string"
          },
          {
            "name": "verified_creators",
            "type": {
              "vec": "publicKey"
            }
          },
          {
            "name": "whitelist_root",
            "type": {
              "array": [
                "u8",
                32
              ]
            }
          }
        ]
      },
      {
        "name": "V2",
        "fields": [
          {
            "name": "collection_mint",
            "type": "publicKey"
          }
        ]
      }
    ]
  }
}

As seen above though, shank does not include those names which is why solita fails to render TS for this case.

thlorenz commented 2 years ago

Fixed in #27