👩‍💻 Web2 to Web3 🚀 - Week 2, Day 5 - 🏗 scaffold-eth Introduction and Solidity Recap

Web2 to Web3 Curriculum · 18 Aug 2025 · 1:51:45

Austin Griffith and the BuidlGuidl team introduce Scaffold-ETH as a fast way to tinker with Solidity. They install it, deploy to a local chain and see changes appear in the front end, using burner wallets. The session revisits primitives, globals, mappings, events, a token, OpenZeppelin Ownable, the receive and withdraw functions, contract to contract calls, msg.sender versus tx.origin, and a re-entrancy attack and its fix.

Speakers

Topics

From Web2 to Web3 Curriculum, hosted by BuidlGuidl.

Transcript

What's up? I'm Austin. I'm here with Eda and Carlos. And we are on day five of week two, the 10th day, the 10th day of building. Uh today, we're just going to kind of rehash those not rehash. Hashing is like a a verb already. We're going to cover we're going to recover we're going to cover again the Solidity basics. Uh I think last uh yesterday last session we did hard hat and kind of like how you can write solidity and then run tests against it. I think today let's do solidity but let's do it in scaffold ETH and we'll we'll use a little bit different tool and that tool is going to help you have a lot more insights and give you the ability to tinker with your

contract. So I feel like we should start with just like a simple installation of scaffold since it is kind of a pain in the butt to install. It takes like 10 minutes. So maybe we could kick it over to you, Eda, and you could show your screen and we could start from like complete scratch, pull up a terminal, go to the the repo, start cloning it, do start the install. That's going to take time. And then I'll steal the screen back and show my version that's like already installed and ready to go and we can kind of tinker while the installation is going. All right, let me kick it over to you. Yeah, you got it.

You got it. Awesome. We just basically Googled Scaffold Eat, right? >> Yeah, just Googled Scaffold Eat. is the first repo that comes up and let me pull up my terminal over here. So, but before that, um, so yeah, you also have the read me over here on how to do this, but let me just go over here, get the get the repo. Going to go to my machine. Um, I'm just going to go to the desktop and then let's make going to call it Zoom because that's what I've been calling everything.

So, this is going to take a few moments to to get all set up. Um, but yeah, basically just Google scaffoldy, go to the first website that pops up. It's going to be GitHub, and then clone the repo down. >> Awesome. Okay, so we're cloning it down. I might even steal the screen back because I don't even know how long that's going to take. >> Yeah, it takes a few moments. >> Yeah, it does. Okay, so here we go. Uh, let's see. Let me pull this over here. And we got the Sanford token we might mess with a little bit. But once you get scaffold ETH uh all the way installed you have So there's I did the scaffold ETH.io. So you you clone it down and then you're going to do this step next,

but uh I've already done this and then you yarn chain and that brings up your hard hat node. So, just like hard hat like we had before, you'll have a hard hat node with uh uh scaffold e and then you have a front end. This was the yarn start. Okay. And let's do this all again on your machine too, but I just I kind of want to speedrun through it. And then you do a yarn deploy. And that's going to deploy your contract. This time it's probably going to say it's reusing your contract. This is the first little you need to do a yarn deploy-reset if you want to force that contract to go. Okay.

So then we've got our contract here and oh man, I've got this zoom bar. Let me get out of here. There we go. And then uh here's the front end. So So this is that that same mode that we were in where we were kind of making a small change in solidity and then compiling it and then making sure our tests run. you get into the a similar iteration loop with scaffold ETH, but what you're doing is you're basically tinkering with your solidity and then yarn deploying and then you'll get your contract over here in the front end and you'll kind of tinker with it. See how see how we got a new contract address there. Let's do it again and we should see a new contract.

So, this is a little bit different than the hard hat version where we were just like editing solidity and then we were running our test or writing our test to make sure it works. This is a little bit more like visceral and upfront. You kind of can make a small change. Let's let's just add some more exclamation points just to make a just a simple string change. You deploy and then you should see it show up in your front end as it as it deploys and hot reloads. So this is just a different iteration loop that you can get in, but very very powerful and very quick to to be able to edit some solidity. Uh but did you let's see. So you did a yarn you did a get clone. I I'm suppose you're probably about to run your yarn install or did you already run

that? Eda, >> I'm on 72%. >> Okay. Okay. >> So, we're still waiting for that. >> Okay. I'll just steal the screen and hold it here. Yeah. Okay. So, uh let's just edit some solidity, right? Let's let's get into this mode of editing solidity. So, let's see. We talked about primitives and data types and globals, right? So, we talked about globals. Uh solidity. Let's have those up, right? You'll want to have your, you know, globally available units. Let's even just like pick one out real quick. Let's look at the uh block.timestamp. Okay. And let's just create over here. Let's just create a read function real quick.

And we'll call it tiny. And it's going to be public. And it's going to uh returns a unintent. And let's just return the block timestamp. So, anytime you're just like dinking dinking around with these uh globals and you want to know how they work, let's go ahead and deploy some new function that has some new timy. And let's see if it adds it over here. There we go. And we can call this Oh, wait, wait, wait, wait, wait. That should be a read function. public views.

Come on. Did it even it gave us a warning even? Yep. There we go. This is the kind of iteration public view. Is that right? Why didn't it turn view into maybe I have an old There we go. There we go. And now we've got our time and and as we kind of make uh transactions that time should change or if I deploy again we get a new time stamp. Okay. So now we've got our global units. We've got a way to kind of view those global units right away. A nice way to tinker. The next thing is solidity by example.

Right. And we talked through I think primitives. Right. So, we talked about, you know, you can bring in a bool. Let's let's throw one in here. Okay. And you could bring in a UN 256, right? Uh let's do that. Right. Oops. Okay. And you could bring in an address, right? All of these all of these primitives and data types we're just kind of pasting into our contract. And then I'm going to do a yarn deploy. And then I'm going to go look at it on the front end and tinker with it and make sure it does what I think it's going to do.

There we go. So, our Google is true. Uh let's maybe even build a function real quick called toggle that does something like uh boo equals not boo. All right, just simple functions. Let's see if we can flip that bit. Let's see what it does. So now we should get some uh what did we call it? Toggle. Okay, we should get some toggle function over here. So now boo is true. And if I call toggle Oh, it's not going to because I don't have gas. We need to explain uh wallets real quick, but I'll get to that. Grab some funds from the faucet. And then when I hit toggle, boo toggles, right?

We're toggling our boolean. There we go. There we go. Okay, let's get I'm going to dump that stuff out of there. just just kind of getting getting it shown off how you can get into this habit of throwing some solidity in here and tinkering with it over here on the front end. Uh I'm gonna delete is our Uint there. Let's just pull out all this stuff. Get back to our uh simple contract. Okay, wallets, right? We did something really weird with wallets there. I feel like that's worth explaining with Scaffold ETH. If you open up a new incognito window, look up in the top right here. We're going to have a new address. There we go. There's that red address. Now, if I close it, see how he's kind of like this red dude?

You can grab some funds from the faucet for the red dude. Let me close that and open up another one. We should have a new account. There we go. Now, it's like green dude, right? One more time. Local host. So, Scaffold ETH creates burner wallets for you on the fly, and it makes uh editing and tinkering with your contract a lot easier. Uh let's go ahead and redeploy our contract. Okay, so let's see. So we've got we're we're able to put in uh globals. We'll we're able to bring in some primitives and tinker with them. Uh let's let's uh let's see. We've got burner wallets.

We'll bring in the burner wallet in a little bit. Um what I'm thinking is we should just probably write a small small contract that does some kind of like using these primitives. Maybe we should even explain the contract that comes with Scaffold ETH out of the box and then do some solidity tinkering. Does that sound good? Okay, so we've got it looks like there's a string here. Is that hopefully that's very readable? This is this is very much like the uh the greeter contract. There's a famous greeter contract where they uh you you have this greeting or you have this string and then you have some function to set that string. Something really interesting here is a nice little console log here. Whenever someone calls set purpose, uh, it it does a console

log over in the hard hat node. Let's go do that real quick just to see it. So, I'm going to need to call set purpose with some new purpose. So, I'll say hello world here. And I have some money for gas. And and you'll notice with that burner wallet, there's no MetaMask popup here. There's no uh it just fires the transaction off, which is really nice. And then over here in the console log, we see see sure enough, we set the purpose to hello world. Okay, so we're in the mode, right? We've we've downloaded scaffold ETH. We we know what our globals are. We know what our primitives are. It's time to start tinkering a little bit. Let's let's do what we did last time. I think we brought in like a little bit of an ownership pattern, right? We had some boss. Let's see. Uh so we'll make an

address public boss and we'll set that equal to our frontend address. I'll just copy and paste that in and let's ship it. Okay. And we're just gonna do like couple lines of solidity and ship it over and over and over again. We should see it. There we go. So now we we see our boss, right? Over over in the hard hat version, we kind of had a test that we would run, right? We would set the boss and then we would make sure that the test made sure that the boss was set here. We're just kind of doing it visually, right? We kind of like we've got our boss, we've got it set, that looks good. Then maybe we require that you have to be the boss to set this, right? So we're going to say message.ender. That's the that first global that we learned. It's whoever is

making that message or whoever that it's coming from. Once we get to contract to contract, this will be a little different. Equals equals boss or we say not the boss. Okay? And deploy it. Right? So just small changes to your solidity. Test your assumptions. Try it out. Now we should only be able to set the purpose as the boss. So this should work. Okay. Okay. But then how do we test the counter right in in well in the last week we had to basically write a test where we had a second account try it.

Let's just try it with the second account here in in just an incognito window. Right. So we've kind of got green guy over here. A this guy's kind of green too. Let's let's get a different one. Get an account so so we can have a good bad guy. Yellow guy. Okay. Uh it's kind of still green. Come on. All right. Last try. What? Whichever one we get here we're going with. Okay, so this is bad guy. Kind of yellow, yellow, red, yellow, red guy. And then we've got green guy over here. Grab some funds from the faucet. And if we try to set the purpose trying this, hopefully we get a message that we're not the boss.

