patractlabs / redspot

Redspot is an Substrate pallet-contracts (ink!) development environment. Compile your contracts and run them on a different networks. Redspot's core forks from Hardhat but changed a lot to suit substrate.
https://redspot.patract.io/
Other
67 stars 22 forks source link

Sometimes types fail to construct #122

Open KaiserKarel opened 3 years ago

KaiserKarel commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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",
      },
    },
    },
    ...
  2. run redspot console npx redspot console --no-compile
  3. 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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

I'll write a reproducible example this weekend