Back to All Episodes
Season 1Episode 16

LayerZero Overview

April 16, 2022
29m

Listen Now

About This Episode

In this episode of DevNTell, Narb introduces Derked, a smart contract programmer who provides a comprehensive overview of LayerZero and its cross-chain messaging capabilities. Derked explains how LayerZero facilitates the sending of arbitrary messages across multiple blockchains, supporting networks like Ethereum, Arbitrum, Optimism, Binance, Polygon, and Avalanche. He demonstrates the technical implementation using the `lzReceive` function and showcases a live example of sending a cross-chain message from the Optimism testnet to the Rinkeby testnet. The discussion covers practical use cases such as cross-chain swaps, multi-chain NFT marketplaces, and interoperable decentralized applications, highlighting LayerZero's potential to reduce friction in the blockchain ecosystem.

Key Takeaways

1

LayerZero is a protocol that enables arbitrary cross-chain messaging across various blockchain networks, including Ethereum, Arbitrum, Optimism, Binance, Polygon, and Avalanche.

2

The core technical implementation for developers involves using the `send` function on the source chain and implementing the `lzReceive` function in a smart contract on the destination chain.

3

LayerZero can solve significant interoperability challenges, facilitating seamless cross-chain swaps and enabling 'omnichain' NFTs that can move freely between different networks.

4

For reliability, developers are encouraged to use a 'non-blocking receiver' pattern with `try/catch` logic to prevent failed transactions from causing processing delays in cross-chain communication.

5

LayerZero's cross-chain capabilities open the door for more complex decentralised application logic, such as executing specific contract functions on one chain triggered by an event on another.

Timestamps(click to jump)

Episode Transcript

Narb

Welcome to another what's going to be a fantastic installment of DevNTell. So if you didn't know, DevNTell is a 30-minute window for members of the DAO to showcase something they are passionate about or have been working on. This could be an awesome project you've been working on, demonstrating unit testing best practices, automation goodies, smart contracts, how to structure a project, etc. Basically, if you've got a passion for something, this is your opportunity to share with the community. I am happy to introduce to you Derked, who is going to give us a overview of LayerZero. Take it away, Derked.

Derked

Hi, thanks. Okay, so just quickly before we start here, I just want to make it clear, I'm not affiliated with Stargate or LayerZero in any way. I don't work for them or anything. And two, all the examples that I'm going to show you, if you want to use them from GitHub, they have no security guarantees. So you're going to have to double check that yourself. All right. So when we get going here, I'm basically just going to give like a brief explanation of what LayerZero is, what Stargate is, and then we'll go over increasingly sort of complicated use case examples that I've been toying with. And then I'll just take questions along the way, because otherwise it's just going to be me talking for 30 minutes and it might get a little old.

Derked

Okay, so what LayerZero is in like a layman's terms understanding, or like the way I understand it, is it enables you to send arbitrary messages across all types of different all different blockchains. So right now they support seven, excuse me, seven different blockchains. So most of the Ethereum rollups like Arbitrum, Optimism, Binance, Polygon and Avalanche and mainnet, of course. There may be a couple of others I'm forgetting, but anyways. And what you can do is you send just a message that has basically arbitrary data and you send it to the source chain endpoint that the LayerZero team has launched on whatever network. Their infrastructure, basically like their oracle and relayer system, picks it up and then they send that, they relay that information to the endpoint on the destination chain. And then that calls your smart contract that you've put on the destination chain. So I'm not really going to go into any of the details surrounding the infrastructure, like the oracle relayer system. I'm not really an expert on that. I don't really have opinions on like the tradeoffs they've made or how it will compare to other solutions in the future. I'm more just going to focus sort of on the developer side of how you use the tools that they've provided for you to send messages and send, in case of Stargate, send messages with value across different across the different blockchains.

Derked

So LayerZero, basically what you do is you call this function that's called `send`. And `send` is provided on all of the different endpoints, which are smart contracts as launched by the LayerZero team. And you send it, you know, where you want to go, like the chain ID, like where you want to send your message to, what address you want to send that message to. So this is usually a smart contract that you've implemented on the destination chain. And any sort of arbitrary payload, which is the coolest part, we'll get to that in a second. The refund address and some just like, these aren't as important. This is like for token payment in the future, but it's not really relevant right now. And then Stargate is the same thing except you send money. So maybe it's best if we just like hop into the examples quickly and this might make a little bit more sense. So I have like a playground that I've set up and I've implemented this thing. Should I make the text bigger in VS Code? Does that help?

