NFT Development Lesson #5 | Solidity Programming: Modifiers and Constructors With Hemang
Hemang: Very healthy and warm good afternoon, good evening, good morning to another present over here so we are today here present with the lesson you know already going solid redevelopment class where we promise to make you a good nft developer so that you can go and find out some job opening source or I mean to explore the web key domain space has a developer site and for the today’s lecture and webinar we are going to consider on the solidity in which we are be focusing on modifiers and constructors of so before starting I’ll ask Chris to do something about us.
Chris: All right so yeah we are TokenMinds, just in case you have not been here yet. Before we’re talking mines we have we are a full-service web free agency we started around 2016 and now working on different projects since then so we work with more than 100 projects already so that includes token projects and coin projects and now with the recent rise of nfts we are working mostly on nft projects as well so I’m Chris the community lead for token minds and this is among our coach and blockchain developer for TokenMinds as well.
Hemang: Thank you Chris thanks for the introduction. Let’s see so for the people who are here for the first time in the previous webinar we covered mainly three topics which included state variables local variables and global variables in case if you if you may have missed that thing you don’t need to worry about it because state variables are something that we are going to use on our day-to-day classes local variables are not something that we are going to use every day but yeah global variables are also same as the state variables that we’ll be using so when we are going to use it in today’s webinar, I can show you guys around so that you can also catch up now let’s start with our today’s topic that is modifiers. So function modifiers are used to modify the behavior of a function we can understand that a modifier aims to change the behavior of a function to which it is attached for instance automatically checking a condition prior to executing the function this is mainly what they are used for. Basically, modifiers are useful because they reduce code redundancy let’s say you have written code that needs to be repeated in 100 functions then you won’t be needing to write the same block of code for the hundred functions right? So well you can reuse the same modifier in the middle function if you are checking for the same condition for your smart contact and that’s where modifiers are used, but let’s ponder on the question that exactly when to use a modification in the solidity. So the main use case of modifier is for automatically checking prior condition to executing a function, now if the function does not meet the modify requirement an exception is thrown and the function execution stops and below you can see the syntax of the modifier and we will be also see this this index when we are going to develop it and it looks something like this, so basically let me take my pen and yeah so it is the keyword that you are going to use and the only owner is something which where you have named your modifier as you can also give any name let’s say modified TokenMinds, you can give it anything I mean missing anywhere and you can literally give it I mean anything and here you see something little to like this now let’s try and understand what exactly is this the symbol is called merge wildcard, it is called merge wildcard now if the modifier, now it merges the function code with the modify code when this symbol is placed as I’ve shown in the example below, in other terms the body of the function to which the modifier is attached to will be inserted when the spatial symbol this will appear in the modifiers definition using exactly the term which is used in the solidity docs it we can say that it returns the flow of execution to the original function code, so that makes it this point clear to us and what exactly the wild card does. Now let’s move forward to our next question which is constructors. Now, what are constructors? So here so what are constructors a constructor is a special function declared using the constructor keyword now if you may have been wondering why wouldn’t use why I’m not showing the modifier example on hard-coded well I’m going to show you in a bit file because first let’s compute constructor and modify theory section so that we are going to focus only on the coding portion so yeah so constructor is a special function declared using constructor keyword it is an optional function and it is used to initialize the state variables of the contract and it may have and it has several properties so the properties include a contract can only have one and only one constructor you cannot define more than one constructor in a contract. A constructor code is executed once when a contact is created and it is used to initialize the contract state now after a constructor code is executed the final code is deployed to the blockchain, this code includes public functions and code reachable through the public functions remember I have been using the character function. So the constructor code or any internal method used by only the constructor is not included in the final code. The next point includes a constructor can it can either be public or internal it can never be external and an internal constructor marks the contract as abstract in case no constructor is defined a default constructor is always present in the contract so let’s go and see more about on the coding section so we go here this little website is known as the remix ID it is an online id which is used to provides the environment for smart content development now we have already covered solidity part one and these were in part two so we are going to see part three where I’ve already created two blank files modify and constructor. So now let’s first go and check the constructor file will come to the modifier a little later you might be wondering if the file always comes like this no it is usually a blank file I’ll just copy and paste the statement and I’ve written it just to be prepared beforehand and it is just a simple statement it is this pragma version which tells our contact at which Solidity version we are going to use okay so now as per the rule we first write the contract and we give it the name, so I’ve explained it earlier that it’s a good practice to always write the name same as a file name contact name is. Fine I will write it contract constructor so remember the first point there can only be one and only one constructor in a contract and the constructor is deployed whenever consider is called whenever contact is deployed so unlike functions we are going to create the constructors are do not need to be called they’ll be auto deployed so let’s first define a state variable now as I said earlier that I’m going to use the state Vapor so you don’t really need to worry that you missed the last session but if you still really want to understand about the seed variables in details you can check our recordings now we’ll give some name to it and we’ll define it public let’s say we give it a new token supply and we also write constructor public token address. Oh, so you in public and it is not actually public we need to give it something as address because we will passing an address of it you might not be getting it but you know after two or three sentence it will start making sense now we’ll write constructor so the main part comes here we are going to define the constructor here, so constructor now see constructor is a keyword and so it is of a different color with respect to other ones so that’s how you may know that in your whatever you are writing you are doing correct so let’s first write something which have which have no parameters so it is going to be a nascent and rookie stage so since they’re going to be no parameters we’ll just open the bases and we are going to provide name as hardcore, let’s say the token supplies something like this, and the public the token address is similar to the address that we are going to use to deploy it so which status are we going to use exactly we will use as you copy centers from here and write it down and let’s see. So now you might see that there is a warning or error, so if you guys can guess and in five seconds that what the error is so let’s wait for five seconds one okay so if you might have guessed give yourself a pat because we are using the same name so can supply two times, now we want to change it to address so the spelling is wrong here okay so we have to change it to token address see the error is gone and now we are going to go and deploy our contract and remember we are not calling anything we have just deployed our contract but when we go and check the value we see that it is already assigned so that’s the power of construct actually that’s what the constructor does now there can be more type of constructor there can be a parameters constructor as well so let’s see how to create a parameters construct as well where you won’t need where you might not need to pass the value hard coded you want yourself to provide the value when you are going to deploy it so how does it work let’s see so for example you have written again constructor and in here you are going to pass hit a value so since we are using unit and address as state variables so we will be also passing event and address in here as local variables as well so let’s write it token and event address and cool now let’s pass the token supply here, and give it the value token, and let’s pass two connectors here and give it the value to connectors and we see that we might be having some error so let’s see what the error is so I’ll let you guys decide what the error is for three to four seconds and then I’ll solve it now you may see there is we are using wrong kind of closing closings here, so we are actually using square brackets instead we need to use these. So you get my point basically what I’m trying to say is, the more error you make while writing it the more you get to know about it now you see it is gone and even I didn’t even have to go and look in the error here so if for example if you do not know if I remove here then I’ll start seeing an error here so that’s how it works, I’ll remove the error and I’ll go and deploy the contract. So I’ve deployed my contract now to deploy it I am not able to deploy why but earlier I was able to deploy this contract because as I’ve told you earlier a constructor is called as soon as a contact is deployed now when I’m deploying it I am not passing any values right now which is given here I’m not passing any one of this, so let’s why not go and pass some value like one, two, three, four, five as a token and let’s say any address from here, and copy it and we pass it into the token address and then call transact now we see a construct is deployed and let’s go and check the values so the token address is this and the token supply is this cool makes sense. Now let’s go and try to see modifiers. Now you might be wondering why I’m using modifiers after because let’s include modifiers in the constructor so now we know exactly why we are like how we can use construct modifiers inside a constructor because from now we are moving on to the more advanced section. So let’s make a contract and as I’ve explained earlier modifier is something that you use to remove the redundancy in the code specifically modifiers are used for functions that are in the nft world modifiers are used for the function that only the owner needs to access for example those functions can be like transfer ownership so if you want to transfer your ownership you won’t really need the public to make transfer ownership right you would want that only specific people or the people who have deployed the contract can only pass the ownership, so for certain functions, you may use what modifiers now let’s try to create one modifier so we’ll create a contract by the named modifier, and let’s go and delete all these transactions. Okay so we have created it and okay so we’ll first create a constructor, and the constructor will say that owner is equal to the message dot sender. Now I am already seeing an error that’s because I haven’t defined owner anywhere here so I’ll define address public owner, now we don’t see any error let us first deploy this and see what is exactly happening so when I deploy it I get donor equal to the message.sender which is the address that I’m used to call the function, if I change this address from this to something like this and then I’ll deploy again, now in this case the owner will be different to the address that I’ve changed so that’s what global variables, so that was I was saying that if you missed our previous session then also no worries because we have used boats I mean all three of the state variables, local various and local variables in this lecture itself, now let’s go and create a modifier, so to create the modifier we need to write the keyword modifier then only the compiler is going to know that you are creating a modifier and let’s give it any name so for single simplicity I’m going to give it a name only owner that if I’m going to add this modifier to any functions so only certain owners are going to access it, and who is the certain owner? The owner that has called the contract that means only one owner can call this function now what first thing is that I’ll write a required statement. What is the required statement as the needs is says it makes the statement inside the parenthesis to be must be used I mean it must be required string so we’ll say that the message dot sender equal to owner and if not we’ll give passive error not the owner foreign, and then like this tool, so kudos to you just created your first modifier now how you are going to use this modifier so you are going to use the modify incentive function so let’s say that you create a function to set owner that okay let’s use the same word transfer ownership that we mostly use in the nft development world, so you create a transfer ownership function now inside this function you would pass an address where you are going to, which is going to be a new owner and you will make it public, and you will give it only owner and the first statement is going to be obviously required statement and in require statement will say that the message dot sender, this is a good check so you will check that the method or sender is not equal to address zero, and if it turns out to be some of the to be one of the address zero, then it will show an error not, and let’s say invalid address then it will go ahead and then it will set the owner is equal to new owner, now let’s go and deploy a contract and see how it works. We will close these contracts for the sake of clarity and let’s deploy it so it is now deployed, and let’s see, first the owner so the owner of this contract is this now what if summon wants to call this and I want to transfer ownership to some different address but I’m a random person, let’s take this address so if I am going to call and I want to transfer the ownership to my function so basically what I’m trying to do is I’m trying to hack the smart contract because I am someone from outside and I want to transfer the ownership of the function to myself so that I can withdraw all the funds in the contract so I’ll write the address here and I’ll click transact but were we able to transact no why? Because it says not the one of the contract which is just the message similar to the message that we pass in a modifier so basically what I’m trying to say is for example there are thousands, not thousand like yes I mean you can write thousand functions but if there are hundreds of functions that require only owner to call them, then at every function you may not necessarily need to write this code block you can reduce this code block by giving by making it as a modifier and just using it here in the function so that’s what I was trying to say now let’s see if we try to pass it from the address that is actually the owner of the address, so I think so I have lost the address okay this is the address and let’s transact to this address okay now we are going to set this address as the new model so are we able to do no so I am choosing the wrong address on the function itself okay cool I think so this is the address that we use while deploying the contract that was the editor that we use and the ownership is now passed so now just here and check if has the owner changed or not kudos to you it has changed, so perfect you have just created a modifier and also you have also printed a hack to happen a hacked a hack that could mainly lead to the exploitation of your contract. So, you also saw how to use a constructor and within the constructor and also how to use a constructor and a modifier together so that was for today’s lesson, and let’s hope to see you in the next one.
Chris: All right so thank you Hemang for that. All right so we have this solidity programming that is modifiers and constructors. So any questions so far guys for our coach for today Hemang? Anyone? Okay so guys I’ll update, I missed updating the previous recordings in the forum channel, the links, so I’ll be up uploading those today in the forum channels links so yeah in case you missed some of the lessons you can always come back to those lessons all right so thank you Hermang for today’s lesson. So yeah I think there are no questions for today from the audience.
Hemang: Thank you guys for attending the lesson. Thank you Chris for having me here, and it was a great one.
Chris: So thank you, thank you, everyone. So see you again in the next lessons and then bye-bye guys.
Hemang: Bye guys.
Chris: Have a nice weekend, have a nice weekend guys, bye.