Lib Evm Mantle - Master the Art of Slurping Yummy Data with a Single Query
ETHCC 6 · 20 Jul 2023 · 13:41
Lib Evm of Mantle shows how a frontend can load all of its non-historical onchain state with a single RPC query. The talk starts from parallel queries, moves to Multicall3 and its limitation with dependent data, then demonstrates how eth_call, a little Solidity and state overrides can fetch dependent values such as a vault address and balance in one round trip. The talk closes with a proof of concept, practical caveats about RPC provider support, and a short Q&A on contract size limits.
Speakers
Topics
From ETHCC 6, hosted by Ethereum France.
Transcript
foreign [Music] hear me all right perfect uh welcome to slurped Master the art of sloping yummy data if that doesn't make sense to you uh don't worry I'll explain it uh what it means in the next slide so one query so one query is all you need to populate your front-end data that's what I'm claiming to to present to you today so for all you front-end developers out there who have done any State Management you will notice that this is a very difficult task because you oftentimes if you want to query data
from ethereum you have to query it multiple times and oftentimes it's latency there's delays you don't know when your data is going to arrive and I'm here telling you you actually can query any kind of data any kind of structure however long in one query so a little bit about me I'm a developer relations at mantle.xyz a new Layer Two those are my GitHub Twitter and a website if you guys want to check it out so the number one thing for adapt does anyone know what it is it's the front end that's the num that's the thing that users interact with that's the thing that you that your user sees you know no one really cares about
the back end as long as the front end is clean and sparkling uh that's all it matters from a uh from a user's perspective and from uh the co-creative react.js you know Albert Einstein once said you know front end bugs are like the bane of my existence so today I'm going to tell you how to avoid um all those problems we'll take a very simple case study of the uniswap front-end where you as everyone here I'm assuming is familiar you would swap some tokens to swap some tokens you would input x amount of tokens in and you might get x amount of tokens out but behind the scenes is actually quite convoluted
there's a lot of data querying going on behind the scenes how many if am I inputting in how what kind of asset am I trying to trade from how what kind of asset am I trying to trade to how much of the asset am I am I going to receive what are the balances of the assets that I have uh and and what are the price impacts um how much slippage will I incur and then would I have enough if or would I have enough of the asset for the bond to appear so all these are crucial information that you have to extrapolate before you can before you can kind of create this clean UI so a very simple approach on how you would do that is actually create a query them individually you would call let's
say I would get the user's address and after I get the user's address I can get the user's balance after I get the user's balance I can query the die balance or usdc balance and then only afterwards can I query how much die they will receive given how much if they're willing to put in so this is a very simple approach this is a very uh ineffective approach it blocks each query is blocking because you're awaiting it one of the more naive ways to make this quicker is to wrap it in a promise where you would call a weight promise.all and you would get everything in parallel now the problem with this is that you're still using three queries but you're
querying them in parallel I'm here telling you that you can get everything down to one query regardless of the complexity of your uh of your of your queries one query is all you need so can we do this in one query well as we can with multi-called three so multi-call 3 is actually start it was originally started by the folks at make a Dao it was named monthly call one then multi-call two and then some other guy some other Twitter guy which I forgot the name of extended it and made it into multi-call 3 which is now deployed on I think 100 plus chains all on the same address so if you would like to use multi-call 3 it is very very handy
because they're all deployed on deterministic addresses the essence of multiculturally is that it is a smart contract behind the scenes normally if you query some kind of data if your user would like to use adapt your user would talk to the dab and then your adap would kind of query the blockchain for example the balance of its die or the amount you would get from uniswell or the balance of its Eve so each time you query it that's one separate query with multi-call 3 you actually call the multi-call smart contract on the blockchain which then does the querying for you now because the state of the multi or uh the multi-call contract is located in the same location as all your
contracts you can actually just do one query to the multi-call contract and you would receive all your data so this is a an example of how you would do it with multi-call 3 you would first uh you would the issue with Multicultural is that there's a lot of encoding and decoding um if you're using unsupported libraries such as etis to to encode your data so it's quite convoluted um you have to first uh encode it in the right structure and then once you get the the data the data from multiculture you have to decode it up now luckily a VM I don't know if anyone from you from here knows what VM is it's
a new JavaScript library from Paradigm it actually supports multi-3 encoding and decoding out of the box so right off the bat if you would like to use multi-call 3 you can actually use VM from I think Paradigm to to paralyze all your query sorry not to perils but to but to make all your make your parallel queries into one query now multi-call 3 is good but it's not good enough the issue with multi-core 3 is that it doesn't support dependent data and what I mean by dependent data is that it doesn't support the situation where your inputs to the query is dependent on a previous query so for
example in this I'm getting a vault address for some kind of yield Optimizer and then I want to query The Vault address uh the balance of my uh the the my balance in the vault so I first need to get the address out from some some registry and then put that address into the second query so this is the concept of dependent data multi-call 3 doesn't support that however I'm here telling you that you can actually achieve that with if call and a little bit of solidity Magic so if everyone here I'm so people who are not familiar with the eve call uh up if call RPC uh call it's
actually very simple so it's actually a Json RPC call that you use to query the ethereum blockchain so be on the front end you might get you might call like if you're used to some JavaScript library you might call die.balance off but in the back end what's actually happening happening is that it's calling the provider and it's using the eve call Json RPC to extract the data from from the blockchain now traditionally if call is used on read-only functions so for example balance off or decimals uh or owner so traditionally that's what it's used for but a little known fact is that you can actually use if call to query mutable
functions so one example of this is how the unisaw quota actually does this it actually the units of quota by by default is actually a mutable function meaning that if you called it from its standard notation it would change State and you would actually have to use gas to call it to retrieve some data but you can actually use the eve call functionality to to get some data from the quota and that's how the unit swap peripherals operate so that's cool so we can read on write functions so what what's so cool about that okay hear me out
what if we hijack solidity and made it return data in the Constructor and write the data extraction logic in the Constructor then we use Eve call to perform read-only contract deployment to extract our data I'll just I'll just let you think about that for a second so what if we hijack solidity you know what if we hijack solidity made it return data in the Constructor and wrote the data extraction logic in the Constructor we then use the eve call to perform a read-only contract deployment read-only contract deployment the extracted data that we want okay I I hope I haven't confused anyone
here but just to recap what the dog said we would want we won for first we forced The Constructor to return values by default and Constructor and solidity doesn't support returning values we write out the business logic in the Constructor and then we use if call to quote deploy a contract but this deployment is read-only and you don't actually deploy the contract you're just deploying it so that it extracts the data and you're able to uh you're able to uh sorry you deploy the contract so that it performs your business logic to organize and transform your data which you then can extract
so how do we make a Constructor that returns these values to do that we actually need to write a little bit of an assembly of magic So In traditional in this in this Constructor okay my cursing is not appearing we first have to create some kind of API sorry where you first have the ABI encoded and then do some kind of assembly magic to return the data because traditionally solidity doesn't support returning data I'm just going to gloss through this so once we have it Constructor that Returns the values we can write our business logic in between to extract the data we can then use if call to deploy the contract it's not really a deployment because it's a read-only function and
but once we deploy the contract we actually have the data that we need and that is how you can query anything you need from the blockchain in one query regardless of the complexity of your your data set of your of your query so you don't have to do parallel queries you if you want to query ens you want to query multiple enses you can do in one query I know there's an aggregator online if you want to perform some kind of convoluted dependent um query data dependent query you can also write it in solidly use the eve core operator to force it to to return some values and you can have everything in one go that way your front end doesn't have to manage the complexity of dealing with uh updating certain kind of
data sets for example if the price updates what's my health Factor if do I have enough balance in my wallet you can just do this in one query very cleanly um some parting remarks is that you should read the eve call docs because it's actually really powerful there's an additional parameter which allows you to overwrite the state so what I mean by that is then you you might not even need if you so this um state overriding is only supported on certain RPC providers it's not supporting all RPC providers so you could overwrite for example if you would like to do some uh some kind of analysis some kind of searching and you're too cheap to deploy a contract you can override the contract code on this
particular call it doesn't actually override it on chain but it will override it for that particular call you can also override account balances or even storage slots if you know what you're doing additionally for anyone who runs their own node GAF has a graphql interface just thought I'd point it out it's very very handy and uh yeah thanks for listening to me ramble about querying if you guys have any oh and there's a there's a POC it's on my GitHub github.com evm slash Eve call abuser so if you would like to check out the POC on how you can retrieve data in one query please do check that out apart from that um I'm
open to any questions thank you [Applause] all right there is time for one question if you want so if anybody wants to ask if any is there a size limit or how much logic can we actually fit in there um so it's the contract size limit I'm not sure how big it is but sorry plenty of space plenty of space yeah awesome thank you foreign [Music]