Narb

Maybe a little bit bigger.

Derked

Better? Okay, cool. So to use LayerZero, you need to implement a function in your smart contract called `lzReceive`. And the LayerZero team provides an interface for you called `ILayerZeroReceiver` that gives you the function signature. So you need to put in, you need to implement `lzReceive` in your smart contract and you take in the chain ID, the source address, so basically where the thing is coming from, a nonce. You don't actually have to use this, they handle the security for you, it's just like if you want to do something with it. And then the payload, which is the most important part, which is sort of the information about what you want to do with the information you've received. So I have a little example here that I call MultiReceiver and it's based on the Stargate smart contracts. And what it does is it takes in a message using `lzReceive` and then it decides what function type to run. And the function type is sent in the payload. So if you're not familiar with assembly, this may look a little wonky, but basically what it's doing is it's just reading a one, two, or three from the payload. It's offset by, I don't really want to get into the specific of the way the EVM 32-byte words, but there's a length offset that comes at the front of the payload and you have to skip that and then read the first thing in. So it'll read the function type which is either going to be one, two or three. And then based on that, it'll decode some parameters. So like for the if we send a one, it'll just set a number. If we send a two, it'll set an address. And if we set a string, it'll set that string. So I'll run an example of this and maybe like that is probably going to be the best example that we can see.

Derked

So I have this MultiReceiver contract already deployed up on Rinkeby. So if I find it here. So you can see this is the same thing with this address. And you can see like I've played around with this a little bit before, but we're going to try something in real time here that hopefully is going to be cool and you guys can see it work in real time. So I have a script here called `sendToRinkeby`. So we're going to send a message from Optimism, or the testnet, Optimism Kovan, to Rinkeby. And then when it arrives, we'll change some data in the contract. So it's going to be a cross, basically like a cross-blockchain call. So let's just say like 'Hello Dev DAO'. And if you guys aren't familiar with like Ether.js, I don't know kind of how the experience level in the audience spans, but for completeness sake, what this is doing is just like I'm using Hardhat and it's basically taking the destination address, making the contract and then making a contract call with estimating how much like gas I need to send along with the transaction because you have to pay gas for the cross contract call. So I will run this and hopefully.

Derked

So does anyone like very confused right now? Because I'm happy while we wait for this, I'm happy to just like clarify some things. So if I go back to the Optimism explorer. I'm trying to figure out what address I sent this from. Give me two seconds. Aha, so this is the Optimistic Kovan explorer and this is the transaction that I just sent 35 seconds ago from the terminal with this fee to this address which is the LayerZero endpoint. And it has you know we paid some gas and you know so hopefully if in a minute or two, it usually takes like I don't know, two to three to four minutes depending on what's going on with Rinkeby and their infrastructure. Looks like Rinkeby is working like digesting blocks right now which is awesome. We'll see this pull through and once it does, I'll show you that the value here should change to the string that we just sent in that says Hello Dev DAO. So we're kind of just like in this waiting period now.

Narb

So yeah Derked, I don't know if people have clarification questions. That was probably a lot of information relative relatively quickly and I can go into more detail or anything, I'm just trying to give like an overview. Yeah, yeah, no this is excellent man. I guess from my end, what main use cases do you see LayerZero kind of solving? Like from your brief experience of like playing around with it. Like what do you think it'll solve?

Derked

So I think that there's probably like there's a couple obvious ones. The first couple would be like cross-chain swaps or like basically cross-chain liquidity. So I can take a native token, so say like native Avalanche, and I can swap to USDC or like any stablecoin that's supported by Stargate, bridge it across Stargate with a payload, and swap it to whatever destination token I want on another chain all in one click. Oh, dang. Or I can also enable like cross-chain NFT marketplaces. So if you have like money on again like another chain, you can buy, you should be theoretically able to buy an NFT on another chain by communicating with the destination smart contract if they implement the receiver. Okay, so cool, our message is coming in, it's indexing, so this should show up in probably like 20 seconds. Hopefully, and you can see sort of the power of cross-chain messaging.

