Mantle 🛠️ Debugging your smart contract with Foundry
ETHGlobal·Wed, Oct 9, 2024, 12:00 AM
Join us for a workshop titled, "debugging your smart contract with Foundry". From November 17-19 2023, ETHGlobal welcomed developers from around the globe to take part in shaping the future of Ethereum at ETHGlobal Istanbul. Over $600,000 USD in prizes were given away at the prestige Istanbul Congress Center... thanks for an amazing weekend hackers! ----------------- Follow us: https://x.com/ETHGlobal?s=20 Check us out: https://ethglobal.com/ Email us at: hello@ethglobal.com
Transcript
[Music] we're just going to be going through debugging your smart contracts with Foundry we're be walking through some of like the cast utilities and the debugger um if they fix the issue with the debugger um in the past couple of weeks so this is something that you're going to see pretty often on some block explorers especially if they're block explorers like blog Scout um where sometimes the internal transactions aren't being parsed correctly so you'll just see that contract is failing or the transaction is failing when you're calling a contract and it's just not going to tell you why um which is not ideal um so if you want to get started if you can get your laptop out and you can download the installer over here so you can just curl The Foundry installer and then execute it it takes like a minute to do uh it's pretty quick uh it's a really really great framework U you don't even like have to use it for everything I would just even just use it for debugging if you want to use something more simple like remix to write your cont contract and if you're a complete like Noob to this right um so while people are getting that set up and getting this installed who am I I'm a backend engineer at mantle Network um I'll be at the booth if you guys need any help if that makes sense uh Twitter AFK Z bite telegram AFK bite and you can check out my GitHub as well uh profile picture and everything is that wooden plank uh fairly recognizable um so what is Foundry to begin with so Foundry is a modular like smart contract development framework it's got four components you can use cast which is CLI tools that interact directly with the chain which is what I find really useful for debugging um you got Anvil which is a local test note um like the hard hat node if that makes sense except this is like much faster than hard hat node I highly recommend that you use this over the hard hat node if you want to run like simulations or anything um you have chisel which is like a it's an encoding Library you're not going to have to use it if you're like brand new but it's pretty useful to have and then we have Forge which is what most people are probably the most familiar with here it's a smart contract testing and scripting compilation a library very very useful um highly recommend that you use this over hard hat um so Anvil which is pretty important it's basically just a faster hard hat node so you can just Fork any network um at any at latest or at any specified height so let's say that you have deployed your contract on mayet or you've deployed your contract on mantle which is where I work and um you get an error what you can do is you can actually Fork the local state of the network pretty useful so you can see the instruction over there it's like Anvil D- Fork URL as long as you put like an RPC URL over there um you can Fork the state of that Network um if you want to put an additional argument um you can also put like block height um Das d block Dash height and then put the exact block you want to Fork at if you want to get the state at a particular block but I think that if you want to get the state from a block that's a bit further back it's going to have to be an archive note that you're pulling from uh for mantle our RPC test nut like that URL is an archive node so you can pull arbitrary state for from however many blocks back that you want um so when um you are um testing on a fork of mayet instead of like mayet itself your call traces are going to be more detailed you can see what functions are sort of being executed um by your contract call even if they are in remote contracts um it's not really mandatory again you can use hard hat note instead I just recommend this because it's much faster and the traces are much more detailed um and then cast this is the second part I highly recommend scanning this um to be able to go over to the docs and sort of take a look um so it's a nice command line um tool for interacting with evm based chains if you want to like send transactions or do anything like that so um you can cast call cast send which is you can use to broadcast arbitrary static calls um or transactions cast call being the static call cast send being like a transaction um you can cast ABI decode cast ABI uh encode if you want to decode or encode call data uh like to transmit to chain or like if you want to encode or decode your own transaction there's cast transaction C T block which means that you can just pull arbitrary information uh about like a transaction or a block uh from chain so this is really useful when combined with forking off Main net right because you can get information about a transaction that has already occurred on chain or about a whole block that has already been mined on chain if that makes sense there's cast storage this is a little bit more complicated if you know the exact storage slot of like a variable um that you want to like pull information from you can use that the uh the contract address and the the storage address within that contract is just pull the value out of storage over there um and you can cast RPC so if you have like an RPC endpoint you can use this command line tool to just straight up send um like RPC calls to any um RPC endpoint that conforms to the the ethereum RPC spec if that makes sense so yeah when we're using C run it's like the most useful thing for debugging honestly you can run it against a local Fork to debug transactions so basically you can decode function calls even if the contract is like unverified it's got like pretty good error tracing so for example on a block Explorer um that error that pops up might just be unknown error sort of like you saw at the beginning of the talk right um when you Fork it and you run it like this you have the source contract code it'll decode it you can see that oh this um transaction failed uh because the error a must be greater than b if that makes sense so like much more helpful um note that this is only if you um have string errors if you use the custom function errors it'll be an ABI encoded version of the error so it'll be like the first four bytes of the of the hash of your error um and then like whatever arguments are in it so if you're just starting out I highly recommend you just used string errors um it makes it easier to debug in situations like this rather than custom errors even though custom errors are more idiomatic we're at a hackathon we're experimenting with new technology uh we want to do it like the easiest way possible right yeah so that's basically um it for the talk we went through it like pretty quickly because we had like a very short amount of time uh I'm going to go through a lot live demo of this and then you guys can ask any questions that you have um if you want or actually do you guys have any questions now about like the utilities that are uh given by cast and everything yep yeah yes so you have to run it with some specific arguments so if you see um over here um you see um if you just run Anvil it'll start from a zero zero state so let me do that right now sorry this is has a lot of history in it cuz I was doing stuff yeah what oh yes I will make it bigger sorry uh I already have an anvil instance Running Oh god um somewhere let's just close all of my terminals okay so I already had an anvil instance running my bad let me get rid of that it's actually over here um so I have one that's been running for the whole night because I was doing some work and debugging a bunch of things but as you can see um when you're like sending transactions to it like you can start it at like its starting state so if you go like just run the instruction Anvil what happens is you have a blockchain with like no state it like nothing sort of happens and if you want to like pull um transactions and stuff like that from it so for example I'm using cast send over here um you can see that there's like some pre-loaded addresses with Ethan stuff within them um so I can cast send this address some amount of ether um with the private key sort of specified um that's all being pulled off the pre-loaded accounts over here um I can just do the cast send I get like the information about the transaction back uh it gets mined over here and now suddenly that address um has ether and stuff in it um so it's useful for testing from a blank slate and then if you want to like test from an actual like Block Chain so you can go Anvil and then you go RPC URL and then you can grab any RPC URL you want uh we're going to use mantle cuz that's the company I work for um so yeah that's a lot of URLs so you could just go like go mantle RPC URL you can use the public one if you want um um you shouldn't get rate limited uh and if you do get rate limited what I would do is I would use um the blast nodes one those are generally pretty good but yeah all you need to do is not have those arbitrary characters over there but uh put this in after and then suddenly um you have blockchain state it's been cloned from here so what I can do over here is I can go to the testnet URL um and let's go to the testnet Explorer testnet Explorer and then pull some arbitrary contract from it right so this is some arbitrary contract there's some trans transactions within it right um so yeah let's just take this transaction hash uh we can run things like we can just rerun the transaction like I showed in the presentation sort of over here we run the argument um cast run uh and then you use a dash Das for both to get the full Trace uh we can go back over here go into the second tab over here we can go cast run and you see the transaction that hash that I pulled from the Block the chain Explorer yeah it just will run it um and then it'll tell you exactly what error if it was a string error that sort of occurred right um again this can be done on any test Network any main net as long as they conform to the RPC and as long as it's like an archive node if you're using a non-archive node RPC for this you can only pull like detailed history from like I think 200 blocks back or so um but yeah uh any more questions you can also query the RPC directly but you might get rate limited so I find it better to like Fork State and then query off of that uh it's also if you query the RPC directly you won't get as detailed of a trace if that makes sense because it returns more limited information compared to what's exposed in Anvil yeah um any other questions okay to spin up 20 seconds um yeah 20 seconds um I've had like on really bad internet before have it taken a lot more time but yeah normally 20 seconds and again if you have any issues with like forking like the test net or like main net what you can also do is you can just construct the state that you want over here um so let's say that you want like a certain state to be set up within it you can just like write a script or something to send the transactions to Anvil to make sure that your contract is at a certain State deploy your contract on Anvil and so on and so forth uh and then you can like start querying State and stuff from there if that makes sense yeah um but yeah as you can see you can also do like RPC calls if that makes sense so this is like oh I wanted to get Block 498 on my local Fork if that makes sense see what transactions and stuff are in it um you can also use this as like a mock backend uh I big I actually highly recommend that unless prize that you have requires that you Deploy on Main net when you're demoing use this as a mock back end because then you don't have to wait for blocks to be mined and stuff like that anvil on its default mode um what ends up happening is it just automatically mines every time you send a transaction so again like I can go cast send The Ether over there uh and it just will instantaneously confirm so you don't have to wait like however long it takes for a transaction to confirm yep does it work for out of gas error uh it does work for out of gas error so normally when you get an out of gas era you just get like a revert exception and then like some numerical code um yeah and then when you're doing it over here it'll tell you like out of gas and you can use the the forge like debugger to trace through to see where exactly you're running out of gas um I think that the forge debugger does work on the latest version of Foundry Forge but I would just take a look at like through the docks you can literally like step through your contract see how much gas is consumed at each step uh and it'll work from there yeah it's pretty nice I also recommend print line debuging yeah big fan um and yeah I guess that's sort of it we ran through it kind of quickly uh but if you have any questions uh I'll be at the mantle Booth I'll also wait behind um so we can go through more detailed examples if you want I wasn't able to go through everything because I arrived late sorry again for that everyone but yeah this is a really really useful utility um when I was like when I was new to City I really wish that we had something like this and not like truffle or yeah hard Hut this just has made debugging significantly easier and like being able to like trace and go into specific things much much easier effectively yeah thank you thank [Applause]
Automatic transcript — names and jargon may be misspelled.