Okay, so instead of writing a test, which a test is really nice because you can have it automated and any little change you make, you can run your entire test suite. But when you're just getting started and you're tinkering with Solidity and you're trying to figure it out, this is really handy. We can we can write a small require statement and then just using the front end we can kind of quickly test if that require statement is working and it seems to be working so far. What's the next concept we should we should go after? I feel like I'm talking a lot. What's what's the next uh thing we attacked yesterday or last se session? Uh I was thinking that maybe we could take a look at the deploy script that we are using for this uh scaffold

deploy and maybe like um putting like some params in into the constructor function. So we can like send like some you know some if or or some maybe that that the owner yeah like the boss address right like we can send that into the constructor. >> There we go. That's a good call. Okay. So uh we're going to have some address. I'm even going to make it immutable. Uh, is it public immutable? So, it's only going to set get set once and we'll call it boss. Okay. And then we'll set the boss to the starting boss. And it should never change again. Right. Quick little thing.

Let's see if that worked. Quick little interaction. Let's see if this deploys. Oh, no, it did not. What did I do wrong? Let's see. Oh, arguments. Right. It needs an argument passed in. Right. So, like you said, we go here. We're going to go to the deploy script in scaffold ETH. And this is already commented out for you. There's a lot of scaffolding in scaffold ETH, right? This is just another little piece of scaffolding here where it's like almost ready to go for you. What do we need to do? We need to put in this dude's address probably. Okay.

And let's try that. There we go. Okay. Uh, and one other thing I want to do, let's do let's do another address. And let's call this the deployer. And let's set the deployer equal to the message. sender. There we go. So, we're going to deploy our contract. We're going to pass in some boss. And then we're also going to have some other address that we track that's called the deployer. That's going to be whoever deploys the contract. There's kind of two addresses. There's the boss address and then kind of whoever deploys. And we'll see that these are slightly different addresses.

the deployer is going to be that first hard hat address. We we've talked about this probably a couple times before, but as hard hat spins up a new chain, it has a whole bunch of addresses attached to it. And so who who's deploying our account is this the F39, right? We've seen old F39 before. Yes. And another place you'll find F-39 is down here in the faucet. If you open this up, this is basically F39 totally loaded with with 10,000 ETH. And and this is a good time to kind of show off wallets and scaffold ETH. Uh but if I copy this guy's address and I open up the faucet wallet, look what I can do kind of right here in the UI, I can give myself a whole bunch of money. There we go. So now I have I have

enough money to do a lots of stuff. Uh instead of kind of sending them around or dealing with accounts here, you don't want to be copy pasting private keys. You want to generate a nice burner address and fill it up with the money you need and then go go interact with your contract. Okay, so we do have construction arguments now, right? And we are passing in some starting boss and that boss is immutable. It only changes once and gets set here. But we've got our boss, we've got our deployer. Uh only our boss can set this. Okay, this this is a good time. This is usually where I Sorry, I'm like stealing all the time here. You guys jump in if I need to. Uh, if you need to. One thing I like to do is I like to do a payable here

instead. So, I make this payable. I turn it. Right now, it's basically like an attestation smart contract. One person can get in and change something, right? One person can set the purpose. We know that smart contracts are more like these vending machines, always on permissionless uh uh programs that people can access and run. So let's make this a little bit more decentralized and let's say message value needs to be greater than or equal to some amount of ether. Right? And then we'll say not enough. So we just made a small change to our contract that instead of one boss gets to set things now anyone can get in here as long as they pay a little bit of value. And sure

enough our our front end auto adapts to that right? It notices that the set purpose now needs to take in some transaction value. And so let's set that to hello world again. And if I put in zero transaction value, it's going to say not enough, right? What we need to put in is 0001. And then here's another little trick from scaffold ETH. You need to take that times 10 the 18. Right? We need to take it to weigh. And scaffold ETH is going to force the developer to do this. If you're a builder, I want you to be forced to say, well, it's one, it's 0.001* 10 18. I want you to be thinking about that. So, but when you build your final app with scaffold ETH, obviously you'll

you'll do a little different and you'll have it so your front end handles that kind of stuff. But here we go. Hello world. Let's put in 0001. Let's take it times 10 18 and let's pay. There we go. So, we were able to test this line and make sure you have to pay. And now we see that there's a contract here and the contract has value, right? So, we're able to set this up. Let's maybe even do a 256 public price. Okay. And set that equal to 0.001 ether. And then we'll put that in here.

All right. So, we just have a price function now. And anybody can see in the contract what the price is. Let's deploy that. Just making small little iterations. Just showing you at home that you can paint some happy little trees here. Like you can build things very quickly. You can try things out and you can see them in the front end. Let's add one little line of code. Here's here's where we're going to get weird. We're going to get weird and financial mechanism. We're going to say once someone sets the purpose, the price is going to go up by 1% or something like that. Right? Notice notice how we uh don't have decimals here. Let's see if this works. One one little change to the smart contract and now we have a price function that after I say hello world

the first time and hit go, the price goes up. See that? Now the price is at 101. Hey, hey, hey. And I hit send again and the price goes up again. Right? So with the single line of code, our vending machine now uh accepts money only on a price curve, right? Just random little things we can add in here that can make this uh uh really interesting. Okay, what things did we cover yesterday? I'd like to dive in and cover them like speed like quickly with scaffold. What what are some more topics that we covered uh yesterday?

Um I I think like also it's nice to have like solidity by example on the side to like >> Yeah. >> search for it like what can I use it really helps out. >> Awesome. Yeah. So we did primitives and data types variables constants immutable uh ether to way is kind of like happening a little bit right here. Right. We saw this where we typed in 0001 uh ether and we took it two-way. You'll that's another big gotcha of solidity. You'll have to kind of learn that gas if else looping. I'm I'm assuming you're like a programmer once you've landed here. So this stuff you should be able to pick up almost instantly. Mapping, right? Let's get to the mapping. Let's

build a token. Right. Okay. So we've got our deployer, our boss. I'm just going to clean a lot of this stuff out of here. Uh let's see. May >> maybe we can set like the the count. I mean right now only like the the boss can set you augmented the line so anyone can set the price right so maybe we can store the count of the people that has set like the purpose so we can have like a mapping with address and account like so every time that you set okay >> a new purpose you can like increment that count for that for that sender >> I'm going to make it a U8 too for some reason it only goes to 255 so There's like a your count right and

basically every time anybody sets the purpose uh we do your count for the message sender plus+ something like that pretty simple straightforward code. Uh let's see our price isn't going up anymore. We've taken out our price curve. Let's see if this works. So just right right now all we're doing is we're learning how a mapping works, right? We're saying, okay, a mapping is just these a mapping of some data type to another. For any given address, there's going to be some uint. I think we already talked about this, but the downfall of a of a mapping is that you can't iterate through it, right? So, if we want to keep track of a bunch of balances, but we don't have to iterate through everyone's balance. A mapping is a great way to do that. Uh, what we're

showing off here is that in scaffold ETH, this mapping is going to show up right away for you, right? Let's see. Oh, I forgot to make it public. Boo. [Laughter] Classic. When you add something here and it doesn't show up here right away, it's that public and you got to remember to set it. Okay. Now, we have a your count variable down here, right? And if I check my account for green guy, it's zero. Okay. Then if I set the purpose, hello. I can't spell world today. Hello world. And we pay some money. And then we do hey, hey, hey. And we do it again.

And then we go check our account. Get out of here. We check our count. Now it's a two, right? And then if we bring in another bad guy, they go to local host. I don't I like how all the incognito are bad guys. Everyone's a bad guy on the blockchain. That's what's cool about it. They're all adversarial parties and it still works, right? Okay. Hello. And this dude pays uh the 0001 ETH. There we go. And let's check his count. Red guy's count is should be one. Boom. red guys count as one. Okay, so that's just showing how mapping can work, right? We're we're going to solidity by example. We're picking up these uh uh uh you know ideas, these concepts, and

we're playing with them. Let's let's do a token real quick. I'm just going to clear out. Yep. Go ahead. >> Just just wanted to add because I saw also events over there. So, I think it's nice to show like how the events reflect to the app homepage as well. like it we're like looking to the smart contract but since it's over there >> uh might be worth like just showing >> okay before we build a token let's make sure this is deployed >> okay so we have okay let's forget our mapping for a second let's let's forget this mapping and let's just think about uh do we even need the deployer that let's clean this out just so we have a lot less stuff here we go we don't even need this stuff really okay So from from first principles here, we have a

constructor that sets some boss and the boss doesn't even matter right now, does it? Nope. The boss is we're not requiring anything. Maybe there could be a withdraw function and only the boss could withdraw. But what we're doing here is we're allowing anyone to set the purpose. They pay a little bit of money when they set the purpose. And this event is emitted. Right? We talked about storage and how onchain storage is very expensive, but events can be used to kind of like power your front end. So, let's see how this event works. It should emit a set purpose every time someone sets the purpose. Uh, let's go let's go tinker with that. So, let's go to the front end. Let's reload this.

Uh, then let's set the purpose to hello world and pay a little bit and hit send. Okay, that should have triggered an event. And within Scaffold ETH, you can use uh an event listener. And I think it's right down here. Yep, there it is right there. So over in our example UI, which we can see um here, if you wanted to really look through the front end side, the the there's a front end that's created for you with scaffold ETH. And what we're doing here is we're just showing you how you can uh there's there's an input field here. And then when it changes, we have some uh state variable that's the purpose and the set new purpose. And then we have this button called set

purpose. And what it does is it calls the set purpose function on your contract. So it's a nice little UI scaffolding that shows you how to build Whoops. Shows you how to build a UI that will talk to your contract. And so if I say hello world here and my hello world hopefully this works. Oh not enough. See this isn't paying money. Okay. Okay. Okay. Okay. Hold on. Let's chat it. Let's set it so it's not payable. No money going in. Very stock a stock set purpose. So our our example UI works.

Okay. Now over in the example UI, let's try a hello world. So there are no events, right? So, our events are empty. Let's do hello world. This time we're not paying anything. We should just be able to set set purpose. And let's see if those events show up here. There it is. Hello world. And if we do hey, and we set the purpose, we see another event, right? So, to go full circle on this, basically we're emitting the set purpose. Uh, and then the front end has an event listener, uh, in the example UI that shows you how to handle your events. Nice little React

