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

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

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 un swap e to USD pairs from multiple different blockchains aggregating that price offchain using our dvns 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 uh contract off of the first thing that we're going to do uh is go ahead and use layer zero CLI 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 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 too it'll give you a list of contracts that you can start from there's of OA on uh some upgradeable variants if you want in this case I'll do OA 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 oap 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 meantime I actually have them pulled up for us here 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 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 in 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 get command works is that it 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'm 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 pams 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 edra 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 quoting 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 and 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 way price go to Ether converter just to simplify this process a bit punch in the conversion ether then go to write contract read average Unis swap price throw the amount in don't have any extra options that I want to add we'll write uh on the Block Explorer this call confirm and what will 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 Shan 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 SW 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 resp payload and then deliver that response payload L receive so yeah I hope this gave you kind of a Goodview 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 sure yep sounds good yeah yes oh I I have to

Automatic transcript — names and jargon may be misspelled.