👩‍💻 Web2 to Web3 🚀 - Week 2, Day 4 - Solidity & Testing with Hardhat

Web2 to Web3 Curriculum · 17 Aug 2025 · 1:40:29

Austin Griffith and the BuidlGuidl team help developers get confident tinkering with Solidity. They compare Scaffold-ETH, Remix and a local Hardhat project, then work through functions and visibility, constructors, global variables, require statements, modifiers, a simple token, mappings, payable functions, structs, events and tests. The emphasis is on writing a little code, deploying, poking at it and repeating.

Speakers

Topics

From Web2 to Web3 Curriculum, hosted by BuidlGuidl.

Transcript

Boo. I'm Austin Griffith. I'm here with Carlos and we are I don't even know what day we're on. We're like probably day nine of our Yeah. >> Yeah. Day four of week two, which is about day nine of our hourlong series or hourong sessions. Today we want to get you confident with tinkering with solidity. And what do we mean by tinkering? I mean, you write a little bit, you deploy it, you poke at it, you see how it works, and you repeat, right? Lots of iteration and lots of tinkering. So, uh, I I'm going to steal the screen just for a second just to it's going to be more of a preview of Scaffold ETH that we'll do next week just because, uh, I I've written something that I

think makes tinkering really, really easy, but then we'll kind of zoom back out and look at other ways to tinker and then we'll zoom in on Carlos and we'll tinker mostly just with hard hat. So, let me share my screen real quick and show Scaffold ETH. And in particular, I want to show uh Solidity by example. So, let me hit share. There we go. Okay. So, here here is Scaffold ETH. Uh and and this is just a preview. Don't worry, there's a lot going on. There's a lot going on here. Uh what I want to show is that you make a change to your smart contract. So, I've got a smart contract here, and I'm going to hit save, and I'm going to go ahead and deploy that. And then over here are uh Oh, man. Compounding

interest. I have a whole bunch of stuff on this. I need to probably reload. Over here it uh what? Oh, it's on coven. Oh, no. Oh, no. I was just doing a demo. Sorry. Sorry. Local host. Okay. My bad. Let's show that again. Okay, there we go. Okay, so tinkering with smart contracts. You'll you'll have a smart contract and you want to be able to make some small changes like this time I'm going to change this to all question marks, right? And you'll deploy it and it will show up on your front end and you can kind of tinker with how that works, right? Maybe maybe you uh want to

create a counter or something. Okay. So the first thing we're going to look at probably is uh primitive data types. I think as a as a programmer when you get in you want to kind of understand the syntax first and the primitives are probably the first place to start. So let's just say we uh wanted to look at uh integers and uints. Let's just grab some uint and paste it in. Right? So solidity by example is the first place I'm going. I'm grabbing a uint. I'm pasting it into my code. I don't need any of this stuff right now. Let's just get rid of all of it. Here is my whole smart contract, just some counter. And then let's just make a function real quick called increment.

And we're going to tell that thing, holy cow, here we go, to what? Counter++, right? This this to me is tinkering. We write a little code, we deploy it, and then in our front end, we should see that show up. And it should allow us to Okay, we got a counter of one and an increment. Should allow us to sort of increment our counter, right? And what if that is like plus equals 2. Now all of a sudden our counter goes up by two when we click it. Uh or something like that. or uh maybe we have some address public county and that's going to start blank and then we set that to the message sender which

we will get to in just a second. We'll talk about globals but the message sender is the person who's calling the function in solidity and if I deploy that now it's going to switch to me when I end up doing this. Notice there's a count and it's set to zero and the count and as soon as I count it's set to me, right? And this is tinkering. We're writing a little code. We're seeing it in the front end. We're poking at it and we're getting uh uh kind of some instant feedback from it. So, uh we'll get to scaffold soon. I think the key here is just like there's a lot of different ways to tinker and uh hopefully EDA can show us remix also. I think that's another cool way to tinker. Really, you're just going to solidity by

example. You're picking out some of these topics. You're pasting them into your contract. You're compiling and deploying. And then you're kind of like poking at your contract and kind of learning about it and kind of just like testing your assumptions. So that's that's scaffold ETH, that's one way to tinker with it. Uh if if Eda, do you do you happen to be able to Oh, I probably have to set you to share. There we go. Do you think you could show off uh remix for a second and then we'll go to Carlos and we'll start speed running uh solidity.

>> Awesome. I'm just going to share my screen. >> Oh, we should Where could we go for globals and units? We need to be able to have a good global units. There we go. There's Remix. >> So, this is browser based. Yeah. Go ahead. Go ahead. Sorry. >> Exactly. I was just going to say like it's an online ID, so you don't need anything else. Just remix.ethereum.org. or uh and it gives you like some basic smart contracts which you can direct directly like compile in the browser what Austin was go doing with like the deploys um and yeah when you go there it already has like a default workplace I think I think this >> what's the contract in there yeah if you go to contracts is there just like a contract we could see >> maybe let's create a new one because I

think this is bringing my like >> oh there might be some >> contract >> you don't want to alpha leak your new NFC I have something over there. Uh but I'm just going to choose like the default. Let's do this like again zoom and yes. So it brings us like a storage sol the sol extension the solidity file. >> Seeing it what am I I I still see I don't see anything changing. >> Are you see >> Oh there that I see it now. I see your mouse moving there. Oh.

>> Oh okay. I shared the wrong screen that >> I think that's what it is. Yeah, I think that's what it is. I was like, "Oh no, am I going crazy? I could be going crazy." >> I had a wrong screen shared over there. Let me try again. Um, one second. >> Oh, yeah. I got global units now, too, in the solidity docs. Okay, cool. All right, >> you see like my owner. >> Yep, we've got it. >> Super. I actually just created like a new workspace. Let me do that again. So when you come here, it gives you like a workspace.

>> I'm going to create zoom 2 because I did it on the wrong screen. Um, but you basically have like your storage.stall file and then owner and I think this is valid valid. >> Bunch of smart contracts. >> Yep. We've got strrus and arrays and mappings. All sorts of things. >> Yeah. And the cool thing is you can directly like deploy the contract over here. Uh and also connect to your like MetaMask and deploy it from your own wallet. Um but yeah, I think it's easy to get started but of course like online so limited functionality.

>> Yep. So the goal here is just to get you the coder if you know how to program. The syntax of Solidity is going to be easy. We're going to give you a great introduction today, but the goal here is just like there's a bunch of different ways you can get started tinkering. whether it's scaffold ETH or Remix or even uh just using hard hat locally. And I think Carlos can show us that is basically just like you're gonna write a little smart contract, you're going to run compile, you're going to deploy it locally, and then you're going to kind of poke at it on your local chain. And you can do that with Remix, you can do that with scaffold ETH, or you can do that with hard hat locally. And I is it cool if we switch over to Carlos now and and see the the hard hat stuff? Yeah.

Okay, let's me check if I can share. >> So, so either any of these different uh idees or environments, right? You you pick your you pick the one that you like the most. You're going to need some kind of environment that allows you to write Solidity, compile and deploy, and then access that information in some way. And you're also going to need a handful of resources. Uh the first resource is Solidity by example. Solidity dot I don't know if I drop it in the chat. Do you think maybe I'll have it? Oh, you've got it up on your screen, too. Yep. Oh, and you've got the units in globally.

The second thing you're going to need is units like these these global units that are available to you within Solidity. And then we'll just kind of like go through the list. I don't know. Maybe Carlos, do you want to you might want to steer I think you've got some ideas about how we get started. My my gut feeling is like we would just do primitives and and start pasting stuff in. But you you take us away. Let's go from here. We'll ask questions. >> Well, I have to say that scaffold each is really hard to to beat, right? Because you just like deploy a contract and you have like the UI right there. So, it's super easy to interact with the with the contract. If you go with with hard hat, that's like the hard way, right? Like you start the hard project and if you are really geeky like me,

then >> it's more. >> Yeah. Yeah. >> Yeah. >> Because it's like more low level, right? Like you are interacting with the contract with your script. So, it's like more low level, I think. But if you just want to play a little bit and tinker like scaffold is is great. But that's something for next week, right? >> And let if we even zoom in more, if you're a command line hero and you're writing uh you know low-level stuff and you like that stuff, check out Foundry. Also, Foundry is a new one that's come out that's written in Rust. It's even faster than hard hat. Uh it it probably lacks some of the deployment stuff, but I bet it it'll catch up quickly. So, just like putting that on everyone's radar, there's truffle, there's hard hat, and there's Foundry, and probably some other ones. But here we go. We've

got it up. We've got uh what do we have? We've got a node pro. You You go. You go. You go. >> So, I guess like let let's start with a with a new folder with day four >> folder, right? Uh day four. >> And let's go let's install hard hat again. I mean we are installing like hard hats and everywhere and what happens is that then you are going to have your computer filled with all node modules folder and it's going to fill up all of your space but that happens.

