🏗 Learn to Build a dapp Quick with Scaffold-ETH 🥷 - Kevin Jones
ETHGlobal Bogotá 2022 · October 2022 · 25:02
Kevin Jones, a developer advocate for Scaffold-ETH and member of the BuidlGuild, demonstrates how to build a dapp quickly with Scaffold-ETH. The live demo covers the prerequisites, then the yarn chain, start and deploy workflow that spins up a local Hardhat chain and a React front end with the contract ABI injected automatically. They show the debug contracts view, the example contracts, deployer accounts, verification, IPFS publishing and testing, and point learners to Speed Run Ethereum and Solidity by Example.
Speakers
Topics
From ETHGlobal Bogotá 2022, hosted by ETHGlobal.
Transcript
All right. How you guys doing? Thank you so much for coming. You guys having fun or what? Welcome. All right. So, uh quick introduction. My name is Kevin. Uh I actually work full-time for a web 2.0 company, uh Engine X. You might have heard of them. Uh most popular web server out there. Uh but I also uh am a part-time developer advocate for Scaffold-Eth. Oh, thank you. Uh and I work basically in the Build Guild. If you're not familiar with the Build Guild, it's basically a group of developers that uh like to build on Ethereum. Uh most of them are fond of Scaffold-Eth, which I'm going to be talking a lot about today. Um and I also have a nonprofit that I'm I founded in California uh just to kind of educate people about uh blockchain, specifically
Ethereum. Uh I'm also a mentor for GrowEth, which is a a free Solidity track. You guys, if you're interested in that, it's a free track and uh it's basically an eight-week cohort where you can learn more about Solidity. And I'm a photographer and filmmaker. You'll see me running around. I'm actually shooting photos for the event today. So, anyways, that's me. That's my contact info if you guys want to follow me at all. Um so, what is Scaffold-Eth? So, um most of what I'm going to be doing today is actually a demo, but I just want to give you guys a brief kind of overview of how it works um and what you need to get kind of going and get set up. Uh you need Git, obviously, cuz you need to be able to check out the repository. Uh and then you need Node.js and Yarn installed. Um
Node.js is pretty much the prerequisite. You need at least version 16 or above. So, make sure you have that. Make sure you have the newest version of Yarn. Don't be using like an old version of Yarn on Linux. There's this kind of old version of Yarn that doesn't work properly. Uh so, just get the newest version of Yarn and then you'll be good to go. That's really the only uh tools you need to get going. It's going to spin up Hardhat for you. So, Hardhat Hardhat's like one of the main components to Scaffold-Eth uh as well as React. So, it's going to spin up React for you and it's going to do a lot of like the magic that you don't have to worry about like setting up a uh uh hackathon project or any any web 3 ethereum based project can be a hassle
cuz you got to spin up all these different programs and make them talk to each other and figure out how you're going to inject your ABI into react and yada yada. Everything's kind of already done for you and I'm going to kind of show what that kind of looks like. Um and then everything obviously that I'm going to talk about today is based on solidity. So, you know, we compile our contracts in solidity um and that is pretty much the you know, the smart contract language of choice for scaffold-eth. Um so, if you're a developer, how how do you spin it up? So, the first thing you're going to do is clone the repository. You're going to do everything is based on yarn uh yarn scripts. If you're not familiar with yarn, yarn's really awesome because it is a package manager, but it also allows you to run these cool like kind
of command line scripts with simple commands. So, you can just be like yarn this, yarn that, yarn whatever. So, yarn install is going to do all the dependencies for you and install all the prerequisites. So, you don't really need to like figure out what version of this particular react you want to use or hard hat. It's going to do that for you based on the current version of scaffold-eth. And then you're going to run yarn chain. That's going to spin up your blockchain, hard hat, right, running locally. And then you're going to run react on localhost 3000 using yarn start. So, it's yarn install, you only do that once, yarn chain to spin up your blockchain, yarn start to spin up react, and then you do a yarn deploy. And what that is going to do is going to use hard hat to take the There's there's kind of
like a hello world smart contract I'm going to show you. We're going to start toying with it and it's going to ship it and deploy it. And that's going to also automatically inject the ABI. So, if you don't know what an ABI is, it's basically the way that the smart contract uh your front end can interact with that smart contract in a way because um obviously when you compile down your smart contract into byte code, uh it's not human readable. Specifically, it's not readable from your front end either. So, as you interact with your application, pretty simple, uh you start to form blocks and you can start testing, right? And every time you do a deploy, it gets a fresh copy of your smart contract. We'll show what that looks like as well.
Uh so, that's pretty much all the slides. I don't like doing slides cuz slides are are pretty boring. So, let's just get right into it. Um so, if you go if you do a search for Scaffold-Eth, the first one's going to come up is going to be the GitHub repository. So, um the primary repository for that actual Scaffold-Eth is here, Scaffold-Eth. There's also this like challenges directory, which is linked to like the speed run Ethereum challenges, which I'm going to talk about in a second. And then there's some examples as well. So, the cool thing about it is like you can find like ERC-721 example. You can find an ERC-1155 example. Uh there's pretty much an example for like everything you need to know uh or need to build on top of. So, it's a great starting point for hackathon projects, right? You don't have to worry
about, you know, injecting the um OpenZeppelin contracts. They're already pretty much set and ready to go. It's even got a little bit of a front end going for you. It's already got Ethers.js hooked up. It's already got your API plugged in, yada yada. Everything's kind of set up. So, if you like as an example, let's just say you went to like Scaffold-Eth examples and then you did a search here. You could type like ERC. There's an ERC-20 demo. There's a simple ERC-721, 1155. And you can kind of pick and choose like from that starting point. And that's a per- perfectly valuable starting point for your hackathon project, okay? Um but we're going to be focusing on Scaffold-Eth, the the actual main build.
Uh and that's where you should really if you're just like toying around and like building, uh you're going to use that. Uh and by the way, all the instructions of everything I said is going to be inside of the repository as well. So, like yarns yarn install, yarn chain, yarn start. And pretty much everything I'm going to go through today is in here. So, don't feel like you have to follow along. I'd rather you guys just kind of watch me do it and then um unless you're really good, you can follow along. Um and then after you get Scaffold-Eth set up, I always recommend people if they're still learning to go to Solidity by example and like copy and paste stuff from Solidity by example into your smart contract and and start testing that way.
Once you get like comfortable with Solidity, that's when you'll say, "Okay, well, I want to like test my abilities." This is outside of obviously the the hackathon, but when you want to start learning more, Speed Run Ethereum is a great resource. Now, it's um a basically like a set of challenges that you go through. And real quickly, one simple challenge zero is you build a simple NFT. Challenge one is you build a decentralized staking app, a token vendor, a dice game. And you go through and eventually you get invited to the build guild, right? So, uh and then you can actually start kind of uh being part of the build guild community. And then you you obviously get access to the special chat channels too as well, where you can like talk to the uh developers and the other builders uh and kind of work together to kind of solve these
challenges. It gets really hard after challenge four, so if you can get through these challenges, um it's a lot of fun. All right, that's enough shill about that, but um this is a good uh scan this if you want to know anything about Scaffold-Eth. All the links for Scaffold-Eth are in this one Hi Hello card, so if you're interested in that, scan that. I'll give you guys just one quick second. And then, I'm also going to give a POAP away. So, uh come see me afterwards and I will uh bring this up and you guys can scan the the POAPs as well. Okay? So, let's uh let's get going here. So, um I I use tmux. I don't know if you guys are familiar with tmux, but it's basically like a um terminal multiplexer. It allows you to have more than one window open at a time.
You can do this in in multiple windows, but the general like rule of thumb is you need like three windows open when you're working with Scaffold-Eth, okay? Um the first window is pretty much where you're going to uh do the yarn uh install. So, you're going to set up, install the dependencies. I've gone ahead and done that. It takes a little bit over a minute to do, okay? But, once you get it set up, you don't have to do that again. You're good to go. Then all you have to do is do a yarn chain. Yarn chain is going to spin up block the hard hat node. It's going to run on local host. That's it. You can pretty much leave that window open all the time. I like to have it just here in the corner because like you might use hard hat console to like print stuff to that
console and you might want to like debug your contract. And then over here in a new window in the same directory, you're going to do a yarn start. Okay, so what's that going to do? It's going to spin up react on local host 3000. That's going to take about a minute, so we will let that do its thing. Um and then the last command, if you guys remember, yarn deploy, right? So three those three commands, basically we've deployed a dapp locally on our local host. We can see here that we have we saw some contract calls. Uh let me zoom out a little bit. We get we have some contract calls. We have some uh it tells us how much gas was used. We can see that react just started on the right. Now we see more contract calls. And we see that how much uh gas
was used to deploy our contract on the bottom. We even have our address. So we get this kind of like cool printout. It tells us how long it took. Um let's take a look at at scaffold eth. So it says I'm on main net. Oh, let me disconnect from uh metamask. Okay. So this is scaffold eth. So when you first get in scaffold eth, uh it's basically react, right? But what's cool about it is it has all this stuff already ready for you, okay? Um this first app home, you can think of this tab here, like everything in here is like where you would build your dapp that you're actually building for the hackathon. It's like your dapp inside of scaffold eth, right? And this is a special app.jsx file that's inside react, and you can just build around that, right? So you can inherit all of
the, you know, components that you have in react and whatnot. Um but what's cool is this debug contract debug contracts folder. So what this does is essentially takes a copy of your contract and it builds like a full functional like debug UI for you. So it has your your variables, your your functions. It has your contract address. It has the value stored in that all in this one location. And then you can really like kind of like test your assumptions as you're like developing, right? In Solidity. So, uh let's take a look at what that contract uh looks like. So, Adam I'm going to use Adam.
Um you don't you can use whatever you want, but obviously Adam is is uh just a basic text editor. And what do we got here? Um we got a packages folder, and then we have a hardhat folder, and then we have a a react folder, okay? Uh most of everything I'm going to show you today is really in the hardhat folder. And we're going to be working out of the contracts folder. And inside of that is your contract, okay? So, this is kind of like the de facto hello world contract that comes with it. So, we can see here that we define a version of Solidity. We define a license. We import hardhat console so that we can do some logging to the hardhat console. We name our contract. And we also have like an event that we're we're doing that that that basically broadcasts the address
that calls the purpose function and the string that they pass into that function. And then we have uh another variable that we define. And then we have an empty constructor, which is not doing anything, but we could do that. I'll show you about that in a little bit. And then we have set purpose, which is a function that allows us to change that that variable, right? We have this kind of like public public uh variable here. We could change it here. Like if we say unstoppable. Like that. And then save that. And then do another deploy. And by the way, you might want to reset and force a deploy sometimes. Just do {dash} {dash} reset. And it will force a new contract to be deployed. So, I just did that small change. And then now we can look at the change here. It's been changed in here, right? Uh and so you're
able to kind of just do your changes to the smart contract, deploy, go back, start testing. Um that's obviously just a very simple test. But let's get going a little further down the contract, okay? Uh then we have inside this set purpose function, it just takes a vari- it takes a variable essentially, a string, and passes it to the new purpose. And then we log that with console log using hardhat console. And then we emit that event, right? So that the uh we can capture the event and see that. So it's a pretty smart it's like just a hello world it's very it's like the simple starter contract. Let's like let's make some changes. Let's make some interesting stuff. So the first thing you'll learn you might want to do some kind of like access control. So let's just do like a quick kind of like janky access control.
We'll make it owner sorry owner public or address public owner equals and we'll go back to our thing here. And what do we see up here on the top right? So this is a burner wallet, okay? The burner wallet's cool because you don't have to have metamask installed. It's just using a browser wallet and if like you were to look up open up another tab you still have access to that cuz it's in the browser, right? So you don't lose it. But if you were to come over here and say a well new incognito window and do localhost 3000 we would get a new instance of a of burner wallet cuz it's the incognito one, right? So let's leave that open for right now cuz we're going to get back to that in a second. But my point is that we can just use this
wallet and it's empty right now but we can just grab some funds from the faucet clicking that button. We can even grab more funds if we want. You can grab the address. We can come down here to the to the faucet and say oh I want like way more. Let me give me like I don't know 1000 bucks. Send. I'm able to just grab that that those funds from hardhat just dump them into my burner wallet. So I'm instantly ready to test. I don't have to worry about metamask and nonces and junk, right? So let's let's make a change. Let's grab this address, okay? And we want to do like access controls. Now we have access to this wallet. By the way, the private key is here as well so you can export the private key that it generates. So it's full functional wallet, right?
We can also import private key. So if you just want to use like some one that you keep keep using. And then let's go to the thing and let's define that variable, save it and then redeploy okay? Let's make sure it deploys correctly first, which it does. Okay. And then now what do we see? We got a new variable, owner, right? So, what we could do is say like, all right, let's make sure that only someone who calls this function here is an owner. Uh like an access-controlled uh function. We could do something like a require statement right?
And we could say require that the message.sender uh is equal equal to owner, otherwise you are not allowed to call this. I don't know. Something like that. Um so, what I'm doing here is I'm just basically like toying around with my smart contract, redeploying my changes. Instantly, I was able to get up and running with a DApp uh and testing my assumptions. Uh so, there we go. Um we have our contract. We got a new one. And then now if I want to come over here to this incognito tab, get some funds, go to the debug contracts, and try to change the purpose, I don't know, to foo, like this, it's going to say you're not allowed to call this, right?
I've done some basic access control, right? But if I come over here and I can say foobar, shh, send, and it lets me, right? So, good, bad, right? Uh but let's let's go a little further. Let's let's actually like wipe out like this concept of like hard-coded addresses, and let's use Anyone familiar with OpenZeppelin? OpenZeppelin, right? Awesome. If you don't know OpenZeppelin, got to follow those guys. Um The first thing The second thing you'll learn with smart contracts is inheritance. So, we can see right here, we can do like an import, right? We can import an OpenZeppelin contract, and then we can make our contract inherited from that, right? And you'll see a lot of the examples like ERC-20 example, ERC-1120 1155 721, use this import and inheritance function.
So, we let's do that, and then let's save that, and then let's deploy again. So, instead of doing a hard-coded owner, we're just doing this inheritance method, and we don't have only owner as the thing we got to do is we got to inherit the modifier. So instead of using a required statement, we're using a modifier which is part of inherited access control owner contract. But you can see what we're doing. We're toying around. Let me deploy again cuz I forgot that. And then let's go back. But what what's different now? Someone want to tell me what's different?
The owner address. It's unexpected. What owner address is that? It's just a random string, right? Well, Hardhat uses the very first uh contract address or I should say the first like it has a set of 20 accounts. Uses the first account to deploy the smart contract. So what's happened is we've had Hardhat deploy our contract, but it's hold on to the contract. Now we can we don't have any access control cuz before we had hard hard written the hard coded the owner. So the next thing you'll learn you'll learn with Scaffold Eath is that you need to be able to change the deployment. So if you look right below the contract folder, there's a deploy script. Okay? And this is where when you do a yarn deploy, this is
what's what's being processed, right? This is what's running. And you can see here that we get a copy of our contract. We we set the deployer as the deployer, which is from the named accounts coming from from Hardhat. And then we can see here that we get an instance of the contract. And so in this script now, we have a copy of the contract here. So what we what we can do is call an inherited function. Right? And if you know owner or owner OpenZeppelin owner, it has what's called transfer ownership function. Okay? And you'll even see it down here on the bottom cuz we inherit it. And you can see it's right there. So now what we need to do is actually transfer the ownership of our smart contract to us.
And so we can grab that address again like this. And then you actually you can see right here that we have uh I I have it like kind of like grayed out in there. Uh so we can just grab that and then here we can take the contract, call the transfer ownership, and then put the address in here. So, sorry, let me grab it again. And then save and then redeploy. Give it 1 sec. That worked nice and fast. And now you can see that it owns and matches again. So, we deployed the smart contract, called the transfer ownership function, and transferred it to our self.
So, that's cool. We we've been able to kind of toy around with our contract and you see how you get in this kind of loop where you like mix it. You have hardhat running, you have React open, you're testing your your smart contract, building some stuff. Um and then you just kind of start testing those changes. Um and you can even do things like um I'll just do a couple more things here just to show you. We're done with the deploy script, so we're good. Now we have kind of access control. Let's pretend that we don't want to actually call only owner on that function because it's kind of boring to have like one function that only the owner can call. Let's make it a public function.
But we could do something like um let's require uh like a amount of value uh is sent. Um let's do uint256 public price equals I don't know, point uh 0.001 ether. So, we can set a a a a variable price and then we can come over here and then we can get back into our requirements, right? And we can say something like require uh message.value, which is the the transaction value, is equal equal to price or we say not or not Something like that, all right?
So, now what what if we deploy our change, which uh oh, sorry. One I always forget this. You have to make that because it's we're accepting value now, you got to make it a payable function. So, let's make it payable. Save that and deploy. Did I spell it wrong? I did, thank you. One more time. But you guys can see what how how easy it is with Scaffolding to get up and running with a project, um, you know, using this and start doing some changes.
So, cool. Now we have a function, but what do you see? Well, the UI is adapted and it said, "Oh, well, now we need to pass the value." So, then you can start testing and we even have that new variable here, which is showing you the value in way. So, um, the first thing, another thing you'll learn about, you know, when you're, uh, writing smart contracts is you need to send your transactions in way and not in Ether. And so, um, you know, what this does is it kind of just gives you the variable. And if you didn't have this variable here, like you could use this little cool green little icon that does the math for you, which one Ether is, uh, equal to times 10 to the 18th power in way. So, what we could do is say, "Okay, well, now we have this this function. We
want to change it. Let's set the purpose to Bogota." And then we have to pass in if we were to pass 0001 Ether. Sorry, let me do zero just so that doesn't freak out and then click this little button. That would be the wrong amount. It's going to tell me not enough, right? But I could just grab this and copy this. Paste it here. Send. Boom. It worked. So, now what happens with our smart contract? We have it has value. Right? Now it's actually storing some value, which is cool. Uh, it's like this little vending machine, right? So, we could probably write it like a withdraw function in there. That would be cool, right? And then we that one we would definitely want only owner, right?
So, we could use that only only owner modifier. Uh, but let's just do one more thing that I just want to show you with like one small change. You could do something at the very bottom of your function that says price equals to price, I don't know, times two. Like that. And then deploy that change. And with one small line in the contract, you completely change the behavior of the of this, um, kind of vending machine. It deployed successfully. And now if we change the, um, thing to, I don't know, uh, Colombia. Boom. Send. Now we'll see that the price adjusted, right? We did this simple modifier, right? Okay.
So, I think you guys get the idea. It's It's It's just kind of like toying and like tinkering around with your smart contracts, uh testing your assumptions with multiple identities, right? And multiple browsers. Um if you have problems and you want to like uh you know uh if you have any issues, there's a Telegram channel for getting uh getting like uh problems resolved. Um I'll be around. I'll be shooting photos. Uh but I'm more than happy to help you guys out if you have questions. Um yeah, that's pretty much what I wanted to go over. Uh any questions? I'll try to at least leave some time for questions. Yeah. Uh yeah, you said that it launches like the contracts from the first account Yeah.
Um can you not do that? Like on like a chain or like a test Uh yeah, yeah, yeah. So, if you're um yeah, if you're I mean it Are you saying like for a test net? Well, so let me let me kind of show you real quick. You can generate an account. So, you can do yarn uh Here, let me open this so it's full screen. You can do yarn run generate. And what that's going to do is generate a a special deployer mnemonic. It's going to place it inside of your uh uh directory, which I won't click on it, but it's right here.
So, it has that's the the deployer address. The mnemonic is in there. So, you can inject that in a MetaMask as well, so you have access to it. And then you can do a yarn account like this. Um and you can also like What I do is like I have a punk wallet on my phone, and it pops up a QR code for you. It goes by real fast. But, you can just scan the QR and send some funds to that one, and then now you have this kind of like wallet that you have access to the private key, right? No. No, this is a it's stored right here on your thing. Yeah. Yeah, that's your deployer account. So, yeah, that's a good question. I was going to show that, but I didn't have enough time, and I don't I want to be respectful of other people. I was going
to deploy it, and you can also verify your contract with a yarn verify. Um you can do all this cool stuff. Uh you can also do yarn surge, and you can upload your um, your dapp. Like you can build it with yarn build and then yarn surge and it'll upload it to surge. You can also yarn IPFS and push it to IPFS. So, there's all this stuff that's just already written for you. You don't have to hassle with it. Just take a look at the package JSON file and it has all the yarn commands. And you have one more question? Yeah. Testing, testing, writing tests. Have you talked about that?
No, there is a test in there. So, you can do like uh, yarn test and there's a there's a test in the hardhat test and you can create and build around that. Um, if if I did it right now it would fail because the test is designed for like the hello world one that comes with it. Um, but yeah, you can definitely just do a hardhat test and I think that directory is uh, right here, test, my test. And you just you write it like a normal hardhat test. Okay. So, it uses chai basically. Hardhat and chai.
Yeah. One more? Yeah. How hard would would it be to get this working with Foundry? So, uh, there's uh, sneak peek, there might be a future version of uh, scaffold-eth that has Foundry. Um, there actually is one I think someone's already built actually that um, it's not I don't think it's in here yet, but I would just keep an eye on the repository here and see. I could actually check right now. Um, Oh. There is one. Yeah, so there is one with Foundry I think. I think it's kind of like Yeah, I don't know if it's been tested too much, but um, probably not the recommended one to to use for this hackathon, but um,
yeah, looks like there's already one. Yeah. Thank you guys. Appreciate it. Mhm.
