DevNTell - Nader Dabit - Smart Contract Development with Foundry

Developer DAO · 24 Jun 2022 · 35:12

Nader Dabit, founder of Developer DAO, walks through smart contract development with Foundry. He compares it with Hardhat, Truffle and Brownie, then covers the forge, cast and anvil tools and the forge standard library. The demo scaffolds a project, builds a counter contract, compiles ABIs, writes Solidity tests with fuzzing, sets up remappings, and tests an ERC-721 transfer while impersonating users with prank cheats. A companion open-source cheat sheet collects further resources.

Speakers

Topics

From Developer DAO; community: Developer DAO.

Transcript

am gag everybody welcome to what's going to be another great devontel if you didn't know devontello is a 30 minute window for members of the dow to showcase something they're passionate about or have been working on this can be an awesome project you've been working on demonstrating unit testing best practices automation goodies smart contracts how to structure a project etc basically if you've got a passion for something this is your opportunity to share with the community and today i'm very happy to introduce to you a man who needs no introduction uh nader dabit the founder of developer dao who is going to be giving us a overview of smart contract development with foundry take it away nader hey what's up thanks for the intro and let me see how sharing my screen works here so

it looks like i have to share my whole screen huh um it'll be nice if i could kind of like take a portion of it because i'm using a wide monitor and it's going to be hard for people to read so what i'm probably going to do just give me one moment i'm going to go ahead and attach a separate monitor to my workspace and then use that one because it's a square monitor sure thing so just give me about 45 seconds and i'll have this up and running we'll give some more time for people to show up anyway i might even not be that long almost have it ready already no worries no ways all right there we go and i guess i'll go ahead and um share

my screen and i created something specifically for this presentation like an open source little thingy so i'll go ahead and share that too awesome this will turn on my camera too can everyone hear me all right yeah yeah you're doing great you can hear and see yeah well it looks like my other camera isn't working so i'm just going to use this camera all right can everyone see me streaming now uh yeah i can see your screen yeah cool so um today i actually

decided to go ahead and put together and release like a cheat sheet because like when i was learning foundry i kept needing like certain references and then and the documentation is pretty good but it's kind of spread out so i decided to kind of like condense it into i think i actually kind of was creating this for myself it was just like a it was just basically a um a gist and i was just going and referencing it and then i was like oh this is actually something useful so i figured i'd open source it but uh before i kind of get into this i guess i'll get an overview of what foundry is so foundry is a solidity development environment and it is something like you could

maybe compare it to hard hat you could compare it to daptools brownie or truffle and it is really really great and i'm liking it so far i feel like it's probably my favorite solidity development environment now whereas hard hat was only maybe a few months ago and it's created by or it's funded in a sense by paradigm which is a venture capital uh firm and uh it's probably my favorite one because like they're not they're they're more in tune with developers and they create a lot of really great open source and stuff like that and this is probably the best thing

that they've created maybe and it's worked on by probably two also of the best engineers or i would say not just like uh this in particular but found uh paradigm in general has a handful of some of the best engineers in web3 and um one of them is george i don't know how to um pronounce his last name but if we go to i guess to your contributions for contributors you'll see that he's this guy right here georgia's cons constantinopolis or whatever and then i guess matthias seats but anyway so that that's just kind of giving an overview of where this came

from and some of the main differences between foundry and hard hat or maybe foundry and any of these other ones um are that with foundry you have the ability to write your tests and your scripts in solidity so instead of context switching between python or javascript and solidity you can write your smart contracts and solidity and then write your tests in your scripts and solidity it also has something called fuzzing which allows you to create test functions that essentially create arbitrary function arguments so you don't actually have to define the arguments it'll kind of like allow you

to to do more robust testing where you can kind of just create the test and it will populate the arguments for you and then it's really fast and then finally uh it has a tool called cast which is a really great stli tool for interacting with live contracts that have been deployed to test or or live networks so instead of actually having to write any scripts to kind of like test out whether a transaction is working properly you can actually just use your command line so it's pretty nice so in this i guess presentation what i want to do is

