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

Loading player…

DevTools Summit | Having fun with Hardhat - Adam Fuller

ETHGlobalSun, Oct 9, 2022, 12:00 AM

Welcome Adam Fuller from Nifty Ink for a talk titled "Having fun with Hardhat". Part of ETHOnline's DevTools Summit. ETHOnline is our biggest event of the year! Collaborate with thousands of developers and creatives from around the world and win over $300k+ in prizes! We're bringing in 30+ of the biggest teams in web3 to give you access like never before. Learn from ecosystem leaders and build your biggest ideas as the merge nears reality. The future of Ethereum is being written right now, and it's time to make your mark. --------------------------------------------------- Follow us: https://twitter.com/ETHGlobal​ Check us out: https://ethglobal.co​m Email us at: hello@ethglobal.com #ethereum #eth #blockchain #crypto

Transcript

foreign [Applause] [Music] Talk of the day and that is Adam talking about having fun with hard hats so without further Ado let's welcome Adam on stage hey Adam hey how's it going it's going great I'll I'll let you kick this off and uh welcome cool thanks so much good to be here um all right so um I guess let me show my screen um so today you were talking about having having fun with hard hat um so I I guess I preface this by saying that this is not a um a sort of uh walkthrough of how to build um an app specifically with hard hat there are loads and loads of really great um loads of really great um guides out there that'll show you just that and show you how to build um like all all manner of apps um here here today I really want to I guess dig into um hard hat give a bit of an overview of what it is the toolkit and then um I guess run through some of the cool stuff um that you can do um that you can do with it so um I guess uh we're gonna go gradually up up the meme um so we're going to start obviously talk about some of the basics local evm compiling contracts deploying them um we'll talk a little bit about testing um and some of the cool plugins that you can leverage if you're using hard hat and then some other stuff around like custom tasks evm manipulation forking chains um and contract verification so um a whole bunch of stuff um we're gonna obviously try and do um try and do stuff live um uh and so we'll see we'll see how that goes um but uh that's uh that's how we can kicking things off I guess just uh to really like Orient level set um yes I'm a um I guess uh web3 developer I worked on a bunch of stuff I'm going to be working with scavalife that was how I got into the space and I know um loads of folks um came out the same way um doing much work with um Austin so I've worked on um Nifty Nifty Inc which was built with what was formerly known as bridler and that was then then became hard hats I've been using hard hat for a while it's been cool to see the tools the the tool set develop um and I'm currently a PM I'm working on on the graph and so hopefully if if there's time also pull in a um a work in progress a hard hat plug-in which is making it easier to develop subgraphs as well um so a bunch of context there um as I said we're going to start at the top so a hard hat is uh first and foremost is the hard hat Network so it's basically a local ethereum node that lets you do development and be debugging is that super cool um in terms of getting started there's really good documentation I encourage folks to um jump into that it's actually been revitalized quite recently also with your like organization so hard at used to just be um pretty agnostic it's um it had essentially the evm a bunch of stuff that you could do with it and then a really diverse um set of plugins um but more recently there's they've introduced the hard hat toolbox which is a sort of bundled commonly used set of packages and Hardware plugins that they use to get um to get started so um one thing I did in sort of preparing for this was that I um aligned that I updated scaffold um to use the latest version of um of hard hat and I've also pulled over to the um to start using the toolbox so we'll run through all of that today so if I've got my I'll share the whole screen if it'll let me yeah perfect all right hopefully you can see the whole screen now um maybe things cool perfect so um yes as I said I'm I'm I'm into scaffolding we're going to run through a bunch of things today um within hardhat just to orient folks um there's again great content online on on getting it installed but the like the Heart of heart hat in any place is the hard hack config file so essentially this is a configuration file where you say okay when you're running hard hat like here are the things I want to pull in here are the things I want to use um so one of the first things that you'll almost always do when you're working with hard hat is is you're on a local chain so um the chief I'm running this here I'm running a hot hot chain um and what does that mean it's basically running a little ethereum virtual machine um uh on my on my laptop which I can um deploy contracts to so maybe we'll start with that and if I run deploy um I'll see a bunch of stuff happening um essentially uh to orient folks I'm in this hard hat directory um I've got um my hard hack configuration file which as I said um uh I like has a bunch of stuff we'll talk a little bit about that in a second and we've got my smart contraction here and the contracts folders this is a super simple this comes with scaffoldeeth um but it's a super simple contract which lets you set purposes um so just so this one doesn't let you um do much apart from set a new purpose um and it emits an event whenever that's that purpose is set um so what I did was I spun up that local chain so you see this um it's train running um and then I essentially uh deployed so actually if I deploy to that Network so if I deploy to this network fantastic Good Start um let's see it seems super deployed so now if I run um back and now if I actually run a local app just so we can interrogate and see some stuff that's going on we can get a sense and this is yeah as I said using the scaffold Leaf framework but we're going to really dig into some of the stuff that you can do with hard hat um so essentially if we look at this um this contract um and if we pull it out here if we go to the heart attack conflict just to like Orient people to some stuff that's going on in here so um firstly hard hat has this really cool plug-in based model where you can pull in plugins which others have developed or that the hard hat team have developed which are relevant and interesting um so one example um is the uh so that's called the localhost um so one example of a plugin which is imported here is is the tenderly plug-in that lets you leverage loads of stuff that um the tenderly team um to do everything from like interrogating local transactions to verifying your contracts the hard hat toolbox I kind of talked about earlier and the Heart had to deploy plugin is a really great uh community-based plugin from wiggle wide which essentially lets you manage all your deployments so just to dig into that a little bit more um deployment um in uh in hard hat can be done in a bunch of ways but this hard hat deploy plugin is is really great because it gives you lots and lots of control when you're deploying your contracts so uh to orient you in terms of what happens here um if we're going to this deploy folder which is how rdap deploy Works it'll essentially run all of these um these deployments um so this is deploying your contract um and uh so when you run hard hat deploy it'll essentially run through these and run all of these uh like run all of these migrations um so this first deployment of your contract is essentially getting deployed here um it's then getting deployed and there's a bunch of other stuff here which shows you some of the power of what you can do with um with hard hat but it's pretty simple to start with so if we jump back to the front end deeper contracts we can see this first one in here so that doesn't seem to have worked nicely um okay now perfect this is good always good live demo stuff um okay let's go up here five four five um cool see if we can fix this okay actually maybe we will see if not deployed okay I know what this is [Music] okay classic knowledge compatibility and then use um perfect okay perfect that's uh we're off when we're rolling um all is well now so um if we now go back to the localhost we can see this contract um so building Unstoppable apps and now if I call this a contract firstly if I grab some funds from the faucet so one really great thing about a local chain which you're running is that you can pretty much do whatever you want so um here I um initiated a little transaction to myself uh sending myself to me so I can actually start transacting so testing with Hardware okay if I send that fantastic you can see I'm making this uh transaction I set the purpose um and then you can see that the purpose is now updated so I'm interacting with a local chain and so then one cool thing that often um when you're working with hard hat is you'll be debugging your smart contracts um a cool thing which you can do which you can't do with a normal chain is essentially console logging so that classic thing that you want to do essentially when you are um you may be debugging a contract so hard to have a special console um DOT log um console.sol um contract which you can import into your contract um so you can start to do some debugging maybe not so relevant to this like in a super simple case but it might be that you want to test why a certain logic Branch isn't being passed down and so console logging within you from within your contract will let you do that so to keep moving forwards it I'll save this I'll redeploy my contract um so essentially it gets detected the contracts changed so then needs to compile again it's compiled you can see it's deployed again if we go down here um and now if we go in here you can see that we've got a whole new contractor at the front end's hot reloaded but now if I res call a different one hopefully log my smiley face you can see that this got console logged here so this is a really cool thing you can do with hard hat um to essentially be uh maybe debugging contracts maybe checking the variables it changes like as you'd expect during execution um I would say the one thing which I've sometimes done when deploying is to forget to get rid of this um this import because it's obviously extra code to deploy to the chain that's one cool thing you can do with hard hat um so let's just jump back to the things um as I said the um some cool things um but also potential gotchas when you're using different plugins so one thing is that uh if you want to say redeploy your contracts um uh in in this case Hunter deployed detects that actually the contract's not changed at all you might just want to re um and so it just reuses the same one might be that for whatever reason you actually do want to reset the um deployment um so in this case you can call reset and it'll redeploy the contract and give you a whole fresh new app again so that is um the basics of the Bare Bones and this is really cool because you can basically be adding new functionality to your um to your contracts so let's see this is going to be relevant times um and if we go in public view returns next two five six we can do this and we can do um block number as well okay these two new functions we could deploy these guys um it's essentially uh it's a really good place to be iterating and developing your contracts so these will get deployed we know these new two fields which show up here um and yeah we'll talk about um demo again in a second so um then in terms of The Next Step that you can do with hard hat which um so we've covered pretty much the local evm compilation and deployment next thing that you can do is testing um coverage and gas reporting so let's run through those so if I run yarn test here um essentially I run some tests so so these are some unit tests which are run um and actually I've jumped ahead here because you can kind of see um uh this uh at the bottom so um essentially these are just unit tests so if we jump into my test you can see um essentially the tests that we've just run so um what are we doing here so um essentially uh this is getting run on a again uh it's an instantiated local hard hat node um and essentially checks a bunch of things which you um expect to happen when you're um with the contract so for example um it you can test that your contracts deployed properly you can test that when you call a given function it has the effect that you'd expect um and then you can also check for example that an event is emitted um when you when you call that contract so um that's again the kind of stuff which is moving to um from sort of hacking iterating working on contracts to something that looks more like sort of software development best practices um and so uh again a really rich toolkit and um and one thing that had also recently introduced is their own sort of matches um which I haven't fully dug into but um again is moving away from whether we're using a sort of um a prior truffle Library um but so uh testing can give you a lot more confidence when you're making changes to your contract that they're not having sort of side effects that you wouldn't expect and to make sure that actually when you've implemented things um that the result is is what you would expect so um you can all do all that within the sort of um within the hard hat framework there's some other cool stuff that you can kind of get when you're running your tests so this table at the bottom is one of them um so this is again something that comes in the hard hat toolbox and if we jump back to the hard hat config you can kind of see where this is configured which is the gas reporters so obviously there's a large school of thought who are very focused on gas optimizations within smart contracts um and so this is one where um if we look at this um this uh this report this essentially takes all of the tests that were run during the tests and sees what the sort of gas usage was for those tests and so you can see within this gas currency this gas reporter thing this is coming bundled in with the um bundled in with the uh with the toolbox but essentially if you call it enabled get through so I mean if I run this again without it you just have the sort of output of the tests um whereas if I run with it again you can kind of see this table which gives you this put and so then you can start to see as you're making changes to your contracts um how that's then impacting different things so for example if we add a un256 and then if we do now if we do yarn test see if I give it a second you can now see that um the increase in gas usage because I'm sure I've added this new um this new storage update um and so that's again a thing where you can start to optimize start to keep track of um of the cost of just different actions um the last thing I think I'd call out and I in the interests of time I think I called this in was essentially the coverage command so this is um again suddenly running your tests but also giving you a sense of the coverage of your tests so and if we jump back to my contract you can see that um my contracts I added a bunch of functions here um and if we look at my tests my tests actually only really test out the the set purpose function um so this is identifying if we jump in here that um that actually there are some lines which haven't been covered by my tests some the potential functions which haven't been covered by my tests again this is a thing that just comes bundled in with the hard hat toolbox so again you can not only be running tests and feel confident that your changes are um not having unexpected impacts but you can also have an understanding about actually how thorial test coverage is so that's testing and there's a bunch of cool stuff there if we go back to the meme we can see that we've covered testing coverage and guest reporting and so then I guess we can jump into um some of this um like slightly more um advanced stuff you can start to do with hard hat so we're going to talk about tasks evm manipulation forking and verification um so for example so if we jump back to the code um if we go back to the um hard hat config um again just to orient you on the stuff that's going on in this file we're importing a bunch of um of plugins that are useful we're configuring a bunch of um essentially exports or modules and so a lot of these are actually networks they want to interact with you saw me like changing the network that I was interacting with um so that network configuration is a big it's a big thing here and obviously focusing scaffold want to interact with a lot of networks here um we've got the ovm the like uh you've got some of the um again uh API keys or like configuration for some of the other plugins um but then at the bottom here we've got a bunch of tasks So within um within hard height you can deploy you can define a lot of tasks and scripts um so tasks and scripts there's there's actually a post and a documentation about when you should use one or the other and focus on tasks today but essentially um these uh essentially little Snippets that can be run in the context of um in the context of the hard hat um environment so you can start to interact with your um with your chain with your contracts so let's run through a couple of those um I guess yeah certainly go ahead of that oh on this one you can see that I just ran this task which didn't didn't do much it just uh not console logged Hello World um so that's not going to say that that interesting but then you can start to see I like other interesting stuff that you can do um so one thing you could do is you could avoid contracts or you could um update um State variables or you could do you could interact with your contracts on chain and through these tasks um but the thing I want to now dig into is some of the other things you can do within hard hat which was kind of interesting um so that's um essentially starting to manipulate the EVMS so if we take this one so this MPX hard hat oh if we jump back to here um essentially um this is one of the sort of uh essentially hard hat manipulations you can do where you can interact with the um Hardware virtual machine to change some variables so essentially um there are a few of them I called out here one is to mine in new blocks that's just to add a new block number to the chain and this one is to like add that like change the time on the Chain so you might want to do this when you've got a variable which is like like maybe you've got a game and actually you can only close the game after a certain amount of time but you'd actually don't like you don't want to wait that amount of time you just want to sort of move forwards in time um so if we go back again to the um to the ethereum app um just because then we can I guess have a look at the stuff that's going on in the front um to get a sense of what's going on if we go again add an hour um okay I don't know what this is list perfect so now we can see we mined an empty block to remind that empty block you can see that actually jumped forward an entire out so these this is a timestamp um similarly the block number is updated so um this thing where you can start to interact with the interact with the chain um and so these feel like relatively um meant to be small things that you can do and but there are some others that actually let you do other stuff so for example like you might want to um adjust your balance so that so there's a help which actually lets you set the balance for a given account so if we go back to this guy using um hello.adamphiller.eath um if we take this um and if we say okay give me eth I'm going to run essentially this one give me eth ask my address in here so you can see this task was defined with this name I added this address parameter and then what happens is that the helpers set the balance of my account to five um ether so you can see currently I've got 15 so not one so should we run in here and now if we refresh I've now got five meter um you can see it's it's not added five it's actually just set my balance to five um so if I then wanted to change this maybe if I wanted to give myself 50 or 49 either this again you can see my balance again you've got 49 ether here so again just ways in which you can start to manipulate the state um uh and and and so again so we've gone from like having the time we've gone to um uh mining mining blocks we're going to set setting balance what about doing something um even more interesting so this this task lets you take some dice so um this should actually be die um so this um this is a helper which lets you essentially Define a um a user who's uh essentially impersonating an account so um to impersonate a Ascender which then lets you to make transactions on their account which you could never normally do in Us in in a sort of normal in normal context um and send yourself some uh well of any any specified token in in this case so I just to interrogate this again this is called take some die given a token address a sender and a receiver it creates this and this can actually be much simpler um um it essentially takes uh and it takes an ABI which is an erc20 API um it's um then impersonates the sender accounts so heart out we'll just assume the signer is the sender account um it then um essentially instantiates a contract with ethers and then transfers um some uh some of that token to you so again this is one where actually um has an interesting Dynamic here so we're jumping into another thing you can do which is to um not just start your uh for your local evm from nothing but to do it for our Fork so this is one where I'll run a special command so just cancel my local evm and I'll run a yarn Fork um so if we take this see outside of the yarn Fork um and I'll deploy things to it if I refresh so actually I've got yeah 17 um on this which is the account I have if I were to connect on localhost search my network switch Network refresh my network you can see that actually it you can see it's from my neck because it's got the amount of that I've got in this account on mainnet um you can actually then uh deploy um a contract in the same way um that you otherwise would um and so then um uh then essentially um given this I'm now essentially interacting with the chain which looks exactly like maintenance again this is a super cool thing um which harder lets you do because you can actually start to interact with this chain um let's just interact with this chain as if it is a real change so um so this is one where um essentially if we go to die um refresh this like this is the this is the die contract on um this is the die die contractual mainnet but but um my local Fork is essentially looking at listen so the way that this works if we go to the package.json is that when you run Fork essentially um you run a normal hard hat node or you specify a um an RBC to use for the fork and so then essentially when you whenever you call for State um that's only on the main chain um that you'll essentially defer to and use this RPC um and so that's again like a really powerful unlock if you want to maybe simulate or test what happens I've used this in scenarios where I want to just check what happened or or verify that a bunch of transactions would have the outcome that I wanted them to like a fork main net run those transactions on my local evm maybe generate a bunch of signatures that do those kind of things and not worry too much about um and and then I could have confidence when I actually executed them um on them like on the real on the real chain that things would happen as expected um anyway so where was I if I jump back to um what that then allows me to do is that then if I say okay let's take some die um I've saved this so this is um again on the localhost network this is the die contract I'm sending from this die person to and I'd want to specify my address here I think that's the right one I'll run this yeah um you can see now if we take my address take my balance you can see I've got five die I've just given myself five die again if I run that again um and I run that address again I've got 10 die and so again I want to find a really cool um cool thing you can do you can so you can fault the network you can send yourself die you can act as other people um is a cool thing to do um with heart hat um final thing I think um so again this is just uh to talk talk to the plug-in um like all of the plugins that exist on um uh well actually yeah one other thing you can do is essentially verify contracts so one thing I could do if I want to deploy is I could actually deploy to ring could be I could deploy this contractor incube I'll do that now um this is actually deploying live to a a test now one final thing that you can get out of the box is verifying um verifying your contracts um so now if I essentially take this contract um um ID um if we actually go and look at it on Linker B uh that one could be we are going to be literally up to the minute we run in here take a look at this contract did that work s like it may not have um maybe we're not gonna make it at the last minute um let's see okay still it's still indexing um but it should show up in a second one final thing that you can do maybe it's not going to show up until this index is um is essentially verified contracts um and so if I run this last yarn verify Network you know what ring could be command with this address call this this will then verify the contract we take the contract here perfect this is submitted for verification and any minute now should hopefully come back and it'll be verified and that's it I think I'll wait for the verification result so now if we refresh back in here last thing there's the verify contract and that's all I got amazing well uh as usual this wouldn't be a talk if something doesn't go slightly according to plan but uh this was great a lot of interesting tips and tricks and uh I'm glad that it's recorded because I gotta really dig into some of these commands to actually do this on my own too so uh appreciate this Adam thank you so much uh I hope everybody in the audience enjoyed uh this primer as well and uh unfortunately we are a little bit over time so we have any time for questions but if there are any that come up I will send them to you directly and then we'll answer them back to you uh the person who asks those questions on the chat so thank you so much

Automatic transcript — names and jargon may be misspelled.