component that that shows you how to handle those events and display them. Okay, awesome. Uh so the let's see we we did events we did before we do like contract to contract interaction. I really want to do a uh a quick little token with a mapping just build it's the same thing we built yesterday or the other day. Uh I'm going to just let's see we got a boss. Do we even need a boss? We don't even need a boss right now. Let's take that out of this. Let's clean this down to a very simple contract.

Uh, we don't even really need the purpose here. Yeah, let's let's look at this. There's nothing left here, right? Nothing. It's just a constructor. Why is the constructor payable? Does it need to be? All right. Let's make sure this deploys the basically an empty contract. It probably won't even show us anything. Yeah, it's nothing, right? Okay. So, let's make that token again. Let's start with that mapping. So we had a mapping from an address to a unintent 256. We need to make it public and we're going to call it balance balances.

Balances I don't know balances right. This this is basically all you need to create a token. You need to have some list of balances and then maybe uh when we deploy our contract we'll have our address get 100. And let's deploy it. Let's see if it works. Right? We're just tinkering with Solidity. We're seeing it in the front end. We're poking at it. We're making sure it does what it needs to do. Let's see what happens when we check our balance. Awesome. And if I like connect my MetaMask or something and check its balance.

[Music] Oh man. Was that it? Oh man. Uh >> yeah. Carlos and I had to come in there. >> Yeah. Yeah. Yeah. Come on. There we go. Okay. And now we're just waiting on MetaMask. Still waiting on MetaMask. All I All I want to do is just get get an account. Okay. There we go. Yep. Oh, it has this. This is another thing about Scaffold ETH is it notices what network you're on and switches you uh with a nice little button. Lots lots of little nice things with Scaffold ETH that like I just needed for hackathon after hackathon and I built it into this. Okay. So now if I check this guy's address after all that it should be zero, right? Okay. Log out.

That was that was not worth the wait. Okay, so we're keeping track of balances. We're setting someone's balance at deploy. But really, we need one other function to make this into a token, right? If we want to make a digital currency, we need some kind of transfer function. And we'll have our address 2 and our 256 amount. We'll make it public. Okay. And then what do we do? Uh we talked about this last time. Usually we would we would want to have some kind of require statement here to make sure that the two address has the amount. But since we're using solidity 8, we don't need safe math. Uh it's built in. So basically we do something like this. We say balances of the message sender

minus equals whatever this amount is and balances of the two address plus equals the amount. Right? And if we were writing this in hard hat, we'd build a bunch of fun. We'd build a bunch of tests to make sure that I send it and my money goes correctly and I can't send money I don't have. But with Scaffold ETH, uh, we're just kind of like trying things out here. We don't need to build any formal tests yet. We're just kind of tinkering with Solidity and kind of learning the the basics and the ropes. And this is what you're presented with.

You've got balances. You've got transfer. Uh, if I check my balance, I have a hundred. Okay, let's make another incognito address. All right, let's bring up this dude over here. We got blue guy. So, we got green guy and blue guy. Blue guy can grab some some funds from the faucet. If we check blue guy's balance, he's at zero. So, let's transfer two, right? We're transferring to blue guy. Let's transfer 10. And by the way, this is 10 way. This, by the way, this is 10 way. We're we're doing all of this here as 100. Whenever you write a number in here, it's just way, right? That's the same thing as way. the smallest amount of of ETH you can have if I put ether this is actually 100* 10

the 18 and a much bigger number right now we're just talking in way we're just moving around a little bit of way so I'm going to transfer 10 way right then if we check this dude's balance should be 90 and if we go check this dude's balance it should be 10 right so so we're able to create uh a currency by just having balances And then we give some initial mint to someone. And then we create a transfer function that allows people to move it if they have it. And if they don't have it, let's try that. So let's just grab some random address and try to send 999.

And it's going to fail. It's going to give us an overflow. Right? What it does is it's trying to subtract and it's getting an overflow there. So, this is this is yet yet another way to to tinker with Solidity and build a little token like we did last time, but in a way where you're using scaffold ETH and you can kind of quickly spin up these wallets and these frontends that can kind of help you tinker and and learn. So, I think eventually we're going to move to contract to contract interaction, but maybe we want to fill in some more details here. Uh maybe we could even go back to Eda and we could take last week's contract and kind of paste it in and you could show us how that looks on your scaffold. How does that sound?

>> Oh, I think you might be muted. I'm going to go ahead and stop sharing. Hand the screen off to you. I've been blabbering too long. It's your turn. Let's let's let's uh let's do scaffold eth on your side. >> Okay. Um I'm gonna share my screen. Okay. So, all I did was a yarn install. Um, so basically just went to the folder and did a yarn install. Um, because it takes a while so not to wait to see it install. But yeah, I have it open on my ID as well. Didn't like do anything.

Just open the package. So you can see a new scaffold eat package. Going to go down to my contract over here. We can actually like run it first as well. Oops. This is like verifying something. Uh, but maybe it would be good to like just start by deploying the contract, like setting up the chain, right? I didn't do all that, but just like quickly run through it. Um, >> there we go. So, yarn chain is like the first command. So, you did a clone, you did an install, and now it's yarn chain, and that brings up your hard hat node.

>> Yeah. And then a yarn. We like you can directly do a yarn start as well. It's like get the front end up. >> That'll start your front end. >> Yeah. So it's supposed to pop up. Okay, I have it here. >> Like if you are curious about like these commands like yarn commands, you can also check the package JSON file and you can see like for example like the yarn deploy. You will see that the command that is actually executing is like hard hat deploy like the things that we did the the other day. >> Yep. And and yarn is like a fancy npm, right? The the reason why we're using yarn is because it allows us to do workspaces and allows us to bring in like three different projects at once.

We have uh create react app, but we also have hard hat coming in and yarn sets that all up for us. But yeah, you can just look in the package json to see like what commands it's actually running underneath the abstraction. Okay. So, we've yarn installed. We've yarn chain to bring up our local node. We've yarn start to create a front end. And then, did you do the yarn deploy? >> Yeah. >> The yarn deploy is the third one. Perfect. And then we deployed our contract. Awesome. So, we have our stock contract. We have our front end. Here it is.

>> Yeah. And you can see like the burner wallet. Everything is up and ready. So, just what Austin was showing. Yeah. Just pull it. you have all the tabs before you do like yarn deploy, you won't have this. You need to deploy the contract to have it show up. Um, but yeah, so like shall we go to last yesterday's? >> Yeah, let's just let's just set the purpose real quick. Let's make sure the wallets are working. Let's make sure like we can make a small change to the contract and that works. Just just just a little like just double-checking things are going. See see if you can set the purpose I would say would be the first thing to test.

>> Okay, sure. We can just use the UI. We can use the example the debug. >> It's going to tell us we need some funds from the faucet. >> Yeah. Um, so I'm just going to use this over here. >> Three different ways to do it. You could hit the little blue button. You could paste the address in and hit faucet. Or you could bring up the wallet. And now we've sh we've I've we showed all three in this in this run. There we go. >> I have tons of fun. So I'm gonna get more.

>> Got it. You got to do that. Yeah, it's it's more gratifying. Yeah. Okay. And we set the purpose, right? Did it go? There we go. We see that we see the event. We see that it's tracking it. The example. Yep. Awesome. Okay. So, this is the stock scaffolding setup. This should be if if you have if if you have this set up locally, you should now have the ability to like really iterate on solidity. You should be able to grab some Solidity by example and paste it in. Let's go grab our old contract, our Sanford token, and let's paste in some of that stuff and let's see it in the UI. Let's see how the UI auto adapts to that uh new contract. I suppose you could just grab the whole guts of the

contract and try pasting it in to your contract and see what happens. >> Yeah. So, that's the the one of the days to like one of the days contracts, right? Not sure which day it was. It was it. Oh, here, here, here. I'll send it in chat. It's this one right here. >> Awesome. So, yeah, I'm just going to week two and getting we can I can actually just pull this up over here as well. So, you can see like all the contract. I'm not going to get like just going to get the inside of it.

>> Yep. >> Um, we can just copy this. >> Think you might have one extra brace there, but we'll be good. Brace yourself. >> Okay, then I'll just delete one. >> We'll figure it out. Yeah, we'll get we'll figure it out. >> Perfect. There we go. So, this is an example. You just find some contract on the internet. You can randomly just like copy and paste that thing into your contract and then deploy. Does it have con uh constructor arguments? Nope. This should compile and deploy, right? Like without any Let's see what happens.

There's only one way to find out. Then we need to do reset as well. >> Uh, anytime the contract changes, you don't need to do the reset. It'll notice that there's a difference. Okay, so it gave us one little tiny warning, but we will ignore that for now. We don't need that. But our contract just totally deployed. And if we go to Scaffold ETH and we look at the debug contracts tab, now we have all of these cool things from that that original contract. We have the boss, which is the deployer. We have some buy, we have some create, we have some send. I don't even remember what this contract does. This this was you, wasn't it, Carlos?

>> Yeah. But I mean, it's amazing to see, you know, because like we have been like for 20 minutes like tinkering with solidity and it was like super fast like if we try to do like the same thing like using hard hat like like yesterday is way slower to do that. And I think like we talk about the workflow which is for me perfect which is like going with um scaffold e first you know at least like tinker like the first you know when you're writing for from a scratch your contract and tinker a lot and then when you have like kind of like the final version maybe you can start like thinking about like making tests and because if you want to make quick changes like the hard way like it's like the hard way right so yeah so that's like want people to appreciate how fast

is like thinking this way like because you can see, you know, like super fast. You make a change, deploy in two seconds, you just get like instant feedback from the from the UI, which is great. And hard hat's like a wonderful tool. Like we we love some hard hat, but uh basically writing your smart contract in in the form of like test driven development, right? If you write the test first and then write the smart contract, you really know what you're doing. you really understand all of solidity and you know exactly what that solidity needs to look like and you're writing it for the test. Most of the time you're still learning and you're still trying to like do I want to keep this in a strct in an array? Do I want to keep

this in a set of mappings? So, so Scavable Leaf allows you to kind of like tinker with things first and figure it out and like you said, Carlos, build the test after you have the contract kind of working the way you need it to, but assume that you can come into this kind of like pretty blind and you can try this out and you can learn things. So, what do we have here? We've got a total supply. We've got a total Oh, yeah. We we had this total created and we paid attention to Yes, there's a creation price. We kept a strct of votes and an array of all the votes.