>> I wrote a script that goes through and looks for node modules in my home folder and deletes it if it's a certain uh age. It was like 250 gigs on my machine. It was so much >> that what happens when you tinker a lot. Right. >> That's right. Okay, so let's start with installing hard hat. If you remember from I think yesterday uh we did like MPX hard hat and that setup can set up like a new project with hard hat and we are going to choose the sample project which is going to do a bunch of stuff for us like installing dependencies and the hard hat um config file and it's going to set up some tests some some

scripts. So it's like a really nice way to start because you have everything is wired up for for you. So you just have to you know start like writing solidity which is what what we are going to do today. >> And I wondered if it relies on the soul C compiler but when I first got in I basically brought I didn't have hard hat or truffle. I built my own orchestration software from the start and it's basically you use soul C and then you have a bunch of orchestration scripts, right? That's basically what all of these are. Uh but I think that actually some of them have rewritten it might not even use soul C anymore because I think they've rewritten some of the EVM stuff.

So then you can have things like console log uh and also uh with Foundry I think he rewrote it in Rust so it even runs faster. So even some of these old dependencies like soul C that really got got us all started are probably rewritten now in in faster languages but it's still still still installing. >> Yeah. >> Okay. So maybe we can start like checking out some of these. >> Yeah. >> Right. >> Well, let's Yeah. Maybe hello world. What what is a what is a solidity smart contract consist of?

>> Oh, maybe we can also talk about the the pragma, right? Like the version. Mhm. >> So this is like the same thing that we what we do when we install like a new like for example node dependency right that you say okay I want to install something that is above this version. So this is the same but just for the compiler. So you're telling the the the solidity compiler that you want to use high at this version or higher right I guess like this should go until like 0.9 not included right so that's very important because for example all these overflow and underflow issues that we have like before.8 8 right this is like how what is the library safe math right

that that was like the library that that we yeah so before 8 like every one needs to use like this um safe math library because um we have this overflow issues or with solidity but now with this version and above uh you don't need to that to do that anymore right I think I think >> yeah yeah that's exactly right I think if we have time Today we should do like an unchecked uh math on a U and 8 and show some overflow. Let's put that up put that up as a pin as a thing to show.

But that's exactly right. We we had overflow issues in Solidity versions before 7 and they built it into Solidity after you know version 8 or above. So your contracts are going to change. So that versioning is very important of which which one you're choosing. >> And what else do we have here? This is like the just like the contract decoration, right? Like when you declare a class you just have like the contract keyword and the name and then have we have like the first state variable. I think we talked about this the other day, but usually when you are thinking about object-oriented programming in your you know in node for or in PHP or Java whatever you think about this that this will be stored in memory probably right this going to be stored in memory

and then you will store that into a database at some point but in this case all of these class properties are going to be stored on chain right that's what we call storage I think like store variables >> storage variables yep exactly this This class that we're talking about is actually going to live on thousands of machines when we deploy it. And this storage element is going to be stored on thousands of machines. And you can go to any one of those machines and say, "Hey, what's the greet variable?" And it it will say it's hello world. And so that's why it's pretty expensive and pretty redundant when you have thousands of machines all storing that information for you.

>> What else do we have here? >> All right, first application. I'm surprised no constructor. I I figured the constructor would be the next place we go, but nope. It's going to be we're going to be functions, right? >> Yep. >> And we have a view function verse a read versus a write, right? A read you can ask any node in the network and they can read. So reads are super easy and super cheap because everybody communicating peer-to-peer has all the information and you can ask any of them, right? So a read is very cheap but a write is very expensive when you have to write something. Yeah, you go. You go.

>> No, when you say read is cheap, it's actually free, right? I mean, at least when you do like from your client, it's free. It's free, right? >> It's it's basically a network call or even a local, you know, HTTP request to your node. >> Okay? >> Kind of like when we were writing the scripts, right? We would we would do a read. And when I had my local node, it was way faster because it was just hitting the node and responding right away instead of going to an API like infura. >> Yep. And what about this? Um because for example, we have the this count is this state variable, but we have like the type and what is this public? Is it the visibility right?

>> Yep. >> So is like in other languages. So how many visibility options do we have? We have public, right? Uh we have >> private. Private >> immutable is that I don't know external it's probably just public private and external right >> immutable is something else I think visibility here we go solidity by example will just show us oh internal oh yeah >> internal and external yes >> okay that's good >> but the gotcha here is great big disclaimer everything is public we're writing this stuff to a public blockchain so even if you mark something as private that thing is not actually really private it. Private means if someone inherits your contract and

deploys a new contract, they're not going to be able to access that private variable, but everyone can see everything on a public deterministic blockchain. So, just making sure we're not like private private. It's more like access and programming control private. >> Okay. So, you were mentioning also like this view, >> right? >> So, what is that view? because we have the function, the name of the function, then the visibility, and then this view. I'm not sure what this is. >> By by thanks for the slow pitch, Carlos.

>> By by marking it as view, you're saying, "Hey, we're not going to change any state here. This function is is you're you're able to call it and get a value right back from it." When we call increment or decrement here, that's actually going to update that state variable. So that's a transaction and that's going to have to get mined into the block and a whole bunch of expensive things. But since this is marked as view, anyone can go ask that contract what what the count currently is and it will immediately return you uh an integer and tell you what the count is.

Does that cover it? >> Yeah, I think I think so. And what happens if I try to put this view inside this function? >> I don't think the compiler is going to like it. It's going to say, yo, you're changing some kind of state variable. this can't be marked as view or or if you forget to put view on the get you'll get a warning that says hey you this can be marked as view and you can make it so people don't have to pay to call this function right >> and we also have pure function right I mean we have view and pure >> so the thing is that view is something that is going to return something from the state but is not going to change anything but pure is something that doesn't read or change anything from the state so it's going to be always the

same. It's deterministic 100%. Because this view is not deterministic, right? Because you can have for example this tox changing X. >> Yeah. So, so this X can change in the future. So, this is going to be different each time you call it. In this case, I mean, I'm not sure why you want to do this, but >> yeah. >> Yes. Like, this is going to be because it's going to be like a, you know, like expensive zoom, right? a very expensive calculator adding for you. >> Yes. Okay. So, I don't know about you, but I'm starting to feel a little nervous and I need to paste some of these codes into the into the ID and start like compiling and doing some >> I got a tinker.

>> Yeah. Want to tinker. >> Just one just one note I wanted to add there. I didn't realize before but apparently by on solidity by example like when you go to the contracts you can also directly open it in remix like there was the the line below so >> oh like >> yeah like you have the try on remix so I think that's just like to flag that if you know you can just try it over there >> yeah that is good yeah >> that's awesome >> yep so I guess this will like paste this code into remix, right?

>> But okay. >> Or maybe just opens remix. >> Uh I bet it's in there somewhere. I don't know. Maybe not. >> No, I don't know. >> No, >> but my my grief with with remix is it's just so complicated there. There's so much stuff here and it's hard to land here and like feel comfortable. But uh I don't know. Use the tool that whichever tool that gets you to the point where you're building and you're tinkering, use that tool. So I don't I don't mean to be uh you know opinionated about the tools. Use whatever tool gets the job done.

>> But for example, let's see how easy is to paste this into hard hat into a hard project. >> Here we go. >> And let's Okay, everything is installed now. So, as we saw yesterday, we have like a contract, some tests, some scripts, some configuration. So, it's all set up for us. So, we even have like this um contract. So, we were talking about having view. Let's Well, let's see if we do. >> Yeah, maybe compile and run that one first.

>> I think this is our compile, right? Not too fast. It may it may be downloading the compiler. >> Yeah, let's do that. >> There we go. It's that slow every time. Huh. >> Okay. So, let's try we saw this is like a set greeting. So, let's try to do like view, right? We talking we talking about this having like the view here. So, let's see what happens if we do that.

>> Did you hit save? Yes, I think so. >> Okay, cool. Yeah, there we go. It's like, wait a minute. You're updating the state, but you have this marked as view. Something's not right. >> Well, they the say potentially modify the not saying that you are modifying potentially you are modifying it. Okay, cool. Um, so I don't know what else. Um, >> let's yeah, let's start copy pasting examples in. Let's start compiling and let's poke at those. Uh, I think >> the like the the first app is great, the counter. I really like the counter because it's just like it's one and you can uh increment or decrement it. And I feel like the it really leans into this

is really simple code. Most Solidity is very simple obvious. It's just the the substrate on which it's running that's really complicated. The fact that you have a counter where you can increment the value is like blah that's pretty boring. But like if that value lives on thousands of machines and anyone can pay money to increment it, then it's like, "Oh, wait a minute. What kind of stuff could we build with this?" So let's uh Oh, >> what didn't it like something about your compiler version in hard hat? But this is good. We need to run into these problems and have this.

