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

Sometimes types fail to construct #122

Open
KaiserKarel opened this issue Jun 15, 2021 · 11 comments
Open

Sometimes types fail to construct #122

KaiserKarel opened this issue Jun 15, 2021 · 11 comments

Comments

@KaiserKarel
Copy link

Even though we registered types in the configuration, sometimes they'll still fail to be created. I thought that might be due to incorrect order of definitions and usages, but that does not seem to be the case. Are there known bugs here?

@ii-ii-ii
Copy link
Member

@KaiserKarel Are you using @redspot/known-types. If so then there is a real possibility of a conflict, if not then it should be fine.

@KaiserKarel
Copy link
Author

So I managed to track down parts of my error to incorrect configurations, however it seems that sometimes, redspot also fails to create a type for an event:

createType({"foo":"AccountId","bar":"Text"}):: decodeU8a: failed at 0x5901000000000000… on bar: Text:: Text: required length less than remainder, expected at least 88, found 50

where bar is defined as type Bar = String.

Any clues what causes this?

@ii-ii-ii
Copy link
Member

ii-ii-ii commented Jun 17, 2021

First you can check if the types have been correctly injected into the api.registry. For example:

  1. redspot.config.ts should look like this:
...
networks: {
    development: {
      endpoint: "wss://kusama-rpc.polkadot.io",
      gasLimit: "400000000000",
      types: {
        foo: "Text",
      },
    },
  },
...
  1. run redspot console npx redspot console --no-compile
  2. Run these statements and you should get a result like this:

image

This indicates that the configuration is OK.

If there is still a problem, then you can post your code and I will check what the problem is.

@KaiserKarel
Copy link
Author

Thanks, useful tool to sanity check.

This leads me to the following error:

> network.api.registry.createType({"foo":"AccountId","bar":"Text"}).toHex()
Uncaught Error: createType([object Object]):: Cannot read property 'trim' of undefined
    at TypeRegistry.createType (/home/karel/Privi/Privi-Ink-Contract/node_modules/@polkadot/types/create/registry.cjs:308:39)
    at createType (/home/karel/Privi/Privi-Ink-Contract/node_modules/@polkadot/types/create/createType.cjs:67:10)
    at createTypeUnsafe (/home/karel/Privi/Privi-Ink-Contract/node_modules/@polkadot/types/create/createType.cjs:55:11)

@ii-ii-ii
Copy link
Member

ii-ii-ii commented Jun 17, 2021

Of course you can't do that. there is no problem with this error.

The first parameter of createType is the type name and the second parameter is the value. https://polkadot.js.org/docs/api/start/types.create

If you want to define a structure, you can do something like this:

...
types: {
        baz: {
          foo: "AccountId",
          bar: "Text",
        },
      },
  ...

Then run:

network.api.registry.createType('baz', {foo: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', bar: 'hhh'}).toHex()

@KaiserKarel
Copy link
Author

KaiserKarel commented Jun 17, 2021

Yes but this is exactly the error message that is happening during a test. :)

await expect(
      contract.connect(receiver).tx.doFoo({
        ...
      })
    ).to.emit(contract, 'Baz');

Will sometimes fail with createType({"foo":"AccountId","bar":"Text"}):: decodeU8a: failed at 0x5901000000000000… on bar: Text:: Text: required length less than remainder, expected at least 88, found 50, and other times just work.

So I'm wondering why it is attempting to create that type, and why it only sometimes fails.

@KaiserKarel
Copy link
Author

I'm thinking that it is due to an event; based on the fields. Do events need to be registered as well?

@ii-ii-ii
Copy link
Member

decodeU8a: failed at 0x5901000000000000… on bar: Text:: Text: required length less than remainder, expected at least 88, found 50

I think the type should already be injected. This error does not say that the type cannot be found.

But I'm not quite sure why it fails to decode. https://github.com/polkadot-js/api/blob/121bfc79132283d802299cec4a82c50f2a6ecc8f/packages/types/src/primitive/Text.ts#L14-L38

@KaiserKarel
Copy link
Author

So it seems like replacing String with Vec inside the contract does fix the error, although due to it's flakyness it is difficult to test.

@ii-ii-ii
Copy link
Member

Are you using the latest redspot, 0.11.6-3? and if you can provide a test case, we'll debug it and find out what's going wrong.

@KaiserKarel
Copy link
Author

I'll write a reproducible example this weekend

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

No branches or pull requests

2 participants