metaplex-foundation / solita

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

[bug] HashMap incompatibility #75

Closed austbot closed 2 years ago

austbot commented 2 years ago

An IDL with this generated where DataItem is an enum

{
            "name": "data",
            "type": {
              "defined": "HashMap<String,DataItem>"
            }
          }

Gets

AssertionError [ERR_ASSERTION]: Unknown type HashMap<String,DataItem> is neither found in types nor an Account
    at TypeMapper.definedTypesImport (/home/austbot/Development/metaplex-program-library/subscription/js/node_modules/@metaplex-foundation/solita/dist/src/type-mapper.js:379:190)
    at TypeMapper.mapDefinedType (/home/austbot/Development/metaplex-program-library/subscription/js/node_modules/@metaplex-foundation/solita/dist/src/type-mapper.js:146:34)
    at TypeMapper.map (/home/austbot/Development/metaplex-program-library/subscription/js/node_modules/@metaplex-foundation/solita/dist/src/type-mapper.js:176:24)
    at TypeRenderer.renderTypeField (/home/austbot/Development/metaplex-program-library/subscription/js/node_modules/@metaplex-foundation/solita/dist/src/render-type.js:24:52)
    at /home/austbot/Development/metaplex-program-library/subscription/js/node_modules/@metaplex-foundation/solita/dist/src/render-type.js:44:34
    at Array.map (<anonymous>)
    at TypeRenderer.renderTypeScriptType (/home/austbot/Development/metaplex-program-library/subscription/js/node_modules/@metaplex-foundation/solita/dist/src/render-type.js:44:14)
    at TypeRenderer.renderDataStructs (/home/austbot/Development/metaplex-program-library/subscription/js/node_modules/@metaplex-foundation/solita/dist/src/render-type.js:91:37)
    at TypeRenderer.determineIsFixable (/home/austbot/Development/metaplex-program-library/subscription/js/node_modules/@metaplex-foundation/solita/dist/src/render-type.js:101:14)
    at determineTypeIsFixable (/home/austbot/Development/metaplex-program-library/subscription/js/node_modules/@metaplex-foundation/solita/dist/src/render-type.js:127:21) {
  generatedMessage: false,
  code: 'ERR_ASSERTION',
  actual: undefined,
  expected: undefined,
  operator: 'fail'
}

Instead of properly mapping to Map...

thlorenz commented 2 years ago

Thanks for reporting.

shank generates a different IDL for maps, i.e. it'd be { hashMap: [ 'string', { defined: 'DataItem' }] }.

The way that anchor seems to do it is not composable, i.e. there is no way to express what DataItem is. It's interesting that it does it entirely differently for Vecs for instance. My guess that the only way that this works is because no TypeScript is generated from this kind of IDL.

Therefore the solution here is not to make shank adapt it's IDL to the anchor way, but instead we'll have to make solita adapt this format to what shank does (as the latter composes and is more flexible). I'll add a step for solita to adapt that IDL before transforming it into TypeScript.

austbot commented 2 years ago

I think anchor simply doesnt support hashmaps, I think the way shank does it is actually better, I think we perhaps needf to convert anchor idls to chank idls and talk to armani to use our way

thlorenz commented 2 years ago

OK cool .. I'll add that step for now then :)