There we go. Yeah. >> So I think we have like 84 here and here. Oh, this is 813. So I guess we have two options here. We will do this, right? And this will work. >> Yeah, maybe set that to 84 or if there's some like new thing, we would need to just set that one to 813 and have hard hat. >> Uh >> let's see. >> Let's see. >> Maybe maybe >> let's see if it'll download it. Yeah. >> Oops. Not here.

>> My assumption is it will download it and compile it fine. It may give us a warning that's like, well, this is like a really new version of Solidity. You're not fully supported. Yep. Exactly. >> It compiled. >> It compiled. Good enough. Ship it. >> Warnings are not important. >> As long as there's no error like warnings. >> Yeah. Okay. So, what else? >> Well, can we count now? How do how do we how do we deploy it? So, okay. So, it compiles, right? And basically compiling means it's going from this solidity that we can all read and understand into bite code. Should we look at maybe the artifact that comes out of that just for

a second? I think there's an artifacts folder. Cool. >> Yeah, there it is. Either one of those I think is Oh, no. I'm sorry. It'll >> Yes. So here we have the AI, right, that we talked about it the other day >> and the bite code. Right >> there it is. That's that's actually the contract. That's what's going to live on chain. Is that all that information right there? Right, our little simple counter with one little state variable and two functions, one to increment that state variable and one to decrement that state variable is all of that bite code right there.

>> So what if we create like a simple super fast test? >> Okay, sounds good. >> So we already have we already have this here. Yes. So let's call it well counter. Maybe we need to change the name of the let's do that. Let's change the name counter then. Okay. So what is this inside the deploy function? I guess this is the >> I don't think Yep. I don't think there's any arguments.

>> Yes, this is the the argument for the constructor, right? But we don't have uh actually maybe >> even have a constructor. Yeah, there's no constructor. Yeah, >> maybe we can addit one, you know, like like for example >> count by default, right? >> Yep. >> Well, let's first create the test and then Okay. Sample counter. Okay. Okay. So, this is going to deploy the contract, right? These first three lines. And now let's um call counter well ink transaction and it's going to

be uh counter ink. Then as we know we can wait until the transaction is mined and then we can do like the the asset or the expect. So we should if we I I guess the count is start at zero right I mean we don't say that the count is start at zero but by default when you declare a new variable it start with the with some defaults right maybe a good mental model there is just thinking of like embedded programming like we're programming a little chip or something and that chip has memory and that memory is empty but

it's not actually empty means zeros right so if we just declare a counter it's just full of zeros which comes up to zero, right? And that's everything has this default value of zero because you have to allocate memory for it. And if you go ask that memory what the value is there, it's going to tell you zero. >> Yeah. So now if we do uh ink, no sorry counter count, right? Count this will be equal to equal one, I guess, right?

>> After incrementing. Yep. >> Okay. Let's see. Can do the test with >> you could even expect it to be equal to zero after deploying it and then increment it and then expect it to be one after that. Yeah, something like that. There we go. >> I think it's test. I think it's like that. >> Oh, home. Okay. Okay. Yeah, >> there we go.

>> Yeah, this is like the factory of the contract and with the capital C and then this is like the instance of the Okay, it's working. Nice. >> Cool. There's a lot going on behind the scenes though. This is why I like scaffold a lot because I can make a small change in the contract and then I can see it over here and I can poke at it like how how could we deploy the contract locally and just read the count from it or something like that? Is that possible within hard hat? Just like deploy it, go talk to it. I feel like we almost need scaffold ether remix for something like that. And maybe we should just like maybe we should just lean on the syntax right now and kind of go through all the different interesting

syntax within hard hat to get you the developer prepared for when we have a more uh you know fancy environment where we can tinker more. >> Yeah, I think the only way to tinker with contracts is just like creating test and scripts. >> Yeah. Then read you know and write against the the the contract. It's not super fast, but I think what I like is the like tinkering with the scaffold e and then when I want to when I have like a final version h then put it into hard hat and then you know writing test against it because it's way faster doing it that way because if you start from the beginning with this it's going you're going to be slower. I mean but that's like my preference >> no I think that's great. So, so know that eventually you can take your

contract and you can write an extensive test suite and go through and test all the different things about the contract, but for now we're probably just going to write and show off a bunch of solidity and just get you the programmer ready to to understand this language. >> Maybe maybe tomorrow we can, you know, create like a react application and make it to talk with with our contracts right in our heart network and that will be I think like a good introduction to the scaffold is next week. But for now, let's just stick with the with the black screen and and you know like the geeky stuff. Yeah.

>> So, okay, we talk we talk about creating a constructor which is like a special type of function which is called when the contract is deployed like only once, right? And it can take also arguments. So, let's say um well, so what is the difference? Do you know the difference between uint or uint 256? >> Same thing. It's just shortened. And I think we should just write in 256 just to be explicit about it. When people write you in I I kind of cringe a little bit because we're writing a smart contract and it should be very explicit.

>> Yeah, I feel the same. I always try to be like 256 because I mean it's Yeah. >> And that 256 is the number of bits you're using, right? If you if you did a Uint 8, you would be using eight bits and you could have a number between 0ero and 255. If you did a UN 16, you could have a number between zero and like 65,000 something, right? So, it's the number of bits. A UN 256 is freaking huge. U 256 is such a big number. Somewhere between zero and two to the power of 256.

So, it's a very large number. >> Yep. Okay. So, what about this? >> Looks good. Looks good. >> So, I like it. >> Yeah. So, if we What happens if we try to to test again? Is going to break, I think, >> because it's going to say something like the constructor needs one argument and you're not sending in any arguments or something like that. >> Yeah. >> Yeah. >> Missing arguments, right? So, let's fix it. Let's start with uh with 10.

So then >> then our test will need to change. Yep. 10 and 11 >> like this. >> All right. Ship it. >> Okay. Awesome. So we know we know how we know the structure of a contract. Uh we know the constructor. We know how storage variables work. We know how to write functions now, whether those are view functions or write functions. Uh what what are some other primitives?

We've got numbers down. Addresses are really weird when you put an address in. Can we put I don't know how we could put in an address here, but maybe I think Yep. Go ahead. Yeah. >> I was thinking about introducing in our contract the concept of owner. >> Yeah. >> Then we can something like that, right? >> Yeah. Maybe and then and then we can introduce a require statement and require that only the owner can increment or something >> and also the modifiers with being like only owner. So yeah, I think I think we are going in the right way. >> Yeah, >> the right direction. Okay, so other let's say public and owner, right?

>> And always remember that public uh you'll you'll get into a situation where you're writing your smart contract and something's not showing up in the front end. It's that dang public. Just make them public. Trust me, they're all public anyways. Unless unless you're you're building something that's going to be inherited somewhere else and you don't want people to access it, just remember to have that public there and just get really used to adding it. >> So, what is the default? Because if I don't say is private, the default internal, >> it's not going to show up in the API, I think. And then anything that parses the AI or any kind of front end that wants to talk to it can't just call some function. I think it maybe the public getters or something set up, but still like the nerds can get in there and find

the bits and still read that number. Just it's not as accessible to us uh you know non giga brains. >> Yeah. Okay. So when should we um initialize the owner? >> So technically you could just say owner equals right there and it's the same thing as setting it in the constructor, right? Yeah. Uh but for us, you know, we're programmers, maybe we should put it in the constructor and make it obvious right? >> And also we can take a look at these globals that you mentioned before. >> Message sender. >> Yeah. Because I mean you can do if you want, you could do this if you want. You can do like address and then the owner and you can pass the address directly on the constructor.

>> But you can also do owner equals to message sender. >> Okay, let's dive into globals real quick. Can we go look at them? Because it's like, whoa, message. What is this? You have a bunch of cool variables available to you within the execution of your smart contract. The base fee, the chain ID, the coinbase, the difficulty, uh the ones that you're out of all of these, you're going to use message.cender the most. You'll use message value now and then and you'll use block.timestamp now and then. And that's basically it. Yeah.

>> And also this is interesting like the TX origin and then well this is a like >> we'll get to this when we start talking contract to contract interaction. You can you can talk to a contract that can talk to another contract but not today. That's too complicated. >> Yeah. Yeah. Because it's it's tricky, right? Because it says sender of the message and this is the sender of the transaction which is weird, right? It's like kind of the same thing but not but Yeah. Yeah. That's for for another day. >> Yes. Okay. Yes. So that's perfect, right? I mean the owner and then we can let's create a new test and well let's do it here deploy and this will have the the contract deploy the with the initial

count of 10 and then the owner should be who is the owner here because uh hard hat is doing a lot of things for us out of the box so we don't know who is really like sending the transaction. I think we saw something the other day maybe but um you can do um with with hard hat heathers you can do something like um size shiner zero equals to I think it's ethers get cers yes so also keep in mind that we are using we are not using ethers directly we are using the ethers version of like the hard version which is like

