Hey, how's it going guys? My name is Daniel aka Hashlips and welcome back to the full beginner tutorial series on learning Solidity programming language. In this tutorial, I'm going to show you how to write blockchain smart contracts using the Solidity language which you can then deploy on the Ethereum blockchain, on Polygon. or any other subsequent forks of Ethereum, which is pretty cool.
I believe that I'm going to finish this series and you will have a true understanding with me on how I got to learn Solidity so that you can do anything in the blockchain space. A lot of people want me to do other blockchains such as Solana, WAX, Binance Chain, and that's fine and we will get to that. But first we need to learn the core.
and we need to stick with a programming language so you understand the basics. Solidity language is also based off of JavaScript, which makes it a good contender to start learning. And the reason for that is because most of what we've done before with our generated art code programs is JavaScript.
So you'll find the program Solidity very much similar because it's based off that, and it also will just feel natural to you. I'm going to try and do this the best way I can, the hashlib style way, so that you can understand every single bit of what I'm going to explain. I also noticed that the GitHub channel and the GitHub page have been blowing up.
People have been actually starring these things and forking the repo like crazy. Now, the problem with each one of these repos is that they solve a particular problem. The first repo is very nice for random attributes, but the second one gives you more control and adds weights to it. I'm going to create a third and final repo, which we will version and brand name as the Hashlips Coded Artwork Generator, whatever we're going to call it. In that repo, we'll actually have released versions so that you guys can have a trusted version to download and know that everything works perfectly.
I really appreciate the input that you guys are giving me on these and keep on doing so. And for anyone finding these repos on videos and people showing you how to use this repo, please say thank you for those people for me. Because it's really been an amazing experience to learn and see. people use the code and actually all the nfts that's being created lastly i also want to mention that the nerdy coder clones have been doing very well i appreciated that people actually gone and gone ahead and minted them one there's about 39 now minted and i think there's 15 holders congratulations to those 15 holders we will definitely be doing something very special with all the holders of these nerdy coder clones apart from that and my style i always want to give you guys something whether it's education or something cool to use so i went ahead and took all the code that you see here in front of you and i went to update it on the git repo now i did update it in the example nft mentor over there and i will just quickly go in here and you'll see that it's been updated but also what you'll notice is that there was one thing that i changed the thing that i changed was if you go to the redux and into the blockchain where we actually connect to metamask and all i added this library i did add this library because i noticed that metamask was acting up when i connected to the blockchain and it was giving me these RPC errors and that's why I made this change so that you guys find it easier and can just run the code. By the end of this tutorial series you will not be bored because I'm going to try and make it fun.
I'm going to do cool comparisons so you really get the hang of this and also after this series I'll be doing subsequent videos on cool and interesting smart contracts that we can come up with. Things where there is a real live use case for that you can actually take your knowledge, apply it to something and maybe start your own little business on the blockchain, right? So that is what I want to accomplish with you guys and I want you to fully understand this so that after this series you will be able to code your own ERC 721, 1155, whichever contract you want to. So let's get down to it and let's get started. The first thing that I'm going to explain in this video is remix what is remix you might ask well remix is our online IDE to compile solidity code and that's basically it it might look very fancy but don't be intimidated the first time you look at an IDE sometimes people feel intimidated because you see text at the bottom there's all sorts of little buttons but I'm here to tell you that it's very straightforward so I'll explain it.
First off, Solidity is the programming language that is written by the Ethereum people and a lot of contributors to actually be able for us as human beings to write smart contracts. The reason why we need a language such as Solidity or JavaScript or Rust is because we are human and we need to be able to write code in human readable format. What the Solidity compiler aka this remix.ethereum.org can do as well, what a compiler does is it takes the code and it compiles it up and makes it into bytecode, machine learnable, you know, executable code that humans can't really read.
We can read it but it will take us very long. But anyway, that's basically what it does. So I'm going to explain the IDE that we'll be using in this course so that you get a comfortable feel for the IDE and also know exactly what I talk about when I mention things. Now, let's look at Remix.
When you start up Remix, you should see this homepage. You can just close that by clicking on the X here at the top. It's not really important. That's the space where you can find out more about Solidity, upload files and so on. But we're going to be focusing on these sections over here.
Now, firstly, on the very left-hand side, you get your plugin section. You should start off with this top plugin, these little file icons, which is the File Explorer. Now, in this File Explorer, you get things like your workspaces. So if you click there, you can have multiple workspaces if you create new ones by clicking on this plus icon over there.
You can also then see all your files and folders in this particular workspace. I'm just going to stick with the default workspace for now, just for this tutorial. Inside here, you have numerous folders and it might look intimidating, like I said, but it's really not.
In the contracts folder, you'll get your Solidity files, your programs. These are referred to as our smart contracts. And if we select one, we can see that there's some code written into it.
This is the code that I will explain how to write yourself from scratch. So let's not pay too much attention to what's in here. But something I do want you to note is that every Solidity program and smart contract has the .sol extension.
That just means that it's a Solidity file. Let's close these files and let's open the next folder. You will also find a folder called scripts.
These scripts are usually used by web3 or ether.js to show how you can deploy the actual smart contract when using things such as Truffle, which is another framework you can use to deploy Solidity code. But that's not really important for us to set up in Remix. Remix does this all for us. Tests is another folder, and testing smart contracts is important, so I'll cover that as well.
But this will do on a later stage and it's not really at the most important now before we even get to variables in Solidity. So we'll get to testing, but that's just what this folder is all about. Let's close these off.
Let's look at the next plugin. So the next plugin on the left hand side is the Solidity compiler itself. And right below it, you should get the deploy and run transaction section.
If you do not have any of these plugins, you can go to the right at the bottom here, this plugin manager, and you can go and search for them in the search bar and just activate it. There's also multiple plugins developed for the Remix IDE, which is pretty cool to play around with. But you can explore that on your own.
So let's just go back to the Solidity Compiler and just explain briefly what it does. Now, in the Solidity Compiler, after you've written your smart contract, this is usually the section that tells you if your contract passes or if there's some kind of error. If there's an error, it will show you up in the bottom section as a warning or a red box with some text in it for you to debug. You can select here right at the top your compiler that you want to be using for your Solidity code. We'll be talking about compilers and pragma lines just in the next video.
Then you get to select a bunch of these options. Nightly builds just mean subsequent builds in between all the... heavy big builds and the big version so you can see it's very very tightly committed builds but we don't really need to go into that much detail so that you can switch off and just select the main version then you get the language obviously it's solidity and you can select the compiler all these settings you usually can just leave to default what you can also do is switch on auto compile which will compile your code every time it's saved automatically and enable optimization. Enabling optimization allows for Solidity to actually minify and tightly clean up your code so that it's cheaper when you deploy it on the blockchain. So sometimes you want to enable that as well.
Then we get to the deploy and run section. In here we will often select the JavaScript virtual machines. When you select one of these virtual machines, your Solidity program that you've written over here will be deployed on a virtual machine on the browser. Now, you have two options. I really don't know which one is best.
Any one, for me, works fine. You also get an injected Web3 provider which uses MetaMask. if you have the metamask extension so that i usually use when i want to deploy smart contracts on the real blockchain in the main network you also get a web3 provider and that's for if you're running your local blockchain such as the ganache cli which you want to connect this to then in the account section you get the basic accounts that's usually given to you by default for you to test with with some test ether in them you get to set the gas limit for a transaction a value that you want to pass through you get to set the i would i call it always the currency or the fractions but you get to set ether finny g way away i don't know how you call that way but anyway i'll go through these as well and explain what the units are after deploying a section usually your smart contract pops up here in the bottom and this is where we can interact with it.
Now that we understand most of the left part, these main parts over here that takes up most of the screen is the part where you'll be writing your smart contract in. Once it's saved, it will go and save it automatically here on the left. You might see that sometimes when you alter a file that some artifacts gets built over here on the left-hand Don't worry about that too much and you can even delete the folder and it will do it again. This is just being built by the changes that you make in here and most probably some of the comments that's written in here.
But anyway, at the bottom over here on the bottom, we get the terminal and the terminal tells us what's happening when we do deploys, when we call transactions. It's basically our go-to if we find errors. or if we want to check if something was successful. Now that we know what's happening regarding Remix and how it basically works, we are now going to start writing our first smart contracts.
Well, that being said, we first need to learn how to write a smart contract and what a smart contract even is. I will be going along this introduction course and taking you along and through the solidity. programming language documentation.
I feel that documentation is probably the best way of learning code that there is. Tutorials help like these, but it truly helps for yourself to go and read through what I explain and better understand it by yourself by reading the documentation. Not only is documentation the best to learn, but it's also the most up-to-date way.
I want to mention when you land on the documentation and you see this note. It might tell you that you're not on the latest version. You can verify the version by clicking on this little down arrow at the bottom and selecting a version you want to be on, or you can just click on the recommended latest version. My advice for choosing versions in Solidity is choose a version like this one like we are going to be doing.
I'm going to be focusing on all the versions of 0.8 and higher mainly because there was a few breaking changes which I want to include in this series but also because it's kind of the latest version. Solidity updates very quickly and they make changes and they do version updates so if you find this tutorial it would still be valid because a lot of this code You can deploy on the blockchain and it will still work. But take this to heart. And if you see this, please refer back to the documentation, specifically to the section where there's breaking changes. There's usually a breaking changes tab over here, which you can inspect.
And then once you find it, you can just go and check. So there we go. These are the breaking changes files and it will tell you exactly what the breaking changes is for a particular version.
So if you're learning version 9 after these tutorial series, don't mind not to watch these videos. You can still watch them but just keep in mind that you need to go and check if there's any breaking changes so that you can rectify it. Let's get started with the very first tutorial. I'm very excited to now get diving into the code. So to set up and get ready for this series, I want you to go back to your Remix code editor on the browser.
Go to remix.ethereum.org. Go and clear everything out. I'm going to select all these files and delete them.
So I'm going to select these two and this and that. Let's just delete them all. And also this artifact.
Let's do that. Before I delete everything. I'm just going to copy this little line here at the top.
I'll tell you now why, because I always forget what to type there. Anyway, we now have a clear workspace so we can remove everything. Then start off in your contract over there.
Say new file, right click, new file, and let's start. We're going to call this contract, my contract. So type out mycontract.sol and this will be your first contract that you're going to write with me.
Now, first off, when you see me type out Solidity files, you notice that I start with a capital my and then a capital contract. OK, so this is the convention we follow in coding in Solidity, how we name files. It's also the convention how we give our contracts their names.
So, the first thing that you want to do when writing Solidity code, in the most recent versions, Solidity wants to know what the license is that you're going to be using. How you tell the Solidity compiler what version you're going to be using is with this line that I just copied and now pasted in here. It basically just identifies the license type of the code that you're going to write in this file.
It allows people to kind of reuse your code if it follows a specific license that you specify. I know that the GPL is kind of the general public license in some way similar to the MIT, but you can read up more on these licenses yourself and find out which one is appropriate for your project. Generally, I go with this GPL.
and the MIT one because I feel free to, well, I feel that people sharing my code or using my code, they shouldn't really worry about any of the licensing issues. Sometimes it's nice for people to give you credit. So you can probably put a CC license in there.
I'm not pretty sure, but you can find that out on yourself. We just need to start off with a license. If we do not, then the compiler would not know what to do and it will show a warning on the license part. Next, what you want to do is start off with a pragma line.
Now, pragma lines are very important, much like how we tell the Solidity programming language and the compiler what license we want to to use by commenting this in, right, and this is considered as a comment. and I'll tell you now about comments as well, we want to tell the language what version of the compiler this code should be running at. Like I explained, there's numerous versions as you can see. And we need to tell the compiler which version are we complying with for it to run this code.
You start off by writing pragma. And the pragma line is going to start specifying what version we want to use. So you can write pragma, solidity, and then you write a version.
Let's choose 0.8.0. And that is the pragma line. So... the pragma line specifies in this case that our compiler if we go to the compiler section that i showed you guys right over there that it should choose 0.8.0 and yes it did it went to choose it right there now if we specify that this pragma line should be 0.2 check what happens on the left hand side the compiler automatically updates if we say we want 0.8.5 it updates as well.
A good thing to do when specifying a, how can I say, versions, compiler versions like these before writing your code is actually giving it a little bit of leeway or specifying that solidity code written in here can be compiled by anything that's 0.8.0 or higher. How you would do that is in front of your version you would add the caret sign. That will explain to it that we can now compile this code with even 0.9, 0.10 if it comes out. We can compile our code with this.
So that's basically how it works. If you wanted to go between versions, you would just simply use a space and you can say 0.9.0 for instance and you can say that You want anything greater or equal than the 0.8.0 and then obviously less than 0.9.0. So you basically use this as a if statement, you know, if this is greater or less than, and you can specify which versions it should use between. I like to keep it simple by giving it a caret.
giving it the version that's my favorite and saying anything upwards will be able to use this. Now if there's any breaking changes coming out it's good to put that extra one to cap it so that it doesn't go and include those breaking changes by accident. You can always go if you've specified that there's a higher version that can be used. You can specify a higher version but you cannot specify a lower version.
If you specify a lower version it would give you an error. The errors that I talked about in Remix pops up here at the bottom left corner if you are on the Solidity compiler. Here it says worker is undefined, meaning it can't find the appropriate worker. So I'm going to just select anything above, maybe make it the same, and then be ready to rock.
So I hope you know and that you've learned that we get the license line, which explicitly tells us which license it should be. We can also go and make it the MIT license. And then the pragma line is very important to tell the compiler what version our solidity code is and that it complies with the specific version that we have coded in. We are going to keep to versions 0.8 and above for this tutorial just to keep it to the utmost up-to-date code.
I hope that you guys have learned a lot so far. Next, we will learn about comments. And I know this might seem boring, but in Solidity, commenting is actually very important. So, comments, yes.
In coding, comments can be the most redundant thing to write. It's also the most boring thing. But in fact, and after being a coder for so many years, it's actually the most important thing you can do. Think about it like this.
You go back to your code a few weeks later and you're like, cool, I still know what this is. A few months later, you say, you know what, you recognize this code, but you don't know what it does, but you figure it out. A year or so later, you won't even think you wrote that code.
That's how different the code will look to you. And if there was no comments in place, you would literally have to read it line by line to find out what it does. Trust me, guys.
This does happen. Now, in Solidity there's two ways of commenting. So, you get normal comments which you append with two forward slashes. Two forward slashes is ignored by the compiler and seen as not there. Think about it.
The compiler only compiles things that doesn't have these two forward slashes in front of it. This means that you can actually write anything in here. Okay, so we can say, okay, this is a comment, right? And By doing that, you tell yourself that this is a comment.
You can write useful information over there. And usually you won't write stupid things like that. You would just say maybe made by Hashlips, which I really want to include in all the source code that I write.
So I want to say to people that this is what I wrote. Maybe use it like this and you can do all sorts of things. This is also referred to as a single line comment.
And usually you. place it above things such as contracts interfaces and functions right you can place it above anything you really want to explain to the users now there is a way to do multiple comments because if I now go and say space and I keep on writing this will give me an error because it doesn't have the forward slashes on in front of it now I can put two forward slashes there press space to forward slash to start typing but what if I wanted to type a big paragraph. There is a way of doing paragraph comments and you do that by pressing forward slash and then you put a star.
Then you go and open your comment you put another star and an ending forward slash. Now you can basically write whatever you want to in here with spaces and even emojis I think. But anyway you can write whatever you want to.
This is considered as a multi-line comment. Now, this will not be useful to the user, so please write something descriptive if you want to use comments like these. These comments are also not picked up by the browser, well, by the IDE, really. It's just seen as not there, and the code won't really care about it, and just sees it as you wanting to comment something to someone reading the code. In Solidity, we get a special way of commenting.
And the reason why I want to cover this in the very first tutorial is because it's quite boring. But it's very necessary. It is called the Nutspec.
So there you can see it's called the Nutspec format. And the name sounds quite funny, right? Nutspec.
It sounds like something that must be important. And it is. But...
Basically what it is, it is a format of commenting that basically Solidity Compiler can read and then inform people both on the developer side in messages and the front facing side, the actual users that interact with your smart contract. And it can actually decide to give you these messages back. So it's not like...
error messages or something. It's more informative messages on your signatures. It helps to clarify the code that you write better.
And let's say there's multiple developers working on your code, then it will help with that. How does Nutspec look and how does it work? You can read through this part of the documentation here on the Nutspec format, but I'll briefly explain to you. If we go back to Remix, we have to kind of re-read through the code.
Write quickly something that we can actually comment on. So let's go and write our first smart contract. Now, I'll explain the smart contract as well, seeing that we're just diving into it. But the way you write a smart contract and think of a smart contract as a container that contains all the code that goes with that contract alone.
OK, so it's a little box we're writing. So imagine a little box. How do we write this smart contract box?
We can write the word contract first defining what this is that we are going to write. Then we can give it a name. A good convention is to call the smart contract the same as your file name. So we will go and do that.
So we're going to say this is the my contract smart contract. And then we're going to put a opening brace which is a curly brace and a closing brace like so. That is basically our box.
And anything between these two lines that is what our box should contain and variables state variables Functions anything the smart contract does lives inside of it You might be thinking if this is a box can anything be communicated outside of this box? Yes, once this box is deployed on the blockchain, right, each smart contract has an address much like your wallet, your Metamask wallet. So if you look at your Metamask wallet, each wallet has an address over there.
Now this is my address but your smart contract will have a different address. And what can an address do on the blockchain? It can receive funds and it can send funds, right? With a smart contract, it works very much the same. If you send money to a smart contract, it will receive it.
If there's no really receive function, which we'll get to later on, it will just keep it in the smart contract, okay? And that's basically what a smart contract is. It's a box. It's basically an address. which behaves like an address with extra features and functions.
And we, the developers, can write those extra features and functions. That's basically what it is. And yes, a smart contract can communicate with another smart contract. So if I had a smart contract over here, and this was called my A underscore smart contract, and maybe this one was B underscore smart contract.
A can communicate with B and vice versa. We will get to that as well in the tutorial. I just wanted you to understand that. And also, we as the public, if we know the smart contract's address, where it lives, basically, and the ABI, which is all the data that's inside of it, how it looks, basically saying this is the menu, we can interact with the smart contract. So this is all I wanted to explain to you guys.
Now what we're going to do is we're going to look at the Nutspec coding commenting method, right? So what you want to do is whenever you've written a line of code, we want to communicate that to either the developer, the person using it, the person who's going to edit the code in a special way. How can we do that? So... Looking at this special way that we can use, we can go back to the documentation.
Now let's look at this special NAT spec way of actually commenting. And here it is. Here you can see someone has created a smart contract called a tree.
And remember, this is our box. So anything in this smart contract, the tree smart contract, everything in here is to do with this tree, right? This tree smart contract. But how do we tell people what it does? This is the way that they commented and you can see they're using single lines for this.
Now you might notice something that there is three forward slashes. Now three forward slashes tells the compiler that we are about to comment in the Nutspec way and you can also do the multi-line one which I'll show you as well, but let's quickly read through it. We have a thing called add title, add author, notice development and custom now these are very explanatory where the title basically tells you gives the contract a title it doesn't define the title on the blockchain but it tells you as the developer what this is about it gives you the author it gives you a notice what this thing might be doing and to the developer it gives a little dev tag to tell the developer any of the things that a developer should know Not really so much the public should know.
When we get later on to the comments on top of a function, you can go into depth on what parameters, what it returns. And for you guys who do not know what parameters and return values are, we'll get to that. But just know that that is very specific to a function.
And that gives us more information about the function. It doesn't change the functionality. And in fact, You don't have to comment a single line of code in your smart contracts. You do not have to include Nutspec at all.
It just helps you as a developer to grow and other people to read your code. So, getting back to my contract, what we've seen now is that we can add these special comments. So, we can either do one line comments, which is three forward slashes, then use an add sign to determine or define something. And we can go ahead and say, well, this is the title.
So our title will be the best smart contract. How's that for a title? right and we can also then have another line where we can then define anything such as let's say the author and the author here is is hash lips right so this just gives you a much better understanding of your smart contract we've got a title and we've got an author so When you guys read the smart contract without me even saying a word, you would know that this is the best smart contract and it was developed by Hashlips.
If I wanted to leave a comment for the developer, I would go one step further and say, well, at dev, please do this. So, please use this in this or that way, right? Maybe something to the developer that might explain how they can use this smart contract or something that they need to know.
Now there's also the notice of how the notice tag of how a smart contract behaves or what it does. You can play around with it. So I would recommend if you really want to know more about this go to the section of the NAT spec format and go and read up on all the little tags. that you that you might find here you can see each one's description that i've started explaining but there is a little bit more we'll start implementing these as we code along just to keep to convention i usually don't do that but i would like you guys to learn the best way okay so now i have determined and written out how a normal single line comment looks like and the nut spec way of a single comment The one has two, the one has three forward slashes.
The normal multi-line comment with just a forward slash and a star, and then the nutspec way, which is a forward slash, two stars, ending with a one star and a forward slash. Now, that is pretty cool. I don't even know if this is called forward slashes. Is this forward slash or backslash? Anyway, I think it is forward slash.
But, um... I hope you guys enjoyed this little tutorial on how to comment on something and you also learned the basic way of how to set up a contract. Just my last note on the contract is that contracts can have extensions meaning that you can inherit from contracts which we'll discuss on a later basis but this is the basis of a contract and the basic contract and this will actually work it will obviously be deployed and nothing will run you can go ahead and compile this and it will compile but it won't build anything and it won't do anything if we select it and we say deploy our smart contract is there you can even see that it has an address if we copy this address and we paste it now in here you can see that this is the address of my smart contract that was deployed but This address won't do anything. If we open it, you can see that it doesn't even have a function, a variable, nothing to look at apart from this call data. So what I want to say is that don't create empty contracts, it won't get you anywhere.
But this is definitely where we want to start. Now I want to leave all this data and all this stuff that I've written inside files for you on a GitHub repo. which I'll do.
So everything I type out here you will have on a file on on getup which I will refer to in these video series in the comment section okay in the descriptions sorry. So basically that's what I'll do and we're going to carry on. So the next thing that we'll have to look at after understanding the Nutspack format is basically to look at the rest of a simple smart contract. As you've seen, we've deployed this My Smart contract, and the way we deploy it is we go to the Deploy and Run section. We have to make sure that this compiled successfully by getting the green tick, and we have to select the contract and click Deploy.
Every time you deploy a new contract, you can see a new instance gets created, and each one of these instances will have a different address. So, we've just deployed this three times, meaning there's three. three different contracts of the same code now on the test blockchain right on our virtual machine blockchain but they don't do anything so we can go ahead and delete all of them by clicking on this delete icon.
Next what we want to do is actually write a simple smart contract in here and that's what we are going to do. So let's get started with an example. You can go ahead and write this just to start off. Start by writing the word string then go and define it as public and we want to get this give this string a name let's call it name let's give the name a name right so we're going to say this variable is going to be called name this is considered as a state variable and this variable is going to live on the blockchain along with this smart contract forever we can go and directly define this variable and say my name is hash lips okay and I'm going to close it. So this is how you close any line in Solidity.
You add the semicolon at the end of a line. This basically tells the compiler that we are done with the logic here. And it's very necessary. If we do not put that ending line in, it would say that it's expecting it. You can see that there would be an error by Solidity's compiler, the Remix compiler over here, telling us expected a semicolon.
This also brings us to a question on how to debug things. The best way is to hover your mouse over the error and it would usually tell you expected the semicolon but got the curly brace. That's because the program kept on reading and hit this curly brace and that's why it got this.
So we need to tell it to end over here. You will also see that in your compiler it will pick up this error and give it a message to you here. If you cannot find the error in your code, it's sometimes a good chance that you might need to Google it. Everyone has had some issue with Solidity, so if you Google your problem, you might get an answer. All right, but anyway, let's add the semicolon and finish our basic smart contract.
So our basic smart contract has a state variable, which we'll discuss in detail, but for now... Just see this as a name variable. Okay, so we've added a name variable. And remember, these comments don't do anything. They might as well not even be here.
So I don't want to confuse you with the comments. You can see that even if it's not there, it still works. It's blue, right? Let's just take that out.
It's still green over there. But I just want to put them back because I'm going to save this code for you. Let's go ahead. and compile this smart contract now and see what happens.
So we're going to click on compile to make sure that it compiles and turned whatever I have into bytecode. Next, we want to deploy this contract. So we saw that it compiled successfully by clicking on it.
Also, auto-compile is on, so it automatically does it for us and enable optimization. This doesn't need to be on, but I always put it on. Let's go to the deploy and run section, then making sure that we are on the JavaScript virtual machine, either one will work.
And we can see that we have our smart contract selected, my contract, and then we can click deploy. Once we've deployed this, we can see that now we have a new instance. And like I said, every new instance of a smart contract that we put on the blockchain has a new address.
So this one's new address looks like this because I've copied it and you can copy it by clicking on this button. But anyway, let's open it up and we can see name. Now, this will be the same as the name over there.
It just basically puts the variable name as a call function. Call functions are functions that you can call without sending a transaction to the blockchain. It's basically like just reading of the smart contract the fields that you want.
These contract calls are free. They do cost a little bit of gas, but you don't pay that gas. It's basically just to read the data of the smart contract.
Now, having this smart contract is pretty fancy because when we deploy this on the blockchain, and we definitely can, it's a valid smart contract. Forever and ever, it would just have one call function, which is basically given to us automatically because under the hood of Solidity, whenever you declare something as public, it automatically creates. a getter function. A getter function is created under the hood of Solidity that allows the smart contract to return the actual value of this variable and that's exactly what we see here when we click on name and it's returning hash lips. Anyway this is not the most exciting smart contract so I'm going to show you a way how we can update this.
We know that we have an automatic getter with a public variable but we need to create a setter function. This we'll have to create ourselves because we can't just update things on the variable on the fly. We need to give it a way of how to update it.
How can we update variables and how do we perform actions inside a smart contract, inside this little box that we have enclosed all the code in? That's very simple. We do it through functions and actions, right?
So... Think of it as an action that we want to perform. We call it a function in code because a function does something.
We give it some parameters sometimes, some rules, and it performs according to the rules we give it. We also get basic, simple functions, such as the one I'm going to show you right now. To start off a function, you just simply write the function keyword. Every time you declare something in Solidity, you're going to get a new function called the function. You basically kind of have to give it a keyword of what value or type is going to be.
For instance, if we're writing the contract, you need to write contract and give it a name. If we define variables, you write the variable type name and then the rest. If you don't want to declare a function, you do the same. You create the function keyword first and then give it a function. So let's call this function update.
name functions we type in this case where we start with a lowercase name or whatever word it is and then we capitalize the next word if there's another word that would be also capitalized all right so i think this is called camel case and then over here for our contracts we use pascal case this is camel case because of the humps that it creates by using the characters just as a coding educational thing. But anyway, so we start with a function, give it a name, and then the next thing that we want to do is put two brackets, like so, normal brackets, and then our square brackets. In this effect, we create yet another box, and I want you to think about this as boxes. The reason why I want you to think about it like boxes is because ... Each of these little boxes, whether it's a function or whether it's the main smart contract, contains things.
And the outer box sometimes can't know what's inside this box. Or sometimes this box doesn't know what's in the next box. If we have two functions, unless we tell it that it can have access and give access to other boxes.
How do we do that? How do we say to this? How do we? tell this box in this function that it needs to be of a public scope that the outside world after we have deployed the contract that the outside world can communicate with this update name function this action that will eventually update this name well we have to give it as well a public field a public declaration that will say this is public right the public can use it and you do it by just adding the public scope variable or the modifier here at the end where we can now tell this function that it should be public.
We'll get into identifiers or scope or scoping functions and variables on a later session much more in depth but this is just the main simple example of ADAPT so I just want to explain it as I go along. Next what you want to do is we want to test out that this all works. So I'm going to just see if it compiles and yes it does. We can also go to the deploy and run section, select my contract, make sure I'm on the virtual machine and deploy it.
We will see that if we close the previous one and we open the latest one we've deployed, that there is an update name function over there. And you can see that it is a function and something that we have specified because it is orange, right? Now, if something is orange, it's not considered as a call variable. and it might take gas to execute.
If we click on this update function you can see here down in the terminal that stuff is beginning to happen. When I open my terminal and I click on update name you can see that there is some wording down here. This is the hash that it created, the transaction, the confirmation that it was minted or mined on the block.
Something happened to this address. contract and the miners verified it and says yes this is what happened this is what changed and here is the receipt the confirmation you can read through each one of these variables but basically it gives you basic key and value pairs things like the to and the from fields from where this request came from what did it update you can see that it also called this function update name and then um executed. You can see how much gas it actually took to execute because every little thing in a smart contract does take gas depending on the complexity. But enough of this. Let's get back to what we wanted to accomplish.
We want this function to actually update this name variable because technically if we click it now and we click on the name it still hash lips. Nothing has changed. We need to actually physically tell this function to update this name.
That's very simple. What we can do is put the name variable in here. Also, another thing, no two variables should have the same name.
It just creates confusion and also won't work because it will overwrite each other and the compiler won't compile. Next, what we want to do is exactly what we've done up here. So we want to type in name.
and set this equal to another name so we can say this is new name all right and what do we have to do once we need to close um close statements is use the semicolon for it to close it now we've got this set up in place so i'm going to close the two contracts that i deployed i'm going to redeploy this smart contract i'm going to open it and i'm going to click on the name field it says hash lips but now if i update and click on update name i can see that something has happened because in the terminal down here again we get the confirmation and then if we click on the name we can see that it changed to new name by the way in the terminal when we click on call you can see that it says call but it doesn't show a transaction that took place so that's why these transactions don't really take any gas it basically just reads the contract it doesn't alter or change any data in the contract only the update name does and because we clicked on it and we added more things inside of the function it should actually be costing us a lot more gas and there you can see that the gas execution should be a little bit more now because we added functionality keep in mind that big smart contracts do have gas limits that it can handle. So you cannot write the biggest smart contract, but you can write quite big programs. There's numerous ways where you can split up programs and smart contracts in different files, in different contracts, and then call the other one, you know, from contract A to B to save on gas. But we'll get into that as well. I just want to mention that as we go along.
But now this smart contract is all good, but it will forever change it to new name and we cannot do anything else with it. This is where I want to introduce parameters. The parameters will be discussed in our functions update, but to give you an idea what a parameter is, is that we can actually tell the function that we want to give you a value that you need to update this name variable with. So what you can do is you can enter name.
Right, let's give it a new name. And we want to set this new name to this name as in here, right inside the function. So we're going to say a new name, a good convention to use when you're using parameters is putting an underscore in front of the variable name.
This just makes it clear for yourself that you're using the parameters that was that's coming into this function instead of anything else in the smart contract. Next, this function will not work. The reason for that is because we need to declare what data type this is.
Because this could be an integer, which is a number. It can be a Boolean, which is either a true or a false. Or it can be anything. The smart contract is not going to determine that. So we need to tell it what it's going to expect.
We know that our name is of type string. So we need to add string in there. It's still going to say that it's missing something. And the reason for that is string types and I believe mappings and or struct types and arrays they are all values that needs to use memory right to you can use them in memory but you have to add the memory field so what you need to add after string is memory and then it should work. Basically, what memory says and tells us is that it's going to save this name in memory just for a second, change it to our new name that's going to save it permanently and then get rid of this at the end.
So it does not require this to be there forever. OK, that's what it's basically saying. And that's why we need to specify it with memory.
You will see this a lot through the application and you'll get used to when you should use memory. or not. Basically it's in parameters or if you're using any variables that needs to only be kept in memory until the function ends and it discards of it because maybe it's not too important to save that as well because we already changed the variable. By doing this and saving the smart contract we're going to make sure that it can compile. We're going to go to the deploy section and deploy it and if we open this now Now you can see something interesting with our field, with our update name function, we get an extra field and it's called string underscore new name.
And this is exactly what we've now added to it, right, as a parameter. You can add multiple parameters and it will have multiple inputs, but we only need one. What will this do? This will basically take the new name that we pass it into the contract and set it to the name field.
Remember that this interface is just an interface that Remix gives to us. If we look at smart contracts in general, we can look at them on Polygon Scan or Ether Scan, and we can actually write to the contract and read to the contract. I'll give you an example now to explain it in detail. Let's go ahead and change the name. I love the name HashLips, but you know what I love even more is maybe...
My real name, which is Daniel. So I'm going to update this to Daniel. So I'll write Daniel in there. You can then click update name.
And something has happened because there's the transaction. And then now if we call name, you can see that the name is updated. This makes it a reusable function. Because not only can we write Daniel, we can also write Ashley.
Update the name. Check the name. and it's now Ashley.
And that's basically a very simple smart contract on how to write functions, updating state variables, which is our name. And this is a working smart contract. I'm just going to explain something on Etherscan quickly and then get back to this tutorial.
By the way, I just came on to the nerdy CoderClones contract because I want to explain my website. tutorial to you guys and I noticed there's 41 nerdy coder clones already sold, 16 addresses, so thank you for that extra person coming to buy one. I just want to use this contract example to show you what we are busy doing in Remix.
If you scroll down on this contract on Etherscan, Binance scan or Polygon scan, any of the scan contracts basically just exposes the underlying contract to you. If we go and click on read contract, all these read functions that you see in here, you can compare to in Remix as the blue functions. All right. So for instance, if we go back to Polygon Scan, we can basically query these for free. What do I mean when I say that?
I mean that we can enter values. So let's go to the top. Let's look at the balance of function. this is a erc721 function which is simply a call function it doesn't change anything on the blockchain and that's basically what a call function is so because Because it doesn't change anything on the blockchain, it just returns some data to us, either to a function or a direct variable.
We can call it for free. So I can enter something. So I can say, let's click on an address like that and query it. And it will return to me saying zero because this address doesn't have any of that of these NFTs. We can do different calls.
So let's see this one. token id so we can say okay we want to well this is a get approved so let's maybe go to owner of let's see who's the owner of token id one i'm going to query it and i can see it's this address and that's basically what call functions does it returns to you values like i said without paying any gas yourself but it does consume gas when you write to a contract You're basically setting yourself up to change data on the contract. And if the contract allows your authority to do so, you can do so. That's why if you really want to mint one of these NFTs straight off this contract, you can basically connect to this with Metamask. And once you're connected, we can go down here to the mint and you can enter the fields, which is the address to a 100 Matic.
But just remember when you're working with... values it should be by times by 18 decimals but we'll discuss that later on but you can write to the smart contract you'll notice when i click on write that this should pop up metamask so let's let's actually click on something that i want to see so this is the state boolean okay let's let's actually do this let me copy my address you Like so. Let me go into the mint function.
Let me paste my address. And then I want to mint one and I'm going to say, right, let's just make sure that I'm connected to this. And it looks like I am connected. I'm going to maybe specify a value in there. Say right.
And there's Metamask. Now, this will fail because the NFT costs 100 Matic. So this will fail.
But I'm just showing you what a write function does. In Remix, it's the same. When we click update name, in the real world, when it's deployed to the blockchain, it won't do it as fast as this. In the JavaScript virtual machine, it runs a virtual blockchain, meaning that it verifies the calls and the transactions almost instantaneously. When calling this function on a real blockchain, it will first go and open up Metamask or do some kind of confirmation where you have to confirm the address and the transaction that you are doing and then it will have to go and be mined by a miner and that's why miners get paid to mine transactions because they go and verify that this came from you you want to update the blockchain and you do so by updating the name and once it's mined then it will show the change that usually takes a few seconds and that's why transactions are not instantaneous but i just want you to know that so these are write functions and this is a call function so i hope i taught you something in remix and i hope i taught you how to write your first small smart contract and as well as the nutspec way of commenting right which is very important and just to carry on with that tutorial you would now go in here above your function and put your three commas and then you maybe add a at not dev maybe a notice right and then for your notice you might say this function changes the name of the name variable right whatever you want to write But you can do this and this is a perfect way and you see that the smart contract doesn't complain.
It will complain when you take these away because then it is not considered as a comment. So we want to do it the nutspec way and that's how you can comment. And it makes it really easy for people to read. Like I mentioned, now I can go in here and just say that, oh, this function changes the name of the name variable.
right like so and i'm going to leave it like this and then i'll save it for you guys on a file and i'll call it my contract and then you will have access to this on the repo i really hope that you guys enjoyed this very first part with the introduction to solidity and remix and getting your way around how to write smart contracts in the next series in the next video of this series We will continue doing basic concepts, going into depth on variables and functions and all the wonderful things that we have to our disposal to actually write smart contracts. I'm very excited to see you in the next video. So until then, cheers for now.