MASTER Solidity for Blockchain Step-By-Step (Full Course)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm gonna give you an introduction to the solidity programming language so that you can become a blockchain developer and it doesn't matter if you're brand new to blockchain or if you're just learning to code I'm gonna teach you everything from scratch I'm also going to show you how to do this directly inside your web browser so that you can get started quickly you won't have to install any special software on your computer to get started and the best part is I'm gonna show you the most updated version of solidity because a lot has changed so make sure you subscribe and click the like button down below so you can catch those videos when they come out so before we get into all that if you're new around here hey I'm Gregory from DAP university and on this channel I teach you how to become a blockchain master so if that's something that you're interested in then head on over to DAP university calm for it slash bootcamp solidity is the main programming language used for writing etherium smart contracts its object-oriented and it looks a lot like other common programming languages such as C++ Python and JavaScript so if you're familiar with any of these languages already then you're gonna have a huge advantage learning solidity so it's statically typed as opposed to dynamically typed it supports inheritance and also libraries so don't worry if you don't understand what these things mean just yet you learn all those things inside this tutorial series and let me quickly mention that we're going to use the latest major version of solidity for the year 2020 in this tutorial so we're gonna focus on version 0.6 and that's one of the big benefits of this tutorials series is the blockchain landscape is constantly changing solidity is always updating new versions and I'm gonna educate you and all those changes inside this video this is a website that allows you to write a theorem smart contracts directly in your browser this is remix so head on over to remix etherium to get started and follow along with me in this video so if you use remix before you might notice that this has changed quite a bit from previous versions of remix and that's another reason I want to make this video is to show you what those changes are and how to use the new version of remix alright and so if you haven't used remix before let me quickly explain what it is it's a browser based IDE that lets you develop smart contracts directly in your browser you don't have to install anything on your computer get started you don't have to use a terminal you don't have to use a text editor or anything like that you can do everything here so you can create new files like this I'll show you you can go here to the sidebar and click the plus icon alright I'm gonna go ahead and create a new file here called counter alright counter the capital C Sol this is the file extension for the solidity programming language Sol and it's convention to capitalize the name of your smart contract files whenever you create them ok so click OK and here we go this is the file that we'll use to write all the solidity source code for the smart contract and I'll get back to that in a second but I want to continue on with the tour here of the new version of remix so you can create new files for your smart contracts you can see that there's some already saved here alright this is a storage smart contract ok there's an owner smart contract ballot so you could even learn a lot about solidity just by reading through some of the examples that are provided here inside of remix but I'm gonna show you how to build a smart contract from scratch step-by-step in this tutorial and I'm also going to take you through many different features of the solidity programming language throughout this is series so in addition to writing smart contracts inside these files remix allows you to do a lot more things like compile the smart contracts deploy them to a blockchain and actually run a blockchain inside your browser so remix is actually a fully fledged IDE so if you're using this brand new version of remix you might have to configure some things yourself it may look really bare where you know you can only see these files and a plugin manager so I'm going to tab over to a separate terminal window excuse me a separate browser window that I use in my regular remix setup which you can see I've got a lot of other things configured like here's the files here's the compiler and things like that so I'm going to show you how to add these plugins to your environment so that you can do the same things that I'm gonna do this tutorial so let's go here out of the plugin manager and let's look for compile so the compiler click activate and dude deploy and run transactions so just search for deploy and it should be the first thing that comes up so deploy and run transactions click activate and there you go those are all the plugins that you'll need for now in order to follow along with me so I'll show you what they do first this is the compiler this you know takes your source code and compiles it down to bytecode that can be run on the etherium virtual machine and I'll explain that more when we get to that step and then finally this allows you to actually run a blockchain in your browser right here you can see the environment is a is a JavaScript virtual machine which basically means it's a virtual blockchain and it gives you several accounts and we'll go over all this here in a minute but I just want to give you a quick tour of remix and how it works and how to get set up so you don't get stuck if you see the new version of remix and don't quite know how to use it all right so let's get started open up your counter dot sol file that we created a minute ago and go ahead and do this at the top say pragma solidity and then a carrot where's in 0.60 followed by a semicolon so this line just tells the solidity compiler which version of the solidity programming language that we want to use this must be the first line inside of our file and we must declare their version like this next we declare the smart contract like this we say contract capital C counter and then we do an open curly brace integer for a newline and it should automatically create the closing curly brace for you all right if not you can just type it manually like this so this is the basic skeleton for the smart contract we use this contract keyword to declare it and then we give it a name you know counter is the actual name of our smart contract and then all the source code for the smart contract will go inside of here and then all the rest of the code for the smart contract will go inside these curly braces all right so the first thing that I want to do is create a basic variable inside of solidity so if you're brand new to programming variables are how we store information and specifically inside solidity you know we create variables to store things like numbers strings of text and all different kinds of things all right so I'm going to show you how to do this right now we'll say you know variable can look like this we just give it a name like count we're just draw a basic value for this counter smart contract okay so we're gonna followed by semicolon but also we have to give it a type all right we have to tell solidity what kind of information is inside this variable so in this case it's gonna be a number all right and solidity doesn't have something called number like this it has something called a you int all right so u n stands for unsigned integer so if you think about an integer like you know 1 2 3 etc etc well let me actually do that in you know comment that out here just so they don't get confused like 1 2 3 dot a dot these could be integers but unsigned integer also means that it can't be positive or negative sorry it can't be negative which means it can't have a sign in front of it or a minus sign all right so it means a positive integer and that's the smart contracts we want to do it's going to take this number we're going to be able to read it out from the blockchain and we're also going to be able to increment this so think maybe you have an app on your phone where you're playing a game and you want to keep track of the current score you could use something like this you could just increment the value by one and that's exactly what I'm going to show you how to do so next I'll show you how to read the count from the smart contract we're going to create a function like this say function get count alright when we follow that by parentheses like this and curly braces alright so a function is just a way to create reusable code inside of a smart contract that performs some sort of operation so inside this function what it's going to do is actually read this count from this variable and return it back to us so that we can you know inspect its value so if you're going to use this counter to keep score in a game for example and you want under the current score well you can call this get count function to return the current score all right so we can do that just like this we say return count this is just a special keyword inside of solidity whenever you finish a you say return and then you give it the value that you want to return in this case it's the current count okay so next what we want to do is make this function accessible outside of the smart contract so we let's do this we say public so this is the visibility of the function alright there's several different things we could do here we could say this is internal or we could just not put anything here we'll learn more about that in the series but for now just know that we must use the public modifier here in order to call this function on the smart contract and our browser here in a moment alright next we add view here which really just means that we want to view the value of this count here we don't actually want to change it and it means more something more complex than that which we'll find out more in the series but that's essentially what you need to know for now this just views the count it doesn't actually change the count all right and so finally we want to specify the type of data that gets returned here whenever we say you know return count so we say returns and then a unit alright because we said this was a you nth here or unsigned integer and we want to specify that it also returns a you nth here with this function so next what I want to do is create a function that actually changes the count okay so this function gets the count alright and this is the variable will restore the count and then finally we want to create a function that actually updates this count so we can say function increment count okay and we followed by open and close parentheses and this one will also be public just like the previous function because we want to be able to call this function outside the smart contract we'll be able to use it in our browser which we'll see momentarily all right and we'll do it open and close curly braces just like the last function and then everything that we'll do and decide this function goes here so inside of here all we want to do is change this count by a one a so we can say count which reference is this right here equals will update it must say count the current count plus one followed by a semicolon so whenever we call this function we're gonna take the count and add one to it so anytime we do this it'll just be like a counter that you might use to keep score in a game which I talked about a minute ago so let me talk really quickly about this count variable so this is a special type of variable called a state variable the count value actually gets stored to the blockchain itself all right so never we put this smart contract in the blockchain and we update this count amount it actually gets saved to the blockchain and we can read its value later alright so there are different types of variables and solidity there are local variables that you might just see inside of a function but there's also variables that are more permanent and that's what state variables are we have to actually store the spy on the blockchain and read it off so whenever we call this function we're actually updating the blockchain we're updating its state all right so you'll see here you know we used a view modifier for this function we don't use view here because whenever we call this function we're actually gonna change the state of the blockchain whenever we update this count so more on that in a minute but I just want to make a quick note of that while we're here now we want to set an initial value for this count all right we're returning the value and adding one to it but we want a starting point and we want to specify what it is so we're gonna use something called a constructor function to do this alright so that looks like this constructor public and then open and close curly braces so this is a special function that looks different from these other functions that we just created a minute ago this is a function that gets run once and only once whenever this smart contract is deployed to the blockchain so any code that we write inside of here will execute whenever we put this smart contract in the blockchain so in this case we want to do is just set the count value I want to say count equals zero all right so that's gonna be very explicit that we're starting at zero whenever we you know use our counter and then when we add one with increment count it'll go to one and then you know when every time we click this this will change by one but we start at zero and then we can fetch the count like this and also note that we must use the public modifier on this constructor so that it can be run in the migration all right so that's our basic smart contract for now we're gonna come back and modify this code here in a minute but what I want to do now is actually compile the smart contract and put it on a blockchain so that you can use it and actually observe how the code works that we just wrote okay so go to this first plugin all right this is the compiler plug-in that we added a minute ago click compile all right make sure you have 0.6 dot one selected here first compile oops it looks like I have a typo here I can't believe I had that the entire video that's okay so pragma PRA GMA solidity version 0.6 dot 0 sorry everybody if you've been sitting or staring at this the entire time wondering how I'm gonna fix this here you go alright so let's try it again click compile and there we go so let me explain the compilation step essentially what this does is it takes your human readable code and it compiles it down to machine readable code alright so you can see I'll see your click on byte code and this shows you you know what the value looks like so I'm actually gonna open my text editor here and you can see this is what you get whenever you compile this down to byte code alright next let's go ahead and deploy it to our virtual blockchain so click this icon here and make sure you click JavaScript virtual machine and make sure you have your smart contract selected and click deploy alright there you go that's how easy it is to deploy the smart contract to your virtual blockchain so you can see the deployed instance of the smart contract here all right you can see our two functions so increment count and get count so let's click the get count button this calls it and should return zero like we expect it to all right so you count I there you go zero and increment count alright so then let's get count again all right there you go boom try it again get count too awesome it's working so let me mention this this is the sort of transaction law that you can see for the smart contract on the blockchain here so remember when I said we create a transaction whenever you do increment count here that essentially means that you're you know writing data to the blockchain so the blockchain is you know basically comprised of bundles of Records called transactions so every record in the blockchain is essentially a transaction and those groupings of transactions are contained in bundles of Records called blocks which are chained together to make up the blockchain okay so you can see all the transactions that go through right here and anytime that we create transactions you must pay a gas fee so you can see our account here has gone down so the rest of these accounts start with a hundred but this particular count has gone down and it will every single time that we do that so increment count again and again and our balance keeps changing okay because we're paying for gas anytime you update the state of the blockchain and we call this increment count function alright so that's how you deploy the smart contra to the blockchain and use it and call its functions next I want to show you how to refactor this code a little bit alright so the first thing I want to do is actually get rid of this constructor and show you that you can just set the default value of the count here so I can just a count 0 all right we can remove the constructor function like this and then we can just compile all right still works and deploy I get count to 0 increment count all right it's changing so we can you know change this value to 1 if we wanted to you start at one click deploy again we can skip the compilation step alright and say here get count by default actually me try that again we do have to compile anything let's do this deploy get count as one by default awesome and increment count still works okay so next what we can do is remove this function and do it automatically with the variable okay so instead of you know creating a function like this to return the count we can just call this public so because a you int public count equals one all right and that removes this functions need entirely so let me tell you what that does this assigns the visibility sorry the visibility of this state variable to public just like these functions are public and whenever you do that on a state variable solidity automatically generates a function for you behind the scenes that gives you this variable value so I'll show you what that looks like what does click compile and then I'm gonna delete this click deploy and now you can see that there is a function called count here which automatically returns the value guys the increment count then count perfect so next what I want to do is refactor this function a little bit so change it and instead of doing count equals count plus one here we're gonna sound say count like this plus plus and that should automatically increment the count for us so that means this is a shorthand way of saying add one to the current value of count all right so I'm going to compile that and then dude clear this out and do deploy all right count increment count goes up by two or goes up by one two two three how high it works so that greatly simplifies the code for the smart contract it reduces the amount of code we actually have to write in order to achieve the same types of results but I think it's really valuable to see how everything works the long way first so that you better understand what the short way does all right so that's an introduction to the solidity programming language congratulations you just bring your first smart contract all right let's start programming so head on over to remix aetherium org to get started this is a browser-based ide where we'll write all the source code for the etherium smart contract in this tutorial we'll compile it and we'll deploy it to a blockchain all inside of our browser alright so go ahead and navigate to the file browser and click the plus icon here this is how we'll create our first smart contract so we're gonna call this my contract all right and the point this contract is going to be to showcase lots of different features of the solidity programming language in particular we're going to talk about variables all right data types and also custom data structures all right so I'll show you that right now the first thing we want to do is create a smart contract for showing these types of things we do that like this we say pragma solidity and then we pass an aversion so I'm gonna say version 0.6 dot 0 all right I'm gonna bump the font up here and now what I want to do is create the smart contract like this who say contract my contract and then we put all the rest of the code inside these curly braces just like this alright so the first thing I want to talk about our variables okay and variables are basically just a way of storing information that you can reuse inside your smart contract okay so there are two different types of variables that are we want to talk about all right so state variables and local variables so let's start with local variables so what basically what local variables are are ways that you can store information that can be reused you know inside of functions essentially so I'll show you what that looks like like this will say function I get value this will be a function where we basically just return an arbitrary value inside of our smart contract we'll say function get value and inside of fear we will return the value of a basic local variable all right so in this be a number so say you ain't value equals one so let me explain that this is a variable it stores the number one and the type is an unsigned integer we'll talk more about that in a minute okay so basically we can do things of this variable we can you know do math with it we could save value you know add one we can manipulate it we do all kinds of things but for now we're just going to return it all right so basically it allows us to destroy this information and do things with it later for now we're just going to return it with this function okay so we'll observe this by saying public pure returns you int all right we're actually gonna create this function so that we can call it outside the smart contract all right so that was how you do a basic local variable and you can do a lot more with these which we'll see in this tutorial series but I want to first illustrate what a local variable looks like as opposed to a state variable all right so we can do a state variable like this right so a state variable would look like you went you know my you int and that would look like one right here okay so what's the difference between these two things well a state variable here is actually stored on the blockchain itself and we could potentially even update this value later especially if we did this you went public my you int all right this basically creates a way for us to fetch the value of this alright so this value here is just local to this function you know value cannot be read anywhere else like we've create a new function down here we couldn't access value okay but if we do create a new function down here we could access my UN all right so at this call variable scope the variable scope is only available for value inside of this function but you know if we create you know like function two like I was saying a second ago let me say like get value two or say we could say get let's see or get my event we wouldn't be able to return value here because value only available inside this function but we would be able to return my you end we'd say my you nth and it would work just fine okay so the other point is that state variables exist on the blockchain so you can think about the blockchain like a database in this way we can update the value of my event later and change it to two or three right and now information would get stored in a transaction on the blockchain and this would actually update all rightbut value here doesn't really change unless we tell it to inside of this function and its value doesn't actually get recorded to the blockchain it's just stored in memory inside this function alright so that's the difference between a local variable and a state variable so let's look at some more state variables to talk about different datatypes all right so that's really important so in this case the first you know data type that we looked at is a UN which stands for unsigned integer I'll explain that more in a second but we can also look at like int all right and say public my int equals 1 all right so my int is an integer and then my UN is an unsigned integer so what's the difference well an unsigned integer can't have a sign which basically means it can't have a negative in front of it all right that's what a sign is you know plus or minus that search between an int and a you int all right so there are different types of UN's we can say you went 256 all right public my UN 256 what could also equal 1 you could also equal like you know 1 billion or something like that but for now we just leave it is 1 and then finally we can say you know you meant 8 public my you meant 8 equals 1 all right so there are all different types of unsigned integers and so what's there between you and 256 and you int 8 well these are just different sizes and this specifies you know how big the actual number can be so if you don't want the number to ever be very big and you want to optimize performance inside your smart contract you would use something like you at 8 but if the number needs to be really big and you allocate more space for it and you're willing to sacrifice performance you can you 2:56 all right so what's the difference between you int and then we you know what is you in without any number at the end well it's just short for you into 256 so by default it defaults this value right here okay all right so that's how you can work with numbers let's look at some other data types all right so these will be state variables I'll take this away only leave it for now let's look at strings all right so what is a string well basically it's just you know a collection of characters that you see on your keyboard strung together surrounded by quotation marks so a string could look like a hello world all right we can store these inside of smart contracts as well would you say string all right public my string equals hello world okay so that's what a string looks like all right and also notice that I'm putting the type here before the variables this is what it means for solidity to be a statically typed language you must declare the type before you do the variable so I just be my string and also once you assign this value to a string you can't later assign it to a number or something like that that's also a feature of a statically typed language like this all right so that's how you'd store a string I mean you can do that here with value as well this could be my value you can change this to string value you know it could be hello world all right and you just return value like that that's how you to work with a local variable all right so let's see here we can also do bytes 32 so that's gonna be more performant than a string which you might want to use in some cases so like bytes 32 public my bytes 32 equals hello world all right so you can see that there are some times this you can do things the same way or sorry the same thing different ways in solidity and a lot of times the reason for that is performance because you know a lot of times you know you're thinking about Optima I see how your smart contracts work to minimize the amount of money that has to be used instead of transactions and also the amount of time that something just takes on the blockchain because black chains by by nature are very slow okay so the next thing I want to talk about another data type would be an address so on the blockchain every user that's connection to the blockchain has an address every smart contract on the blockchain has an address this basically tells us where it comes from so we can store addresses in the smart contract like this we can say address public my address equals I'm just going to paste an address in here from all screen all right like this so this is what in aetherium address looks like so if you have a meta mask installed or something like that you can just put your own aetherium address or you can go to a website like ether scan and just find any etherium smart contract address and paste it in here so make sure you don't put it quotation marks or anything like that around it just put the raw address here of course with zero X in front of it all right and we just store that as an address type on the blockchain all right so the last thing I want to show you in this video are how to create your own custom data structures and we do that with something called a struct alright so you can create a struct like this because I struct my struct alright and then you just put curly oops sorry put curly braces after it like this so structs are basically a way for you to just model any arbitrary data alright so this could you know represent a person it could represent you know a vote or really anything all right you can put your own data types inside the struct so for now we'll say you know you int my event and then string my string alright so you know this for example this could be a person and the person could have an ID and the person could have a name alright so sorry this I don't know why this keeps highlighting things it's kind of annoying but you get the idea here you know I can could be a person this could be a vote this could be you know ballots or something like that alright or you know whatever you're voting for so for now let's just keep it as my struct and will say my int it's being consistent here with other examples alright and my string okay alright now let's go ahead and create a way to fetch this struct I'll show you how to use it so this just creates this just defines the struct right here but we haven't actually used it anywhere inside the smart contract so we'll do that like this we'll say my struct so this is you know you define a custom type here and we'll put the custom type before the variable the public my struct equals my struct this is how you instantiate it and say one and then hello world alright so you can see that the struct is sort of a way of grouping values maybe so we do Myatt here I guess one in my string is hello world we can just put those inside of a struct like this it's a public my struct equals you know the UN tis one in the hello world it's about you okay all right so let's watch all this in action okay if you didn't watch the last video on how to set up remix go ahead and check that out but if you don't yeah you need to get out because I'm gonna compile it and you may not actually have this button here so if you don't have this button go check out that last video on how to find it really quickly you know you can go to the plugins and just search for the compiled plug-in and then also the deploy and run plug-in and that will get you there okay so let's compile it make sure you do sort of two versions you're at 6.0 compile the smart contract actually let's do let's do is you're not 6.4 let's try that okay so we have a little issue here let's go back instead of get value let's do let's change this to go back to the int or you it value as one alright let's just avoid this problem altogether okay so make sure you're smart country looks like this I have the code for this down in the description below so if you have any trouble you can just copy and paste what I have all right so I'm just a newbie 0.6 dot for or I just do 0.62 at zero all right let's be consistent here all right I'll do that so it's compiled just fine now let's go to the deploy step all right so this is from earlier okay this out make sure you click it and make sure you click JavaScript virtual machine just keep the first smart contract selected and click deploy all right there we go there's a smart contract so now we can fetch all these values so getvalue should return one all right boom there we go so my address here should return this value all right there we go my by 32 should return this encoded which we should see here boom there we go all right so you could convert this in your front-end but that's the more performant way to pass the data around so my int should be this alright my string hello world yeah hello world my struck should return these two values one in hello worlds yep my you nth all right and my 256 and my unit eight all right perfect so all these work just like we expect we're gonna cover two essential features of the solidity programming language these are going to be two different data structures the first one is going to be an array and the second one is going to be a mapping all right I'm explain what both of these are and how to use them inside this video okay so if you haven't already head on over to remixed aetherium org to get started you know this is just an in-browser ide that lets you write aetherium smart contracts directly in your browser without having to install anything on your computer it's really nice so I'm going to create a new file here I've got one called my contract Seoul and inside of here make sure that you add the version of solidity that we're going to use in this video so 0.6 dot 0 just like this and then also go ahead and create a smart contract called my contract like this all right so now let's talk about arrays so what are they basically they're just a way of storing information basically a list of information in order so you might be familiar with this from other programming languages but if you're not I'll show you what a basic array looks like inside of solidity all right so you can create one like this let's go ahead and delete this out and I'll say you int all right and then open curly sorry Open bracket close bracket public you int array equals one two three all right so that's how you create a basic array inside of solidity right so I bump this font up a little bit so essentially what it is it's a list of information in this case it's just a few numbers all right one two three there's three items inside of this array and they're an order right we can access each of these values based on the order that they exist inside the array we call that the index which we'll see here in a minute okay so basically arrays inside of solidity have a very specific type inside of them so in this case this is an array of unsigned integers so if you're not familiar with those go ahead and check out that previous video where I talked about different types basically this is just an integer that can't be negative and you know we create an array of them like this all right we call it public so that we can read it outside the smart contract and then we give it the variable name of this array which is just you int array all right just an array of unsigned integers we can also create arrays with different types like strings for example we can do that like this say string alright and this is a string array public and we'll just call it string array and we can create items inside of it like this we'll say apple banana and carrot all right we can also create arrays without any values initially right we can basically just say string public my array all right we don't add any items to the array we just create it we initialize it here all right inside the smart contract and then we can add items to it later inside of functions and things like that all right so I'll try to do that right now so basically you can cut a new function like this say function all right we'll say add value okay and we'll pass in a value this function like this will say string memory value and whatever we do inside of here we'll add it to this array right here all right we'll say public and we'll pass in the by like this I would say values push and then value okay so let me explain what this does essentially it just takes this values actually sorry I said my array here the shipping values okay so it takes this array that we create right here values and then just adds the value to it from the function so we use value with an underscore in front of it here and that represents the being passed in and we add it to this array like this which is a state variable right we store it inside the smart contract okay so we do that with the push function okay so what push does is it takes one argument this is the value here and adds it to the end of the array okay so if we had done this - you know you int array it would you know or sorry let's do string array since this is a string let's say we passed in you know D will say like a dog food inside of here it would add dog food to the end of the list okay so that's what push does it adds an item to the end of the array okay so we'll see that in action here in a minute when we actually test this out in the in the compiler here in a minute are actually in the runtime environment so the next thing we'll do is show you how to get the number of items that have been added to this Ram okay say function value count all right you notice a public view returns say you int okay and inside if here we'll say return values all right its values right here dot length all right and that will let us know how many values exist inside this array okay and really quickly it looks like I have a typo here say function sorry about that okay so the last thing I want to show you what they're raised or how to do two-dimensional arrays so what does that mean well you see how you know these arrays have you know numbers inside of them and these have strings inside of them well you can create an array that has other arrays inside of it okay that's what a two-dimensional array means so I'll create one like this so you int it basically it'll be a two-dimensional array and then the innermost elements will be numbers which we'll see here in a second right like this it's a public array we'll call it 2d all right equals one two three and then four five six okay there you go that's a 2d array so you can see there's an outer array here and then two inner arrays which contain one two three four five six and those are unsigned integers and so you just declare the 2d array like this okay all right so let's watch some of this in action go ahead and click on the compiler here I'll click compile contract if you don't have this you can install it from the plugins down here ok all right so that worked just fine no issues then let's go to the runtime environment so we can deploy the smart contract like this all right click deploy and there we go so let's interact with this we'll do it one by one here let's look for the UN array all right so I'll explain this does essentially this function does not return the array all right remember whenever we put public we put the public visibility on state variables it gives us a function right these are the functions right here that it gives us for free we don't have to create a function like this it doesn't automatically behind-the-scenes whenever we call this public okay and so the function exposed by this variable right here you enter array doesn't actually return the array itself it returns a function that we can pass indexes or indices into to read each item out individually so I'll just show what that means that might sound kind of confusing just watch this and I think it'll explain everything okay so you enter a basically I put in 0 right here and it will return the return the number 1 all right see boom done so this is an important thing to note arrays and solidity like most other programming languages are what's called zero based index alright so if you put in the number 0 it will return the first item if you put in the number 1 all right it'll return the second item all right and then you know put in to return the third item and if I put in a number and index that doesn't have a value it will throw an exception so if I put like you know 99 all right there you go so the vm error right so you can see that in action also with the string array alright if I put in 0 that's the first item in the list all right string boom done okay and then the next item would be we'll see here one okay alright then the next item would be two alright the next item would be three or do it you know yeah you know I'm saying if I put a number it does the same thing you know it'll just basically return a VM error okay all right so that's the string array so look at values so in order to read items out of there we can do get value so we can add a value like this so that's gonna be a string we can just say you know foo all right click add value all right then we can add another value and say bar and then add another value and save as all right boom so now we can get the value count to see how many items exist in this values array all right value count is three okay and then we can read the first value like this but in zero and there you go foo the second item index 1 alright bar as and then you know if we do 99 it'll do a VM error okay okay the last thing I want to look at is how do this 2d array works so essentially we can see array 2-d and here's how we access each item in there so it's like we basically put in two arguments we put the index of the first array and then the index of the second array so 0 0 should be 1 all right all right then 0 1 should be 2 all right so likewise I can put like 1 which would be the second array in here and then at position 1 should be 5 right so 1 1 should be 5 all right and then 1 2 should be 6 all right there you go that's how you access to the arrays all right so that's pretty straightforward where I'm going to show you a really critical data structure that you're going to use all the time whenever you develop smart contracts for aetherium so first and foremost what is a mapping well it's a data structure inside of solidity kind of like an array which we talked about in the last video you know an array is a linked list of items are sorted list of items I should say that you can you know put put information inside of and do things with it you can you can fetch the items back out later you do lots of stuff with them so a mapping is similar in that regard but it's a little bit different ok a mapping is a key value store where you can look things up based on a key and then get a value back so what do I mean by that well here's an illustration that might help here is a key value relationship ok it's a table where you have a key here and then the corresponding value and essentially the key is unique so there's only one k1 and if you look information up on this table if you look for k1 here are the corresponding values right k2 stands for key in this case so you can kind of assume that there's no K and just say 1 2 3 4 so if I say show me the information in row four well here's the information in row four or show me the information in row 5 here's the information in row five so also if you want to create new information on this table you just say here's the information that would go on case six for example and you could give it some new information and later you could go and look that back up ok so that's what a mapping allows you to do inside of soluti it is a key value store it's just similar to other programming languages where you might have a hash table or hash or maybe an associative array okay so I'll show you how to create a mapping inside solidity like this you just say use the mapping keyword mapping all right and then you tell it the key you tell it the value and then I give it a name like my mapping all right but I'm gonna repurpose this and show you what it look like for a real mapping so in the in this case let's do a mapping of so we'll call this names so names like Adam Brian and Carl alright we'll call it public and what you do with the key and the value here is you specify the datatype so the names here are going to be strings so it's J string and the key is going to be an integer we're just gonna store information for like name one name to name three name four so we'll say unsigned integer or you int alright and that's how we'll do it it's pretty simple it's a pretty simple lookup will just say name one is going to equal one string name two is equal to a different string we can set that information and we can look it up so let me pause here and talk about how a mapping is different from an array you know the last figure that's what we talked about and you could store names inside of an array but there's a few limitations one is there arrays or zero based index so like if you want to look up the first name you know you have to look up name 0 and if you look at the second name its name 1 and so on so forth sometimes you just want an ID right and that's essentially what this does is name 1 will be accessible by the ID 1 so it makes the blockchain work somewhat like a database where you can just look information up and mappings are one of the primary ways that we use solidity smart contracts somewhat like a database ok because don't forget the blockchain essentially is a database you can put information on it and read it back out later ok it's a public ledger and this is a way that you sort of treat it like a database you give it an ID and then you here's the information that corresponds with that ID so you could use mappings in different ways than just this which we'll see later but let's continue out with this example all right so let's actually add some names this mapping I'll show you how it works we'll do this inside the smart contracts constructor function say constructor alright and then don't forget the constructor function is just the function that gets run whenever the smart contract is initialized or deployed to the blockchain so it gets run once and only once and here's how you add the names to the mapping basically you reference named Sophie names and then use these brackets alright and then you say like name one alright is equal to let's say Adam alright so names one equals Adam alright we do that again for names two is equal to karl names three is equal to well sis to zu Karl so an ABC order here Bruce and then curl okay so basically we're gonna say the name with an ID of 1 is equal to Adam name idea of 2 is equal to Bruce and names idea of 3 is equal to Karl so we can read this information back out of this mapping directly because we call it a public here alright this is a way that slitted to give us a function to read these names back out alright so let's go ahead and compile this and run it you know if you if you're having trouble following along don't forget to go to remix tie theorem don't work and create a new contract called my contract soul and you can just use a source code at this point of video okay so going to go to the compile step alright and then click compile all right I've got a little issue here so the constructor needs to be public my fault let's fix that alright and then deploy okay so now we can just read the names out of the mapping so say names 1 all right Adam names - Bruce names 3 is Karl alright so what happens if we do name four empty string okay so essentially what the mapping does is if you provide an ID that doesn't exist it returns a default value in this case is just an empty string okay alright so I'm going to kill that contract and go back to our development mode so let's continue on with this let's let's talk about how we can treat the blockchain more like a database with mappings essentially what we can do is model arbitrary data and more complex data okay then just like strings or numbers or something like that we can create mappings of our own custom data structures so in one of the last couple videos we talked about how to create strokes which are a way to create your own data structures size solidity so let's create a database of books okay so we start off by creating a book struct like this say struct book okay and inside of here was a string title and then string author okay so this is how we create our own custom book with two attributes title and author both are strings okay and we can create a mapping of these books like this say mapping we do an ID of an unsigned integer you meant and we say book like this okay in public books so that's how we create our books mapping so we can say book one and then we can add a new book here with a title and an author and then actually let me lowercase this and then we can a book to a title author etc etc etc it'll work just like a database so now we can create a special function to add a book so we won't do it automatically in a constructor like this we'll create a special function to add a book like this a function add book okay and then we'll just do public will add the arguments in a second but here's how we add it so we say books well actually let's go down there at once so what do we need we need to tell it the ID all right and then also the title and then the author okay so let's say you int ID and then string memory author or title and then string memory title okay so now this information we can add the book to the mapping like this just like we did here they say books ID equals book this is how you create a new book and then say title and the author all right so that's how you add a new book to our database okay the next is well let's see yeah let's just go at that and observe it in action so let's compile it does it run properly okay I see if the issue is sorry I did title twice as me title and I was to see title and author okay try that again yeah my fault all right so now let's compile it deploy it all right and then let's try to add a book so we can say book one will be my book and we'll say the author is Dappy diversity and then say book two is let's say Tom Sawyer and then we'll say Mark Twain all right and then we'll say book oops that was book 1 I think it did book 1 twice let's just let's just see so I was going to read it out look at book 1 and then there you go title Tom Sawyer author Mark Twain so sorry I actually over wrote the first book my fault let's go ahead do book 2 will be too and we'll say war and peace say Tolstoy which give his last name book books to war in peace by Tolstoy ok perfect alright so that's how you would add books with this mapping so the next thing I want to show you is how to do nested mappings alright so this is a pretty critical thing to understand with solidity because it allows you to model more complex data alright so in this case we just created a way to store books and store you know all the books that would belong to this smart contract but what if you wanted to store you know books that belong to just you right or books that belong to someone else and then sort of namespace those books under your username or your account on the etherium blockchain well that's what nested mapping would allow you to do so what is that well a nested mapping is essentially just a mapping inside of a mapping alright or a mapping that returns a mapping so you basically say mapping and then like you know key and then the value would be another mapping with like you know key - value - all right them public my mapping okay so in this case we want to do is model all the books that belong to us or someone else okay so we can do a mapping looks just like this we can basically have a book's mapping with a an ID au int and then a book but we can assign them to specific users based on their theory amad r s so the key here can be address the address type okay so basically I can say pass my address in and then get all of my books based on their ID okay so I'll show you that works a function add book or I'll say add my book and say you ant ID string memory title string memory author public and what we'll do here is basically just store it based on the user who calls the function all right so solidity has a global variable called message sender or MSG sender that allows you to capture the address of the person calling the function okay so in this case we'll do it like this so say my books and pass it msg sender this would be the etherium address of the person calling the function alright then you use a nested mapping and then use another racket like this say ID and then equals book and then title and then nothing all right so look it works just like this here's how you would do a normal mapping right so you see that here and then that just returns a second mapping and then you do the same thing again okay but you do different keys so here's the key for the address and then here's the key for the actual book that gets returned okay so I'll share that works in action compile it okay so we got an issue here title author they got a typo here maybe let's see you're undeclared identifier okay so I forgot to change this up here so my books should be the name of the mapping not my mapping all right so try it again compile and then we'll deploy all right so now let's see that my books in action all right so we want to do is actually get the address that we're currently using so go up here to this menu and copy the account all right so we'll keep that handy but go ahead and add a book let's say ID b1 and we'll say to war and peace your Tolstoy all right and then that book and then we'll say you know book 2 and then we'll say the Republic Plato add my book all right so now we can paste in you know click on my books right let's see here so just paste in your address here ok and then we'll look at you int 1 click call there we go warm peace then - there we go Republic so you do the same thing and you can switch accounts here click a different account click copy and then you know get do book 1 for them could be will to power this could be Nietzsche I think I spelled that right maybe wrong all right and then click my book here do that one call and there we go well it's powered by Nietzsche all right so that's an overview of how to use mappings inside solidity all right this is a pretty common thing to use for like database type development for smart contracts there's lots of different ways you can use mappings but that gives you an introduction on how to you know use them inside your own smart contracts we're going to cover two different essential aspects to developing aetherium smart contracts the first will be conditionals and the second one will be loops all right and you need to know both of these things in order to you know effectively become a blockchain developer we'll start off with conditionals all right so what is a conditional well basically it's a way to determine that something happens in a programming language based on whether a condition is met that's not a very good explanation so I'll just demonstrate what I mean by that basically like if something happens you know then do something all right and if it doesn't happen then do something else all right this is not really code this is just sort of a pseudocode explanation of what I mean right so this is commonly referred to as like control flow and programming or computer science or control structures so I'll just show you how this works in action let's go ahead and create a function that determines whether a number is even or not all right we'll just create a new function like this a function is even number all right and we'll say this is public save you or turns you into and inside of here we'll just determine whether the number is even like the function says and we'll return a boolean true or false so let's add the number the function like this will say you went number and what we'll do is take this number and determine whether it's even so how do we do that well essentially we divide the number by two and see if there's a remainder so what I mean by that so for example 4 divided by 2 will equal 2 all right 5 divided by 2 will equal a see or 2.5 or 2 remainder 1 so if you think about like you know elementary school mathematics 2 remainder 1 and you know so on and so forth basically this will not equal an even number it will be it'll be a fraction or odd something like that all right so basically like do like if number and then we'll say module O 2 equals 0 then we know that it's even and if else return false okay so what is this well this Madhu this is called the medulla operator this is the % basically this determines whether there is a remainder so like I showed you a minute ago like you know 5 divided by 2 equals 2 remainder 1 so this module o will basically represent the remainder here 1 so you know 5 module Oh to the medulla will be 1 all right but for even numbers the medulla or the remainder will always be 0 so let's say like 4 divided by 2 equals 2 are 0 or 2 medulla 0 all right so that pattern persists across all numbers right so basically we can just this is how we determine whether the numbers either not this is the condition right so if this condition is met then we want to do something in this case we just want to return true alright and if it's false or if it's not met then we just want to return false okay it's really simple example that we'll start off with just go ahead compile that good pilot so we got some problems here so this is saying bool is not okay so basically this should be bool here sorry guys all right so I specified the wrong return value this should always return a boolean sorry if you caught that earlier all right there we go so now let's compile it and deploy and let's take a look here so it is even number we'll say is one an even number nope is - yes all right is three no it is for true okay awesome so this function works now we can use this function to create an even more complex workflow like this well we'll create a array inside of here with numbers and will check if the numbers inside of the array are even or not alright so I'll say int or say you int say public numbers and I'm just gonna set this equal to numbers one through ten all right like this boom so now is just go ahead and compile this and a point and see like numbers we can say the first one you know is one second one is two etc etc alright so now we can do is create a function that will actually iterate through this entire array and then count all of the even numbers all right so we're gonna do inside this function is create another conditional like this so a control structure conditional and then we're also gonna do a loop all right which is the what I mentioned earlier at the beginning the video as well both of these things inside this new function in order to get the count of even numbers inside this array right so a function will say count even numbers you call it even numbers count if you want to but that's just what we'll do for now public view returns you int okay and so we want to do is create a count variable say you int count equals zero all right and then now what we want to do is loop through this entire array to count all the even numbers so here's here's the first introduction to a loop so we're going to do is a for loop okay you might have seen this inside of another programming language maybe you have a JavaScript background maybe you are brand-new to programming that's okay too so I'll explain this is so what this loop does is it essentially executes code inside of here runs code and it keeps doing it until the condition is met inside of here so for loop kind of is like a conditional and that's actually a while loop so it's a really a for loop it does it for really for it iterates over this entire thing right so I'm not a very good explanation but I think you'll get what I mean when you see it in action okay so just bear with me and we'll continue for so what I want to do inside of here this is a very common pattern you might've seen it in JavaScript I'm gonna say you int I equals 0 and then I less than number re ask you numbers got length mm say plus plus yeah I'm sorry length I plus plus okay so what it's gonna do is it's going to count through each number in this array so we'll start at 0 this is gonna stand for index here and for each number in the entire length all right as long as I is less than the numbers length do this and then whenever this is done do this all right so this let me break that down again this is the initial value for the variable that's there like the counter for the condition and then this is what they basically do this until this is while this is not satisfied and then after each time in the loop do this okay so I'll just show you what I mean by that inside of here we'll say if num or is is even number all right we can use our function right here and then pass in I actually let's do this let's say numbers I all right and then we'll say count plus plus all right followed by this and then we're trying to count so what does this do well it loops through all these and then it checks to see if the number inside of here is an even number and if it is then it increments the count and if not it does nothing and it moves on to the next item in the array all right so let's just watch that in action we'll go here compile all right got an issue here I see your function count even numbers public you a syntax error returns my faults all right and then compile deploy kill this one so let's say you know count even numbers and there we go it's five perfect so it works as you can see here you know - one two three four five which five even numbers in this array so we can change this and this would also return a different count based on you know how even numbers run there so that's how you use a for loop inside of solidity and these has many applications which you can see if you start developing smart contracts but here's the basis on how to do it and here it is in tandem with conditional like this I have these control structures so the last thing I want to do is show you how to do conditionals with in a different setting so let's let's just say address public owner all right and then what we're gonna do is store the ownership of this smart contract whenever it's deployed so basically you know an aetherium account is gonna put the smart contract on the blockchain and what we can do is determine whether the owner is the person who deployed it or not so didn't like this a constructor public don't forget the constructor is the function that gets run whenever the smart contracts deployed the blockchain so you can check that I explain that more in the previous videos but that's it for now say owner equals MSG descender okay and then we can have a function like this that just checks what the owner is the smart contract owner so function is owner alright it was a public view returns bool okay and inside of here will say you know if MSG descender equals owner return true and then else return false all right so that's what's going to check whether this person is the owner of the contract or not okay so we'll see that in action but really quickly I want to refactor this to show you how you can actually get rid of a conditional when you don't need it alright so we see this really simple conditional pattern here you know if condition return true else return false that's really okay when you're just trying to understand how conditionals work in the first place but you don't have to right all that code alright so for this basic function you can do this you can say return MSG to sender equals owner all right and that's because essentially this condition right here is already going to return true or false so the function returns true or false you don't need a you don't need to specify the return value of true or false inside the conditional okay all right so let's just compile this now with the ploy I'll clear the old one here so count even numbers it's still work yes all right so let's say is owner true all right if I change the address to a different one say its owner and false all right so that's an introduction to conditionals and loops inside of solidity so these are both really essential aspects to developing a theory of smart contract so you may really want to make sure you understand me so up until this point we've kind of covered topics individually like arrays for example variables and we've kind of just created you know a single contract called my contract that demonstrates these principles in kind of an abstract way ok and I think that can be helpful approach sometimes but we're going to do in this video is actually build a smart contract that has a specific purpose that's going to illustrate several different concepts or introduce you to several different concepts that we haven't quite seen at this point in the tutorial series ok so what we're going to do is create a smart contract for a hotel room and what's going to happen is basically the user is going to be able to pay this smart contract to book the hotel room and it will automatically open the room for them and you know allow them to occupy it so let's go ahead and get started and when we build out this use case you're gonna see several different things in this lesson let's say here like how to work with ether you know pay smart contracts let's see here how to do modifiers see visibility and also vents and enums alright so don't worry if this looks like an overwhelming list it'll all make sense as we build out this entire project and it won't even take very long but once you finish you'll understand all these things and how to create a hotel room smart contract alright so the first thing I want to do is create a way to actually book the hotel room alright so we'll start off with a function I'll see your function called book all right and we'll do is you know write the logic inside of here to book it alright and what we're gonna do is basically allow the user to pay this smart contract with this function and it will transfer the payment to the person who deployed the smart contract in this case we'll just call it the owner okay so let's go ahead and model the owner so I'll say address payable public owner this will be a state variable and remember this state variable gets written to the blockchain all right so the blockchain will know who the owner of this smart contract is all the time and this is who will send the payment to whenever this hotel room gets booked okay so let's store the owner you know we could assign it here and hard-code the address but what we want to do is dynamically assign it to the person who deploys this smart contract they'll essentially be the Creator okay so we'll say constructor this would be the function that gets run whenever the smart contract is created all right it's a public and owner equals MSG sender all right so let's recap this here don't forget MSG sender is the or message sender is the user who deploys us are the user who calls this function in solidity right so anytime you reference MSG dot send our message this is an aetherium address of the user who's calling this function alright and this constructor function is the function that gets called whenever this smart contract is created it's once and only once whatever the smart contract is deployed to the blockchain all right and we're saying the person who deployed this the person who called this function they are the owner of this smart contract okay and we store that ownership here in a state variable this is actually written to the blockchain all right we say it's an aetherium address type all right address and then payable we must use this whenever we create the variable so this owner can actually receive aetherium or ether for the payment and in public so that we can determine who this person is later good alright so inside of the book function what we want to do is pay the owner to book the hotel room okay so we do that like this we say owner transfer and then we want to actually send the etherium that comes in with the transaction whenever this function is called so just like we have msg sender or message sender we also have message value here msg value all right so whenever we call the book function we can send in a payment we can send an ethereal ether and we can determine how much that is right here with message value all right so in order to do that we must call this function payable all right and then now whenever we call this function will be able to just transfer the ownership sorry not transfer ownership transfer the etherium payment to the owner right okay so that's step one just pay the owner to book the hotel room now the next thing that we want to do is we want to make sure that we can actually book the hotel room all right so if it's already been booked once we don't want it to book again again so we can keep track of the hotel room status all right we can basically say you know is the hotel room vacant or is the hotel room occupied all right and what we can do is inside this Songshan we can check to see whether the hotel room is vacant or occupied before we book it all right so we can keep track of this status like this we can use an enum alright we'll sit we use an Inu alright so the enum will look like this let's say enum statuses okay and then we'll say vacant or occupied all right so what does the idiom does is it is it keeps track of a collection of options basically that will never change and we can reference this collection of options in this case statuses and we can change it we can kind of see what the current status is so we can track it like this we can say statuses current status all right and basically we can set the current status like this will do in the constructor basically we'll set a default status whenever the smart contract is deployed right just like we set the owner we can also set the default status say current status equals statuses that vacant all right so equals here all right so basically whenever we deploy the smart contract this will be created whenever the hotel room is not booked yet so we'll say the status is vacant all right and that changes it or that keeps it that basically is is tracked here yeah so now we want to do is whenever we book the hotel room and we also want to change the status to occupied so we can do that like this we say current status equals statuses occupied all right and that will actually change it okay all right so the next thing that we want to do now that we've tracked the status we have a way to track it right here all right we update this data so never you book it to show that the room is occupied and whenever we deploy the smart contract for the first time we set the default status all right the default status is vacant because by default no one will actually be in the hotel room all right so now we have this book function okay and this book function will update the current status to occupied all right and then it will transfer the ownership of the ether or it'll send the ether to the owner all right keep saying transfer ownership my apologies that's a different concept when you're talking about a notable smart contract so anyways I know this book function by the way is not properly named like when you book a hotel room you don't automatically occupy it I don't worry about that which is gonna change in a minute but I just want to make note of that really quickly all right so the next thing that we want to do is add some constraints to this okay so basically we have a function that will you know update the status and transfer the ownership we have a problem right like you could book this twice right the same hotel room and it doesn't check to see if the it's currently occupied right and it doesn't check to see if you paid the right amount right so we need to add these things to the smart contract and order for it to even make sense okay so solidity has these things called requirements or require all right and basically what we can do is ensure that some conditions are met inside this smart contract for this code gets executed so for example we can check the price and we can check status and basically we can say if you didn't pay the correct amount then don't do this and if you didn't if it's booked already if the status is wrong then also don't do this right so it's two conditions if you want to implement in order for you know a successful booking to take place so we can require that the status is correct like this we can basically say current status equals statuses vacant all right and so we can also give it a error message currently occupied all right so let me show you basically what's gonna happen here is if they try to call booked twice or you know while it's currently occupied and this condition will basically say if the current status is occupied then this will fail so let me explain why basically what required does is it checks to see if this is true okay and if it's true it will just resume execution like this line will just basically skip and these lines will be called right but if this expression evaluates to false then it will halt all right won't satisfy the requirement and basically these lines won't execute and any like aetherium sent in the transaction will get reverted and sent back to the user okay or at least any unused ether at that point all right so also we can specify an error message here which is really nice in the transaction so if if this fails and it doesn't meet this condition we can say that it's currently occupied okay all right so the next thing will be we want to check the price we're gonna require that the user actually sent in enough we checked the status now we watching the price so do that like this which they require currents I'll sit here or having to do this I'll say well check basically we'll check message value okay MSG or message value and we want to require that it's greater than or equal to an amount so we'll specify the amount here we'll say actually we'll say it's greater than equal to two ether okay and if it fails we can give a message we can say not enough ether provided so here's our two checks alright so basically what's really cool about solidity is we can abstract these requirements and something called modifiers and that's the thing I want to show you next all right so here's the requirements now and time on modifiers so basically what modifiers are is they allow you to take these lines add your function and put them into modifiers that will go on the function like this right and you'll be they'll run before the function gets executed so let me show you example so we can create a modifier like this a modifier that says only while vacant and inside of here basically we can just move this like this and this will run the requirement and then after this line we need to do an underscore and followed by semicolon and this will basically execute the function body all right so now we can just say only while vacant here and that will do the exact same thing all right so we've just moved that requirements a man out of the function and here so where this really is helpful is if you have a lot of functions you can basically say only well vacant and you know add this to a bunch of other functions that's where you get the most benefit out of this all right so I mean you didn't get a benefit by cleaning up your code like this but sometimes it can be a little hard to understand if you're having to look at multiple places but if you have a lot of different functions that require this this is how you reduce the duplication which is nice alright so we can do this with the price check as well and we can have a modifier that says costs so we'll do like this basically will say modifier and let's say costs and what we can do is also pass in an argument here you say you int amount and inside this function we can basically move this inside of here and let's ear say require message value all right it's greater than two ether we want to do is actually pass it in here so you int amount require message values greater than equal to two ether we want to say ammount here all right so I'll see here we want to move this here costs and then pass in to ether yeah all right so now both of these checks will run before we execute this code and we've moved the required statements out of this okay all right so the next thing that we want to do is let's see here talk about events so events are pretty cool inside of solidity basically they allow external consumers to subscribe to them all right and find out that something happened in a smart contract so basically if you want to create a smart lock or something like that that actually unlocks a hotel room and listens to events on the blockchain or something like that this is a good way to do it so it could subscribe to an event inside this smart contract and it will do things or whenever this event is emitted all right so you can just create a new event like this will say event occupy all right what's the address occupants this will be the person who opened it occupant and then I will say the amount they paid you mint value all right so we'll say we can omit the event just like this say omits occupy say MSG to sender MSG dot value all right so it's pretty simple you just define the event like this and then whenever the function is called it will omit the occupy event and you could subscribe to this like I said in a smartlock or something like that to actually open the hotel room and let the person or dispense them a key or something like that alright so now there's one last thing I want to do before we test this test this out we want to basically make this shorthand so instead of having a named function called book we want to create a special function and solidity called receive all right and basically what this will do is create a function that will get triggered whenever you just pay this smart contract so if you just simply you know send aetherium to this address this is the function that will get calls like if you open a theorem all like meta masks for example I don't have meta mass pulled up right now but basically if you pay it you just send aetherium straight to this smart contract then it will do this okay so let's say receive external all right it must call it external so that it can be called outs at the smart contract it's payable only well vacant and costs to ether alright so there you go all right so let's go ahead and compile this test it out click compile all right so we got a problem here let's see your modifier let's just update this got a problem with the Inu here let's just try this let's try it all right so got another problem here let's just check the modifier I've got the code for this down the description below so if you want to actually check it with what I have on screen you can just look at the github gist so let's fix the costs I think I just have a small syntax error here sorry everybody yeah there you go okay perfect so like I said I got back I've got the correct code down the description below if you want to make sure that yours is right before you do this step it's going to come pile next let's deploy it all right so just click deploy click go to the first address here all right so now we can check the owner like this there we go there's the owner that's us so now let's go to a different address and actually try to book the smart contract I'll go to this one that has a hundred ether and let's try to pay it basically let's let's do this let's say value equals one let's try to send it not enough ether first basically what you can do is do this low level interaction this will send aetherium to the smart contract so you know we don't see a function name here called receive right that's because this is a special function of solidity this is I interact with it you you send a transaction to the smart contract directly alright so click transact make sure you put in one either here alright so the value looks transact and boom there you go we got an error it's what we expect so we can make you can expand this and see the error down here not enough either provided okay perfect so now what we can do is I'll see here do enough ether - and then transact and there we go it worked it's green perfect so let's try it again now that it's already booked click transact again and we should see an error yep we do perfect all right this error is currently occupied perfect all right so that's our hotel room smart country act we've demonstrated lots of concepts in this video the status you know the e-news the modifiers only well vacant the costs everything there's so many concepts in this video alright but we're going to cover a few different topics that deal with working with multiple smart contracts in solidity okay we're going to talk about inheritance factories and then working with other smart contracts and basically what I mean by that is like calling other smart contracts from inside of a different one okay or interacting with smart contracts maybe it's a better way to say that so smart contract interactions alright so let's get started what we want to do is create a basic smart contract that handles a secret alright and so we'll start off building this inside of my contract you know this is the basic smart contract we've been using throughout this tutorial series so what this country will do will basically hold a secret value and allow a user to return the secret value so we'll start off like this we'll say a string secret all right so will store the value inside of here but notice this won't be public all right this won't give us a function that reads it will create a special function for that which will become clear why here in a second okay so let's just create a way to get the secrets a function get secret and this will be public view returns I'll see your string memory and then it will say return secret okay so instead of making this public and getting this function for free a secret function for free what we want to you want to create a get secret function because we want to restrict who can call this function and I'll show you how to do that here in a minute so the next thing I want to do is be able to set the secret and we're going to do this whenever the smart contract is deployed alright so inside of the constructor function will say constructor I will say string memory the secret and then public okay and then here we'll just set the secret like this all right so remember the constructor function inside of solidity is the function that gets called whenever the smart contract is deployed it's run once and only once and it's you know we we create a constructor with this special constructor keyword it's a function like other functions but they don't doesn't get this function keyword with a name it just uses a special keyword that you know tells it it's the constructor function okay so that's the basic functionality that we want to implement but we want to make this a little more sophisticated alright so right now this get secret function it basically can be called by anyone so we want to create a modifier like this where only the owner of the smart contract can can call it okay so I think I showed you the ownable pattern in a previous video don't worry about like skipping back to that video I'll refresh you on it right now so remember we can create modifiers and solidity like this is a modifier only owner okay and we can say require MSG ascender equals owner and it fails we can say must be owner okay and then we run the function and then we can put this modifier here we'll just say public view only owner all right and then lastly what we want to do is set the owner inside here we want to say owner equals MSG dot sender all right so really quickly let me just refresh you on what's going on here so this is a modifier and solidity which basically is something we can add to our functions that will get called before this code and the function runs like basically we could just put this here we could just say you know require MS you got sender equals owner all right must be owner but it's better to do this inside of a modifier well a lot of times it's common to do it inside of modifier and it can be advantageous especially if you do this kind of thing in multiple functions all right let's say like you have multiple functions that could only be called by the owner you don't want to repeat this line every time so it reduces duplication to put it inside a modifier like this and you can add it to every function that wants to be restricted to who can call it alright so we set the owner in the constructor function like this we just say owner which we need to store here say owner all right address owner equals Ms you get sender this is the address that deployed the smart contract okay and then only this person will be able to read the secret alright so if I switch to a different account inside of remix then they won't be able to call this function right it'll just it'll throw it an exception so this is really common in solidity to have an ownership model where someone owns the contract and we restrict what the owner can and can't do and for that reason it's really common to use a library and a lot of times libraries and solidity are just you know they're there other smart contracts that can be reused so you don't want to reinvent the wheel every time you want to restrict the ownership on a smart contract so a lot of times you'll have a separate smart contract called like ownable or something like that that you import in your project and you create a smart contract that inherits from it right so that's one way to work with multiple smart contracts is to use inheritance to use libraries all right so I'm going to show you how to do that right now so what you'll do is create a separate contract like this a contract ownable and we'll move a lot of the logic um related to the ownership into the ownable contract so we'll put the owner there okay I'm sorry put it here and then we uh put the modifier in there as well oops sorry there you go and then what we'll also do is add this line inside of there but here's where it gets tricky okay we have to create a constructor inside the owner ownable contract okay but we don't want to run both these lines all we want to do is run this line okay and we don't want to except a secret argument because we don't need that in here so how do we tell how do we call this line inside of this contract well we say super alright so super what it does is it calls the constructor of the of the smart contract that it inherits from like this constructor function and you can call super in specific places so you know it doesn't really matter in this case we could call super here but sometimes the order does matter and it's nice to be able to call super in an explicit place okay so the last thing we need to do is say I need to actually inherit so we'll say is ownable all right and that allows the smart contract to just inherit from ownable like this all right so now all of the ownership logic has been abstracted away into a separate smart contract and we can just pull it into this one and yeah that's how it works basically we could have multiple functions that are you know we restrict who can call it based on the ownership we could have multiple smart contracts that you know inherit from this ownable pattern it really drives things up and makes it really nice and this is a very common pattern and solidity that you need to know if you want to become a blockchain developer yeah so the last thing I want to show you before we compile this test it out is how to call smart contracts from other smart contracts all right and then how to also create new smart contracts inside of a smart contract okay so we'll do this in the same was the same thing essentially what we'll do is we'll move all of this secret storage into a separate contract okay we'll call this secret vault say contract secret vaults all right and we'll store the secret inside of there okay and then we'll also let's see here will return the secret so this thing can have a constructor too we'll just move this constructor inside there essentially all right oops sorry all right we won't do super because it's not only Bolin right and then we'll move this get secret function inside there okay now we will move the ownership here okay so we've moved all the get secret and you know all the secret behavior into the secret vault okay so now we want to do is basically make this contract sort of proxy for the secret vault and this is called a factory pattern basically where a contract will create another smart contract and I guess really a factory is a case where a smart contract will create multiple new smart contracts in this case he's just gonna create one so it's not a true factory like a real factory would create multiple secret vaults and store all of them and like you could reference each of them so it's not really a factory but what it does is it it's gonna allow us to create a new smart contract and fetch the value from it it's sort of like a proxy it's honestly probably not the best demonstration of this but I think you'll get the idea and you know you could research other ways and how you might use something like this but this is definitely something that is worth understanding and solidity again so what we'll do here is accept the secret and we'll say well create a new secret like well first of all we need a place to store the address address secret vault oops vault okay and then like this will say see we'll say secret vault I'm gonna scroll up here alright secret vaults oops equals new secret vault and then pass in the secret so this is how you instantiate it basically no secret vault and the constructor accepts one argument which is the secret which will create here we'll just pass it down over create the new one so new secret vault passing the secret and then basically we'll create an instance of this like this and assign it to this underscore secret vault variable and then we'll store the secret like this will say vault address all right secret vaults secret all right perfect and then say super okay and then that will create the new secret vault with the secret in it and now the owner will be the only person that will be able to get the secret and so we can call another function from a different smart contract like this with a secret vault oops sorry equals underscore secret vault capital secret vault oops and then pass in the secret vault address all right so whenever you fetch the smart contract basically you give it the contract interface you pass in the address that tells you where it is on the blockchain and you assign a new variable I'm sorry like this okay it's a secret vault type and then we can say return secret vault get secret okay all right so let's see if it compiled that maybe smear sheer alright let's just change this where you factor this all right perfect there we go so let's compile it alright there we go we got to call super there - all right there we go all right so let's compile it and then let's deploy it so let's kill this old one all right remix we go so let's put the secret here we'll say fubar okay click deploy my contract try to see the owner all right the owner is our current address right that's we deployed it with and then we'll try to get the secret fubar awesome so now let's change to a different account here and we'll say get secret and if we look at our transaction log here our our log here we'll see that it reverted so we'll try it again and there you can see it alright so that's how you work with multiple smart contracts inside solidity that's how you you know use inheritance that's how you create new smart contracts inside of a smart contract and that's how you call other smart contract functions inside of a smart contract as well alright so I hope you like this video this is definitely stuff that you need to know if you want to become a blockchain developer alright so if you haven't already go ahead and subscribe to the channel cause like button down below and if you want to take that next step and master blockchain step-by-step from start to finish then I can show you how to do exactly that okay I'm just headin over to Dappy diversity comm forward slash bootcamp get started alright until next time thanks for watching DAP University
Info
Channel: Dapp University
Views: 89,631
Rating: undefined out of 5
Keywords: ethereum developer, ethereum solidity, dapp ethereum, ethereum app, ethereum development, ethereum dapps, ethereum application, ethereum tutorial, ethereum mist, decentralized applications, ethereum web3, dapp, ethereum contracts, solidity, programming ethereum, ethereum programming language, ethereum coding, ethereum contract, ethereum code, ethereum virtual machine, ico
Id: YJ-D1RMI0T0
Channel Id: undefined
Length: 111min 33sec (6693 seconds)
Published: Fri May 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.