Cross-Chain EVM Calls with XCM on Moonbeam/Polkadot with Kevin Nielsen
Ethereum Denver·Mon, Oct 9, 2023, 12:00 AM
Cross-Chain EVM Calls with XCM on Moonbeam/Polkadot In this talk we'll discuss how Polkadot's interoperability protocol works at a high level. And we'll go into more details of how this can be used to access Moonbeam's EVM from a different chain. This opens a new realm of possibilities, as contracts in Moonbeam can access app-specialized blockchains inside the Polkadot ecosystem. But also, these specialized chain can tap into Moonbeam's EVM and all the benefits smart contracts offer. Infrastructure + Scalability Stage Kevin Nielsen
Transcript
uh thank you so much for uh stopping by our talk so um is that too loud it's okay cool yeah it's working yeah it's perfect cool um so today we'll we'll talk about uh cross chain evm calls and this is a way for us to be able to send a call to moonbeam's evm uh from another chain within polka dot and uh my name is Kevin Nielsen I'm a developer relations engineer at Moonbeam and I'm joined by Stephen Shelton who's a senior blockchain engineer at Moonbeam so to kind of Orient ourselves a little bit into uh what's Moonbeam and uh what's moonbeam's role in a multi-chain future I'll go ahead and talk just a little bit about Moonbeam and give you a bit of background and to do that I also want to give you a bit of background about polka dot so Moonbeam is a layer one blockchain running on top of polka dot and polka dot uh work on polka dots started in about 2016. and they took a pretty uh Divergent Approach at the time which was saying that the future is going to be multi-chain so now you hear everybody say that the future is multi-chained but back in 2016 that was a pretty contrarian approach and so it gave polka dot tons of lead time in order to build up uh an infrastructure and a blockchain that supports this vision and that's enabled by the relay chain which is the layer zero and there's a lot of networking connectivity uh and polka dot provides you with a lot of functionality out of the box to make it easier to launch your chain on top of polka dot and so polka dot calls these layer one blockchains pair chains just a fancy name for a layer one blockchain and the idea is that they all provide some sort of specific functionality so maybe they provide a decentralized identity uh smart contracts various things like that but the important thing about polka dot is that it's not meant to be just an ecosystem of a bunch of separate chains all doing their own thing the important thing is that they're able to talk to each other if they'd like and they can send value and they can send messages so you can do cross-chain contract calls and other things and this is enabled by xcm and that's what allows the chance to to talk to one another and we'll talk lots more about XDM so xcm is just a it's polka dots version of General message passing and xcm is kind of available to you if you'd like to use it you don't have to but it is available uh to you and you can choose to set it up with neighboring chains and so it allows you to send messages and we can think of this is kind of like a you know form of bridging uh where uh bridging tokens is just one aspect that you can do you can do much more but if you'd like you can also send uh send tokens along with your messages and so where this fits into Moonbeam is that Moonbeam is a hub of uh tons of different cross-chain connections um so that includes uh connectivity within polka dot via xcm uh we're connected to many other chains within polka dot and then we also have a ton of GMP protocol Integrations uh outside of the polka dot ecosystem and that's enabled uh by teams like axillar hyperlane layer zero uh Wormhole multi-chain and others and so this allows you to do things uh like uh make a remote cross chain call to ethereum or Avalanche or polygon and then you can also do things that kind of unite both sides of this and to give you an example let's say a particular parachain wanted to send a cross-chain contract call to ethereum for example but that pair chain is not connected to ethereum so instead it can use Moonbeam as a hub it can first send a message to Moonbeam and then Moonbeam can send a message via a GMP protocol to ethereum or to that chain and another example just within polka dot is uh interlay for example provides an insured form of Bitcoin and you can then go ahead and use that Bitcoin in defy on Moonbeam and so that works by uh the interlay BTC is moved as an xc20 which is a cross chain asset and then that can be used in D5 uh very seamlessly and the reason that we're so excited about this is because we want to provide users with much better user experiences so we know that the future is going to be multi-chain we have rollups we have app chains we have layer twos I hear people talking about layer threes uh there's so many changes there's a proliferation and so we don't Envision you know the genie going back in the bottle we think that there's going to continue to be a huge number of chains and we want to work to support that and so in our minds you know do we want users to have an experience where they're bridging from chain to chain trying to get the native token and then switching their Network in metamask or do we envision one where they're on a chain like Moonbeam and they're able to interact with many different chains under the hood in a seamless manner and with this they're able to pay for the cross chain transaction on Moonbeam they don't need to go and get another gas token uh they're able to do it all under the hood uh interacting with many different chains and to just give a quick example of this actually within polka dot is that Lido has a liquid dot product and as you may know you don't have Smart contracts that are deployed to the polka dot relay chain so instead Lotto deployed their smart contracts onto Moonbeam and you can take your XC dot which is a cross chain version of dot just called XC Dot and you can take that and you can stake it in Lido smart contracts and Lido smart contracts will then take that XC Dot it'll move it up to the polka dot relay chain all in a trustless manner and then it will stake that and this is one single interaction one single click there's quite a lot that's going on behind the scenes but it's a seamless uh very easy experience for the end user so this is something that's quite powerful and uh Steven's going to tell you a little bit more about xcm so we looked at some of the things that Axiom can do and I'll talk a little bit about what exactly Axiom is so at its core Axiom is really just a list of instructions that one chain wants to send to another chain and ask it to perform on its behalf so these instructions are really granular and they they generally don't do anything interesting by themselves um so as an example if I asked you to stand up we wouldn't really accomplish anything but if I asked you to stand up take 10 Paces to the left and then flip the switch I could get you to turn the light on or off and so this is kind of how xcm instructions come together to do really interesting things so one trivial example of this is getting a chain to send tokens to another chain but perhaps a more interesting example one we're going to go into today is uh asking another chain to do some native functionality on itself on on our behalf uh and so on on Moonbeam one of the things that we have to worry about is that we don't know what a a source account looks like so if we have Alice on polka dot uh we really don't know anything about um necessarily about what her account ID looks like um how long it is how it was hashed or any of the security properties of that and we can't make any assumptions that it's ethereum compatible and so what we do to get around this is we uh we take the Blake 256 hash of the multi-location and then truncate that to 20 bytes and this 20 bytes is then an ethereum compatible account ID and so a multi-location is really just a way to represent uh some other account relative to us so in this case we represent Alice on the polka dot relay chain relative to us and we call this a multi-location derivative account on Moonbeam uh and so here's here's kind of an example where um Alice is going to ask um her relay chain to send an xcm to Moonbeam and she's going to ask it to do some call which is encoded as some some bytes representing representing an encoded call so if she were to do this send it over to Moonbeam the first thing we would do is derive this multi-location derivative account ID for her and uh then if everything checks out we will go ahead and make the call that you requested on her behalf so it would look as though it came from this derivative account and not from from Alice on polkadot foreign and so we'll we'll talk a little bit more about um a more a more specific example of using moonbeam's evm through xcm um so Moonbeam is connected to other pair chains and through into the relay chain um and so it's going to get these xcm messages and one of the things that it needs to do is make sure that these messages are are valid for us and so the main thing that this really means is that it attaches some way to pay for fees which will take a closer look at later but if an axiom instruction checks out and there's a a transact call that wants to invoke the evm then we'll invoke the evm and it'll look just like a normal contract call in the evm at that point uh so here's kind of a under the hood look at Moonbeam so we'll see a lot of the same pieces that we would find elsewhere we have a user who's going to send a ecdsa signed transaction it'll go to some RPC service in this RPC service will be able to put it into a transaction pool which can do some validation on the transaction if everything looks good um this transaction could be played against the evm and we could observe some State change at that point when we go through xcm things look a little bit different so as we talked about earlier we we don't know much about this signature of the transaction or the account ID so we derive a multi-location derivative account ID that we talked about and then we also need to make a couple other changes before it goes into the evm and we do this through our what we call our palette ethereum xcm but once we've done all this it can go into the evm just like a normal transaction would and we would be able to observe State changes just as before so some of the other changes that we need to make because there's no signature it's possible for two different transactions with the same inputs to produce a hash collision and so we get around this by just introducing announce in this case it's a global nonce and so we sought the transaction with a unique number um also because there's no signature there's no EC recover so there's no way to derive the public key or an account ID from the transaction itself and so what we do is we replace the from in the message.cinder when it's in the evm with the multi-location derivative account ID that we talked about and then finally we also set the gas price to zero and the reason for this is because xium itself has a mechanism for charging for fees I note that while we do set the gas price to zero we also do have a normal gas limit and this is respected in the normal way in the evm um and so if we were to submit something like this we could uh then do a request for get transaction by hash there's an example on the right of using curl to do this and if we were to do this we might get something that looks a little bit like this so this looks pretty normal but what we've done here is we've replaced the nuts with our Global knots our from is now the multi-location derivative account ID representing Alice on polkadot and we've replaced our our gas Fields with zeros although there is still gas being consumed and then finally we have set our VR and S to some arbitrary constants because they're entirely unuseful in this case um cool um so we're going to show a really cool or we have a demo and just due to the Wi-Fi situation we we took a bunch of screenshots and we wanted to make sure we're going to take you through step by step uh exactly what we'd be doing and it'll be you know smoother than uh than doing this live and so we're going to do is we're going to make a swap uh on moon base Alpha it's a uniswap clone here and we're going to trigger this swap via xcm and uh I'll be walking you through the the beginning of the flow and Steven's going to take you into more detail so to just kind of start from an overview we're going to be uh starting from the polkadot relay chain uh and so let's say Alice has an account uh on polkadot so we're going to build an xcm message there and then we're going to send that xcm to Moonbeam so we'll specify what the destination chain is and then this xcm message will be looked at it'll be analyzed and uh it will eventually dispatch an evm call but there's an important step here and that's that first this evm call is going to be dispatched from a multi-location derivative account so what the heck is this and why is it important well uh polkadot uses a different type of account structure uh than ethereum and for moonbeam's ethereum compatibility it was very important that we used ethereum accounts we want to provide a very seamless experience and having an ethereum account is crucial and critical to this so we don't assume that Alice has an account on Moonbeam and so instead we have this we create a derivative account that belongs to Alice um she's the only one that can control it and only via xcm and so nobody knows the private keys to this account uh it's a special type of account and you can send funds to it and then you can control the funds that are in this account call various contracts and do anything you'd like but all these instructions need to come uh via XDM so this evm call will be executed and we're going to have to pre-fund the multi-location derivative account with whatever funds we want to swap so we need to make sure that they're already there and we're going to make the Swap and then of course whatever the outcome of the swap is uh the tokens that we get will will go to that multi-location derivative account and to go through kind of step by step on what we're going to do in the demo we're first going to get the call data from the uniswap swap so we'll just go uh we'll go ahead and prepare our transaction we'll go ahead and initiate it and it'll open up in metamask and we'll copy the hex call data we won't actually submit the transaction at this point but what we'll do is we'll take the call data from that call we're going to make some changes to it to make it more friendly to our demo and then what we'll do is we will build um build a call within the ethereum xcm palette and the ethereum xcm palette enables these remote evm calls then what we're going to do is we're going to go in and take the encoded call data that we've just built and we've built this on Moonbeam and we're going to take that and we're essentially going to drop it into an xcm message and we're going to drop it into an xtm message that starts on polkadot and so we're going to have to include a bunch of parameters where the message is going how much gas which would be in the ethereum exam call and we're also going to include a weight and different things like that and finally we're going to send it and once we send it we'll be able to look at it later and it'll look just like a normal evm transaction all right um well let's go ahead and jump in so what we have here is a uniswap deployment on our local test net our Moonbeam test net and we also have the UI along with it um I want to point out real quick that while we're going to do this kind of the long-handed way and show all the the details we would expect that a normal product would uh have a much much nicer ux probably just a one-click ux to pretty much do everything that we're about to do um so what we what we do here is we want to build up the low-level evm call data and as kind of a shortcut to that what we do is start with the uniswap UI so we have here a swap that we've set up between Dev and Mars tokens and we're going to start out by um uh starting with metamask but rather than send the transaction we'll click on the hex tab where we can scrape out the call data so here we have the call data and we're going to dissect it a little bit and um replace a couple things so we start off by seeing that the function selector maps to swap exact ether tokens and then there's a few different fields here we're going to replace a couple of them so since we're doing this by hand and it might take a while we'll replace the deadline and we'll also increase the slippage amount just in case the price moves on us but more importantly we're going to replace the account ID with the multi-location derivative account ID representing Alice that we talked about earlier so now that we've done this we've got our modified call data and this is something that we could send into Moonbeams evm and so what we'll do is we'll go ahead and copy that and we're going to place it inside another layer here so we're kind of doing an encapsulation technique where we have the low level call data evm call data and we're going to place it inside a kind of a higher level call data and then we'll eventually go over to xcm to polka dot and send it as an axiom so what we're looking at here is sort of a crude UI that wraps the low-level functionality of Moonbeam and you'll find the same UI exists for most blockchains inside the polka dot ecosystem and so we're going to use it to build up kind of our second layer here so what we have is um a we're requesting a call to the palette ethereum xcm and we want to invoke its transact call so a pallet is in a lot of ways like a smart contract where it is addressable and it has functions that you can call and so we're setting up a a transact call here and here are the parameters that uh we're going to send to it these look a lot like an ethereum transaction we've got a gas limit we've got an action which is a call as opposed to a create as well as a value and then finally our input is the low-level evm call data that we just looked at before so once we do all this we build up sort of the second layer which is our encoded call data representing everything above basically the ethereum xcm transact call and then of course inside of that is our evm call data so now that we've done this we can take this call data and we can move over to a polka dot uh well in this case it's really our our test net version of polka dot um and so we see the same UI here except it is a different blockchain this is now polka dot um so we have Alice here who wants to invoke the xcm palette and uh it's send function so what she's going to do is uh tell the send function where it needs to go so we see again another multi-location and this time the multi-location represents um Moonbeam itself so we're we're now on polka dot and we need a multi-location that represents Moonbeam relative to polka dot and so again we'll use a multi-location to do that so here we've got parachain 1000 inside of our our multi-location which is how we point to uh Moonbeam so from there we need to fill in the list of instructions that we were talking about earlier and so in this case we start out with a withdrawal asset instruction and what this does is it takes funds held under Alice's control and it moves it into what we call a holding register and a holding register is really just a Axiom thing where we we have assets held for the duration of a Nexium call and then so once we have those funds in the holding register we can invoke a buy execution instruction and this is uh just a way to use those funds in the holding register to pay for fees and now we get to the important part which is the transact instruction and so this transaction instruction will tell Moonbeam that it wants to invoke some native functionality on its chain and then we see here again our encoded bytes that we were just playing with on ethereum so now these encoded bytes are the ones that represent the palette ethereumxium call um and of course again they have inside of it the evm call so this whole thing is being built up into xcm message with the embedded call data and it's going to be shipped over to Moonbeam finally we see the deposit asset and this is really just a way to refund unspent fees so if Alice were to do all this and hit submit what would happen is we take the sexy message it gets routed over to Moonbeam Moonbeam unpacks it and finds the encoded call data realizes that it needs to use that to invoke the pallet ethereum xcm palette inside that it finds the evm data invokes the evm with that which causes a call to the unit swap contract and finally we see that there's a swap for the tokens that we looked at earlier so if we were to do all this and then look at our moonscan Explorer here we would see that we indeed had some tokens transferred notice that the front field is Alice's uh multi-location derivative account ID uh and we also have a zero transaction fee and uh so that's just because um we set the gas price to zero although we did respect the gas limit and uh finally we could um then call get transaction by hash again and we would get something like this as before we've replaced the global nonce uh the nonce with our Global notes uh replace from with our derivative account and we've set the gas prices to zero and again our VR and S constants or every R and S fields are their useless constants good cool so just to conclude uh why are we so excited about remote evm calls uh just to kind of take you on on a journey right with xcm and kind of interoperability within polka dot we've been able to transfer assets between chains for quite some time now then we were able to have a remote execution of substrate functions so you could use the substrate API and you can transfer and you can stake and you can do specific functions but we were really waiting and excited uh and the engineering team was hard at work in order to build uh this remote evm call functionality because it unleashes really unlimited possibilities and it realizes kind of polka dots vision of interoperability that is to have this Union of connected chance which can share features and and offer their you know diverse feature set to other chains and when we say this we we don't assume that somebody knows about Moonbeam that they have an account on Moonbeam that they have any gas or glimmer for funds for gas and so to this somebody could kind of think of this optionality as a moon providing an evm as a service and lastly Moonbeam is very well positioned as kind of a Gateway and a bridge hub for accessing all the different polka dot chains and also connecting to uh many different ecosystems outside of polkadot and so that's something that we're that we're very excited about and I think we have a couple minutes left and so I just wanted to give a shout out to some of our bounties um we have two and uh of course the first one is on remote uh contract calls and so you can use xcm to do a remote evm call that is certainly an option and uh we also have a couple other options here you can use any GMP protocol to make a cross-chain contract call so you can use XLR layer zero hyperlane you can use any of those Wormhole as well and you can we leave it up to you whether you want to make the call to or from Moonbeam or Moon based Alpha so meaning you can make a call from Moonbeam to ethereum or ethereum to Moonbeam it's totally up to you and we encourage you to be ambitious um and you know it's it's best to try something that's challenging rather than you know perfect a simple cross chain interaction and the second Bounty is to use uh the second Bounty also follows the trend of improving the end user experience and uh to that end Moonbeam has a ton of powerful pre-compiles that offer some some really unique uh and really powerful features so one is the batch pre-compile and that allows you to combine transactions into a single call the classic example here is you're making a swap on a DEX and rather than doing an approval transaction waiting for it to be confirmed and then doing a swap transaction you can instead batch those together into a single Atomic call if you'd like and you can choose whether you want it to be Atomic or whether you want it to not be Atomic and maybe one of the calls could succeed and one of them could fail the next one is the call permit pre-compile and this allows you to turn any evm call the user can can sign it and authorize any evm call and then this could be dispatched via a provider like like by economy and we have a couple ideas here you can build anything you want but just to kind of get you thinking you could build an auction you could build a Deadman switch there's tons of options so for both of these we have up to um we have first and second place for each of these so in total that's up to four prizes and first place for each of these is four thousand dollar Bounty and second place prize for each of these is a one thousand dollar bounty we have a booth upstairs which is uh underneath the giant red or orange sign that says devtobia Stephen and I will also hang out outside here for uh outside the talking case you want to talk about anything further and I think we do have a couple minutes for questions so we warmly welcome any questions yeah yeah go ahead up in the sweater I think what's the best way to get going on the bounties yeah I was just curious about how to get going on the bounties sure for sure uh so we have a uh which bounty in particular were you thinking of the first one the first one sure um so we have this uh Moon Builders hackathon Survival Guide uh if you just um I'll show you a I'll show you a link to it right now actually oh the Wi-Fi situation okay yeah it's rough we we have it um we have it in the in the Discord so if you go to the uh Moonbeam Discord uh or the Moonbeam channel in the East Denver Discord and you can also Google search moonbuilders hackathon Survival Guide but we have a bunch of tutorials there for how to get started using axlr layer zero Wormhole Moonbeam Builders hacker Survival Guide yeah that's right okay yeah Moonbeam Moon Builders hackathon Survival Guide and uh we have uh tutorials for how to get started with axillar and all the others and the best thing is to do is to build like a a send a simple cross chain message just to see how it works and then to get started on building your your dab and there was another question here between xcm and XLR like how how that connects is so is xcm the the way in which axelr is able to connect with polka dot chains that's not how that works no so those are those are completely different things um so xcm is is like separate from the evm completely um we've kind of hooked it up so we we talked about invoking the evm through exim but we also have a way for you to use the Axiom from evm itself but technically those are completely separate things except that we've kind of hooked them up and made them available through our evm okay does that explain it yeah yeah sweet well thank you so much for joining us really appreciate your your time and attentions there [Music]
Automatic transcript — names and jargon may be misspelled.