We have the boss getting set. Oh, we have a modifier. So only the boss can can set it. Then we have a create that increases the Oh, make sure the total supply isn't. Oh, that's what it was. It was a supply cap. We created a cap on the supply and we only let you create a certain amount. We let you send it around. What does buy do? lets you pay. Oh yeah. Message value equals creation price. So you pay a little bit and you get you pay one and get one. >> I think we have like the create only for the boss. So yeah can be like for free and then buy just for like the public.

So they need to send some ETH along the transaction. >> Okay. So the first thing I'm seeing here is the boss is the deployer account. And a lot of times we're going to want to have us as the boss. So this this goes back to our deploy script and it shows something really cool with with scaffold ETH. What we need to do is set it up so it transfers the ownership, right? Uh oh. Which which leads to maybe we should do inheritance and have an ownership pattern, right? Okay. So right now it shows the boss and the boss is the deployer but and we have our own modifier. Okay.

Okay. Okay. Okay. Yeah. Yeah. Yeah. I think what we want to do is inherent o open Zeppelin's owner ownership pattern here first, right? Yep. Yep. I think we should Okay. So, man, this is it's kind of a tr tough transition to make because we have so much other stuff in the contract. Like the best way to show this off is just to have just the ownership ownership stuff that we've written and then carve that out and inherit it. And then you see that like, oh, we didn't write the ownership stuff. We've inherited it. But there's a lot here. So, I don't know. It might be extra complicated, but I I say we work through it. Do you have a >> maybe we we can just like test like the only boss modifier. I think the create function is using it right.

>> The problem is that the uh we the boss is the is that F9 account. So we need to set us as the boss. >> But we are using a scaffold key, right? So we can like iterate like super quick. So maybe we can just like paste like copy the the front end address and just like equal to that that address, right? Just >> let's do that. Let's do that. So let's copy that address and let's go to the smart contract. Where is it setting that boss? It's setting the boss in constructor, right? So, instead of using message.cender there on line 29, let's just paste our address in very heavy-handedly.

>> Boom. Now, we're the boss, right? Now, now go ahead and deploy that and let's see if we're the boss. >> And this is this is exactly right. This is what Scald is all about. Quick iterations like >> make make big breaking changes and try things and see what happens. So, look at that. Now, we're the boss, right? There we go. It's deployed and we're the boss. So now let's do that. Uh what was it was create? >> It was create. So you can create like like 10 tokens >> and let's see what happens. >> We can create more. >> Okay.

>> 108. We created 108. >> We can do more then. >> Okay. Now we need a bad guy, right? So we need to maybe >> you can also like check the the balance, right? You can on the on the balance on top you can like check your address there >> and see if we have like the 100. We have 108. >> Okay. Now, we need to have a bad guy. Can a bad guy create? And we should see that a bad guy can't create, right? Instead of writing a test and having the test say it passed and having this weird like, did it pass? Do I really understand what's going on here? You can see it visually and make sure you understand what's going on. Okay. So, first one, it's saying, "Sorry, not the boss." But also, you don't have any gas.

It's nice to like make sure you have gas. Yeah. Now, try it again. It's also it's going to say not the boss again, but it's always just a good practice to do that. Yeah. Okay. So, so without writing a complicated test, we basically just created another user and tried it and we saw that it fails. So now it's set up. So I think only the boss can do the create and that's great, but we've written our own custom logic. We've created this custom boss. We've created the require statement. uh maybe we did it wrong, right? So, uh the the key here is that a lot of other people in the space have written some great contracts for us. And whenever we can inherit

someone else's contract, unless we're like, you know, really low-level, we understand what we're doing here. And arguably with an ownership pattern, you have to keep track of one address and create one modifier. So in this case, maybe the ownership pattern could be something you write yourself, but let's just inherit Open Zeppelin's ownership pattern and let's carve out anything where we had to make our own owner. Instead of us keeping track of an owner and setting it, let's inherit the smart contract from Open Zeppelin that keeps track of an owner and let's use their ownership pattern.

So on line five, we'll uncomment that, right? We'll import open Zeppelin's ownable. Then on line eight, we need to inherit it. So we'll say uh your contract and we'll say is ownable. >> Yep. >> And maybe we can also take a quick look into the contract, right? Like into the ownable contract so we can see what what we are >> inheriting. And the way how do you do that? Do you usually just like Google open Zeppelin GitHub for me like that? >> In my ID if you click line five it go it goes to the contact but I'm not sure if VS code is going to work. I I say like control and click into ownable.

>> No. No. Okay. So >> something happened. >> Oh yeah. Go to definition. Yeah. >> That's good enough. Right. That's good enough. That's So this is the contract we're inheriting, right? Yeah, >> cool. And it it itself in inherits some contract called context. But let's ignore that. >> So basically what do we see here? We see that there is a constructor that transfers the ownership to the message sender, right? And then there is an owner function that gives us whoever the owner is. And then there's a modifier just like our modifier that says only owner and make sure that the owner is

the message. And if it is, it does the underscore and runs everything. And then there's a couple other helper functions. Renounce ownership, transfer ownership, and transfer ownership. Whoa, what's the difference? One is internal and one is >> Yeah, I think >> Oh, this man, this is this is classic like Open Zeppelin. They do a little bit of extra stuff and burn a little bit of extra gas every time. They're making sure you don't transfer the ownership to the zero address. They have a separate function called renounce ownership that sends to the zero address whatever.

>> Yeah. >> But the cool thing is lots and lots of people have had their eyes on this contract and if there was something wrong we would know about it already. Now when you inherit stuff you still need to have your whole system audited but understand that these contracts are pretty solid on their own. >> So let's inherit it. Go ahead. We like we have like the same thing that we have before, right? Like the only boss modifier plus some other stuff that they added like the transfer ownership and also like you say like a lot of people have tested and and put their eyes into the contract. So there are probably no errors or bugs in those contract and if you do it yourself you probably have like some bugs at some point in your solid career for sure. So totally whenever you want you can like go for

inheriting stuff that is already done for you. Unless your transmission's 11 and you think all of the open zeppelin stuff is garbage and waste gas. Once you once you get like super technical and super into the weeds, you may have other but as you get started, this is absolutely the best way to go. Inherit stuff from open zeppelin. Things like even the ERC20 standard. If you're trying to make an NFT, basically if you're trying to make an NFT, you you inherit Open Zeppelin's NFT and you set the name of it and you set up how the mint function works and that's it. Everything else is handled for you. So, lots of things like access control, lots of helpers like cryptographic helpers and EC recover stuff. Tons of different things that you

want to get into, you can grab from the Open Zeppelin stuff. And if you just Google Open Zeppelin GitHub, you'll see all their cool contracts. Okay, so back to our contract. Uh, it is I think we set it so it is ownable. Uh, there we go. Yep, we have is ownable. And then we probably don't need to track the boss anymore, right? So, we can delete line 17 >> and also line 27 >> 27. Yep. We don't need the the line for the constructor anymore because uh it's doing it in the constructor from the one that we inherit. We don't need the modifier anymore.

And now we can on line 30 we can change it to only owner, right? Which is the modifier that comes from the library. So there we go. We stripped out a handful of things from our own contract and we just inherit it from another contract that's safer. Hopefully that's clear, but let's run it and see if it works. Let's see if it does the right thing. >> I think there I think there is another boss in there. So >> is there it'll it'll yell at us right here. >> Yeah, let's see. Yeah. >> Yeah. Yeah. >> 54. >> Yep.

>> Yep. >> What was it? Maybe a withdraw function. Yep. That's Yep. >> Awesome. There we go. Oh, and boss call. What are we going to have? Is it underscore owner or owner or something like that? >> But I'm not sure if the that's private. I don't know if that variable is I mean, we'll need to check the the ownable. Yeah, >> if we go to the ownable.soul, we can see how we get the owner. So I guess do we need to call right? >> Yep. So we'll just call the owner function and it should return the address.

>> So do do we do owner and then open parenthesis close parenthesis call? >> I think so. >> So I think we replace boss with owner parenthesis. Yeah, like that. That's what I was thinking too. Uh oh, it's not. >> And maybe maybe without the parenthesis. I don't know. >> No, it's happy. It's happy. >> Yeah, let's try. Let's try it. >> It doesn't like the fact that we're not using that data, but that's fine. Let's see if this works. >> Yeah. Yeah. All right. All right. So now let's make sure that only the owner can call that create function again.

>> But the thing is that now we are not the owner right? >> Oh yeah. Look who the owner is. >> Because if you check like the constructor of the ownable contract, you will see that it sets the deployer as the as the first owner. >> Yep. And we can't heavy-handedly paste our address into this one. We're going to have to do it right. Right. >> Yes. We're going to have to transfer ownership right? >> Yeah. >> Okay, cool. So, if if we go back to the the scaffold ETH real quick, you'll see that the transfer ownership and the renounce ownership have showed have showed up here, right? We see that we have we we could technically transfer the ownership. And if we look at the owner, it's our friend F39, right? So, the deployer owns the account and we need a way for the deployer to hand the

account off to us. What we need is the deployer to call transfer ownership to us. And the place we do that is in the debug or in the deploy script. Yeah, we could also do like if you want like we could like import the private key of the F39 because like like that's like like really ugly maybe but like you can import that into MetaMask like connect with that account and then you could call the transfer owners here from from here but you >> and a lot of people do that a lot of coders right away they think that's a great idea and it it works right and then then you have this nice account in your MetaMask that has all the local money and it's all connected But that's a bad practice to be copy pasting those uh private keys into your

metam mask like that. You need to definitely have a debug meta or a developer metam mask. If you're going to do that it's it's as soon as you learn how private keys work and how the systems work like everyone does that. Everyone starts to copy and paste private keys around. It's a bad idea. Like you'll get yourself into a lot of trouble with that. Let's let's technically you can do that. You could easily go get the private key from hard hat. You could paste it into MetaMask as a new account, but let's not do that. Let's do let's do a transfer ownership instead. Let's have a nice burner account and when it deploys, let's have it transfer. So, we need to go to our deploy script. It's not that script.

