# Escaping the Multicall of Madness | Marco Kaul (May 2022)

- Channel: [Berlin Ethereum Meetup](https://streameth.org/berlin-ethereum-meetup)
- Date: 2023-10-07
- Duration: 21:59
- Watch: https://streameth.org/watch/yt-oukgzLoMsB0
- YouTube: https://www.youtube.com/watch?v=oukgzLoMsB0

## Description

Join us on Meetup to keep track of our events in Berlin: 
https://www.meetup.com/de-DE/berlin-ethereum-meetup

See you at the next one!

---

Apply to speak at our future meetups: https://forms.gle/bGXFc83MHAcnmMQM6

---

Twitter: @BerlinMeetup

## Transcript

hello everyone I will presenting escaping the multicolor Madness maybe you know multicol it's a contract that you can call and executes multiple like transactions or if calls for you I will explain more of this later in the talk but first a bit about myself so I'm already toxic myself so I'll take off the classes and yeah hi I am Marco I am a footstep developer I do mostly front-end work a bit of back-end work as well and I like to call myself the front-end developer of the back end which means I like to do API design doing like performance caching strategies and so on and this also includes when you access like data from the chain because maybe you know that it's very hard to get like bulk data because you can only always execute like one function um I'm a Serial hacker from enjoyer which means my like ads mostly like all the big hackathons like if Berlin you see the picture there we were doing our society we won like the grand prize and our project was basically just redistributing the price money across all the participants of the hackathon which was really funny and people liked it they voted for us it was like a publicity vote um you can follow me on Twitter um on whatever lens Mastered on Blue Sky forecaster everything that you want and let me show you a little bit my projects that I'm working on so my main my professional job is Astoria it's an nft learning protocol so maybe you know it like there's no blur doing it Finance announced that they're doing it they're like a bunch of projects um we're doing it a bit different so what we're doing is we do fixed term borrowing which just means like the rate the interest rate and the length of field and Loan are fixed so you know exactly like when you have to repay it and we have a what we call a three actor model so that means next to the lender which usually uh defines the terms so how much they want to give away for how much interest and the borrower who provides the collateral like an nft about app or something we have a third party which is the strategist and the strategists they are just generating the terms and competing with one another for liquidity and where you can read more at the docs and our public beta is live as of like three hours ago so if you have a blue chip nft and you want some some money then you can provide us as a collateral and if you have liquidity like some money laying around some Eve come talk to me we want liquidity professors my other project is very we're doing infinitely scalable Raffles which means if you do like an allowless giveaway or an nft giveaway or something with where you need more than one random number we have like a very nice contract a very nice algorithm which is basically like super cheap and you can do like like from a million participants you can have like hundred thousand winners or something and it's basically the same as if you were raffling for one winner it's really cool you can also read more about the technicalities at docs.ferry.dev and talk to me if you want to do some giveaways or some randomness so The Talk itself let's talk about finding your if calling uh small joke so how do you get data from the chain well we have the standard way that everyone knows so here in this example I'm I want to display like how much usdc or how much of any token um a user owns and to do this properly we have to do three calls we need to get the balance first so that's the usdc balance off or with some address as the parameter then we have to get the decimals because while ethereum has 18 decimals and most tokens do usdc for example has six so we can't just put 18 as a fixed um uh constant there but we have to get it from the chain as well and then we get the symbol as well so we can display it and when those three chords are done then we can display so this is probably how most people know it and how most like amateurs do it and the problem here is that we have three API cores three requests which are basically like waiting for each other so you have one request then you have the next request and you have the next request that's what we call a network waterfall and it looks really bad and it's not a good user experience so we have the next level which is parallelization so in JavaScript we have this promise.all which basically can fire like multiple asynchronous requests in one and then they are running at the same time so that's much better experience than waiting for one call but we're still doing three calls here and wouldn't it be nice if we just could do one call which includes all three in like one request right so we have an honorable mention here which is the Json RPC batch Provider from ethos you don't need to know about this and I don't know if you should really use it but basically it's taking multiple calls to the ethereum node and it just batches them together if they are happening like same JavaScript event Loop don't need to know about this that basically does magic stuff so that your requests are a bit more optimized the problem with this is that your requests can still happen in different contexts so for example if there's a new Pro block coming in and the nodes starts executing like one request then there's new block then suddenly you have a different context for your request so we don't want that we want all our calls in the same request so that's where we go next which is the multi-call way which is like the standard I guess for getting like batch data from the chain so what we do here we use a library called ifas multi-core and we basically do the exact same thing we where we instantiate the provider and then we do a multicoid provider.org so instead of a promise.org we do what we call provider and what this does it's like sending the stuff together as basically one request instead of three different requests it's one request that contains all of them so how does this work freaking multicults how do they work so there's actually a contract deployed on I guess every evm chain that exists and it's always at the same address it's this 0xca11 well if you read it it says call and it's at the front and the back it's really cool and what this contract does basically it takes multiple call datas so call data is like the request what you want from the chain which function you want to call and it combines like as many as you want until you hit the gas limit and it combines them into one call and then it gives you like all the data can also do some cool stuff like if one of them fails you can tell it that hey it doesn't matter it fails but please continue anyway and you can also get some like data in the call so the block number for example so you can do caching so good I see you and you're like yeah we already know this so what's what's more like what else can we do you know like what about if you have dependencies so in this example we're using ens and we want to resolve like the primary domain of an address so what we do here we do some like magic to get a node and then we get the resolver of the node so this is the first request and we can't do any multi-call here because it's one request and before we can do any other requests so in this example we want the name and the other top for the user so the ens name something.if and the Avatar which is a ipf SQL for example but we can't do multicore here we can do like a name and Avatar multi-call but then it's still dependent on the other so that's weird and with like multi-colors nothing you can do about it and you have to write the water for you can do some optimizations but it's not like great so um I've actually been working with the flow blockchain for a while we're talking about different blockchains here which is using Cadence so their own language and Cadence and the flow blockchain they have a really cool feature which is called scripts so scripts basically are code that you send to the access node as a read call and then it executes like actual code like in the fvm context so in this example I'm using find which is the ens clone on flow and here we do the reverse lookup to get the name and then we get the profile for the um of the address which is like the general profile where you can get like Avatar name email and so on so hey we can do this on floor why can't we do it in ethereum right so how do you address in ethereum if you want to get like more data than just one function call well you have to do it like multi-call you have to deploy a helper contract so you take a contract you write the logic you want here so this example is um like an ens helper which basically gets you like for an array of addresses you get an array of ens names so kind of what we did before and they deployed it on this address only if you're a minute and well it works so it's nice but you have to pay money to deploy the contract and every time you want to change your helper you have to redeploy it and you have to pay money again and only if you're a minute that's bad it's very expensive so like what else can I do well we have if call right like what we've been using before and with ifcall you can also simulate contract deployments so why not just put our whole logic in the Constructor and return it and that's good well contracts they just can't return stuff I don't know why maybe someone can explain it to me but contracts in solidity can't return data well or can they because we can do a little bits of assembly magic and basically force it to return data and this works by where we first encode the data so that the types are really nice to work with and then we do this assembly magic here which is where we do 0x20 at to the address of the encoded data which is basically getting rid of some like header of the returning object or the returning memory then we just tell it hey just return like the whole memory from when we encoded the data so just return the actual like stuff the actual memory and even though like contracts don't support it natively with this we can just return anyway and the nice thing is that if you look at the front end it's working exactly the same as if you did like a regular call so in this case we um instantiate the HEPA Factory or the contract Factory which basically just preparing like the code that you can send to the chain to actually deploy the contract but we're not like doing a transaction so we're not actually deploying it we're just doing an if coil which is simulating basically and then we get some data which you can then again decode so we know that here our return type is an array of strings and here we just decode the area of strings and we get the actual strings so hey we can do onshine computation nice so well now you think okay but this is a bit weird because I have to know my types and I have to always like write a contract first then I have to write a front-end so I need to keep the types on sync well actually you don't because we already have typing Solutions in front end so API type for example is being used by what me so that you provide like an API object and it will just Auto generate at runtime the types that you need is really nice or we have type chain which can generate typescript files and types for your contract and then you know like all the functions on your contract you know all the outputs you know all the structs that you use so how do we use it well here we have actual code from Australia where I don't care if you steal it but um I can show here so this is like we're getting we have the vaults which are like contracts that you deploy and they hold your funds and all the logic and so on so what we do here we have a helper function get public wall display data down here which is basically if you remember the ens example from before you can like you have a function that you call and then it returns your data this works if you have like an already existing contract in this case we could also just deploy this contract and use it on chain but we don't have to we can just use our [Music] our assembly magic here and just force the Constructor to return and because we still have this extra function which has like input types and output types and types chain or ABI type will pick them up we actually can go in our front end we can basically use the generated type chain code which is again a helper to deploy the contract plus we're getting the deployed inspection which the with the parameters and then instead of doing like a manual Abid code we can use the generated helper function which is in this case decode function result and this basically just transforms the data into an object which has like named keys so you know like if you look back at the structs that we have defined here well address what balance of total shares this object will also be available like this in the front end so we have full like end-to-end typing we have a solidity contract which is the source of Truth and we have in front end generated code which represents the exact contract and exact function calls and logic so this is really nice we can do like full end to end typed execution on chain well in an if call and we can basically like ask any contract on the chain for any data that we want so that sounds too good to be true right well there are a few drawbacks so when I have the three main ones here which are one EIP 170 which is basically just a hard limit on the code on the size that the contract can be it's 24 kilobytes but it will take you a long time until you hit it then you might run out of gas which is a weird thing because like there is a kind of hard limit of 30 million guests that you can spend because that's the maximum limit a block can have but then every node can just like Define themselves how much guests they want to allow so some of them are as low as like 5 million gas While others are my prototype yes Alchemy Alchemy is like yeah whatever we do 500 million gas so you can do a bunch of computation so like our logic with the public Vault data we tried it with like 100 volts and it was still fine but then if you try to execute this on inferior for example they would say like yeah no so you need to test and try to find like the goods the best provider for this use case foreign other problem is if you use like ethers or you do some blockchain stuff in front end you will see that your bundle sizes they get always blown up like you have like multiple hundreds of kilobytes of data suddenly or bundle sizes so the JavaScript is being downloaded because we need to download like all the apis of every contract that you interact with if you want to do it properly you could always just take a small part of it but like the nice thing is if you have like the full contract typed and when in this case we have an extra problem that we also have our actual byte code of the contract because we need to fake deploy it so we also have our byte code in the bundle so we have even more content so you can fix it by for example taking all this stuff and putting it in a serverless function or like some API route on your server so that basically the bundle size is not on your client's desk browser anymore but it's somewhere in the cloud and this is much nicer you can also do some caching for example so this whole thing the whole talk the whole idea came from two people they are um well Dr gorilla.if had like the first like idea I guess I don't know if he she was the original um like creator of this assembly hack but well this is the first time I read about it and then lib evm took this and put it in a contract and built put it in an article built like an example contract an example script how to read it and that's why I learned about it and I basically built on top of that and added like the full end-to-end typing solution as well so well that's it's about on-chain computation so now we know about so you know about the three levels of basic computation which is just do one call each do them in synchronous calls you can do a multi-call and then if you do even more if you need even more computations or you want to do like uh calculations for example like do a big number division or something or you want to request from like 10 different contracts in one call well just fake deploy and contract so thank you and follow me on Twitter then smashed on Blue Sky forecast the Discord telegram email Facebook no icq I don't know if I still have access to this number and don't follow me in real life please thank you [Applause] hello thanks a lot yeah that was an awesome presentation do you want to do a couple questions maybe yeah sure sure any questions for Marco yeah so the question was like right why can't a contract return and how do you get around it right so so the thing is that like I can go back to the slide um here so this is like the Constructor of a solidity contract and in solidity just the Constructor can't return data it's just the hard like limitation of a contract I am actually not sure like why this is I wanted to research it but I only finished the slides like 6 a.m this morning so um I didn't have time to research anymore um but yeah it's a hard limitation of solidity the Constructor can't return data by default so what we need to do is we need to implement this assembly hack which basically forces the code to return something even though the solidity language doesn't um support it director of the videos is what you call a thing and kind of in the first call the fire perfectly the second coil called the country well like what you call you could do like a contract deployment with multicol there is like a transaction functionality so you could actually deploy to the chain but then you can't do like if you require for example the contract in the second call you don't know it because it's like one okay if you if you noted um address in advance of create two then you can probably do something but with this we don't even need multi-core we just do like a fake deployment of a contract and we have like an immediate return yes cool any last questions sweet thanks a lot Marco thank you
