New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Mastering Cross Chain State Retrieval with Matthew Krak - ETHGlobal Bangkok 2024

ETHGlobalThu, Oct 9, 2025, 12:00 AM

Join us for a workshop with Matthew Krak from LayerZero titled, "Mastering Cross Chain State Retrieval." In this workshop, learn how to use LayerZero V2's interoperability protocol to enable seamless cross-chain state retrieval. The workshop will demonstrate how to use popular LayerZero design patterns for nested and iterative calls across multiple chains, enabling complex state synchronization and interactions. ----------------- Follow us: https://x.com/ethglobal Check us out: https://ethglobal.com/ Email us at: hello@ethglobal.com

Transcript

[Music] uh today guys we'll be going over layer zero read it's a new data primitive that layer zero has built for reading on the Chain State uh a little bit of background my name is Matt I'm a developer ecosystem lead at layer zero Labs you can follow me at uncrackable on X or Twitter uh in the meantime uh if you haven't heard of layer zero layer zero to date has done over 135 million main net messages we support 94 different main net blockchains have totaled around 55,000 application contracts built on the protocol and account for around 70% of all cross-chain value transfer why do so many apps use layer zero well layer zero is a global Ledger for permission lists censorship resistant immutable Bridge infrastructure at a high level layer zero is an immutable interoperability protocol that enables any application to send tokens nfts kyc authentication any type of data that you can imagine on CH from a source blockchain to a destination blockchain giving you the application developer tools to configure the type of security that you use to Route these messages and the type of execution that you need to deliver these messages once they are verified and reach the destination chain uh at a high level though let's zoom out a little bit from this perspective bridges in general uh are focused about one main thing connecting isolated groups of people and more importantly the information that they hold using this information we can coordinate the exchange of goods and services and Via that exchange of goods and service we can coordinate broader more financial use cases around that asset transfer this is no different for blockchains blockchains each have their own realities about each isolated Network some are better for Speed cost security Community different types of app use cases Etc and we see this becoming more prevalent in the way that developers build on chain more and more developers are moving to a cross-chain environment but one of the realities of cross-chain development is that it's not like a single block blockchain only when we look at a simple chain itself like Linea for example in erc20 a asset transfer from one user to another simply involves an atomic transaction executed once that debits users from one account in the form of a mapping and credits those tokens to another user as soon as you move to more than one blockchain environment this immediately becomes more complicated suddenly I have to figure out how do I ensure state gets executed on a source blockchain and then relay that state change over to the destination to credit or Target some amount of State change as a reaction to the initial transaction I made all without getting rugged or destroyed in the middle of it it's a very difficult problem and has led to millions of dollars of losses in the space uh historically the way that we solve this problem is with a concept of Monolithic Bridge security the idea that one centralized party in the middle whether that's literally just a central party uh going in routing orders cross chain where that's a collection of permissioned validators or a middle chain in the middle between these two blockchains routing messages determining how they are valid and then executing them on destination uh regardless of the security that you choose the main problem that we have to think about is what happens when that security fails well not just your application but every application using that validator set is now affected and historically this is the reason why monolithic brid security uh exploits account for four of the five largest hacks in the blockchain space uh and if we look at the developer trade-off that we're faced in bridging you now forced to pick essentially as an app developer between two points on the security cost curve either inherit weak security guarantees that have low cost for your average user or high average cost for a gained amount of security obviously reality isn't as linear as this but I think it Paints the picture nicely that you as an app developer are now forced to pick between two realities for your security when you use bridges on any blockchain network uh beyond that layer zero was built to basically solve this problem of forced uh selection of bridge security at a high level layer zero is comprised of an omni chain messaging solution where any application can interact with a layer zero endpoint and configure the type of security that you want to use to relay messages from chain a to chain B both in terms of the type of security you use the number of validators in the set and also what the threshold of security is for that validation on top of the actual messaging we run executors that anyone can either establish themselves on an app use case or opt into to deliver gas automatically for the user on the destination chain once a message is verified and finally the most important part for hackers smart contract application standards that enable you to easily build cross-chain applications like O app for moving any raw data between chains on the Chain funable token or oft for erc20 transfers between chains and the on the Chain non-fungible token for ERC 7 21 what this gives you as a combined set is the ability to not have to choose between point A or B on this cost curve but instead choose any of the points in between that fits your application's use case and as new validation techniques come online permissionless update that validator set depending on what your application needs or lock it in depending on your access controls within the application itself beyond the security the other benefit you get from using layer zero is this ability to gas drop from a source transaction the ability to after a message is verified be able to execute it automatically for the user on their behalf on the destination all while giving them the destination gas token that they need either directly in the call you're executing on destination or just dropping it directly to an end users's address so zooming out a little bit I told you a lot about what layer zero is but not a lot about the topic of this Workshop how do I actually read on the Chain data and state let's dive in a little bit deeper and understand what the protocol is actually doing under the hood to enable this at a high level the way that layer zero works is that an application will construct a send message by calling the endpoint send method this message can be comprised of bytes and can hold any type of data that you want as an application you'll provide this message a destination chain identifier also known as an endpoint ID and then a configuration about what dbn's uh decentralized verifier networks and executor you want to use from there the dvn will go and verify that message using each their own verification schema internally and deliver that verification to the destination chain on chain your application is enforcing that this verification is valid based on the parameters that you've set in this Library configuration from there an Executor can automatically deliver this message to the actual method endpoint dolz receive which gets routed through to your internal applications version of that call uh what this enables is you to basically control the send process validation and then execution of your message that at a high level is what we're doing one of the popular ways that you would bring data back from a destination Network to your Source chain is using a concept of this ABA message pattern what an ABA pattern basically is doing is saying within the receive call execute another send call back to the sending chain so from this model here we had Shain a sends a message it gets verified and delivered on the destination and within the destination call execute a return call back some of the obvious problems with this design pattern are that one you're paying for fees on both sides two you're waiting for execution to finalize on both chains and three you're ultimately just trusting the same internal party for both these transactions and so at layer zero Labs we thought well what if we could streamline this process and make it easier for you to actually receive the target data that you want on a destination chain all without duplicating the transaction cost that's why we built layer zero read layer zero read at a high level is the ability for you to basically have that same workflow to find an origin chain that you're deployed on select some Target data chains to read from and then using your security stack and the executors fetch that data from the target data chain and then bring it back to the source that you want to send from the way that it works is exactly the same as normal protocol spec an application still creates a send message via the endpoint contract it's the same endpoint contract that you use for normal cross-chain messaging the only difference is that now we have a read message library that basically constructs the correct message to emit out to your dbns and the dbns are all running a node to fetch specific state from each of these blockchain networks at a closer look the only difference in terms of messaging is that an omni chain message delivers the same message from The Source chain onto the destination and on the Chain read generates a request that you submit offchain and then that request gets a response that returns back onchain and what's really cool about this is that it changes the normal protocol spec from this cross chain message delivery service to this ability for your dvns to go and look at a Target contract at a specific block number or time stamp wait a specific number of confirmations that you set and then access any call data from that Target contract the second cool thing is that after you actually generate these requests you can then create compute logic so using functions that I've defined in my application LZ map and LZ reduce I can then manipulate the data that's being requested and return something completely different back to my verification ultimately to have it executed back to my contract so what this ultimately builds to is this ability to essentially initiate a request from any chain and bring specific State back on chain that you want the example that we'll be going through today is actually fetching live uh Unis swap e to USD pairs from multiple different blockchains aggregating that price off chain using our dbns and then bringing it back on chain as a request uh for this Workshop if you go ahead and scan this QR code it'll give you a link tree link this link tree will have links to the example code that I'm using uh to the relevant documentation about layer zero read and then also just more information about layer zero in general cool we're going to jump into the code example and I'm going to show you exactly how to get started I have two windows pulled up here one that I've already deployed and then one that we can base our contract off of the first thing that we're going to do uh is go ahead and use layer zero clii tool to get started uh the simplest way to do this is running uh LZ enable read example equals one in your environment and then this command uh npx package create LZ up at latest what this command will do is basically create a prompt for you to build a new repo in this case I've already created one but I'm going to call this uh my read 2 it'll give you a list of contracts that you can start from there's of oap on uh some upgradable variants if you want in this case I'll do oap read this is the one that has all of the read contracts installed pull all the uh dependencies down that I want to use etc etc in the meantime what's happening in this contract is that OA read is just a simple example contract uh based on this standard OA read here uh OA read you can find once these dependencies get pulled in in node modules but in the me time I actually have them pulled up for us here already we can go ahead and bring this contract up I'll actually walk you through live really quick how this Unis swap V3 quote demo works so at a high level I'm going to pull terminal down really quick and just get all this stuff sorted perfect the Unis swap V3 quote demo is just an extension of OA read uh oap read itself is just an extension of oap the base cross-chain messaging contract the only difference that we're actually doing is setting up one function uh a read channel the way that read works is that you can define specific messaging channels that you want to send to and then use them for constructing your data requests and compute logic uh in this case looking at this contract I've defined a couple main constants uh first just a chain config with the Unis swap B3 quoter address for the Target chain that I'm looking for the token in address for rapde the token out for usdc and then also just defining these constants here directly the contract you can see here that each chain has an endpoint identifier that I'm using uh in the Constructor of this contract I'm setting up the layer zero read channel the channel ID is just an arbitrary identifier for where I'm going to be sending to and how I'm going to be reading from and then for each of these chain configs again just pulling all of the relevant Unis swap V3 pool contracts for I quer V2 and setting them up here uh the way that this read average Unis swap price function works is that we're essentially generating a read command and then passing it to the normal layer zero send method that you would use the only difference being we're passing the read Channel as the endpoint ID and a command as opposed to our layer zero message under the hood the way that git command works is that is a standard function that you want to set up within your application this git command will basically Define the number of call requests that you want to have executed cross chain uh in this case I'm just using the number of pairs that I've been putting three and I will define basically as call data uh the type of prams that I want to essentially use as call data on these Target networks uh here I'm using the I quter V2 from Unis swap V3 quote exact input single prams I'm encoding this as call data for uh this function selector and then generating in this for Loop the number of read requests for each of the chains that I want to read from uh under the hood the second thing that I'm doing is defining a compute request the compute request is basically how am I aggregating all this data once it's returned uh there are a couple compute settings if I do for example compute setting zero this is LZ map if I do one this is LZ reduce uh let me just fix that not LZ meow and then two is LZ map and reduce uh again you can look for these function methods wherever you like in this case we're looking back on the same chain at this uh address the function LZ map is basically deconstructing for each response what is the actual output so in this case we're getting the response from the Unis swap contract of four main return types the only one that we care about is the actual amount out that's being returned and then LZ reduce is defining once we have all of these aggregated responses you can see here we're getting a bytes array of call data for responses we're taking all the responses that we get from each of these chains aggregating them and then just doing a simple uh average to basically get the average amount out from each of these networks and then finally the only thing that I'm getting back in LZ receive is what is produced from the ABI encoded argument here in LZ reduce uh delivered to my contract on chain so high level walking through this again we construct in the send call a command that command is comprised of both the requests for data that we want to go get on different Target networks the call data for those chains and the actual address that we're coding from and then some compute settings in this case looking back on the source chain for LZ map and LZ reduce to go and aggregate this function logic and what's cool is that these functions will get executed off chain bar dvns that we've configured and then only this final ABI en code from L reduce will be delivered as a message to L receive so a lot I kind of went over there let's actually look at this kind of an action a bit more we're going to zoom out here I'm going to pop into arbitrum uh first I'm going to query this uh quote price for the transaction I'm going to do in this case in way what I'm going to do from here too is then grab this we price go to Ether converter just to simplify this process of bit punch in the conversion The Ether then go to write contract read average uni swap price throw the amount in don't have any extra options that I want to add we'll write on the Block Explorer this call hit confirm and what'll happen here is it'll generate this transaction on arbitrum for us that got confirmed basically in instantly and then we can go into layer zero scan and actually see what's happening under the hood here inside layer zero scan this basically just serves as a record of all transactions processed by the protocol uh it might take a a second for it to index and come through here we go uh you can see that we have a request transaction hash from arbitrum and we're waiting for the response transaction to land uh you can see here that we have a request payload that we generated there are some decoding instructions that you can follow from the documentation about how to decode any incorrectly constructed calls that are made here uh but what'll basically be outputed back on Shain while we're waiting for this one to land is the aggregated price from all these different networks that we're reading from uh one of the things that I've done here as a transaction already is reading the average price earlier uh I think two or three days ago we can basically see here that uh we're getting executor fee paid dvn fee paid on the send side and the packet sent event and then from the overview section here if we go and add this transaction to tenderly this one's already been indexed we can see the destination here open it up go to our logs and we can see the aggregated price for eth uh a few days ago was 3307 looking directly from all of the Unis swap pools that we have uh configured so what this basically enables you to do is take data in the form of a call data request that we saw here uh aggregate from that call data request uh the responses and return values that we get on chain from any type of view or per pure function or public State variable that's on a destination blockchain and then use the LZ reduce and LZ map methods to basically take all those requests that we want aggregate them down to one final response payload and then deliver that response payload to LZ receive so yeah I hope this gave you kind of a good overview of how layer zero read Works uh now open any questions you guys might have about the protocol about cross chain messaging read messaging things like that [Music]

Automatic transcript — names and jargon may be misspelled.