This is the old one, right? It'll tell us, yep, that's the one. Yep. And if we get in here, we see where those arguments were. This is the same thing I was doing. I comment I uncommented line 23 and I added some arguments. But like that scaffolding, there's lots of scaffolding here. See these comments that are set up on line 29. Yep. Go ahead. Go ahead. >> No, no. I was going to say like yeah, line 29 and line 30, right? >> Like if you see like on the line 30 like it's calling the like one function of the contract like the set purpose. So we can call like the same instead of set purpose, we can call transfer ownership and pasting the front end address that we that we have.

>> Yep. So So line this is our deploy script. This happens when we run a yarn deploy. On line 20, it does the deployment. And then once you have the contract deployed, it's going to do some extra stuff. And we can see on line 29, it loads the contract up. So it connects to that deployed contract. So I think what we want to do is just uncomment that line 30 there. >> And we want to call uh instead of calling >> Yeah. And then maybe trash that other stuff for now. Or maybe even comment that line. Yeah. So, this actually does explain like the ownership pattern. Oh, yeah. It's right there. Oh, yeah. Look at that. Look at that. Thank you, whoever put that in there.

>> Let's just uncomment that one, right? And then get rid of 30. >> And we want our address. >> Yep. I I don't think you want line 36, though. >> No. >> Yeah. No, you don't need line 36. Just >> Okay. >> Yeah. Right in there. There we go. and you need quotes around it when it's not in the smart contract. There we go. And we don't need line 30. We can comment that. And let's just take a second to look at that. So on line 29, we load up our connection to our deployed contract.

And then on line 34, we call transfer ownership to our front-end address. And all of this stuff is being done by that deployer address. So basically the the the F-39 guy deploys the contract and then makes a second transaction on it setting us as the owner. Is that the right way to explain it? >> I think so. Yeah. And another option like if we wanted to do only in one one transaction like we could have our front end address into the constructor into the arguments and then calling the transfer ownership from the contracts because right now we are calling like from from the the deployer account is calling like the the transfer ownership but the deployer account could deploy the contract and then in the constructor

function there we could call transfer ownership. It will be like the same the same effect but in one transaction. Right? Let's let's try it this way and then let's go try that real quick. It wouldn't be hard and and uh we'd be changing our own constructor, not the inherited constructor. That's where we'd get a why why it would be easier. Let's try it this way first. Just real quick, let's make sure it deploys and let's make sure we're the owner. Uh oh, what did we do wrong? Unexpected error. Your contract is not defined. Okay, I think it's a capital Y. uh every once in a while. Yeah. See in line 29 it uses a capital Y and then 34 it uses a lowercase. Yeah.

So whoever helped us out almost got it right. >> And let's see. Are we the owner? Sure enough. F6F. That's us. >> All right. >> Okay. So what you can you can call like the create function. >> Yeah. Make sure we can call create real quick. Yep. Yep. >> Yes. That's great. >> 108. Yeah. I don't know why 108. >> That is our number today. The price of Ethereum next week. >> Okay. 108. Some dark some dark humor from developers. We shouldn't even be talking about price, should we? Okay.

So, our balance the you checked the balance of the contract there. There you go. >> Yes. >> There we go. 108. Awesome. Okay. So that what what Carlos was saying is this is actually more expensive because we do the deployment then we do a second transaction and that second transaction has some overhead. If we do that all in the same transaction it's going to actually cost less. So if we go to let's go back to our deploy script and just comment out the that thing. Yep. Let's just comment out that. And you technically could comment out 29 if you wanted to too but it doesn't matter. Uh, knowing this, if we hit save and we hit deploy, let's let's do that. Let's save and deploy. And let's see that sure enough uh it should be back to F39 as

the Yep. The owner is now F39. Okay. Now, let's go to the constructor of our contract. The one that inherits their contract and let's go look at our constructor. Okay. In our constructor, what do we need to call here? We want to call transfer ownership, right? >> Yes. Like maybe the best way would be to have like a parameter in the constructor, right? So we we can like send our address, right? Like the address where we want to send like >> let's just paste it in. You totally you totally should just have like an address in the thing, but let's just paste it in like we were. I think it's just transfer ownership, right? Do we need to grab uh yeah, you could do that kind of.

We're going to paste some JavaScript into our solidity. It'll work though. I think I think we just need to call transfer ownership though. You need you can delete everything before the period I think and including the period right >> like you can also use like super right like super dot >> is it super transfer ownership you need to do >> I think you don't need to do that I think >> let's try let's see what happens here yeah >> yeah I usually do that because it's like more explicit right like you are calling like the contract yeah >> let's see if this even works and see you don't have to be a gigabrain to figure this out you just paste it in and find out if it works f around and find out that's what scaval is all about. And we're the owner again. It

worked. Yep. Yep. Let's try super.t transfer just while we're here and we're talking about it. Let's see if it changes anything. >> X cap. Cap super. Yeah. Oh, it's likes it. It likes it. It's okay. Totally worked, right? Totally worked exactly the same. But you're you're you're saying this is a little more explicit. And it's showing that like we're inheriting something and we're talking to the parent when we call that. Awesome. And we saved oursel 21,000 gas by putting it all into the same thing.

And probably a little bit of like complication, like a little less less of a headache. Okay. Inheritance. Uh let's see. Let's see. I have on on speedrun Ethereum. I have mapping strrus, modifiers, events, inheritance, sending ETH, and payback function. So basically we're we're kind of like at the point where maybe we could show a little contract to contract interaction here and I can set that up over on my end. I could share my screen again. Anything else from here that we want to cover or anything you were thinking of Carlos that would be good to show off or have we covered it pretty well?

>> Like maybe the receipt on the fallback functions. I'm not sure if we want to cover those. >> I'll do that. How about I do that on my side too? I'll do that over here too. >> Like like trying to send like money to a contract without the receive function and see what happens. Yeah, right. >> Yep. That sounds great. Okay, let me get us back to Let me share my screen. Let me get us back to hopefully like the starting scaffold ETH contract. Oh boy. Oh boy. Okay, back it up. Back it up. Back it up. Back it up. Should be right there. Okay, now we have our stock

scaffold ETH contract. I can deploy that. We have uh our front end here. We've got our units. We've got our primitives. We've got solidity by example. All right. Okay. So, what we want to learn about, right, is uh receive r. Those French don't know how to spell ei. No, it's the American that doesn't know how to spell. Maybe like like search for send back maybe >> the send. I think send. Oh, send either >> send.

>> Yeah, like the the I think that the one. >> Yeah, this is how to send ether out of the contract which we definitely should show. >> I think I think it shows in there like in the other one like in the synth. >> It's in this one. >> Yes. >> Yep. Okay. It's weird that it's there. >> Yeah, it's here. Right. Okay. Let's just experiment with it. Right. again like you don't have to be a gigab brain. You don't have to have all this figured out. You can basically just get scaffold ETH up and you can tinker around with it. You could try it, right? I'm going to trash this stuff and I'm going to trash this and I'm going to comment this out.

So, I'm going to have nothing in here but a constructor. This is just got a blank uh contract. Actually, we probably need some kind of uh here. Let's do this. Let's have it uh that's not very fun. Like does this even do anything? Yes. Okay. So, let's uh this or hello. I'm just setting something up so it shows up in our contract. Hello world. There we go. Very, very simple contract. We're storing the string world. This should get deployed.

Okay, here is our dummy contract, right? Nothing is happening here. What I'm going to do is I'm going to copy this address. I'm gonna open up my wallet and I'm gonna try to send a $100 to our contract. I'm gonna send it directly to the address of the contract. I'm not calling some payable function. I'm sending it directly blable, right? Remember when we had to add that payable uh keyword? It's safety, right? We don't want a con we don't want a function to take in money when the developer doesn't want money to flow in.

Same thing here. You have to explicitly say,"I want this smart contract to receive funds." And once you set that receive function and you deploy it, then your contract will be able to receive funds. And not until you add that will that happen. Okay? And let's try that one more time. So now I have a new contract. Uh grab that address. Grab the wallet. Let's try sending $100 this time. And tada, $100 sends in. Right? So, just by adding that receive function. All right, I'm going to do one other thing. I'm going to say uh let's make this a unit 256 counter and we'll set it. It'll start out equal to zero, right? And then anytime anybody look at this, anytime

anybody sends some money in, we'll increment the counter. Very weird. Very weird. But that's what we're doing. We're just trying stuff out. We're learning stuff. We're seeing what we can do. All right. Now, we have this new contract. And anytime I yolo funds to that address, our counter should update. Very nice. So, let's send another $100. And there we go. Okay. I don't know why, but anytime you send some money in here and this, so this is really nice user experience, right? If you had some contract here that has some complicated investment strategy and there's, you know, some deposit function that someone

needs to call to send in money, right? And that deposit function is probably public payable. A lot of times you'd like your user to just send the money in. So what we do is let's let's set this up. console.log uh this would be message. deposited. Oh yeah, good spelling. Deposited and then some message value. Okay, so we could call the deposit function directly. But what we're going to do is anytime it receives funds, it just is going to redirect those funds to the deposit function. Now, let's try

this out. I probably did something wrong here or maybe it's not going to let me do this. Totally did. Totally awesome. Okay. So, technically I could just deposit, you know, 0.5 ether in there, right? And that's going to Oh, our counter is not our counter is not doing anything. Doesn't need to. And and you're able to I'm able to call deposit. But what I wanted to show here is I can also deposit funds by just sending money to the contract. I can just send a hundred bucks to this just like anyone can open up their wallet. Maybe this is even at an ENS address like Austin'sGreatfunds, right? If you want to deposit into Austin's greatfunds, you can just send to Austin's greatfunds. Send a $100 and

it's automatically going to call that deposit function and we see that so and so deposited uh that $100, whatever that is. Right? So, we can see that over in the contract. Okay, so we've done payable functions fall back. Yeah, go ahead. Please fill it in here. What are we missing? Uh maybe it could be nice like not since we have a bit more time to show like the withdraw function as well. Um so you have money in the contract but no way out right now. So >> could be a nice time to >> so uh public and who are we going to let withdraw? Should we let anybody withdraw for now? Let's do it. So we'll do message sender dot uh call. No, let's just do transfer.