heaters with more with more stuff right so it has this function which is going to return an array of all the signers like the the development this deterministic signer that we saw when we started the chain that I think it was like 20 or 10 signers that had set up for you so is written an array so if we do this we can get like the all the signers that we that we want >> could we even add a little console log on line that says something like deploying counter contract as signer zero just so we see like really really obviously who that signer zero is. Yeah. Cool.

>> And then something like serlight. >> Yeah. >> Okay. And now um we can do some new expect doing something like um counter dot order to be equal to signer zero address, right? >> And one thing that I think people get caught up on is the idea of this owner. It sounds like a contract has like a special keyword for owner. This this could be just like Carlos, right? It could be uh it's it's just yeah boss.

There we go. Exactly. It's any arbitrary thing. So just be careful not to like think do owner is that some special thing. It's not some special thing. We just defined it as Yeah. Let's just keep it as boss right now. I like that. >> It's still that it's not going to be like a built-in theme, right? In Ethereum like boss seems like a madeup thing. >> It it does. Bosses are made up. I think I think I agree with that. >> And just just wanted to add something. So we could also get like the addresses and who's signing by like I think hard accounts or I think that was like uh the command to run if you want to display all the accounts on your console >> like this >> and then accounts. Yep. And it should be that first one should be F1 something or

something like that, right? Because it comes with >> F39, right? >> Yeah. >> Yes. F39. All whole F39. >> Yeah. It's actually here, right? like they you see the console there. This is the one. Yeah. >> Okay. Nice, nice, nice. >> So our pass our tests all pass there. Is that right? It it >> yes >> deployed and the the boss is the message. And we double check that that is our deployer and it worked. >> Cool. So what if we only allow the owner to do like a deck operation and like only like this is going to be public and this is also going to be public but we only want to allow the owner or the boss to decrement decrement the account.

>> Anybody can increment but only the boss can decrement. That's fun. Yeah. I think so I think this is one of the most powerful things of all solidity of all the smart contracts everything we're talking about here. The most powerful thing is this require statement. Require is is going to be one of the most powerful keywords here. And I think that's what you're about to write here. You you basically get to create a rule within the execution that says this rule has to be true or we revert everything and everything rolls or or unrolls rolls back.

>> I was going to see to see if I can like find the revert or the quite here, but I'm not sure where. >> I don't know either. >> Yes. So maybe people can also like look into this. So by example is nice like it has like all the all the thing that you need to start with with smart contracts. So yeah. So let's do a require here. So what what are we going to require to um to run this function? So we are going to require that the message sender right like the person who is sending the transaction is equals to the right. Yep.

>> And this is the message that you say this is the revert message. So if this not if this is not true then this is going to get display display I guess return right. This is going to be like the revert reason. So you can say like sorry not the boss something like that right? >> We should we should we can even flip around. Let's not do it yet, but let's show the atomicity of a of a transaction and let's swap lines 24 and 25 eventually and show that it doesn't matter. But let's let's just run this real quick and make sure it works. So, let's write write something that what do we have to do? We have we need to prove that only the boss can decrement it, right? Again, this is where I want

scaffold where I can just like click the buttons and show it off. It's so much harder to have to write a test to interface with the contract. >> Yes. Uh so let's let's create a new test and then we can do I guess ser one deploy deploy we can also like I think we did it this the other day like we can copy this into a before function. So >> you taught us that already. >> Yeah. So that's that's fine. So let's do um I think something like uh counter I think is connect something like that. I think

with disconnect you can you can like send the transaction with that account by default if you if you don't say anything by default it send the transaction with the with the serer zero. >> It's always ser connect. Yes. And then let's say deck, right? >> I have no idea. I hope so. Let's see. >> Well, I mean, I'm not sure. So, so this is de transaction. Wait. Well, let's see if that uh that should throw an error, right?

Just just trying to call that function. That's perfect. Oh, okay. Cool. It threw the correct error as as signer one. We tried to decrement and we got the sorry not the boss. Awesome. And if you switch that to zero and run it real quick. >> Yeah. Or this thing the same thing. Yeah. >> Yep. >> We can actually like I think on day three we also did that like um we have like this. >> Yep. There it worked.

>> Revert to be revert. We can also like assert or expect that the transaction is going to be Yeah. Yeah, >> it worked. Yeah. >> Yep. >> Okay, so this is working. >> Uh I feel like maybe the atomicity of it is probably for later with scaffold when we can show the front end and the back end kind of more obviously. I think here we should just tinker around with uh syntax, right? So we've got functions down. We've got require statements. These require statements are pretty critical. Uh once we have that, let's see. Have we gone through all the primitives and data types? We've talked about bools. I mean, a bool is a bool.

You know, you you at home should already know what a bool is. It's either going to be true or false. A uint is going to be a number, right? An address is probably the weirdest one, and you can paste it paste an address right into the syntax. But let's assume that we've got our primitives and data types out of the way. >> Do we have we have our global units kind of out of the way, right? We've talked about message.value, message.cender, sender block timestamp. >> What about modifiers? >> Modifiers is that next? Yes, I was um you know I just copied this function and I call it like super inc which is like increment the count by 10 >> but only the boss can call that function right so as you see here like maybe we will have this contract is going to keep

growing and we'll have this line all over again because we are some function are only for the for the boss right to run so this is where modifiers can help us so Let's see. I'm sure that this will have uh yes >> copy paste example. Go to solid example and hit copy. >> Oh, only owner. So I'm glad that we stuck with the with the boss. >> Yeah, we want only boss. >> Only boss. Okay. So can you like Austin do like an introduction of what like a modifier is?

>> Yeah, of course. Um so modifiers are um so basically like when you use the modifier and please correct me if I'm wrong like you can call it before your function. So it can say like check like go to only owner and if it pass like go into the function. So instead of like having the require statement you can create this modifier which allows you to like easily use it over and over again. Uh that's my >> the syntax is super weird. the little like underscore semicolon thing. That is the weirdest part. But just imagine it like it's going to run the stuff inside the modifier and then wherever it finds underscore semicolon, it's going to run your function. So it's like it's like a

fun it like wraps the function, does some stuff and then runs the actual function. Well, it it makes more sense if we see it in the code, I think. >> Yeah. So let's uh where is it? Here. Here. Yeah. Let's copy this one. Um >> like the only >> sorry I was going to say like the only owner one is one which is very very commonly used. Um like you will have a lot of functions which you want only the owner especially like with your own money. Um, just like as a side note, >> and if it's imagine it like this, imagine you need to check that it's only the owner and a certain time stamp hasn't passed yet and the

balance of the contract is greater than some amount, right? There there could be a bunch of different factors you need to check and you need to do that for like four or five different functions. You'd find yourself writing those same require statements in the top of all those different functions. This modifier saves you. It it basically you write those requires once and you test those requires once and then you can add those modifiers to any function. Let's So let's see. We've got an only boss. So >> only boss. Yep. >> Yeah. And it checks to make sure that the boss is right and then an underscore semicolon.

>> This is this is weird. This is weird as often said, but it's the same if you have used like some like middleware if you use like a node for express. So it's the same thing I think in in express is like next. So it's just just run what is here and then it's just like keep keep going with the with the execution. So that is like the the way that they implemented that. I don't know maybe it's not pretty but is the way it works. Yeah. Okay. So how do we use that? uh because I guess we can remove this now and also this and it's just adding the modifier here right >> very clear yeah and and hopefully if you're a programmer at home that makes

sense to you we just added only boss to super increment and mentally you can clearly see like oh this is a function that only the boss can run this decrement function only the boss can run and you can go up to that modifier only boss and double check it but now we can test it and make sure it works the same way. >> It should revert, right? Because we are using serer. >> Yep. >> So the boss, let's see. So only the boss can run a decrement and ser one is not a boss. So it it should fail. Okay, cool.

>> Okay, >> so we've got modifiers. We've got uh functions. I think I think we're ready for mappings. Are we ready for mappings? Yeah. Okay. >> I feel like a good example of a mapping is just making a token, right? Like like once you have a mapping and a transfer function, you have a token. So maybe we should just build a little digital currency here next. >> Yes. I love it. >> Okay. So >> sadly sadly the number like if if Ethereum was an app store, the number one app in the app store for the first couple years is digital currencies. We had digital currencies in a lot of other places. But by creating decentralized digital currencies, there was a big wave of ICOs and money moving around and

sadly some probably a lot of bad things happening in web 3 that kind of gives us a bad name. But we're here for the tech. We're in love with it. I'm all about it. How do we make a decentralized currency? What do we need? >> And we are going to save it the day of course with the Sanford token, right? Ooh, Sanford token ICO. Yeah. >> Okay. Okay. Okay. Okay. Um, where is it? >> Where?

