4

I'm trying to generate documentation for substrate RPC queries using the meta property in the @polkadot/api library. Something like api.query.<pallet>.<storageitem/extrinsic>.meta. This gives me most of the data that i need, except for what appears to the types for the parameters/return type of the RPCs. For these, i seem to be presented with an index mapping like the following:

{
  name: 'palletVersion',
  modifier: 'Required',
  type: { plain: 2 },
  fallback: '0x',
  docs: [ 'Returns the current pallet version from storage' ]
}

For plain types, and for what i assume are custom types (under the "type" key)

{
  name: 'Votes',
  modifier: 'Optional',
  type: { map: { hashers: [Array], key: 83, value: 6 } },
  fallback: '0x00',
  docs: [
    ' A lookup mapping (epoch, call_hash) to a bitmask representing the votes for each validator.'
  ]
}

My question is: What is the most efficient way to dereference these integers (which i assume reference types in a datastructure somewhere) so that my documentation can easily show the base types required for the parameters and return signature of each RPC call?

1 Answer 1

2

It looks like you can do something like this:

let query = api.query[pallet][storage].meta;
let valueType = (query.type.isMap) ? query.type.asMap.value.toJSON() : query.type.asPlain.toJSON();
let valueTypeDef = api.registry.metadata.lookup.getTypeDef(valueType).type;

I took this from the OP of this issue.

It should give you the string representation of the type, which you can use in your documentation. The above example solves for the value but you can do the same for the key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.