go ahead and just show you how to build out a smart contract and uh you know and test it out and deploy it using foundry so to do that what i want to do is go ahead and make this a lot bigger first and is that pretty much viewable by everyone yeah looks legible to me yeah very cool so with uh with foundry you have three separate

uh tools you have forge which is a cli [Music] that allows you to create projects run scripts and run tests you have cast which is a cli that allows you to make contract calls from your command line you have anvil which spins up a local ethereum network or a local ethereum node similar to like if you ran npx hardhat node or ganache and then you also have the forged standard library and the ford standard library is a set of smart contracts that allows you to run things like assertions as well as other different i would say simulations within a contract so you can do things like simulate a user

and um and all kinds of stuff so all those three things kind of make up what foundry is so what i'm going to start with is forge though so like if you want to install this you would just go to the foundry github and look for for the installation you would run this coil command and then after you run the curl command you'll have forge and you'll have cast and then you'll have anvil so these th these three are the things that we're going to be working with so with uh with that being said we're going to go ahead and spin up a new project by running forge net and then

you can either run this from an empty repo or you can create a new repo by saying like the name of the repo so i'll say for our foundry example project and here you kind of have your basic project so it's kind of like npx hardhat in it where you get a example codebase to start off with i'll go ahead and open that up in my text editor so with foundry

the boilerplate that you're given has a test example it has a script example and it has a contract that's really not even an example it's just a very very empty contract so the test is just an example of how you might run a test and uh with with a forged you run forge test and then this command will actually look in your project and we'll look for any function anywhere that starts with the word test so it could be in this test folder but it could also be pretty much anywhere else so if i just change this to example and ran the test and no no test would actually

be executed but if we change the word example the test example then that test would run so um tests you know or kind of like any function that starts with the word test um the the scripts are essentially like the main thing that i've done with scripts is just to actually deploy contracts but you could also use this to call contracts i guess even though i kind of enjoy using casts to call the contracts from the cli so with hard hat you might have like your deploy scripts running javascript and then

and and foundry we're going to be writing in solidity and then you then you have your contract itself and um what we want to do is just create like a basic smart contract so the the example that i've been using is like a counter example so what we can do is to invent a plus one go ahead and create um a basic contract and here we might take like an argument of and then we could set the count to equal

count make that private and then we'll have a function that kind of returns it so for counter we'll have a function for incrementing the counter for decrementing the counter and then we'll have a function for just returning the count

so for the um i think it's because you have to say you enter it yeah maybe there we go okay i guess i need to return um for increment we'll just put counts plus equals one and then i'm gonna decremental minus equals one okay so like really basic stuff here um

and this is kind of going to be a good starting point there for us so like uh with that being said um the first thing you might do or consider like you might need from this contract is your abis if you wanted to build out like an application that that you needed this so you can actually go ahead and compile apis by running forge build and then in this out directory you should have contract dot soul and here you'll see that we have the constructor we have decrement git count and all of these different functions available so like that's kind of how you would create your apis and the next thing we might want to do

is run some tests so for tests um we want to go to contract.t.saw now before we actually go into the test you'll notice that we're importing forge std slash test.soul um we have similar to like in javascript where we have a node modules folder we have a lib folder and you can actually like install and uninstall different dependencies and with with forge comes the sports standard library the one that i went over earlier and you'll notice that we're actually not defining the path for this because like where we are right now is here so we we really should be saying something like

import lib slash board sp or something like that um let me see where we're at i stopped being slashed left oh yeah slash star c actually anyway so like uh this would be the exact import but we don't really have to actually do all that what we're basically doing here is just like assuming that our text editor and our environment knows where that is so the way that you would set

that up is uh by setting remappings and you can kind of like get an idea around what remappings you might need just by running forged remappings here and here we're uh having the the ds test which is an assertion uh library mainly and then the forge standard library and it tells you kind of where these are located so the ds test is part of ford standard library and then the fort standard library is here so um what we can basically do is then tell our text editor uh where to start looking before for those remappings i guess you could

say because like we're saying forge std but we're not including the src here but we're also not defining the exact path so to to to to set all this up i guess the the main thing i'm about to trying to get to is that there's a solidity extension and i've installed that and then once you have that installed you can go to the settings and look for solidity and you can actually set up the remappings here kind of by defining your