>> Oh, the three. Sorry. Here. >> There we go. >> Yeah. Okay. So, let's start from the beginning. Oops. Let's copy this and then contract Sanford token and okay when you think at least when I think about a token or NFT or or whatever um usually I think about the mint function always right like mint is like the creation we can even call it like create if you don't want to use mint um so yeah let's I'm going to do like the skeleton of the function and then I I'm going to ask for help to so Ed or you can ask and help me with with

this. Okay. So let's call it create and this is going to be public and let's do uh 256 quantity. So I want to be able to to mint like um like quantity tokens like 100 tokens for example. So yeah, how can we keep going from here? because I'm going to from the you know from the test or from the UI I'm going to call this create function with 100 and I want to be able to to the contract to track that I have 100 tokens.

>> So >> so I might I might push back a little bit and say that what we want to do is create like some digital scarcity here and it kind of worries me that this create function is public and anyone can get in here and create it. So maybe we would add something like an only boss modifier. But I want to do even better than that. What if what if this is the constructor and we only create the quantity once and it could never run again. It's a little bit different than your mint function in terms of creating an NFT. But I'd like the scarcity to be in place from the start. And I'd like even the owner to not be able to just like create this stuff willy-nilly. I'd like for us to just like have the constructor make a hundred of these

tokens and never be able to mint them again. Right. So, I would replace this with just like constructor quantity and set it. But I think what you're you you've got a different idea here. So, maybe we go down that path. >> No, no, that's fine. That's fine. I I totally get it. So, maybe another option would be have like a because we haven't talked about constants, right? >> Right. Right. Yep. So we can also have something like uh >> is is here con no con I think it's public >> so so just to kind of add uh something to that to just make it like the the Stanford token a bit more clear. I just wanted to uh so basically like we're going to create a token which has like a total supply which is limited to a

certain like number. So you can't mint more than that. Like minting means creating. And I guess like our token will have like a name and probably like a symbol. And I guess like we're going to do step by step right now that just to kind of set the stage. >> Yes. I guess what we need is a require statement in inside of this create. We need to require that the current supply is still less than total supply or something like that. >> So maybe we would be limiting it. >> Oh, sorry. So maybe you can like total created something like that and can start with here.

>> Yeah. Yep. >> Right. >> Yep. So yeah, I mean like like Eda said like usually if you're going to create like a token h you are probably going to use like ERC20 or ERC like 720 21 and inherit from that because we haven't talked about inheritance yet but that's probably the way that you want to go but I guess like here like for the learning purposes I guess for the sake of learning we are going to go from scratch if that's okay. >> Sounds great. >> Okay. So, so um here what do we want to do? Um we we don't want to get over this total supply, right? And we have this total

created. So something that we can do is require that um the quantity plus the total curated should be less or equal that total supply. Right? And if you know we are going to say okay uh total supply rich something like that >> and just by writing that one rule now we've created this idea of digital scarcity like there can never be more than a thousand Sanford tokens because we wrote it very explicitly in this contract.

>> Yeah I'm sorry for dissing like your constructor idea. >> That's all good. It's all good. Same thing. >> It's the same thing, right? I mean the same that that we did here, right? like we initialize the count with this initial count it should be the same like we could do like total supply and then having like the you know like the total supply initialized by the constructor so I guess it is the same >> okay so now we have that problem problem solved we can mint like anyone can't mint more than the total supply even the owner right at least for now but now if I want to mint like 100 tokens what do I need to do Sorry. Here. What do I need to do here to keep track of that minting or that

creation of tokens? >> We're still not really keeping track of who has what. All we've done so far is set a limit. This is where our mapping comes in, right? We need a storage element. That's a mapping that keeps track of for any given address, we'll keep track of a number. >> So a mapping if you are coming from another dictionary dictionary language, it's like a dictionary. It's just like that you have like the the key and the value and the key can be like any of the types that we have seen so far. So in this case I will say that we want to be an address right and then the and we int 256. So we are going to map like people's addresses with the token balance that they have right and we can

call it this is going to be public and we can call it balances. Right. >> Perfect. And and the gotcha here between a mapping and some other data struct is you can't iterate through your mappings. Your mappings are just one primitive to another primitive and you you can't loop through those. If we needed to know like say we weren't keeping track of total supply and we actually wanted to calculate our total supply, it would be terribly inefficient. But if you wanted to loop through all of the balances of all of the people and add it up to get the total supply, you couldn't do it with a mapping. You'd need to use an array and you need to use something that you can loop through. So think of it like a hashmap. It's a it's a mapping

where it's like a one-time like address to number and uh they all start at zeros and it's more memory efficient than keeping track of a big array. Yeah, for that reason like you said you have to sometimes you have to create like some extra variables state variables to keep track of things like that right because if you want to iterate and get like the the total created it's impossible to do with the mapping only >> exactly >> okay so Eda what do you think that we need to do with the balances h because we >> we are m we are trying to create this kind this quantity for for who because also we can also have like an address here and two but for now maybe we can

say that the person who is sending the transaction the message sender is the one that is going to receive the the balance right the tokens so >> yeah exactly um so we can directly like use message center like add into that uh balances mapping who the message centers and the quantity of tokens they they get equals quantity >> if we do this what will happen this what will happen >> we might have just destroyed some value. >> Yes, because if if you do this uh then the first time that they create some tokens, it will be perfect, but the second time it's going to override the their balance, right? So, we can add this little thing in the plus sign here and it's going to keep adding the the

balance >> and and there are some programming languages where like JavaScript where if that balance isn't initiated or initialized and you try to do a plus equal, you start getting weird stuff like nan. Remember this is more like embedded programming and when you create that balance you're actually making a space in memory. So any given balance is already zero. So plus equals works fine without any initialization. >> And we talk about like overflows before and and that we don't need to worry about that with this version. But what if we were using this with an other version?

That's another thing we'd want to check is if you take the balance and you plus equals and it rolls around, something really bad has happened, right? If you're not using enough bits to track this, let's say we're a balance is only a 8 >> and we're at 250 and someone adds 10, they're going to get four. It's going to go from 250 + 10 is four. And you're going to say, "What?" And that's because it rolled around. All those bits were high and now they're low again right? >> Yeah. Yeah.

>> So you would want to double check that you're not going to overflow. >> Yes. Because also if they will go over this require and they will mean for themselves like a lot of tokens here, right? Because they here they will be four but here will be like the full quantity that they wanted to meet. So yeah like be careful with that. But in this case we don't have to worry because we are using this this version. Okay. Um, what else we can do? Uh, >> we need to plus equals the total created in this case. >> I I kind of think we're we're really keeping track of a lot of extra stuff here. And some contract auditors would probably tell us we could probably get away or figure out a way to do this with less variables, but this is very explicit. It's easy to see what's going

on here. We've we've created a quant quantity. We've given that quantity to the message. We've made sure that when you're creating your quantity, you can only go so much. Uh, this looks pretty good here. >> Yeah. >> Are we missing anything else? >> I don't think so. Like now we could create a test to test this or we can keep creating if you want. Maybe we can keep adding like functionality like >> I think we need a transfer function probably. >> Is that what's next? Yeah. >> Yes. Let's let's call it send >> because uh we don't want to give >> Yeah. Let's assume that it works. And what's what's happening is we can deploy this and the owner uh whoever deploys it will be able to

call create with any amount and whenever they call create it's just going to add to their balance but then they can't send any out of their balance right so it's basically just the owner keeping track of some own their own money or their own counter right so this this send function is going to be really critical >> oh and I think something that we are missing >> okay >> like uh but I mean we can do the send first but is like buyable functions. We haven't talked about buyable, right? >> Oh, not yet. Nope. Uh-uh. >> Like if we wanted to be able to let people buy the token and send in some ETH to get the token. Yeah, maybe we could add a buy function down at the bottom or something like that.

>> Okay. Okay. So, let's let's deal with the address too to public. Okay. So we want to send like um some tokens to the to address. So what should we check first? >> Luckily because of safe math being built in, we don't have to check anything. We can basically just say the to address plus equals the quantity and the from address minus equals the quantity. I I'd maybe even start with the from address first. So I'd say balances of message sender.

>> Yeah. So you're saying that we are doing this and then >> minus equals the quantity. Yep. >> So you're saying that because of the version of solidity right if we do this then this will revert if we overflow because this is like a you int right so is it needs to be like a zero or positive number. So if we try to go if them if the this person has like 100 tokens and then we try to send 200 tokens this is going to overflow or underflow in this case and it's going to reverse right so we don't we don't need to worry about about that anymore >> but perhaps if we wanted to be very explicit about it maybe we should write that require statement just to make it

