Enhance your Dapp abilities with Moonbeam feature: XCM.

Drnutsu
MATeS of Moonbeam
Published in
9 min readMay 9, 2022

Ethereum the first and the most famous blockchain that thousands of dapp running on it nowadays, but actually cost to running some transaction is really non- reasonable expensive. dapp user always need to calculate cost that they need to spend to do something very accurately neither they will be loss since didn’t started the investment yet.

Everything sound really horrible right ? actually it’s not just gas cost that be the problem, It’s also have the execution time that too long, user need to wait it on someday that network very congested. Actually, ETH team currently working on ETH 2.0 that will fixed all problem I’ve mention previously, but seems it took a bit long time to upgrade to 2.0, due to many problem still waiting to solve.

However, many project has already introduced and provided workaround like layer2 to solve the pain first. But, you know, it really not the long term solution and also not a perfect solution some workaround lead to new problem, but some solution just partial fixed the problems. However, some project decided to build the new chain to solve problem they don’t need to relied on ETH anymore. One of that chain is Polkadot, the concept is very interesting, they don’t build one chain that best for all which is impossible, but they build multi-chain that best of their own that be able to connect together, collaborate through the parent chain called “relay chain”. The parent chain responsible for finality and consensus for all children chain that called “parachain” which relied on them.

With this design, it enhance the possibilities to let everyone involving by create their own chain adapt to each chain use case, but it all still be connected through the same consensus layer.

Unfortunately, polkadot uses rust language to implement everything on their chain including dapp, and you know, most of dapp is implemented with solidity lang, so they’re a bit problem here for dapp developer to migrate their dapp to this chain. And This is the problem that moonbeam need to solve 🚀

Basically, moonbeam provides a solution for any ETH dapp developer to migrate their solidity dapp directly to polkadot ecosystem. we would say, Moonbeam is EVM compatible chain on polkadot ecosystem. Moreover, the migrated dapp also be able to use polkadot abilities, for example XCM to transfer asset to other parachain on polkadot in very easy way.

In this article, we will deep dive more detail in to XCM feature on moonbeam. What exactly is it ? And how can we use it.

XCM

XCM is abbreviation from Cross Consensus Message Format that polkadot team defined and has some implementation provided. Basically, this feature let users transfer asset cross parachain, and also parachain to relaychain. So we can say, dapp on moonbeam also be able to use this ability to transfer through solidity smart contract. before we going to try it on tutorial, we need to briefly understand about polkadot ecosystem first.

As I already said, parachain is chain that your migrated dapp will be deployed. in this tutorial, we will use “Moonbase Alpha” which is the ahead features testnet, and it’s already support XCM on the chain. So that we will try to send native asset from this parachain to relay chain (Moonbase relay testnet).

First of all, before we will to take some action, If you never know XCM before, let check the mechanism behind XCM first, so I highly suggest you guys to check this LINK first.

So basically, XCM is the way to send asset token across polkadot ecosystem chains, and polkadot team have proposed XC-20 standard for represent ERC-20 token which transferred from. For example, token UNIT on relay-chain when it transferred to parachain the name will changed to be xcUNIT instead.

I think, that enough for theory part, let move to the tutorial, others things we will know it while walking though the tutorial.

Create first XCM message

The easiest way to create some message especially for XCM message is using polkadot.js app to build it one. So first, going to polkadot.js app, and hover Developer > Extrinsics , here is the message builder tool

if you can’t find menu, please ensure that you’re already select “Moonbase Alpha” chain, not “Moonbase Relay Testnet”, please carefully check it again.

Select xToken on submit the following extrinsic droupdown,

You might wonder, what is xToken that we just selected. Briefly, xToken is wrapper functions/pallets that developers can leverage to use XCM features on Polkadot/Kusama. If you are also not familiar with pallet, It basically like a composable module that provided by polkadot, developer be able to build functional code by compose them. We can says xToken is a module design to transfer assets across polkadot’s chain (substrate chain) for more info, I highly suggest checking this LINK.

To make it all clear, I would like to demonstrate, so we gonna try to transfer UNIT asset from parachain to relay chain with XCM message builder on polkadot.js