default dependencies contracts directory as src and the the dependencies directory is lib so that means when i import from lib um i don't have to define like the exact path that's going up and getting that and i think that it's not like that important you can also again just like define the exact path if you want but if you wanted to set that up that's kind of how you would set that up so once you have like your your remappings and your your text editor configuration stuff set up um what you can do is then just go ahead and create your test so here in the setup we can create a

um a function that essentially i believe just gets run before every function i'm sorry i'm in the wrong let me go into the test folder so it's in the setup function you can create a function that uh gets set up or called every time so what we what we might do here is say counter counter and then here we need to go ahead and import counter and let's see here the exact actually we can just import

i think i need to rename this contract counter and i might even change the name to counter so this is the contract i'm just renaming it to counter all right so now we're importing the contract and we're defining a counter and here we can say counter is equal to new counter and pass in the value that we'd like to start with and then here we can now start uh like

using the assertions so the first thing we might want to do is to get the count so i can say function test get count and here we might go ahead and say value is equal to counter i get count and we can expect the value to be whoops i guess we need to you went i guess we can expect the counter to be

10 so we can assert equality by passing the value in 10. and this should be like about the most basic test that you can run without just passing in just a truthy value there so here if we run forwards tests then we'll see that that's successful and if we set this to like 11 or something like that then it would become unsuccessful okay um we might then test the increment function

so we can say function test increment and we might here say counter dot increment counter and then here if we set this to 11 then that should pass because we start off at 10 we incremented once actually start off at 10 and then we incremented once and now we're at 11. so those two paths um so that's kind of some basic stuff there what we might want to do now is

we're going to deploy this contract to a local node and then we want to to create a more complex contract where you can emulate users and do a little bit more like fun stuff but to deploy this like you might have been used to running npx hardhat node and getting up whoops to getting the node well there's also well actually i need to be in a hard hat project i guess but uh with with foundry you have anvil so you can just run the word anvil and this will um actually i might need to kill here there it is i already had that running somewhere so

if you're on anvil then you'll see that um anvil just goes ahead and spins up your local network and you get like uh 10 accounts and you get 10 private keys you could also say anvil help and you'll see that you can pass in all types of stuff so you might say i want to only get like one account and then here you're only given like one private key in one account doesn't really matter though i don't think for what we're doing but with that node running we can now deploy our contract and the way that we're going to do that is by going to the contract script and you have the setup function and then you have this run function

so we're not going to really use the setup function i don't believe what we want to do though is just like go ahead and deploy this thing so to do that we can go ahead and use the broadcast or the start broadcast and they're very similar i think um from what i understand broadcast only is invoked in the next contra and the next uh i guess call that would that would be right here and then you have vm.start broadcast which would run for as many times as it's called in a row and what you basically are doing here is that you're basically uh

when you're calling vm.broadcast whoever is executing this this uh this contract is going to be the user that is deploying it but you can also pass in a contract uh i believe address here or something like that and then simulate that user but for for our example we're just going to be calling it from the command line so we're gonna be passing in a private key and i believe that private key will be the user that that that that does that okay cool so if we call dm.broadcast we can we can do now here is we want to go ahead and import the

script that we're going to be using for the i'm sorry the contract i believe we can just import counter like something that isn't right here does it need curly braces like javascript typescript does i believe these curly braces are

necessary let me just delete this and make sure that's okay now it seems to have gone away unless i'm missing something i think maybe a semicolon oh there we go kind of lags behind huh yeah a little bit for sure yeah for sure actually like after you you know you maybe it's just my machine or something okay cool so we have that and then now what we can do is just call like new counter and pass in the constructor and that's really all you got to do this will go ahead and deploy the contract now if we didn't call broadcast it wouldn't actually deploy this so that's interesting

um so to run this all we need to do is use the command line and use forge so well first of all we can deploy this without actually deploying it we can simulate it so what we can do is say forge scripts script slash contract dot s dot soul and then we can pass in the name of the contract that we want to run and let's just try this because i'm not sure what's going to happen if i don't pass in the arguments um i don't think anything's gonna happen but i just just ran this script successfully so it didn't really do anything