obvious to uh you know developers at home that we're very very sure that we have the right quantity and what we would do is just require error that the balance of message.cender is greater than or equal to the quantity and then we'd send them a message saying you don't have enough or something like that, right? So, it's not super important and it's going to work anyways, but we're going to spend the extra gas. Normally, you wouldn't do this, but in this case, we're going to spend the extra gas to have a nice little error message here that tells you that you don't have enough, but normally that's not going to be in a production contract.

>> Yeah. >> Okay. >> Looks good. Yeah. And this is how actually works like in real contract because this seems like a like we are just like playing here but in reality if you want like a like a ERC20 with you know like in production is going to be way different than this but actually it's going to be look like super similar to this like this is how like a send function look like. This is like like minus the balance the quantity here and plus the balance the quantity here. So it's kind of the it's going to be like the same thing. You're programming money. It's crazy to think about it, but you're like programming value in a decentralized way. It's a really neat like when we think of a token,

>> nothing is actually getting sent around. Going back to the the talk about Bitcoin and sending Bitcoin through wires, right? It's all actually just a smart contract that's keeping track of values. And when you transfer, you're getting in there and subtracting some from one and adding it to another. And that's what a token is. That's how this all works. >> Yep. Should we write a test? What should we do now? It seems like we should probably uh uh try to send something and make sure it sends to someone or something along those lines. >> Yes, this could be >> and then uh token

the first could be like the set should be able to create tokens. >> Okay. First we create then we send. That's that makes sense. >> And then let's let's do the before here. So this is going to run every time. Not every time like well before each is going to run >> before each if statement. Yeah. >> Then we can you know we can have the counter not the counter the token here. And then this is going to be token sort token token.

Okay. So, let's create transaction. I think we don't need this for this one. Create create token. Create and let's do like 100. Okay. So now we can um I think the expect here would be like the token >> expect balance to be 100. Yeah. The balance.

>> So I guess that we need the the address of the of the signer, right? >> Yep. >> So the balance of the serer that address to be equal to 100, right? >> What do you think? >> Yeah, it looks good so far. Maybe run it and see. I'm mildly confident. I don't know. >> Can Can you Can you give me the percentage of >> Oh, I don't know. See See, I would say like 90%, but then like there could be some little weird syntax that could catch us like >> Okay, let's Okay, >> let's find out.

>> Okay, let's see. >> There's one one good way to find out. Yeah. >> Oh, >> we needed an argument in the deployment. >> Oh, we still have an argument laying around. Yeah. >> It was a hard one to catch though to be fair. >> That's true. >> That's why we have all these testing suites. >> Yes. Okay. All right.

So we deploy our smart contract and we call create and it creates a 100 and uh now our deployer has 100 tokens. Can we transfer them? Can we send them? >> Let's do first like the test the the revert if we try to mint more than one house. Right. >> Okay. Yeah. So should revert if um creation creating more than total supply. So I I think we could actually like read the total supply from the contract, right? So we can do um total supply. I mean we we could hardcode it like the the total supply here. But if we want to go

we can do token And this of course is going to be a big number I think right? >> I think so. >> Total supply and then we can do uh total supply then at I don't know like 100. >> Uh we need to do what? Like a thousand though, right? We want to roll it over. Oh, you're just creating. I got it. It could be one. Yeah, I got it. Okay.

Yeah. Yeah. It's the total supply plus a little bit more to cause it to break. Got it. >> Yeah. So, I think uh we can do something. I don't remember correctly. Wait. I think we need to remove this. This is weird when you need to check the the revert. It sometimes is weird to be reverted. Okay. I don't know. Let's test. Let's see if it works. only like 40% confident right now. >> Oh, >> we're just changing stuff around so quickly. I don't know. It worked. It worked.

>> Okay. So, now we we know that we're able to create tokens and we know that we're only able to create so many tokens. We have this sort of scarcity model where there's only a thousand that can ever get created. So, now it's about creating those tokens and can we send them around? >> Okay, so we can copy the same test that we have, right? like the for the signer zero um minting like creating some tokens like 100 and now let's uh send >> this will this uh so so for each it statement it's redeploying the contract.

So up above when we're creating 100 and down here we have you want to be clear that it's like it it starts over at zero again with each one of these it statements and make sure that's clear in everybody's mind. Yes, like we could also like remove this each and this will get deployed once and then this will create 100 tokens >> on top of each other statement. But it's easier with the before each to say yeah within each one of these chunks you get a completely clean contract and you can test that one situation that you wanted to test.

>> Yeah. And you can also create like some describes inside the describes with with the before. So you can create like different setups if you want, you know, press >> and mocha or chai. You're basically you're looking for the syntax. Google mocha testing and you'll find >> it might even be called waffle now. Is that right? Is mocha. I don't I don't even know. >> Not good. Looks good. >> Like a tea, I think. Yeah, >> there we go. >> Yeah, I think you can look it up here. Yeah, like the this is the expect is the one that we are using because you have like like the suit or the assert.

>> Y >> So yeah, I think this is the I think um like um waffle just like add some stuff just specifically just >> probably like that reverted. Yeah. >> Yes. And you can also like create mocks for some contracts and and get like events from contract and things like that. So >> and probably some big number stuff where you can check to see if it's like greater than or equal to some other big number. Yeah. >> Okay. So let's send let's do like this um send transaction and we want to call token send and the first argument was the address and then the quantity. So let's send to shiner one address like 25 for example right

then let's await to the transaction to be mined and now um we could do this right ser should have 75 and ser one should have 95. Okay. 90%. >> Yeah. Yeah. >> I don't know. >> Oh, yes.

>> There we go. All right. So, we're able to deploy the contract. We're able to mint some uh uh amount. We're we're sure that we can't mint too many. So we have some set amount that we're able to create ever and now we're able to send them around and it correctly sends from from you to the other address and we can test that and we can now now these tests are all automated too like so so over in scaffold we could be poking around and doing things quickly and sending it around and seeing it quicker but then if we change one little thing about the contract this is really nice because we can run our whole test suite again and double check that some weird you know situation didn't come up right. Yeah, that's that's what I think um for me is

really useful to to tinker with with Scav, you know, until you have like a kind of like a final version of your contract and then when it's almost ready like going to hard hat and do all the tests because then >> right all those different test cases. Yep. Yep. And even things like in Foundry they talk about like fuzzing, right? like all sorts of like weird fuzzing that you can do and like try a bunch of random values and try this plus one and try this minus one and make sure like make sure like these weird like edge cases don't come up in your tests also and you can automate all that as part of a test suite.

>> Okay. So I don't know I think we can go with with uh sending some e to a contract or maybe showing like some extracts also like the >> Yep. Yep. Yep. Yep. I think yeah, let's let's get that payable function out of the way pretty quickly. Let's see. >> So maybe maybe uh the admin can create, but maybe let's copy the create function and create a buy function. >> And so and in this case, you wouldn't have to be the admin. Wait, wait, can anybody create? Oh, we're missing a require statement there. Don't we need some kind of requirement?

>> We didn't copy like the only bosses in this contract, but we can copy if you want. Yeah, >> we the only boss. Yeah. Get it back in there. >> Yeah. >> So, we kind of >> So, that's something we didn't test, right? You you write this, you know, cool test thing, but you forgot about this one thing where like anybody can create it and you forgot to write the test for that and now it's Yeah, >> there we go. So, we want only the boss to be able to. And are we saving the boss somewhere? >> Oh, it's >> There we go. >> We can copy this. >> There we go.

>> Okay. Then we need to copy also like the state variable. And this is nice because we are making like a lot of changes. And now if we run our test like everything is going to break which is a good thing, right? Is a good thing. >> Okay. So now >> pull that initial count out of the constructor. >> I was I was just going to say the count is going to look like a nerd. >> It's like nerds start getting like uh like goosebumps on the back of their neck when you forget to put the semicolon down or something. when you're watching another nerd code, it's like detected the things that they don't see right away.

>> Yeah. Yeah. For me, something like this works better, you know, like having like this extra space for no reason that give me like bumps, you know, like I can I can stand so I can do this and then a lot of people will be like super nervous at home, you know, >> get that white space out of there. Is that what you're saying? Yeah. >> Yes. >> Okay. So, okay. >> So, we want to create function that's not going to be only boss and we want to make it payable. So, we want people to be able to send in ETH and get back some of our token, right? >> So, what if we only allow to to mint like one to two?

>> Okay, >> so that that's easier. So, we can say like >> Bible and then the quantity will be plus one and then this will be uh I guess we can do this, right? >> Oh jeez. >> No. Got it. Yeah. So, you're saying it could be total created plus one is less than equal to the supply, but since it's plus one, you're just taking it out. That was a little a little programmer magic there, but hopefully hopefully all you kids at home fall in love with that. Okay. So, I think um well, we are not checking anything. I mean, this only means that that this function can receive E, right? But we

are not actually checking anything. they are not keeping track of uh how much they are sending of of is that enough or or what >> global variable >> yes so it can be um oops public and mint price I mean this can be constant right constant mint price or creation price and then we can say something like uh this.

