paritytech / txwrapper

Helper funtions for offline transaction generation.
Apache License 2.0
58 stars 27 forks source link

Getting "cannot construct unknown type PalletId" when getRegistry #441

Closed anhcao142 closed 3 years ago

anhcao142 commented 3 years ago

Hi, currently, I'm using version 3.10.12 since it supports node v10. But today while trying to getRegistry to build tx I encountered this error:

2021-05-15 16:53:49        REGISTRY: Unable to resolve type Outcome, it will fail on construction
2021-05-15 16:53:49        REGISTRY: Unable to resolve type PalletId, it will fail on construction
2021-05-15 16:53:49        REGISTRY: Error: Cannot construct unknown type PalletId
    at new DoNotConstruct (/polkadot-test/node_modules/@polkadot/types/primitive/DoNotConstruct.js:21:11)
    at new <anonymous> (/polkadot-test/node_modules/@polkadot/types/primitive/DoNotConstruct.js:27:9)
    at new Type (/polkadot-test/node_modules/@polkadot/types/create/registry.js:340:16)
    at initType (/polkadot-test/node_modules/@polkadot/types/create/createType.js:49:19)
    at createTypeUnsafe (/polkadot-test/node_modules/@polkadot/types/create/createType.js:69:12)
    at createType (/polkadot-test/node_modules/@polkadot/types/create/createType.js:84:10)
    at TypeRegistry.createType (/polkadot-test/node_modules/@polkadot/types/create/registry.js:297:39)
    at /polkadot-test/node_modules/@polkadot/metadata/decorate/constants/index.js:29:30
    at Array.reduce (<anonymous>)
    at /polkadot-test/node_modules/@polkadot/metadata/decorate/constants/index.js:26:58
    at Array.reduce (<anonymous>)
    at decorateConstants (/polkadot-test/node_modules/@polkadot/metadata/decorate/constants/index.js:17:18)
    at extractProperties (/polkadot-test/node_modules/@polkadot/types/create/registry.js:120:53)
    at TypeRegistry.setMetadata (/polkadot-test/node_modules/@polkadot/types/create/registry.js:456:29)
    at Object.getRegistry (/polkadot-test/node_modules/@substrate/txwrapper/lib/util/metadata.js:114:18)
    at main (/polkadot-test/index.js:33:30)
Error: createType(PalletId):: Cannot construct unknown type PalletId
    at createTypeUnsafe (/polkadot-test/node_modules/@polkadot/types/create/createType.js:72:11)
    at createType (/polkadot-test/node_modules/@polkadot/types/create/createType.js:84:10)
    at TypeRegistry.createType (/polkadot-test/node_modules/@polkadot/types/create/registry.js:297:39)
    at /polkadot-test/node_modules/@polkadot/metadata/decorate/constants/index.js:29:30
    at Array.reduce (<anonymous>)
    at /polkadot-test/node_modules/@polkadot/metadata/decorate/constants/index.js:26:58
    at Array.reduce (<anonymous>)
    at decorateConstants (/polkadot-test/node_modules/@polkadot/metadata/decorate/constants/index.js:17:18)
    at extractProperties (/polkadot-test/node_modules/@polkadot/types/create/registry.js:120:53)
    at TypeRegistry.setMetadata (/polkadot-test/node_modules/@polkadot/types/create/registry.js:456:29)
    at Object.getRegistry (/polkadot-test/node_modules/@substrate/txwrapper/lib/util/metadata.js:114:18)
    at main (/polkadot-test/index.js:33:30)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

Here is the code to reproduce:

txWrapper.getRegistry('Kusama', 'kusama', 9010, metadata);

I noticed that if I upgrade txWrapper to version 4 or 5, the function works fine but that also requires me to upgrade my node version to 14. Is there a way to solve the error without upgrading Node version?

emostov commented 3 years ago

I noticed that if I upgrade txWrapper to version 4 or 5, the function works fine but that also requires me to upgrade my node version to 14.

In theory you could get the polkadot-js compatible type definitions for the ones causing breakage and pass them into the registry, but you would need to not use getRegistry and instead create the registry yourself.

Something like

    const registry = new TypeRegistry();
    // Register types specific to chain/runtimeVersion
    registry.register({ 
          PalletId: 'LockIdentifier',
          ...getSpecTypes(registry, 'Kusama', 'kusama', 9010)
        })
       registry.setMetadata(createMetadata(registry, metadataRpc));

I won't be able to try myself until the work week, but should be the right direction.

Here is some more about extending types (although it focuses on the api, type definitions still apply): https://polkadot.js.org/docs/api/start/types.extend

Note: I would highly recommend switching to txwrapper-polkadot and bumping your node version asap so you are not left behind if there is a breaking change to tx construction.

anhcao142 commented 3 years ago

Thanks for the heads up. Just curious, why does the tx construction changing every now and then? My only use-case at the moment with txwrapper is just to create transferKeepAlive.

emostov commented 3 years ago

Just curious, why does the tx construction changing every now and then?

It varies: increasing compatibility etc. I suspect changes will become increasingly less common, but I recommend staying up to date with whatever latest requirements polkadot-js has so if there is some update you need on super short notice the changes you need to make in your environment would only be small, incremental ones.

Where you able to get the above example code to work?

anhcao142 commented 3 years ago

Yeah, I managed to run the latest txwrapper version in node 10 using esm

Something like this:

yarn add txwrapper --ignore-engines
yarn add esm
node -r esm index.js