Frustration with XCM in JavaScript

Originally posted on reddit:

Talisman and I are looking into building a simple JS module that helps developers build XCM for transactions and abstract common patterns like cross chain transfers into a simple JS function taking inputs like: fromParachain, toParachain, assetId, recipient etc. These functions would then return the XCM object and allow the developer to encode it into hex so that it can be submitted to the network via the submitExtrinsic call.

I am currently working on an example transfer which would allow a user to transfer an asset from one parachain to another. You can see it here: Polkadot/Substrate Portal

However, I am running into the following difficulties:

  • How does one encode the XCM into hex? I know that this is possible as it is done on the web interface but there is very little insight into how this is actually done in practice. I have tried looking through the web portal’s code but it is so abstract. I want to be able to build and encode the XCM I have in the above link.
  • XCM itself is not parsable in JavaScript, it looks like JSON but it isn’t. I am struggling to understand how we can formulate such a simple XCM message in JavaScript and then encode it into hex so it can be submitted as a transaction. I know that it is using something called SCALE but I am at a loss as to how to use it and import the types available in XCM v2.

I am also curious to hear from developers as to whether they believe such a module would be useful for them. I know that it is currently very difficult to make an XCM transaction in polkadot.js so I imagine this would be a very important piece of infrastructure.

Please help.

To actually answer you, yes, XCM (and everything else binary to do with the blockchain) is encoded with SCALE. There are libraries, e.g. those under the polkadot-js umbrella or the scale library by ParityTech.

This type of content is better for the StackExchange, not for the ‘Tech Talk’ forum which is meant for high-signal low-noise deep technical discussion.