Right. We're We're going to go with the oldest oldest trick in the book here. We're just going to do a transfer. Oldest trick in the book. This is weird. Okay. And And here we go. We need the address this balance. A weird line of code there. I think this even still needs to be payable. I think it's going to complain that that's not payable, but let's see if this works. Basically letting anyone get in here and call withdraw and take the money. Yeah, it's going to say it needs to be payable. A lot of little tricks you have to learn to get this quite right here. Okay, there we go. Now, let's try saving that and deploying and see what happens.

Nice. Nice. Okay, and so now anybody can go ahead and deposit half an ETH and then anyone can call that withdraw function and withdraw that ETH, right? Okay. Okay. So, we probably want some modifier there. Making sure it's the owner, but I'm just going to build my own require that message sender equals equals this dude or not the owner. So, norm normally I would use I would just inherit. Oh, man. It's right here. Why don't I just inherit it? We were just talking about it, right? Is ownable.

And instead of doing this, we just say only owner. And in this case, we would need to also transfer the ownership to our front end guy. Whoops. How's that? Is it going to compile? I'm like 20% confident. >> 80%. There we go. There we go. Same thing, but now we have a nice like ownership pattern here. We see the announce the renounce ownership and transfer ownership set up. And if we deposit uh some funds by just sending some money

in. Let's send in 100 bucks. It should go, it should trigger the deposit. And now uh only we should be able to withdraw, right? Only the owner. And we were able to withdraw. Awesome. There we go. Now we've got the withdraw pattern. Oh yeah. Oh yeah. This is the last little bit here. Right. So that transfer uh if if you are transferring to another contract that has a receive function that does some stuff inside of that receive function, it's going to cost gas. So this transfer isn't the right way to do it. You want to go to the sending ether and you want to do this, right?

just kind of a kind of a little like pain in the butt, but you just kind of have to learn this stuff. So, we'll say message.cender call and the value will be that, right? And technically, we can get rid of this to make sure we don't get that warning. And we can go ahead and require that it works. Totally copy and pasting, but let's see if this works. Does this need to be payable? Doesn't look like it. Looks like it's happy. Yep. So, even though this line looks a little bit more uh obvious, this is the right way to do it because of gas limits. And let's go double check that we can yolo into the contract.

Oops, I didn't go to the right address there. YOLO into the contract and make sure the money goes in. Got $100. And then make sure we can withdraw. And the money comes out. Okay, done. We got it. Right. We are able to write our own there. Uh, show the transfer, show the ownable, show the deposit, show the withdrawal. I feel like now we do contract to contract, right? Is that Are we Are we about at that point? Have we filled in any anything else? Anything I'm missing here? Any insights? Should we go for it? Time for a second contract?

>> Yes, because you say like online 22, right? like you said that that's that can be a problem if you send money to a contract right but we always I mean for us all like message sender is always been one of us right one of these accounts >> externally owned account yeah y >> yes so what happens I mean how can it be possible that a contract to be the message sender >> we're about to find out right >> okay so what do we call so this contract is kind of like kind of like a bank where people can deposit and withdraw. Really, what we could do is we could keep track of some

mapping. Ooh, how about we do that. How about we have uh a mapping? Whoops. I'd like that. Bring that back from some address to some unit 256. And this is your public balance, right? And uh there's not going to be any ownership. the bank. The bank is ownerless. I think that's a really neat thing to have, right? So, let's see. So, we're gonna have this. We'll have no ownership. We'll actually not even have a constructor. We'll have a deposit, a withdraw.

The deposit is going to balance of message sender plus equals the message value. And the withdraw we'll do here's re-entry. Let's just set it up so you can re-enter into it. We'll talk about that later. Uh we will first send you the your balance and then we will reset your balance. Okay, we just made a bank, right?

It it allows you to deposit money. It keeps track of your money. Uh oh, no. Minus equal. No, no, it just sets to zero because it send you the balance. Okay, let's make sure this works real quick. Let's make sure our little bank works. And then we'll create another contract that talks to the bank basically. Okay, so here's our bank contract. Uh uh let's have two different players that are going to play on the bank contract. We're going to have uh green guy and purple guy. grab some money for purple guy.

Okay, so uh hopefully purple guy has some money. Uh let's get some more more money for purple guy. Let's go. Okay, so purple guy wants to deposit 20 bucks into the bank. They could call the deposit function or they could just send money straight to it. So we're going to send 20 bucks into the bank from Purple Guy. There we go. So now the bank holds $20. And if we check the balance, we should see that this guy's balance. Oh, that's kind of weird, but yeah, that's 0.1. Be nice if that showed it in USD. Okay, now let's have green guy also deposit some funds.

Uh, green guy will use Let's see. Let's see. How much do we have? Oh, wow. We have Let's deposit an entire ETH. Kaboom. Now, we'll see that the contract value goes way up. Right now, there's 1,200 and and some in there. uh and and our balances are separate, right? This guy has a balance of 0.1. This guy has a balance of one. And if they withdraw, they should only be able to get their money back, right? So, if I call withdraw from this guy, the balance of the contract goes all the way back to 20 and my address, my balance goes uh down to zero, right? So, simple simple simple contract. We're keeping track of a

mapping. We're letting people deposit and withdraw. We're not letting we're making sure no one can deposit and or can withdraw from other people's funds. Very very simple contract, but basically just lets people deposit and withdraw money and we keep track of everybody's balance. Make sense there? Did I miss anything there? Does that look pretty good? Okay, here we go. So, I'm going to call that bank.soul. Okay, so we've got bank.soul. Okay, and then we've got your contract.soul. will do something different. But, uh, let's just set it up to be I don't even know. Uh, maybe maybe we just set it up as ownable or something.

I don't I don't know. Let's see. Public. Uh, I don't even know what to do with this contract yet. Uh, public. Let's just make it a string. I don't I don't even know. String public. Uh, purpose. We always do this, right? This this guy is uh hello world. Okay, simple simple simple contract. Now we have two contracts. We need to deploy both of them, right? First we probably will deploy our bank and then second we'll deploy our your contract. So I'm going to head to the your the deployment. Now I think technically usually you duplicate this for a second contract. I think I'm going to go ahead and do that. So we'll deploy your contract and our zero would will be

deploy. I'm going to rename it. We're going to deploy the bank. Okay. And this will deploy bank. Man, I hope I'm doing this right. Let's see. And this will export bank. So hopefully we should deploy the bank as the first contract. And then we should deploy your contract as a second piece. Let's see what happens. Let's just try it out. Let's just try it out. See if I'm missing something here. I think you will need to add to the to the React application like the contract, you know, like >> Yep.

>> Um something here. This right here, >> it's not finding your contract. >> Yep. >> Good. Good to go. Okay. So now we should have both the bank and your contract. Okay, we got to go edit the front end now, right? We're going to go here. I'm going to look for that contract component. There's a nice contract react component. Isn't this sweet? You just have this nice contract here and I'm going to bring the bank in right below it.

>> So maybe it would be nice to like show the the front end like before editing as well like what what pops up. >> Oh, okay. Yep. It was just showing your contract basically without the bank there. It just showed the your contract with the hello world. And then I'm just going to paste in the bank. So we have a second contract there. There we go. So now we have two contracts we're talking to. There's our bank that we created that lets anybody yolo some funds in here. Like I have zero for my balance. I deposit 0.1. And now if I go check my balance, I've got 0.1 deposited. The bank is holding that. I can withdraw. Right? So, as an externally owned account, I can invest in my bank or I can lock money into my

bank, but what we want to do is have this contract be talking to the bank, right? And that's when it's going to get tricky. Any anything I'm not covering enough here? I feel like maybe there's some any any questions or thoughts here before we keep going. Okay. Okay. So, our contract needs to be needs to set up basically an interface to talk to the bank contract, right? The best way to show that at first might be to show our balance, but it's not going to be set to anything. So, I feel like let's just set up our constructor.

Okay. And in our constructor, we're going to have an address. Actually, let's not even do that. Let's just let's just have a a connect to contract. Something like this. This is going to be really weird. It's not exactly how you do this, but I want to call some connect to contract. I want to set up an interface to it. So, so what we need to do is we need to have the interface up here, right? We need to have uh an interface like this. Here's what an interface looks like where it's just it doesn't have the actual functions. It just has something that looks like this.

Is that about right? Is that what an interface looks like? >> Yeah. Maybe not. If you need like some semicolons at the end of the >> like this, >> I think so. Yeah. >> Gotta have Carlos. Carlos got to have some semicolons. Oh, no. Didn't like that. >> Uh, >> maybe they remove the like the Yeah. >> DK. Oh, they need to be virtual maybe. I don't know. I'm going to go back to just It compiled. I'm gonna go back to this.

Let's make sure this compiles. Uh uh oh, what did I do wrong there? Duplicate output destination bank.json. >> Oh yeah, you Oh, >> because it's compiling this, isn't it? >> Yeah, maybe. Yeah, like you need to put like the interface word instead of lo contract on line eight. That needs to be interface, not contract. Yeah, >> I don't think I've ever done it like that before.

And then maybe I need those semicolons. >> Yeah, maybe. And I think maybe they need to be external or something like that. >> Okay. Yeah. But here here's what we can do. Instead of bringing in the interface, which is really nice to have the interface there, we can just import it, right? We can say import slashbank. >> Yeah. >> And that will do the same thing. Let's see if this works. We're doing it like 10 different ways. Okay, that actually worked. Okay, so it does deploy the bank, then it deploys your contract. Uh we have the bank imported in and what that allows us to do is we create some kind of bank uh we'll just call this bank

contract right? And then what we can do is say bank contract equals oops equals. And we want to now if we say new bank it's actually going to deploy a bank contract right so this this is a factory and and so when you deploy your contract it's going to deploy a new bank contract but instead we're just going to say bank we're not going to say new and we're going to put in an address here which will come in from here bank address.

Okay, so now really really strange stuff, but this is how this is how you set up one contract to talk to another contract. You create an instantiation of the bank and then you pass in a bank address and then it's going to create uh a connection to that second contract and we can talk to this new bank contract in a second. Uh by doing this, we need to edit our deploy and make sure we pass in our bank address to here. So, we're going to have to do like a >> Yeah. >> Get this, right? We're going to say bank contract equals bank and then we'll do bank contract address.

