NFT Development Lesson #4: Introduction to Solidity Programming Part 2
Hemang: What happening actually get everyone on the same pace and on the same scale so we saw that what is remix IDE about this we are actually going to see this again because remix IDE is actually the platform where we develop and then we saw what are the data types in solidity then we finished our lecture with the function so if we have missed the previous webinar, do not worry because these three things are also the thing that we are going to use today as well so I mean I would like to give you a little bit of more about the remix IDE and you don’t need to worry about anything so remix IDE where IDE stands for integrated development environment so it is basically like the platform which is used to write smart contests and solidity and if you have some bad knowledge then you might know about vs. code similar to that but in this you can only light like this holiday languages and which and the main thing is that it helps in deployment debug and administrator of the smart context so it’s a great hands-on ID and for anyone beginners out there it is mostly told to use this ID for the first time because it gives you a good overview and now let’s that what other data types in solidity so there are actually two types of the data types one is value type and the second one is reference type so we covered some of the value types in the last webinar which were unit Boolean address and they are the basic one so and we are going to use it today as well so do not worry we’ll see that again in this webinar and about the reference type it contains dynamic arrays structure, so we are going to cover these all separately in the future webinars so yeah pretty much you haven’t missed anything. So let’s start with today’s webinar, so what are the different types of variables in solidity? Now before seeing the different types of variables let’s understand what are actually variables. So variable is basically a placeholder for the data which can be manipulated at runtime. Good, now variables allow users to retrieve and change the stored information right, and there are three kinds of variables in solidity which are state variable, local variable, and global variable. Now let’s see one by one of them coming to the state variable. State variable in solidity, the state variables are declared inside a contract but outside the function remember in the previous webinar we made the first smart contract that was on the hello world in that contract we wrote the variable outside it and there were no functions so that was actually what a state variable, so now the state variables are stored permanently on the contract storage of the Ethereum blockchain, you need to remember this line that it stores permanently on the contract storage of the Ethereum blockchain, and since it uses contact storage, we have to pay the gas fees to use the state variables, hence we should be more careful to use a number of state variables in the contract because more the suitable the more with the gas fees will be applicable. Now if you don’t know what are the gas fees then the gas fees are actually the real money that you cost while making the transactions but value but that only happens when you are deploying on the main net and for the sake of our webinars we are going to do it on the test net so you don’t need to worry about just creating towards the acid we will create as much as you want because they are the test net. Now let’s see what are the scope of the state variable, so generally speaking State variables there are three scopes that are public internal, and private now let’s see them one by one so the public state variables are available for any external call. The getter function is implicitly created by the compiler to read its value. What is the getter function? The getter function is a small function that we see on the remix ID which I’ll show you in a minute now what are the internal so the same variables are not accessible from any external calls in this they can be accessed only from within the contract or from the contract that is deriving it, if not specified the default scope of the state variable is what internal right and the last is what private. The private, state variables that are neither accessible from any external calls nor from any other child context nor that are driving it are known as the private state variables and they are only accessible from the functions inside the contract, now see now let’s see what an example of the state variable, so here you can see the example of the state variable on which the string public statement it’s a sequel because why, because it is not declared in a function we are going to create one ahead in a practical portion so does not need to worry but just to show and to get a more virtual view we are showing you the example and similarly you can see I’ve defined one, two, three, four, five state variables, and one of them is private also and two of them is private also so that as a subject you can create as much as state variables you want but they need to be outside the function, right. Now let’s see what are the local variables, if you remember there are three kinds of State variables in the solidity first state, second local and the last is global, now what is local? We are on to the local variable portion so in the solidity, the local variables are those variables that are defined inside the functions the scope of the local variables is within the function only and it is stored in the memory due to which it is not permanently persistent. I’ll repeat it again the scope of the local variables is within the function only and it is stored in the memory due to which it is not permanently persistent, now since they are not stored on the Ethereum blockchain can you guess what it does, it does not incur what gas fees right by now you have understood that why it does not incur gas fees because they are not permanently stored, they are stored in the memory, consider them as you can say RAM and generally the local variables are used inside the function for holding the values for some calculation purposes, whereas state variables you are going to declare them to store some values permit like if I want to store, if I’m making a smart contract for employee, so I want to store the employee number permanently then I’ll declare it as the state variable, but that employee number can be changed, so I can not declare that as a local variable, right now let’s see what is the scope of the local variables so the local will be only visible inside the function in which it is declared, it cannot be accessed by any other external function or contract, so you can say that a local variable is only for the specific function in which it is declared for, and the values of these variables are present till the function executes, and it cannot be accessed outside the function. So this type of variable is usually used to show what temporary values that I have already explained earlier, now let’s see the example of a local variable, so here you can see that there is the amount unit amount, it is a local variable because it is declared inside the function and there are no state variables out here, so if there was some state variable here then it would have been declared somewhere related to waiting I’ll write and show it will be better, so something would have been like you went let’s say public and amount if that was the case then it was a state variable, not a local variable so now since it is defined under this contract it is a local variable and you cannot access basically outside this function, so let’s say if I make a function outside of this, now how will I understand that I am outside of the function? We understand by these braces, now if I am writing something below this line then I am outside this function and if I am writing any function here and I’m and I’m trying to make anything and I want to access the variable amount which is right now locally declared so I won’t be able to access because why, this amount can be only used inside the function calculate, so that’s the point, I hope you get it, now let’s move forward. Just a minute, okay, so now global variable, now this is the third part of the variables in the solidity, so in solidity there are some special global variables available that give details about the Ethereum blockchain and transactions that are happening on it now note we cannot declare a global variable global variable is already predefined and there are some of the examples of the global variable that I will show you ahead and which looks something like this, for example there are certain global variables which are let’s say block hash, so block hash gives the hash of the given block when block number is on the 256 recent blocks, and similar to this is message dots and it gives you the address of the caller, so it is one of the most widely used you can say so you are going to use this mostly and actually you are going to use any of these and you don’t necessarily need to remember these you can just go to the solidity Dock and just go and see from here because you don’t need to remember them and as the time will proceed you will be automatically remembering them so don’t give your time an hour and waste it to remember this so and not the most value is also is block dot timestamp, so it gives you the current block timestamp as in the Unix timestamp, so what is the Unix timestamp? It is actually the UNIX version of the timestamp, so I’ll show you this right now as well. Okay, so I think so let’s move on to our practical portion because it’s too much storage that is hard to get and I believe in practical. So let’s go to the practical portion okay, so yeah so for those of you who are coming for the first time this is a remix IDE in which you can create your workspace so you can create a folder it gives you various things like you can compile your contract, you can you want to deploy a contract, then you can also deploy that contract, we covered everything in the previous webinar, and as we are going to proceed in this webinar most of the wild things are going to be covered as well, so you don’t need to worry about that. Now let’s see, so we made this function in our last webinar and can you guess which kind of variable is this? I’ll give you three seconds to guess, one, two, three, it is what? State variable, now why it is a state variable? Because there are no functions right and since there are no functions and it is declared outside of a function it is a state variable, and can you guess in which storage it is going to be stored? It is going to be stored in the blockchain, now if I deploy this contract and if I change this then it’s going to be change like for the for the different blockchain it is going to be a different value but for this blockchain I’ve deployed this it is not going to change, and hence it is stored in the blockchain permanently, this is where the property of immutability mostly comes from the blockchain, like you cannot alter it anything so these are the things so why don’t we go and deploy it and understand it more betterly so let’s go here and when we click here deploy it deploys the hello world contract that is this contract and when we click on the deploy it deploys our content and how do you know it is deployed when you get a good green tick here which we developers really like because there is a real happiness of getting thing deployed, and here you can check the status this is a transaction hash that is going to be very unique for that is going to be unique for this smart contract and yes it is, and then you can also see the gas fees which was cost while deploying this, so these are the stuffs and if you want to interact with the contract you can come here, and you can see there’s a cute little button says statement, so this is what as I have explained one of the slide is in nothing but the what’s getter function? Getter function, so when you create public when you give it the public visibility it creates a get a function like this, and why do you create a get a function to basically just interact with the blockchain because you know you need to test you need to see what is happening and you need to interact because once deployed on the main net, there all of the things are going to be permanently Stored, so it’s better to interact with the blockchain beforehand and that’s why I’ve created it a public and now when I’ll click on statement can you guess what it will show, it will show hello world, right now let’s see and try to change something here and see what will happen next, so let’s say we name our variable something else, so since it’s FIFA time, let’s name it CR7 and in the statement and in the value I’ll pass so okay so if you don’t know this is what I think that Cristiano Ronaldo mostly say and now when you deploy this so remember you still see a statement here which was for the earlier contact, however you also see CR7, why this is happening because they are defined as a state variable and when you change this, the content changes and it deploys on the different blockchain so yeah so you see Sue here, and the word on this contract now how many times you call it no matter what you are always going to see Hello World, you won’t ever see it comes through here or is or CR7 here, so yeah that’s pretty much about the state variable now let’s go, and now I’ve deleted the contact if you have been wondering by the way and we’ll go on to our workspace again and we’ll create a new folder by the name, okay, we don’t want to create a new folder here, so we’ll let’s create a new file by the name local variables, now since I have created local variables here, I can see the local variable file coming here and this yellow thing is just a warning and it will be removed once you write this line as PDX license identifier, basically we need to provide it a license that we are going to use so we are going to use MIT license in this, and if you’re wondering that what license you want to use for the, for your programs then you can write I mean there are tons and several licenses available you can go and check from there and mostly MIT or unlessens will also be I mean you can also use unlicensed or MIT, so it is going to be applicable for most of the programs. Now let’s create a contract I’m going to create a contract with the name, check the name that is local variables now why I’m writing this because it’s a good practice to always give your contract same name as the file name because yeah I mean if other developers want to contribute to your contract then they will be easily doing that now let’s see and try to create some more state variables right so just earlier I’ve told you to create a state variable and we’ll get again so this is unit public and we’ll give it a name let’s say h is equal to 25, okay cool now let’s create a function, and really I don’t know what to do so I’m just writing anything any random function, so I’ll give it let’s say add and I’ll give it two parameters, U index, you went y and we’ll check if we want to return the value or not, so we’ll also returns it and then if you want to write return then you, so if you are from any earlier programming language, then you might see that we are writing returns here, so in solidity when you are declaring inside a function you write returns and when you’re returning the value from inside the function you will write return, so while declaring, while writing the function signature, you write return, and when you’re returning the value, you write return clear we write unit256. Now there are two things that I’ve written here one is unit and one is unit256 are they different absolutely not they’re not different they are just similar to I connect them unit256 as well so they are just similar to unit256, however if I write you went 8 here then this is going to be a different thing so when you write you went it is just similar to unit256, so for the better sake of understanding will pass you unit256 here, and if you don’t know unit256 and send integer which only allows user to input a positive value, now let’s give it some value, so let’s say h is equal to X and let’s say you went I written unit256 I’m giving it a value different value U into 56 let’s say I’m giving it some value age and I’m writing now age is equal to y and let’s see, and return page so yeah we like here aged, an age and aged, okay so yeah so this is what a program and we have created a smart contract, now let’s see that what errors we are getting here, so that we go and we change them right, because I’ve told in the earlier classes that more errors we see, the more we will understand. So now it is saying that I have declared age here but I’ve written aged here, so why not we go and change age to aged, right now here it is saying undeclared identifier did you mean age or aged, so it is taking aged now why it is doing this can you tell okay, so since you are going to make it a local variable then it is undeclared and hence it won’t be used, so yeah so I guess you got my point what I was trying to explain here, and if yes then let’s go and move to our part two, that is we are going to create one more contract that is global variables, so yes mobile variables okay, so global variables are actually nothing but they are the predefined variable already, and to create the local, to create the global variables, I mean I’m just showing you how the global variables look like, and let’s see DX mighty sigma solidity 0.8.0, it just I can also use 0.817 let’s create a contract, and define our first global variable which we can also take help from here and we can write anything from here let’s take block dot coinbase, and it is of the type address so we want to give it address and give it public and we’re going to give it a name block test and it is going to take the value of what block dot coinbase, log dot coinbase okay I haven’t given any name let’s give it a name global variables and yeah let’s come here and it is already compiled and let’s deploy it, and after deploying it when we come and when we click on block test we see the address of the contract that has mined this block, so these are so these are the subs that global variables do, like we can also write one more let’s write address public name is equal to message dot sender. So what is going to do is it is going to give the name of the address it is being called with now we are going to call it from this address, let’s write this address here we have copied this address we’re going to call it from this address and let’s check the name and you can compare it is displaying the same name so yeah that’s pretty much about this and that completes our part of the global variable as well so yeah let’s see that if we want to check one more of them or we can also end this or maybe we can give it as a homework to go and check all of these global variables and see what each of them outputs so I mean it’s better for you to understand and learn. So yeah that’s it for today’s webinar, thank you for tuning in.
Chris: Thank you, Hemang, the professor of NFT development.
Hemang: Thank you, sir, thank you.
Chris: So yeah for those who missed the previous lesson, you know so this is actually part two. So if in case you missed out I’ll be uploading some of the pending recordings for the previous lessons that we have on red. So any questions so far guys with regards to today’s lesson this is solidity programming lesson part two, so anyway anyone here who you know who’s really interested in having a career in blockchain development and nft development, it’s just a really nice and rewarding and in-demand career actually right now.
Hemang: And I can also do one more thing, I’ll provide you all with the code access, so if you guys have been following it and if you also want to check it after this webinar end then you can also check it on your end so that would also help you, I mean upscale your skills in this.
Chris: So maybe a question like for me if example so Hemang for example I’m a total rookie you know so but then yeah since here in TokenMinds Academy, we’re saying that this is a really nice career, etc., so of course at the first time you have this lesson so some people may have may see it like over really overwhelming since they are not yet familiar with coding, etc., so what can you suggest like okay if I’m starting, so which resources, which sites should I review you know to prepare myself for the kind of a career actually, so of course, in the lesson, I cannot absorb really 100 of what’s being taught right so they may be utilizing our recordings once they already read some and pretty somehow had this basic understanding so which sites or resources additional resources could you suggest to them just in case if they are really interested in this one?
Hemang: Yeah, yeah, sure, so I’ll just give one of the resource links right here in the chat box and it is actually a great one because
Chris: Let me post it do we have an anti-bot you know?
Hemang: Yeah, okay, so actually it is one of the great posts if you want to start from the beginning.
Chris: So like else and I’ll send it guys sorry because my bot is so strict you know okay all right.
Hemang: Yeah, so it’s one of the things that if someone likes to understand by reading like there are two kinds of people who like to understand by reading, and one likes to see by the YouTube videos, so someone who wants to go by reading then it is one of the greatest shows to cover because it covers everything from the basic so yeah that’s one of the stuff and for the YouTube portion I’m typing out the name of the guy right now so it is by the name smart contract grammar.
Chris: Maybe send it to me yeah so I can post.
Chris: Yeah, sure to share with everyone yeah. So because like for me an example I see that this is really in demand, especially when Aptos was launched, actually I’m seeing a lot of job openings for Aptos developers or something like that, you know so this is different blockchain, but this is kind of relevant as well, but so yeah as long as there are new blockchain, new technologies since we are still in the early phase of web3, I really believe that you know this will be, there will be more demand in the future as well yeah.
Hemang: Exactly like what people say is that we are just scratching the surface right now, and with all these bear market things actually I see that some of the trends have gone a little low because of the bear markets, but these bear markets are the thing at the time where the real development starts, so I believe that we are just on the surface of it, and like if you ask anyone three or four years ago they wouldn’t have known anything but Solana blockchain now people are developing on Solana blockchain, as well what we are doing here is we are doing on the Ethereum, so as the time is growing there are several other things and tools and especially the tools they’re coming day after day which is really very fast and helpful for the programming and all and especially the development process so yeah it’s going to be a boost.
Chris: Yeah so I really believe in this as well actually one of my nephews, recommended him to you know to take an IT course, and then go straight ahead with a blockchain development career right after.
Hemang: Yeah it’s a great career, like if you see me also, well I’m still in school, but when I also started doing this I was also in school at that time also but now I’m kind of the only guy in my whole batch who knows things about solidity and blockchain development, so yeah it is really cool to understand because you know it gives also you an edge amongst your colleagues and acquaintances that you know some stuff that is giving you pretty high value in return and also just not about the monies, but it is also interesting to see how we are going from the centralized world to a decentralized world.
Chris: Yeah and also for me, for example, you have the basic idea or you know in general you have the idea of how to develop maybe you can you know because you know the mechanics of different blockchains, maybe you can even build your own right, so there are endless possibilities actually.
Hemang: Yeah absolutely that’s also a very great thing that you can also build your own blockchain, like see most people only know about one kind of blockchain which is a public blockchain, there are also others like private and hybrid so these blockchains are also too much in use for example if you are a big community, if you’re in a big organization and you want to transfer all the all your things to a blockchain then you won’t be doing that on a public blockchain right you’ll be doing that on a private one and imagine you being the one developing that so it’s going to be an awesome thing.
Chris: Yeah, so actually it’s a great time to learn the skills at the moment because it’s the bear market so it’s time to build it’s time to hone your skills, your knowledge.
Chris: So yeah, tip guys if of course, I know that everything is hard to absorb just with one sitting or sitting in a webinar yeah so we’re sharing here some links for you so absolutely for example as you can see Hemang states that he’s still a student you know but he’s already mastered some blockchain, and he’s one of our blockchain developers, yeah everything is possible guys.
Hemang: Yes everything is possible, you just need to be consistent enough and I mean it is relatively easier than other languages, it is a high-level language, I’ve explained it in the first webinar which is actually closer to human than machine, so it is basically English, so I was what I was writing there was it’s not actually English but if you try to read and remove the terms that are used in computer language you will understand that it is mostly English so it is easier to learn so yeah I think you should.
Chris: Questions guys before we end this session, or if you’re shy, you can just add Hemang out and you know to send some questions together okay, so if you’re trying out here as well good so.
Chris: Always asking yeah absolutely any questions guys. Okay so if not yeah we will be ending this session. Thank you very much I will be uploading the previous lessons all right so because some people have already been requesting them yeah and in case you missed guys so we have a really interesting feature so in the TokenMinds podcast, so we have actually interviewed one of the big names or blue chip projects in web3 NFT, so that’s where the freemen, so we had an interview with the director of communications at the league, so if in case you’ve missed that okay it’s posted via the TokenMinds podcast and it’s really interesting, we interviewed, okay. So yeah thank you, guys, for joining today’s lesson, so we will be announced as well the lessons for next week. All right sir thank you and have a nice day.
Hemang: Thank you guys, have a nice day.