Narb

And is it only compatible with EVM chains or is it like any chain?

Derked

No. Well, right now it's only EVM chains, but you will perhaps some folks noticed that the source address here is taken as a bytes array, and that's for a reason. That's so it remains flexible for receiving messages from non-EVM chains where the address may be more than 20 bytes. Oh, gotcha. Cool.

Narb

We do have a question in the chat. So BowTiedHeron says, 'Looks very cool. Could you give us a bit about your background and how you learned how to work with LayerZero? Are you using it in a project that you're working on?'

Derked

Uh, so I am using it in a project. Can I just real quickly? Oh of course, of course. Show everyone that the, so you can see that our message came through on Rinkeby. Nice. So we've successfully received the message from Optimism and set our variable or run our logic on Rinkeby. So yeah, my background. I am using it in a project. I have like a math background and then I've been doing like traditionally I worked at like a bank doing like stats and like stat finance for a while and then I've been doing smart contract programming for a little over a year now. And yeah, I'm just sort of like exploring this right now at the moment, LayerZero and stuff, I just saw it on, I just kind of like keep track of new projects and try to like play around with them, and I thought this was a nice sort of like a nice demo type of project.

Narb

Yeah, for sure. I mean, from your experience, looking at the docs and stuff, would you say it's like easy enough for somebody who has like who's been dabbling around with like the Ethereum smart contracts and whatnot to be able to pick up or would you say it's a bit of a advanced topic?

Derked

It's a little bit past like beginner, definitely. I would say that you sort of have to understand if you have experience doing like cross-contract calls and stuff like that, it shouldn't be too much more complicated than that. And I'm sort of moving very fast here and giving like an overview. I actually don't know if my DMs are closed or not, but like you guys can just tag me in like the solidity channel. I'm like pretty available there, I'm like happy to help anyone like get started. I do suggest that you have like you know how to use Hardhat or whatever infrastructure you like to use and you've written some contracts before. But other than that, their docs are actually quite good. So like this is the LayerZero docs and if you go to the LayerZero docs, it tells you you know you should receive you should implement this `lzReceive` message and then you can kind of do, you can essentially do anything you want in `lzReceive`, right? It's just basically what it is, it's the router or their endpoint is just calling your contract. So what you do in `lzReceive` is completely up to you. There aren't really any rules. I was just showing like a wiring example basically about how you can send arbitrary logic cross-chain and then sort of do something.

Derked

I guess it might make more sense if I explain it this way. In this wiring example, all I'm doing is either calling set one, set two or set three and just like storing some variables. It's not particularly interesting, right? Like it's not like a real world use case. But you can imagine a scenario where I send something, I swap some token and then I want to do some token swaps and I want to like move some tokens around or I want to execute like a sort of like mission-critical function to whatever I'm doing and you have that cross-chain interoperability, I guess.

Narb

Yeah, that's dope. So I just make I don't know if you probably already went over it before, maybe I missed it. So with LayerZero I can I can buy any NFT across any chain, like is that what you're saying?

Derked

You could do that. Yes, that is something that is a use case. But the marketplace contract has to be developed. Basically like the technology is there to do that, but it's not like you could just plug that into like an existing marketplace paradigm right now. It has to be built.

Narb

So it has to be built with LayerZero. The marketplace has to be built with LayerZero in order for me to do it.

Derked

Yes, and I think people are definitely working on that. And I know people are building that kind of thing. And there are definitely already cross-chain sort of they call them omnichain quote-unquote NFTs. Like there's this project called like Ghostly Ghosts and basically like you can like move your ghost cartoon character from like chain to chain and you know it basically it just it transfers your NFT from one chain to another chain and I think your character or whatever, like their attributes or their picture, like changes slightly depending on what chain they're on. So it's a little experimental right now, but it's pretty cool.

Narb

Does it come with a lot of friction? Is it harder to move to this chain than it is to move to that chain? You know how it is with like Ethereum, Solana, all those types of things.

Derked

In my personal experience, I've primarily just used Optimism mainnet and Avalanche, and I haven't had really any issues. The infrastructure usually takes about like two to three minutes like end-to-end. Cool, thank you.

Narb