>> I've got another one we can add here while we're here. The boss gets updated once and then never changes again. There's no uh function to transfer. So, what we can do is we can make the boss immutable and it's going to save us a lot of gas. So on line 10 you could do address public immutable just like your constant but this is saying uh two M's imm sorry Spanish speaker >> and then delete the end. Yeah I think >> immutable >> oh no it didn't highlight. Oh no. Oh no.

How does mutable work? I thought I was being >> Let's see. Maybe is my IDE. >> No, it's working. It's working. Maybe >> that was it. Okay. Just the ID doesn't highlight it yet. Okay, cool. >> So that just saves you some like making it a constant. Yep. Yep. >> Okay. So bad. Roll back. Sorry for that side quest. Okay. So So the boss isn't going to change. So we can make it immutable. But what you've created is a price and you've made that price constant. And all of this stuff like immutable and constant is saving a little bit of gas. But when everything costs money to run, these little nice little changes are going to help save your your users money.

>> Okay. So we have a creation price now. So what we need to do is make sure they paid the price, right? So we probably need some require statement here that looks at the message. value, which is our new global variable, >> needs to be greater than or equal to or maybe even equal equal to the price. >> I mean, if it's greater that they can send more money, right? >> They could, which >> which in in the case of I'm running the vending machine, I don't care if you put in a 20, I'm just going to give you a bottle of water back. But in the case of the user, if the user puts in a 20 and then just gets a bottle of water back and no change, they might be unhappy about things. So, you might want to do equals equals in that case.

>> Okay. >> The only time I use greater than or equal to is when I'm on a price curve and there's lots of people buying and uh I want I like that price is changing as each person buys. So, you don't want to have collisions. You could have two or three people buy at the same time and send in just a little bit more and it would work. Uh in this case, let's just do equals equals in case they send too much money. We want to revert to save them uh accidentally like adding a zero or something weird there. >> Okay. So maybe let's run our test again and see how everything breaks.

>> It should it should break, right? Let's see. Well, we're not we're not testing by everything should work, I think. >> Yeah, but no, but I mean I think we check some stuff. I don't know. Well >> Oh, okay. Okay. Let's find out. >> That's good. >> Uh oh. Okay. Because Okay. This this is the constructor and it doesn't have like any arguments. Okay. Yeah. Okay. >> We probably should have written something to test whether ser one can call the uh mint function the the create function and then in this case that would have been failing until we added the only boss thing and then that would work. There we go.

>> If tries to create >> There we go. Okay. Um, and then I think it was uh ser [Music] Not sure if this is going to work, but let's try. Uh no.

>> Create not. It's not dot total supply. It's got to be Oh, >> yeah. Yeah. Right. >> Yep. Yep. >> Okay. Here. Great. >> Yeah. You don't even need to know total supply. Yeah. You can just have it try to create one, right? Yeah. >> Yeah. Okay. Let's see. >> This is just making sure some random person can't get in and call our boss function, which we've just added. There we go. Awesome. Okay, >> nice.

>> So, the boss and only the boss can mint them for free. But now we have this new buy function and we want to test it. Can a rando get in here and pay a little money and mint one? >> How do you spell a rando? I like >> r a n o. Maybe >> like that. >> Yeah. Yeah. Yeah. And uh so it was confusing for me that the money doesn't go to like the owner of the bus, it goes to the smart contract.

>> So just to be clear like it's not going to this boss person >> when you depire. Yep. Go ahead. >> No, I was going to say that that's a really good point because right now uh that money is gone forever. >> Yep. >> I mean we'll get into that. Yeah. Yeah. because that that uh money is going to be locked into the contract because we don't have like any withdraw function or anything. >> But a really cool insight there. When you deploy a contract, that contract now has an address and that contract can hold money just like an externally owned address right?

>> Yeah. >> So, yeah, and we'll get into that too, but uh let's see where we are here. >> Okay, let's get some signers. >> Then let's allow let's connect uh which one? This one. But instead of calling um create we're going to call by and to send some value. Uh >> I don't think there's any arguments is there? Yeah because we just said one. Yep. >> Yes. So to send some value you have to put like this square bracket and send like the value and then we have our friend ether's u utils >> uh parse either and then we want to send >> a couple weird things all at once.

Right? So we're sending an extra argument in to be able to send value. And this is a little shaky like every everything is a transaction and every track transaction has a to and a from but there's also this value and data field and the data field can be like if you're deploying a contract or if you're talking to a contract and then this value is like money that comes along with the transaction and so we're sending a little money along with it when we call this function on this contract and that's that and then we also use the parse ether to be able to get to the big number. A lot of gotchas here all at once.

>> Yes. Yes. We could also check this price, right? Like get getting this price from the contract and sending that directly instead of like doing this hardcoded. But I think we already did that for something. So let's do it this way. So this looks good. Um just creating a transaction that this would be like buy right by transaction. And then do >> we need to connect serer one on line 55 token connect. >> Yeah I guess I guess we're good. I mean it will work the same because uh >> oh we're just going to buy it as the boss.

>> Yeah I mean it makes sense because you can do it for free but uh >> okay >> anyone can including the boss. >> Let's do it because we have SER one. Yeah >> there. So so let's let's do it. >> And so like the hard accounts by default I believe have Ether inside them. Uh but like when you're like using another wallet, they it needs to have like that um test net value or whatever you want to add there. >> Yep. If you were deploying this to some test net, you would need to charge up all these accounts with some ETH so they have money to to interact.

Okay, >> everything's working. >> All right, >> so let's see what what happen if we send like uh less than the money. Yes, I mean that could be like another test, right? like sending less than the >> a little bit less. Yeah. Should cause it to fail. >> We should get uh Yeah. >> Awesome. >> Cool. >> Okay. So, what can our token do? Our token can now it has a limited supply and anytime you try to mint. Okay. So, someone calls uh the buy function. Do are we still limiting it? Yep, we are.

There's that first require statement which since we're require we're writing that require statement more than once we probably should almost make that into a modifier too right there should be a modifier of like within total supply or something like that >> because that happens on both the buy and the create >> we don't need to do that right now in this case also will take some arguments right because um it will need to get like the quantity right to compute this >> yep Yep. Oh, that's right. Yeah. Then then it gets a little hairier, doesn't it?

Yes. Yep. >> Okay. So, so the the goal today is just to get you at home uh the the resources and the exposure to be able to start tinkering with Solidity. Uh we've kind of gone through a lot of those early use cases, but then there's probably like a handful of things to go try or to try next. And maybe we should just jump over to that Solidity by example page real quick and just kind of like let Yep. Exactly. What's inside? Yep, exactly. And a strruct you can see here is there's a strct to-do and it's just a strruct a structure of other primitives, right? You could have strings, bools, uh even other strcts maybe. I don't know if you can do that, strings, bools, uh

uints, all of those things that are are the primitives. You can have that. And notice this is a cool like to-do list, right? So we're getting two things here. We're learning strrus and we're learning arrays. Yes, >> there we go. So, we create an array of our strrus and then you can see in the create function, it's showing you three different ways to populate that array. There's a push function. Uh there's a push function where it's explicit about the data types and then there's like a an equals function where Oh, no. They're all push functions, aren't they? Yep.

It's just like depends on what you're pushing into there. Yep. >> Cool. So I wonder how can we add like some extracts into our contract. >> Maybe like having like a like some kind some kind of identification in our contract. Some people can have like some data from them like this will be like a like a token holder data and have like a name or country or whatever and then we can that in a mapping something like that. >> Something we would need to iterate through basically. So, so going back to that mapping, you can't iterate through mappings. Mappings are like a onedirectional. You you put in the thing, you get the thing back. An array is actually something we can iterate

through. We could write a for loop and we could look through all of them. How about how about just like thinking out loud here, what if there was like a way to vote on increasing the total supply and if enough people have voted on it, then we can increase the total supply. To be able to check those votes, we would have a vote in a strruct and we would need to loop through with a for loop checking all the votes to see if there's enough votes to increase the total supply. And if there is, we would we would uh increase the supply. So just know that you would have to like increment through things. I don't think we have time to to actually build that in today, but just like getting you at home just exposed to this process of

tinkering and you go to Solidity by example. You want to learn each of these topics. You kind of can paste them in here. You can make sure they compile and then you can write tests to make sure that they do uh what you want them to do. Yeah. There you go. Yep. >> Something like this, right? Would be like a >> Yep. And hopefully you at home know how to write a for loop. But that would basically but what would be you you would do these pushes where you would push each vote in. There would probably be some vote function and then you would loop through in a for loop checking that you have a certain amount of vote threshold and maybe some quorum together before you make that that total supply change. I think that's a pretty good run through. What else? Okay, so the the