Am I doing this right? >> Yeah. >> Let's see. Let's see what happens. 20% sure. >> Yeah. Payable address here. Maybe we just say this is a payable Or maybe we type defit to payable. Let's see if this works, we're going with it. If not, well, there we go. Okay, cool, cool cool. So, what is happening here? First, we're deploying our bank. Then, we're getting the address of the bank and we're deploying our your contract with the address of the bank passed in.

So, then what we should see here, let's make this public, right? Can you do that? What happens if I do this? Hopefully, it'll be an address. I don't know if it's going to let us do this. What would be nice is if it showed it. It does. Okay, that's pretty. I like that a lot. I like that a lot. Okay, so let's see this again. We'll deploy our bank. We'll get a new bank contract. Then we'll deploy our your contract. And you can see that the bank is there, right? Notice this is the same as this. So, so our our our contract is tracking where the bank is and it knows how to get our address. So, then let's just go over to the bank. What can we do? We can call balance uh we call withdraw deposit. So let's um my balance right and what that's going

to do is it's going to return bank contract.balance of address this super weird. And this needs to be a view function that returns a 256. And let's see if that works. Basically, hopefully we can call my balance on your contract and it will get us our balance uh at the bank. Okay, it's zero. Okay, cool, cool, cool. Then we need some other function uh >> like deposit maybe. Oh, >> yeah. Yeah, go ahead.

>> No, I was going to say like maybe like a deposit function so we can send like some funds from our account to the to this contract. >> Should we make it payable? >> Yes. And then this contract will send that money to the bank. Okay, let's do it in two different steps though. >> Okay, >> let's do let's set it up. So, okay. Yeah. Yeah. So, right now when we deploy this thing, we're not able to send money into our your contract, right? Because we forgot about this receive function. So, let's set it up. So, our contract, let's just prove that, right? If I copy your contract and I try to send money to it, it's going to fail saying it doesn't have a receive function. Okay? So, let's put a rece that wasn't what I wanted.

Let's put a receive function into Oh man, what did I do? Bank.zero. What am I doing? What have I done? I want I want this line of code. Please copy. And I want it pasted right here. Okay, fantastic. And I'm not going to do anything. I'm just going to allow money to flow in. Right. So now we can at least send money into our contract. This should get deployed. And if I copy paste it, I should be able to send $100 to it. Yes. Okay. Then so so we're going to send money into into our contract. And then we want to call another function on our contract that deposits it into the bank, right?

So what we'll do here is we'll say bank contract.deposit and then do I do this? There's like a value >> Yeah. >> thing, right? Yeah. So weird. >> I'm not sure if you need the the parenthesis of deposit. >> Oh, I don't Oh, it's called before that. Let's I see what you're saying. You're saying move this like this, I think. Right.

>> Yes. Yes. >> Yep. One arguments and expected it to be zero. Yep. Yep. Super weird there. So, I was thinking I needed to use it as an override like you do in ethers, but when you're in Solidity, you do that weird thing where you have the the overrides kind of before that, right? Okay. Now, now we should be set up. We're ready. We're ready to First, let's put some money into our contract, right? So, let's send one ETH into our middleware contract.

one ETH go. Okay. So, now this guy should be holding value. Yep. So, now he's holding 1,200. Okay. Then we want to call deposit of Let's Let's deposit like half of it into the contract, right? So, this should hopefully Oh, wow. It even updated our balance and everything already. So, now we see $600 in the bank and $600 in our contract, right? So half of the ETH still lives in our contract. The other half has been deposited. In fact, I could call that again, right? 0.5 deposit the second one. Right? There we go. So, so now we're doing contract to

contract interaction, right? >> You can you can I'm sorry, I was going to say that maybe you can check the balance on the back contract, the bank contract with the with your contract address, right? So, we grab this address and we go to the balance and we check it and it's one, right? Yep. Which is actually the same thing this guy is calling to. >> The key here is the message. Who is the message sender? Right? That's what we're really trying to to get get figured out. When someone calls deposit, it increments the balance of the message. So when your contract calls deposit, who is the message sender? Right? Let's put let's put a console log in here and

really really see this, right? Console log and we're going to say message.cender sender deposit and then message value. Okay. >> And the other I want to put TX origin in there eventually, but let's see this. Yeah. Yeah, we want to do that, too. Let's see. Let's see that this happens. >> Okay. So, we have a fresh contract. Uh we need to send money. Oh, this is this is why we want to do it all as one step because it's a lot to get set up. But I'm going to send in 0.5.

Okay. So now we've got the $600 in our contract. Then if we do the deposit of 0.5 actually just let's just do 0.1 and we hit send. What we're looking for is the console log over there. Right. Okay. 2 E2. So the message sender is 2e2. It's not us. It's not our 765. Even though we're triggering the contract. The message sender is this guy, right? Of course it is. That makes sense, right? So it's like you you have we have the externally owned account, right? And then we have our your contract which is the middleware and then we have the bank and we're telling our contract to invest in the bank. So the message sender is

our contract. And the the last thing I want to add here, let's see, a tx.org is another uh uh units, right? If we look at TX.org origin. It's the sender of the transaction. It's always the person that signed the transaction. So almost always when it's an externally owned account talking to a smart contract, the TX origin and the message.cender are the same address. They're that originating address, right? But when we're calling a middleware contract and that middleware is talking to the other contract, then that gets a little blurry. The TX.or origin is always going to be us, but the message.cender will be that middleware contract. And we should see that here if

we run. Let's let's go ahead and deploy this last one. I don't know if it's going to let me do that with console log. It did. Very cool. Thank you, Hard Hat. Okay, let's send in a hundred bucks. Let's do this again. Right. So, we send in we go to our contract and we send in uh what? $1,000. Fine. And then we go ahead and deposit 0.01 or something like that. and we hit send there. Let's go look at that console log. Okay, so DC1, which is our middleware contract, is the message sender, but the TX.org origin is our account. Whoops. So see that the 765 is the TX.org origin. So like just like really let's hammer that home. If I go

to e.bill, I can draw something that'll show this off really really simply. And I think it's probably the way to go here. If I select all this stuff and delete it, basically give me this. Right. So here here is our EOA. EOA. Okay. And then it's talking to a middleware contract. Right here is your contract. Okay. And then it's talking to a third contract. We'll make that green guy. Right. And this is the bank.

Okay. And so when we see the transaction here, there's the message. So message dot sender. Looks like a child wrote it. Is the orange guy, right? The message.cender is the orange guy. But the TX.org origin is the blue guy. Okay, s super childish.

We had to draw in crayons to make it obvious, but that should make sense. When an EOA is talking directly to the contract, it's both the message.cender and the TX. Origin. But when an EOA talks to a middleware contract, the message sender in terms of what the bank sees is that middleware contract. Hopefully that explains it all. Hopefully that shows it off. Uh, we can hint at something here. Carlos, you brought this up when we were playing around with the wolf game. There was a specific piece of that contract that wanted to make sure that only externallyowned accounts could access it. So, if you wanted to make your you wanted to make some function like this deposit and you wanted to say only

externally owned accounts can do it. If you try to have a middleware contract attacking it, we don't want to allow that. What you can do is require that the TX origin equals equals the message sender, right? And we say uh only EOAS or something like that, right? This line of code basically breaks if you try to hit it from a middleware contract. This says that the only person that can talk to this function has to be an externally owned account and that TX origin has to be equal to your message.

Maybe >> the other way around. What's that? >> I was going to say that maybe we can try with with I mean you can deploy this and try like >> Yeah, happens. >> Yep. That that's the best way to find out. Let's just deploy it and see. So now we can totally deposit some funds into this contract. Let's close up e.build. We can deposit some funds in here as our externally owned account, right? This guy should have no problem talking to the bank. He wants to deposit 0.05 05 he sends. There we go. Money goes in, right? But if we put some funds into this contract and then we try to do the dep the same deposit

only EOAS, right? It doesn't let us go through our contract and deposit into our bank, right? And when we built that smart contract game, I wanted only contracts talking to it, right? So in this case I required that the TX origin does not equal the message sender and this is only contracts right another weird one you could do if you wanted one smart contract to never accept a transaction from an EOA you have to use some middleware contract you could set up the require like that super weird though let's let's pull that out of there set it up okay I think we've we've gone through a lot of stuff here we've kind of we showed you at home how you can pull up scaffold ETH get it installed immediately. Start like

writing some Solidity and start tinkering with it and learn. We've talked about how the wallets work, how you use a burner wallet at first, but you can connect in another wallet. We've talked about, you know, a bunch of syntax and how to get started with, you know, primitives and data types and globals and just like going through this. Uh oh, we left re-entry, right? We've got we've got a bug right here. Maybe we should end with this because it really does kind of highlight all of the stuff that happens with all the weird things and how to get in here. And by the way, when when you go to solidity by example after you learn all of these different things, which if you're a programmer, you'll figure figure it out quick except for maybe delegate call.

That's some weird But once you once you get that stuff, you'll get it figured out pretty quickly. There's a lot of good applications. But down here all the way at the bottom are hacks. And if you really want to understand how solidity works, once you get the the basics of solidity, you should start looking at the attacks and figure out how this works. Re-entry is is is one that we'll look at here. Uh but learn the attacks. And just to double down on that, there's something called the Ethernet. >> Yes. Once what did I do? Ether challenge. Here we go. Ethernet.

Zeppelin. Here. Here we go. So once you've kind of got a feel for that, you're able to learn how to tinker with Solidity, you want to start really testing your knowledge, uh the Ethernet is a great place to go and it'll take you through kind of a lot of these different uh uh challenges. Okay, here we go. Let's talk about re-entry to close it out. It's going to get really weird here, but we have to talk about it. So this withdraw function, what's happening here? we go let's let's go ahead and just talk to the bank, right? We don't even need the middleware contract here. Well, we we would use the middleware contract to actually attack it, I guess. But, uh let's let's just do a withdraw as us.