Does anybody else in the crowd have any questions for Derked? We're around seven minutes left. I have yeah, I have more than one. Oh sorry, the question kind of broke up towards the end. Could you repeat? Okay, maybe not. Oh, I think they said they're going to move down today. Oh, sorry, I think somebody's unmuted. Hi guys. Hello. Yes, we can hear you. Yeah, so I have one question. Is there any way to know from the source chain that the payload has been executed in the target chain?

Derked

Sort of. Like, I get what you're saying, you're talking about like failed transactions, basically. So what like if `lzReceive` fails. Yeah, for example, if we need to. Yeah. So that's actually a really good question and it's something that I've been reading about lately. So you really don't want `lzReceive` to fail because the way that the router or the way that the router is designed is that it uses like a queuing system. So you can kind of cause like a traffic jam if `lzReceive` fails. So the devs, if you think that there's any chance that your `lzReceive` function will fail for any reason, the LayerZero developers recommend using this paradigm, I guess is a good term for it, that they call a non-blocking receiver. So what you can do is you can use sort of, you can see it on my screen, you use a try-catch functionality in solidity where you try whatever you want to do and then if you if it fails, you do whatever sort of fallback logic that you want. So like this is also especially important when you use Stargate because you don't want funds getting stuck in your in your contract. So I was thinking more about like event cross-chain, like for example just to know that the you know as you know how events are triggered. Is there anything like cross-chain events with LayerZero or it's not something available? I'm not talking about failed execution, but more about events.

Derked

So I don't actually if you're talking about the LayerZero infrastructure itself, I actually don't know that answer. But you can always obviously write like your own events on your receiving contract and your sending contract, right? To just emit when you receive a message. But as far as the LayerZero infrastructure goes in terms of like emitting events, I actually don't know. I see what you're saying though, I think that could be that could be helpful so you can kind of. It can be interesting to have it, but yeah. Okay, thanks. Great questions, great questions. Coding with Manny asked you, 'Can we expect a LayerZero written series from you?'

Derked

So candidly I'm not like a big content producer. So if I had more time maybe. I do have I have spoken with the LayerZero devs like themselves and I know they are on the verge of releasing like a GitHub in like a bunch of like examples for their docs. So I would definitely check that out and I do have a GitHub with all this stuff here. It's just called LayerZero Playground, if you want to go in and read and play around with it. Although to be fair, it's not exactly like a written series. Gotcha. Closing in on time here gang. Are there any final questions here for Derked? So I do see Metas. Yeah, that's what I was going to ask. The message value itself, if you know about that.

Derked

Okay, so there are two sort of I guess like value things going on. The value, like the native value that you send with the transaction, like this right here, this is just used for gas and I don't actually I don't know how LayerZero itself does like that process of converting your native tokens on one chain to like the gas fees for the other chain if that's your question. But if you're sending through Stargate, it's a basically it's a bridge. It's like a burn and mint mechanism for using liquidity pools that are based on Stargate. Okay, so to actually like do any sort of token swap cross-chain, it's not really built into LayerZero itself. For Stargate, yeah. Yes. For the for the gas for like the native gas fees and stuff I don't I don't 100% know how they do it. Okay, yeah, that makes sense.

Narb

Sweet. Yeah, sorry, this went way faster than I anticipated. So sorry, I had a bunch of other things to go like planned also, so and it was a little fast and probably just like a very brief overview. But again, just if anyone's interested in diving deeper, I'm totally happy to answer like very detailed questions through the solidity channel or like DMs. Excellent, excellent. Yeah, I mean we're at time here. So I'll stop the recording, but everybody here is welcome to stay on if everybody still wants to to chat and and mingle. So with that, thank you very much Derked. This was an awesome overview. I really appreciate it. We all really appreciate it you coming on and showing us. Unfortunately, POAP-wise I still haven't heard anything from poap.xyz on the POAPs I requested yesterday. So stay tuned, I will share those when they come in. But with that, thank you very much for everybody for coming in to another great DevNTell. Wish you a happy Friday and a very wonderful weekend and we will catch you back here next week. All right gang. Cheers.

Listen On

Resources & Links

Share This Episode

Share on X

Watch Episodes Live!

Subscribe to our event calendar and never miss a live episode.

View Event Calendar