um i guess it gave us the gas that it would cost so let's now take this and like simulate it and to do that we'll pass in fork url and this is when we're running locally if this is a live network it would be the network url i'm sorry the rpc url and we're going to be deploying to localhost 8545 so i can say and then we also need to pass in the private key here and of course if this was a live uh project you would store this in the environment variable but since it's like a public key i'm just going to pass that in so you notice that it says simulation

complete and this actually wasn't deployed so we don't we don't have a contract address because it wasn't actually ran so to deploy we actually have to pass in one more flag which is dash broadcast and this deploys the contract and it gives you like a message saying complete and successful and it even writes a lot of information to your local environment so you have this broadcast

directory that has some information about like what would just happen honestly i haven't gone into some of these directories that that are this this data that's getting written that that gets written but uh but one thing that's pretty cool is that it actually just writes the contract address for you so you can import that in your project if you need it later but we also see that we have the contract address log here and log here and what we can now do is use cast to make calls against it so i can say cast call and then here we can pass in the contract address and then we can just call the function that we want from our contract so we might want to call git count

and then here we see that 10 was uh returned and then we might next want to call a tran or send a transaction so i'm going to go ahead and save this private key actually in the environment variable oh just fyi another we're a five minute warning oh where we were we have only five minutes left huh yeah got it okay cool so um with that private key and our environment variable we can say cast send we can call

the function we want to call which would be like increment counter and then passing in the private key and i'm trying to see if we need to do anything else i think that should be it and then now if we call cast call again then you'll see that we have 11. so from cat with cast we can kind of uh you know make calls and then we can do this for really any contract in the entire like world you know like as long as it's um a contract that's deployed to an rpc or i'm sorry to a network that's accessible

via rbc you can call and send transactions to it with any private key so um the last thing we'll do is go to solidity by example and grab the erc 721 and we'll create a new contract actually we'll just replace that contract and we want to mock a user in the test and this will be a a good way to kind of show some of the

other stuff that you can do and the first thing we might want to do is mint a token and here we might want to go ahead and set up a couple of variables for addresses bob and mary is what i've been using and here we can go ahead and say um

let's go ahead and test transfer because we're going to need a mint here anyway and we don't have much time so what we'll say here is and here we'll pass in let's see here we need to pass in address 2 and the token id so we'll say bob one and we'll say address owner of is equals to erc we'll first assert

that owner of is equal to bob and run that test getting some air connor am i still using counter somewhere oh i think i think i know in the uh in

the script it's still still referencing that for some reason all right so we so test transfer is actually the i guess the test that we just ran so they went in okay cool that's fine so what we want to do now is we want to simulate bob and the way we can do that is we can say start prank and then here we can pass in the user we want to we want to uh to assume the um the identity of and then what we can now

do is say um erc 721 dot transfer i guess save transfer and then here we can say bob mary and this is an overloaded function so we don't really need that last one i'm going to say bob mary 1 and then we can do the same thing here we kind of address owner of and then this should be mary and this should allow us to transfer and like if we didn't have this prank

here then we're now trying to transfer a token that we don't own and this should fail it says not owner or approved nice and that's kind of bad about it um but i would say if you want to learn more about foundry i kind of compiled all my favorites uh information into this this foundry cheat sheet i have the foundry github foundry book installing foundry i have the video i created i have a a tutorial that someone from developer doc created foundry versus hard hat is a really great uh article and then i have an article that i created back in january

that one that first came out that i linked to actually back in december and all that stuff is linked there so yeah thanks for uh for coming and checking this out i'll go ahead and pull up discord awesome awesome thank you so much for coming on adam and giving us that awesome demo uh it'd be nice uh to actually stay with one language when when doing all the slowly development so i'm definitely gonna be checking it out as i'm sure most of the gang here will be too and uh it's awesome yeah yeah man awesome and with that uh yeah i want to wish everybody a a very happy friday and or weekend wherever it is for you and uh i'll be dropping a link to the for the attendees to claim their pop and

now that i'll send you your uh uh speakerpop through dm but uh with that awesome yeah i will stop the recording

Related talks