polkadot-js / api

Promise and RxJS APIs around Polkadot and Substrate based chains via RPC calls. It is dynamically generated based on what the Substrate runtime provides in terms of metadata.
Apache License 2.0
1.07k stars 354 forks source link

typegen: types generation from defs throws `TypeDefInfo.Enum: Not implemented #4837

Closed filo87 closed 2 years ago

filo87 commented 2 years ago

I am trying to generate custom types definitions in the following branch of our project:

https://github.com/embrio-tech/centrifuge-subql/tree/13-implement-custom-types-for-pools-chain-state-getters

After extending the package.json to include the ts-node calls for type generation and also updated tsconfig.json as following:

package.json

{
  "name": "@centrifuge/pools-subql",
  "version": "0.0.4",
  "description": "Subquery for the pools functionality",
  "main": "dist/index.js",
  "scripts": {
    "build": "tsc -b",
    "prepack": "rm -rf dist && npm build",
    "test": "jest",
    "codegen": "./node_modules/.bin/subql codegen",
    "generate:defs": "ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --package centrifuge-subql/api-interfaces --endpoint ./src/api-interfaces/cfg.json --input ./src/api-interfaces",
    "generate:meta": "ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --endpoint ./src/api-interfaces/cfg.json --output ./src/api-interfaces --strict",
    "interface-build": "yarn generate:defs && yarn generate:meta && yarn build"
  },
  "homepage": "https://github.com/centrifuge/pools-subql",
  "repository": "github:centrifuge/pools-subql",
  "files": [
    "dist",
    "schema.graphql",
    "project.yaml"
  ],
  "author": "Centrifuge",
  "license": "MIT",
  "dependencies": {
    "@polkadot/api": "^8"
  },
  "devDependencies": {
    "@polkadot/typegen": "^8",
    "@polkadot/types": "^8",
    "@subql/cli": "latest",
    "ts-node": "^8.6.2",
    "typescript": "^4.4.4"
  },
  "exports": {
    "chaintypes": "./src/chaintypes.ts"
  },
  "resolutions": {
    "ipfs-unixfs": "6.0.6"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "declaration": true,
    "importHelpers": true,
    "resolveJsonModule": true,
    "module": "commonjs",
    "outDir": "dist",
    "rootDir": "src",
    "target": "es2017",
    "paths": {
      "centrifuge-subql/*": ["./src/*"],
      "@polkadot/api/augment": ["./src/api-interfaces/augment-api.ts"],
      "@polkadot/types/augment": ["./src/api-interfaces/augment-types.ts"]
    }
  },
  "include": ["./src/**/*", "./node_modules/@subql/types/dist/global.d.ts"],
  "exports": {
    "chaintypes": "./src/chaintypes.ts"
  }
}

Some files are created correctly:

However when running the yarn generate:defs command I get the following error:

yarn generate:defs                      
yarn run v1.22.18
$ ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --package centrifuge-subql/api-interfaces --endpoint ./src/api-interfaces/cfg.json --input ./src/api-interfaces
/Users/Development/cfg-development/centrifuge-subql/src/api-interfaces/types.ts
        Generating
        Writing

/Users/Development/cfg-development/centrifuge-subql/src/api-interfaces/augment-types.ts
        Generating
        Writing

/Users/Development/cfg-development/centrifuge-subql/src/api-interfaces/lookup.ts
        Generating
        Writing

/Users/Development/cfg-development/centrifuge-subql/node_modules/@polkadot/typegen/cjs/util/formatting.js:148
    throw new Error(`TypeDefInfo.Enum: Not implemented on ${(0, _util.stringify)(typeDef)}`);
          ^
Error: TypeDefInfo.Enum: Not implemented on {"info":4,"lookupIndex":336,"type":"{\"_enum\":{\"NoChange\":\"Null\",\"NewValue\":\"Vec<(PalletPoolsTrancheTrancheType,Option<u32>)>\"}}","docs":[],"namespace":"orml_traits::Change","sub":[{"info":8,"type":"Null","index":0,"name":"NoChange"},{"info":19,"lookupIndex":332,"type":"Vec<(PalletPoolsTrancheTrancheType,Option<u32>)>","docs":[],"namespace":"","sub":{"info":17,"lookupIndex":333,"type":"(PalletPoolsTrancheTrancheType,Option<u32>)","docs":[],"namespace":"","sub":[{"docs":[],"info":15,"lookupIndex":334,"lookupName":"PalletPoolsTrancheTrancheType","type":"Lookup334"},{"info":9,"lookupIndex":270,"type":"Option<u32>","docs":[],"namespace":"Option","sub":{"info":10,"lookupIndex":4,"type":"u32","docs":[],"namespace":""}}]},"typeName":"Value","index":1,"name":"NewValue"}],"name":"tranches","typeName":"Change"}
    at Object.4 (/Users/Development/cfg-development/centrifuge-subql/node_modules/@polkadot/typegen/cjs/util/formatting.js:148:11)
    at formatType (/Users/Development/cfg-development/centrifuge-subql/node_modules/@polkadot/typegen/cjs/util/formatting.js:281:34)
    at /Users/Development/cfg-development/centrifuge-subql/node_modules/@polkadot/typegen/cjs/generate/tsDef.js:235:108
    at Array.map (<anonymous>)
    at Object.tsStruct (/Users/Development/cfg-development/centrifuge-subql/node_modules/@polkadot/typegen/cjs/generate/tsDef.js:234:20)
    at /Users/Development/cfg-development/centrifuge-subql/node_modules/@polkadot/typegen/cjs/generate/lookup.js:210:187
    at Array.map (<anonymous>)
    at generateLookupTypes (/Users/Development/cfg-development/centrifuge-subql/node_modules/@polkadot/typegen/cjs/generate/lookup.js:207:26)
    at /Users/Development/cfg-development/centrifuge-subql/node_modules/@polkadot/typegen/cjs/generate/lookup.js:258:5
    at Array.reduce (<anonymous>)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

All @polkadot/api package versions are "8.4.2"

Any idea how to fix this?

jacogr commented 2 years ago

Not all paths are implemented for typegen - since these paths do not appear in Substrate master (and Polkadot master), they are untested and unverified to ensure that they push out the correct outputs in all cases. So the approach here is to rather be explicit on what is non-tested instead of doing the wrong things because the type generation it has not been checked for validity.

So in cases like this,

In all cases, it needs to be checked and verified. Generally can make an attempt to support all cases, but without tests this will result in "this doesn't do what it is supposed to" issues logged, so it is not an avenue I wish to go on.

jacogr commented 2 years ago

Something similar (on latest Substrate) addressed in https://github.com/polkadot-js/api/pull/4934

However, the cause of that one is probably vastly different to this.

As for now, going to close this since there is absolutely nothing that can be done without a reproducible case. As it stands this is an issue in the queue that cannot get attention since there is no metadata provided that can replicate the specific issue.

Flee free to re-open once there is actual metadata available.

polkadot-js-bot commented 2 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.