other thing so two two last things I want to cover. Uh let's talk about that payable function real quick. When we made that payable function, if you tried to send value in to a function that's not payable, it's going to revert. If we scroll down, we see that non-payable there verse payable. Yeah. See how there's a deposit function that's payable and then there's a non-payable function. So if you if you as the user try to send value to a function that doesn't that's not payable it's going to revert because it's trying to be safe. It wants to be very explicit about things. So it makes you the programmer write in for sure that this is payable. So another concept very very similar to that is sending money

directly to the contract. it since the contract gets deployed and it has an address then oh so sending ether is the second thing I want to talk about but the first thing I want to talk about is uh the receive function yes >> so oh it is there okay oh it is there okay so the the smart contract has an address and you're able to send uh ether to it uh let's just say we open up our meta mask our wallet and we paste in the contract's address and we try to send it $100 worth of Ethereum the the contract has to have this receive leave external payable in it. And if it doesn't, it's going to it's going to revert just like the payable function, but this is actually like a payable contract, right?

Without having the receive function, that address actually can't receive E. You could just open up your MetaMask and you could paste in the contract's address and you could try to send $100 and when you hit send, it's going to revert. It's going to fail right away because it doesn't have this receive function. So, if you want people to be able to send ETH to your contract, you'll want to write in this receive function here. Basically, you would throw that. Yep. You throw it right down in there. And you can actually like write some things in within those squigglies if maybe you wanted to keep track of, >> I don't know, who who did a deposit or maybe you trigger an event, right?

>> Yeah. >> So, so yeah. So, that's that's the payable. And then the other thing I want to talk about is just uh the the sending ETH because it's kind of a gotcha. Uh if you wanted to send ETH within a smart contract, you would probably do a send or a transfer. Do we have that here? Yeah, there we go. See how it's >> addresst transfer, right? >> That's going to send money to uh uh basically that function is going to send whatever value comes in on the payable to the to address, right? So it's going to like send it back. Oh yeah, send it to them. So address you. So an address is kind of like a first class citizen in insulated. You can say address.balance

or address send or address.t transfer. But there's a huge gotcha here because that's not what you actually want to send. You want to use to send ETH. Another gotcha. You you want to actually use this call. And it's some ugly syntax. But this is the correct way to send ether in a contract. And the gotcha is sometimes you're going to be sending to a contract that has that receive function and it's going to have to do a little extra execution and so there's this gas limit issue. But forgetting all of that for now. This is just one of those like let's put the gotcha out there and put it into your pocket. Don't use sendin.t transfer even though it's easier. You need to use this call with

value anytime you're sending ether within your contract just to make sure that that gas limit thing doesn't doesn't spark up. >> Yeah, I think like a good exercise for people at home is just writing the withdraw function that we just >> let's write the withdraw function as people are buying this. Yeah, exactly. >> So this should be only for the only boss right? >> Yeah. And then do we want to do this live or should we keep keep this for people to do as an exercise? >> Well, I mean it's it's pretty ugly. We should copy and paste thing. I think we should do it because normally you would say, "Oh, we need to send the money to the message." So you'd probably do message.

Send and then the a balance of the contract. >> Yes. So let's copy this and this these two guys would be like the boss, right? >> Yep. Then this this well this is interesting. This wanted to be >> like something like this, right? So this will send the balance of the of the contract. >> Weird line of code. Yep. And you can Yep. Yep. >> You can see how weird this is. But this is just one of those gotchas and all Solidity developers sort of just get used to just doing it this way. And you look up sending Ether Solidity by example and you copy and paste that call in here and you fill it out. And also that one weird thing you did there about

the address, you're basically getting the entire balance of the contract using that uh piece right there. Pretty strange. >> Yeah, it's a a really strange but really powerful line. >> So there's one other thing I have in my list that we haven't covered and that's events. So, we've talked about storage and we've talked about memory variables versus storage variables, but there's another kind of interesting sort of onchain storage that's not onchain storage and those are events. And you can have your smart contract emit an event. And what that allows you to do, let's say, uh, we're building a ledger and we let a little bank. We're going to build a little bank and we're going to let any user deposit or withdraw money.

We would use a mapping to track balances. When someone deposits, it would be a payable function and whatever message.value you put in, it would increment your balance. When you withdraw, it would decrement your balance. Right? The user, if you came to the contract, would only have a current balance. And you would ask, you could ask the bank, what is my current balance? But the user wants to probably see a whole ledger of all their deposits and all their withdrawals. So the way you do that is you would have an an event and you would emit that event each time they call one of those. And what that does is it allows your front end to say, "Hey, what are all of the events for this specific address?" And it can

show you here's all the deposits and the withdrawals. So it's kind of like a way to do onchain storage where you're asking a contract for some information and it's giving you a back those events. But the gotcha with events is that other contracts can't read them. So as we get into contract to contract interaction, probably next week, uh, one contract can't go look at all the events of another contract. They're only for off-chain reading, but it's stored on chain. It's something that's stored on all of these machines, and you can ask any of the the machines for my deposit history if you have those events. So, it's like a way of doing cheap onst onchain storage with the gotcha that other smart contracts can't read it. Did

I cover that pretty well? You you want to have events in your in your front end or you want to have events in your smart contract just so you can have your front end kind of reflect all of these things that have happened and you can read those events. >> Yeah, probably in our example like emitting an event when a person buy a token would be like a nice example, right? Like Yep. You can maybe >> even like sends like if you wanted to look at recent sins, we would want to have that in there like >> Yes. >> Yep.

>> So for for buying, what would be like the buy log and then we'll have the buyer here and what else? Anything else? Or maybe maybe not because they can only >> normally it'd be an amount but you have it at one. >> Yes. So then to emit one, it's just like I think it's emit, right? Emit. >> Yep. >> And then by and then >> and then it's a capital letter there. And that's that's kind of the naming convention. Yep. Looks good. And so then that would allow you to quickly get a list in your front end of each purchase rather than having to parse all sorts of crazy stuff to be able to get that information.

and uh kind of going into how web 2 developers start using smart contracts. A lot of times they'll create strrus and they'll have arrays of strrus and in that strruct is like first name and last name and address and how much they've put in and that is going to cost your user hundreds of dollars every time they interact with your smart contract because you're storing so much information. But say you want that information somewhere on chain so people could look at it. You would use an event, not a giant strruct, right? You want to have the minimum viable stuff in the smart contract and as much else off-chain as you can. But sometime you you need that onchain storage. But in

this case, if another smart contract doesn't need to read it, you can just emit it as an event. And those events are really powerful for the front end. >> Yep. >> Solidity. Solidity crash course. Have we covered everything? Anything anything remaining? I think I think we may we may have really like gone over time here in terms of uh >> yeah you at home you should be able to hopefully use MPX hard hat get get hard hat down. Tinker with smart contracts. Go to solidity by example. Look through each one of these. If you want to start opening your world up even more, you can scroll down a little bit farther on Solidity by example and there's like

attacks and they start talking about re-entry. They start talking about applications. So, just get get in and start tinkering, right? Use Remix, use Scaffold ETH, use hard hat, get in and just start tinkering. Anything anything closing thoughts on solidity? You as a programmer should be able to figure it out. It's easy. I think this was a good one to show like also then we're going to get into scaffolded and show like you can actually visualize what you're doing in the smart contract like the tests are a bit like a bit more pro to like go in and just code that but when you directly see it's also like easier to uh modify and I just really want to reiterate like the mappings part as well that was

really like a call for me because it's really painful to iterate through a mapping like you can't um And yeah, like I think the challenges like really show you that when you get to speedrun Ethereum as well. But yeah, just to reiterate that because it took me a long time to get that. >> This I'm not going to lie, it's it's almost painful for me when we get that counter built and I'm like, "Okay, now let's go increment and decrement and make sure it does its thing." It's like, "No, no, no. We got to go write another file that tests that and let's write a test file." Like for me, I want to I want to like poke at it and I want to play around with it and I want to see if it does what it does like very viscerally and we'll do that soon with

scaffold. But it's good to go through it on the hard hat side first just to show that like what's going on underneath the abstraction there. And writing tests is also a good thing. You should write should write all these tests as you're writing your contract and it's like fresh in your mind how it works. But a lot of times I think traditionally folks have just lived in the hard hat world and just written their smart contract and made sure it was super safe and deployed it and shipped it without writing the front end and then later on, you know, retroactively someone writes a front end and then they realize like, oh my gosh, the user experience here is really not great. And so like this is another thing that scaffold leaf will

show you is as you're building your contract you can kind of iterate on the front end too and it helps like get that user experience solid too but we'll get to all of that later. Thank you Carlos. Thank you Anna. We did it. The day one of solidity. Hopefully you at home know how to tinker and know how to get started and you feel empowered by hard hat. Anything else? Any closing thoughts? >> I think we are good. >> Awesome. Thanks guys. Thank you. Bye. Thanks everyone.

Related talks