So, we go to the bank and we deposit some funds. So, we are green guy and green guy wants to deposit 0.1 and he goes here and deposits and it's fine. And we can check green guy's balance. It's 6. And then Green Guy can call this withdraw function, right? And when we we call withdraw, it's just going to transfer our balance to us, right? No big deal. Uh basically, it works uh out of the box. This works fine. Now, we've got an attack vector here and it and it is kind of interesting. What what happens here is when we call this withdraw, it's actually making an external call and it's actually going to uh uh send the

funds. But let's say we have a middleware contract just like we have here and it does something special when it receives funds. This is how we would attack uh uh this bank is what we would do is we would call withdraw and when it withdraws to our middleware contract we would do something else here. We would actually call withdraw again and we could get into this loop where we're we're looping and we're call we call withdraw. It goes to our attacker contract and then our attacker contract calls withdraw again and it loops over this basically sending our balance to us over and over and over again and it

never gets to this line until it runs out of gas. So we let's let's highlight that a little bit better. Let's actually set up our attacker contract to do this. But the it's really funny. The the solution to this is basically to have a UN 256 uh temp here and we set that to our balance. Okay. And then we zero out our balance. Okay. And then we go ahead and send temp here. So this fixes re-entry. This re-entrancy bug. Millions of dollars have been stolen out of smart contracts because of this re-entry bug. And this is the fix. Basically, you do your state trans you do your state updates. You do

any internal state updates first. You make sure that balance is zero. And then you do all your external stuff. You make sure that like if if you uh call this and it's going to some external thing, they can't reenter and still have a balance. We see we've zeroed out their balance before they get there. Okay. So, let's go ahead and let's leave it uh attackable and let's let's attack it, right? Let's uh what do we We got a little bit of time. We got a little bit of time here. Let's Let's attack it. Okay. So, uh let's make sure all of this compiles and everything is good here first.

Awesome. I think I like your deposit making it payable. This it'll just be easier if we take this out and do message.value here. Go. Of course, Carlos was right. I should have listened to him in the first place. Okay. Okay. And let's make sure that that all works real quick. That we can basically do a deploy by just calling one function or do a deposit by just calling the one function here. So if we deposit 0.1 ETH, this should go all the way to the second contract. Yep. So it goes all the way in, right? So by depositing into your contract, it immediately takes all of that money and deposits it into the bank. So you shouldn't ever have money here. Even if

I like yolo some funds here, what happens? Ah, okay. That's not going to Yeah, funds are just going to go in because it it's not doing anything here, right? It could we could technically do this and then it would do the deposit, but we're we're going to do something special there. So, let's not even mess with that. Okay, so we have our attacker contract here. We have the bank here. And now we have to assume that the bank has some extra funds in it, right? There's going to be lots of people depositing. Let's let's just have some poor some poor second person here who's going to lock their money up in this bank and it's a bad idea. This bank is going to be So, we got purple guy.

Purple Guy is not going to be happy about things. Let's give Let's give purple guy some some funds. Okay. Well, purple guy's happy. He just got $10,000. Okay. So, how does this attack work? Basically, we're going to deposit some funds in and there's going to be more funds in the bank and we're going to want to withdraw those funds, right? So, what's going to happen is we're going to have some function called do the attack. Okay. And what it's going to do is it's going to call the withdraw function on the bank contract here.

Okay, so we know we need to do a withdraw. And let's see. So, this will be bank contract dot. We're going to have to like check gas and stuff. It's been a while since I actually wrote this in solidity. Like, it's a lot easier to explain it. Like, yeah, you have a middle contract, it attacks it to actually write it up. This is going to be a little uh a little bit more time consuming, but let's figure it out. So we want to call withdraw on and let's just do that first. Let's just make sure this works. Let's do this one step at a time and make sure the code all works.

Okay. So now uh we should have our starting contract. Everything is empty. Uh let's put in 0.1. We do the deposit. 120 bucks show up there. My balance is 0.1 and do the attack isn't going to attack anything right now. It's just going to withdraw our funds back, right? And then they land back in the contract for some reason, but whatever. Oh, there's no withdraw function. So, actually, they're locked there, which isn't great. Maybe let's let's build that real quick. Nah, don't worry about it. We're in a hurry here. So you would definitely have we could even do something like pay uh the message sender we do what payable message sender transfer

address this dobbalance whatever. So whenever you do the attack it withdraws and then sends the balance. Let's see if this works. I want the money to go all the way back to us just to see. You know what? It looks better when it ends in the contract. This this is what you would do. I'm I'm like rambling here. This is what you would do. But let's let's just make sure this works. 0.1. Go ahead and deposit that in. And then what I want what I want to do is just do the attack and make sure the money comes all the way out and lands back here. And it does. Okay. So, there's no money left in the contract. Actually, let's leave that out for now. Okay. Here we go. We want to call withdraw. And when we call withdraw on the bank,

the bank is going to immediately send us our balance. And what happens when they send us our balance? It's going to call this receive function, right? Oo, interesting. Okay, so let's let's see here. What if we did just one more of these? Okay. So, we'll keep some some state variable called uh un 256 times uh let's just say attacked and we'll set let's just say bool. Here we go. We're just going to do this bool attacked equals false.

And we're going to say if it hasn't attacked yet, go ahead said and set attack to true and call withdraw one more time. Okay. So basically uh when we withdraw, we're just going to withdraw one more time also. So we should actually withdraw twice for the price of one. We should be able to pull some money out. Kind of kind of weird. We can talk through this some more. Let's see if this works and then we can talk through it some more.

Okay, cool. So, now we have our attacker contract. We have our bank. What we need is some extra funds uh in our bank. So, we're going to have attacker guy. We're going to have just this guy who's going to have a bad time here. They're going to put in uh what point five ETH. Okay, there we go. And so, we could check their balance. Basically, they have 0.5 ETH in the contract and if they want, they could withdraw. Okay. So, poor purple guy has invested in the bank and they think everything is safe. Uh they looked at the code. It looks good. Right there.

They're like, "Oh, this bank looks good. It does a send. It sets it to zero. Fine. I'm good. I'm invested. I'm in. I'm I got value locked in here." Okay. So, we come along. What we're going to do is we're going to deposit uh 0.5. Okay. And that's going to go all the way in. So now the contract has one whole ETH in it. It has our half and it has their half. Now when we do the attack, okay, it's going to call withdraw. And when we call withdraw, the bank is going to send us our half ETH. And then when our contract receives the half ETH, attack is going to be false. So it's going to get into here. It's going to

set attack to true and it's going to call withdraw again back into the bank. The bank is going to send us another 0.5 ETH and then it'll land here. It won't do anything. It'll come back here and set our balance to zero. And if this works, we're going to run away with purple guys half E. If this works, let's find out. Here we go. Let's do the attack. Oh, what just happened? Let's see what Uh oh. The bank is empty and our contract now has the whole ETH in it, right? And this guy over here, if he checks his balance, he still has 0.5. If he looks, right? He says, "Hey, what's my balance?" The bank

will say, "You still have 0.5." And he's like, "Okay, let's withdraw. There's no money left in the contract." Right? So, even though this guy thinks his balance still is 0.5, the bank is empty right? Does that does that sort of sort of show off what happens here? I think what you would do is you would want to drain the contract, right? You wouldn't want to just pull twice your amount out. You'd want to run this thing until you're out of gas. So, you do something like if the contract still has a balance and I still have some gas left. I think there's like a gas left. You could ask like if I still have gas left and the contract

still has money in it, keep attacking and keep going through this loop until either we run out of gas or the contract runs out of money and then you drop out of the loop. Hopefully that kind of explains it. I mean, it's it's a pretty complicated uh attack, but it's, you know, pretty straightforward. Any questions or insight here to add to this? Did that does that cover it? >> Yeah, I think so. I mean it was was great to show like the demonstration. I just wanted to say one thing and ask that when you write like some solidity some methods like just keep in mind that there are not only users or external own account users like are interacting with

your contract. So also other contracts can interact with your contract. So just like keeping that in mind when you are writing like some solidity because things like this can can happen right. Awesome. We're I'm just going to go plus+ and I'll say uh if attacked is less than three or something like that or four. Let's just like loop on it a few more times. Right. Redeploy this and let's see it go again. What did I do wrong? So this time it'll just loop like three or four times. We'll put we'll put some extra money in the contract. So this time maybe we'll only deposit 0.1. Okay.

So let's see. So again to set it up, this guy needs to deposit some funds. What's he have left? Point. Yes. Yeah. So let's put in let's deposit a whole ETH. Okay. And then to attack that, we're just going to deposit like 0.1, right? So we've deposited 0.1. Now when we do the attack, it's going to pull out like 04 or something, right? We're going to end up with with 4x the money. Yeah. 0.5. Right. So, we pulled out a whole bunch more. We looped a bunch more times. Yep. But yeah, I think that the trick here is just knowing that when you write your contract like this, this is bad. You you want to do something like temp here. Oh, yeah. Let's try to attack

it now, right? Let's let's go back to Oh wait wait wait wait wait. Fool attacked. not attacked. Attacked equals true withdraw. Okay, there we go. Let's deploy the whole thing again. Now, it should it should fail, right? It's going to tell us that we don't have a balance or something like that. And the that the attack will fail. But it's worth trying just to show that that it works or doesn't work. And then I think we can call it. I think we I think this was a good a good day of solidity tinkering and a good day of like getting your your scaffold ETH set up. Okay, let's see. We need to deposit one ETH.

There we go. Then over here we deposit 0.1. Okay. Now when we do the attack. Oh wait, it worked. Wait. So what did it do? It didn't withdraw. It only withdrew the 0.1. Oh, it called withdraw again and our balance is zero. Right? So we could set a require statement here to say your balance has to be greater than zero to enter here, but whatever. We're letting them withdraw zero. So it doesn't matter. The point here is that this person's balance is still in the bank and someone wasn't able to steal it. Right?

We did it. We learned more solidity. We helped you at home set up scaffold ETH. Next week is going to be all scaffold ETH. We're going to speedrun Ethereum. We're we're going to assume that you've got the syntax down. you know, how to tinker with smart contracts. And we're going to dive into what you can actually build with smart contracts and what like why this expensive uh decentralized substrate is so powerful and what things you would deploy to this and what things you wouldn't deploy to it. Awesome stuff. Happy happy 10th day. Day day five of week two is done. Thank you all for being here.

>> Awesome. All right. See you guys. >> See you.

Related talks