5

I know that all the objects in the JS API has the toHex() function, so that would answer the first bit of my question - how to serialize when I have an extrinsic from getBlock().

However where I'm burning a bit is on the reverse, going from the hex back into an extrinsic object so I can inspect it. Currently I have something, but not sure if it the best -

const h = x.toHex();
const y = api.registry.createType('Extrinsic', h);

console.log(y.toHuman());

This is only for the same runtime, so I don't need historic capability.

1 Answer 1

7

There is nothing wrong with the above approach, however generally I try to steer people clear from using createType (all string name based) unless there is no other way. Since the API always converts types anyway, the use should be minimal in "normal" code.

For the above, the api does indeed provide a shortcut for quick construction (I'm using your naming here for alignment) -

// re-construct an extrinsic from the input
const y = api.tx(h);

// log, submit, etc.
console.log(y.toHuman());

Although you are excluding historic, I'll just add some detail for that usecase for future searchers. As you alluded to the both your and my construction does require the same runtime/metadata, the latter being quite important since call indexes may not have moved between upgrades, but generally type indexes (as provided since metadata v14) do move around on upgrades.

I took a quick look at the historic api.at(<blockHash>) and the interface returned there does not contain an .tx(<hex-goes-here>) helper, so the only way there to reconstruct is indeed via apiAt.createType(...) as above. (This seems like an oversight, will log it)

1

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.