After select xToken, we need to specific asset that we need to transfer. There are many point that I need to explain first, you can follow the # sequence on the image above.

  • #1 we select transferMultiasset as method for that we will call to transfer asset.
  • #2 select the version of function, we select version 1 here
  • #3 this one is very easy to confuse, the parent here is specific how much hop from the sender origin to relay chain, sender currently on parochial so it’s required about 1 hop to relay chain.
  • #4 this field specific where is the origin of asset which we’ll sent, let set it as here because xcUNIT token (which will be UNIT on relaychain) is native token on relay chain so don’t need to specific any address here, just add it here to specific that this is native token on relaychain itself.
  • #5 it just specific how must asset we gonna transfer, don’t forgot to add zero offset 12 places, for example, we will add “1000000000000” here, if we need to send 1 UNIT.

Next, we will specific destination address that we will send to. So we ned to specific Multilocation here, basically, Multilocation is way to specific the point on entire relay/para chain ecosystem relative to the origin.

Let fill the multi-location, I will to explain it by sequence follow the # as before.

  • #1 as previous field we specific parent = 1 which is number of hop from relay chain, the next field is X1 , you may wonder why this time we select it as X1, it because now we have to specific point on relaychian that will received the asset. and select AccountId32 to specific 32 bytes address (polkadot) address on relay chain.
  • #2 this address here you need to transform you polkadot destination address to hex first (as you saw 0x as prefix). You can use This toolfor helping you get your hex polkadot address,

place your polkadot address, then click on transfer icon it will show hex on the right input, then copy it and place on #2.

  • #3 the last one is weight it is very likes gas on ETH, it usually around 100000000 per instruction, for this transferMultiasset it require around 4 instruction so, 400000000 would be enough.

That’s all 😃, then click “Submit Transaction” your asset will sending to the target relaychain in about not more than 4 minute.

Precompile Contract

You might be curious, Could we make XCM transfer on solidity smart contract ? Of course, we can 😃, moonbeam already provides precompile contract for us. For you guys whose still not familiar with precompile contract please check this LINK to get more understanding of it first.

To demonstrate, I will use a remix to demonstrate the concept. Let’s open REMIX first.

First of all, we need to install moonbeam plugin on remix

  • #1 click on plugin icon on the bottom left
  • #2, #3 type moonbeam for search and click on activate
  • #4 it will show moonbeam plugin icon.

That’s all we’re ready to try moonbeam solidity precompile.

  • #1 Let connect to moonbase alpha, the parachain that will be origin sender chain
  • #2 add precompile address here, the precompile address for XToken on moonbase alpha is 0x0000000000000000000000000000000000000804 , any way you can check all precompile contract address on moonbeam ecosystem officially in this LINK. Then click on At Address to let remix fetch interface of functions.

Here is parameter that we need to specify to call this function.

  • currency_address this is asset id address that will be transferred (address on origin parachain). for xcUNIT, it’s 42259045809535163221576417993425387648, you can check all available asset id HERE
  • amount it very straightforward, yeah amount to send, don’t forget to add 12 zero behind.
  • destination this one is the hard part 😂 , we need to specific multiplication here by concat hex of specific information, let’s build together.

As you can see, actually the Multilocation here is just struct to specific struct we could use the tuple form here.
[1, ["<interior-specific-HEX>"]] the first item on array will be parent field, as we already discussed it 1 for parachain as It need 1 hop form relay chain. The second parameter is array of interior address in HEX form. How can we build it, let's build together.

For easier explanation, I will refer to message that we just built previously. Follow the sequence

  • #1 we selected X1 for multilocation junction here so the first 2 bytes will be 0x01 , so it will be 02, 03 follow the sequence of choice here.
  • #2 the bytes sequence is a bit different from the message builder one, here is address, so we need to transform polkadot address to hex by tool that I have already provided above. (Get just hex part don’t need to prefix 0x here)
  • #3 it’s the network junction id, we select any it’s the first choice here so it would be 00 for this part.

Then, append it all we’ll got the mulitlocation HEX for specific destination address here. The result would be like

[1, ["0x013a8*********************b00"]]
  • the last parameter weight yeah same as previously, it would be 400000000 here, or 1000000000 for sure.

That all 🎉 , then click on transfer button below sign the transaction and waiting the result for moment. Here we go, we be able to call XCM from any smart contract in solidity now 🎉 🎉 🎉

Final Thoughts

I hope you guys understood the concept to send some assets through XCM better after reading this article. As you know, there’re many mechanisms behind the scene that moonbeam working for us to make this thing work in very simple way like this. However, XCM is really killer feature that all dapp on polkadot ecosystem will use commonly in future and also It will be core mechanism of many protocol/application in the future when polkadot ecosystem is unstoppable growth. I hope you guys love this article and also hope it help you guys understand XCM better. Thanks for all interest. Hope you guys have a good day ☀️

--

--