Coding

Solidity Tutorial – A Full Course on Ethereum, Blockchain Development, Smart Contracts, and the EVM

  • 00:00:00 hey everybody this is Gregory from deaf
  • 00:00:01 University so I'm so excited to be on
  • 00:00:03 free code camp today to teach you how to
  • 00:00:06 become a blockchain developer I'm gonna
  • 00:00:08 take you over the shoulder and teach you
  • 00:00:09 how to write a theory of smart contracts
  • 00:00:11 with a solidity programming language for
  • 00:00:13 the blockchain and you don't even have
  • 00:00:15 to know anything about solidity or
  • 00:00:16 blockchain to follow along so I've
  • 00:00:18 compiled this multi-part tutorial series
  • 00:00:20 into one massive video for free code
  • 00:00:23 camp
  • 00:00:23 and you can follow along with the code
  • 00:00:25 examples step-by-step on my website
  • 00:00:28 there's an article that I'm gonna put a
  • 00:00:29 link to down the description below
  • 00:00:31 and finally if you're serious about
  • 00:00:32 becoming a blockchain developer you need
  • 00:00:34 to join my free training on my website
  • 00:00:36 over at Daffy diversity comm forward
  • 00:00:38 slash boot camp and I'll also put a link
  • 00:00:40 to that down in the description below
  • 00:00:42 solidity is the main programming
  • 00:00:44 language for writing smart contracts on
  • 00:00:46 the etherium blockchain it's a contract
  • 00:00:49 oriented language which basically means
  • 00:00:52 that you know smart contracts are the
  • 00:00:54 main way that you organize code and like
  • 00:00:56 store data and write all of your
  • 00:00:58 programming logic and you know it's a
  • 00:01:00 high-level language for implementing
  • 00:01:02 these smart contracts it looks a lot
  • 00:01:04 like JavaScript and you know Python and
  • 00:01:07 C++ as you see right here and it's used
  • 00:01:10 to run on the etherium virtual machine
  • 00:01:12 which is basically you know the thing
  • 00:01:15 that runs the code on the etherion
  • 00:01:16 blockchain so solidity is statically
  • 00:01:20 typed as opposed to a dynamically typed
  • 00:01:22 language and it supports stuff like
  • 00:01:24 inheritance libraries and lots of other
  • 00:01:27 stuff so enough of talking about
  • 00:01:29 solidity let's actually jump in and
  • 00:01:30 start writing some solidity code this is
  • 00:01:33 a website that allows you to write
  • 00:01:34 solidity smart contracts in your browser
  • 00:01:37 and you know it has a text editor here
  • 00:01:39 and allows you to compile them and
  • 00:01:40 deploy them and run them and that's
  • 00:01:42 exactly what I'm going to use for this
  • 00:01:44 tutorial you won't have to download
  • 00:01:46 anything in your computer you won't have
  • 00:01:47 to install anything you can just visit
  • 00:01:49 this website to use remix and will write
  • 00:01:52 the smart contracts that way so in order
  • 00:01:55 to do that I'm gonna go over here to
  • 00:01:56 this browser folder and you can see the
  • 00:01:59 file browser over here this is some
  • 00:02:01 basic smart contracts that come
  • 00:02:02 pre-loaded inside of this IDE I'm just
  • 00:02:06 going to create a new one here I think I
  • 00:02:08 can just create a new file and this will
  • 00:02:10 be a solidity file I will call it my
  • 00:02:12 contract
  • 00:02:13 and we're going to write a smart
  • 00:02:15 contract inside of here I'll do this by
  • 00:02:18 first declaring the version of solidity
  • 00:02:20 that we want to use we just do that like
  • 00:02:21 this we say pragma solidity and it means
  • 00:02:26 a caret to specify a version greater
  • 00:02:29 than the one we want to use and I'm
  • 00:02:31 gonna say version 0.4 4.24 and I'm gonna
  • 00:02:36 end each line like this with a semicolon
  • 00:02:38 and next I'm actually gonna declare the
  • 00:02:42 smart contract so before I do that I'll
  • 00:02:44 explain a little bit more about what a
  • 00:02:46 smart contract is you know smart
  • 00:02:48 contract is code that gets executed on
  • 00:02:51 the blockchain in this case this is
  • 00:02:53 gonna be the etherium blockchain since
  • 00:02:55 we're using you know solidity for the
  • 00:02:56 etherium smart contract and this smart
  • 00:02:59 contract is kind of be kind of like a
  • 00:03:01 micro service out on the web it's going
  • 00:03:03 to be accessible to everyone on the
  • 00:03:05 blockchain they'll be able to you know
  • 00:03:07 see this smart contract they'll be able
  • 00:03:09 to use it they'll be able to read and
  • 00:03:11 write data with it and actually you know
  • 00:03:13 execute any code that we write inside of
  • 00:03:16 here so it's gonna be public and that's
  • 00:03:19 kind of why I call it like a micro
  • 00:03:20 service it's more it's more than just a
  • 00:03:22 class like in an object-oriented system
  • 00:03:24 or something like that it's actually
  • 00:03:26 something that's publicly accessible so
  • 00:03:29 it's gonna look something like a class
  • 00:03:31 though because that's how we're gonna
  • 00:03:32 actually you know keep all the code
  • 00:03:35 inside of here so I'll show you how to
  • 00:03:37 do that
  • 00:03:37 we'll call will start by saying contract
  • 00:03:40 say my contract and I'm open this with
  • 00:03:45 some curly braces and it'll notice that
  • 00:03:46 the editor closes this automatically for
  • 00:03:49 us which is really nice and what I'm
  • 00:03:52 gonna do first is just show you how to
  • 00:03:54 read and write a value from a variable
  • 00:03:57 inside of here now like I said earlier
  • 00:04:01 solidity is a statically typed language
  • 00:04:04 so we're gonna actually have to declare
  • 00:04:06 the type the data type of the variable
  • 00:04:09 we want to store so in this case we're
  • 00:04:12 just gonna store string and we'll
  • 00:04:14 declare a string we'll say value now let
  • 00:04:16 me explain something about this value
  • 00:04:17 this value is going to represent you
  • 00:04:21 know a variable that belongs to this
  • 00:04:23 entire smart contract so this isn't like
  • 00:04:25 a local variable it's at
  • 00:04:27 a variable that this entire contract
  • 00:04:30 will know about and what that means with
  • 00:04:32 a smart contract is that data is
  • 00:04:35 actually going to get stored on the
  • 00:04:37 blockchain in storage so if we set this
  • 00:04:40 value we're actually gonna be writing
  • 00:04:42 data to the blockchain that's different
  • 00:04:45 from a local variable that I might just
  • 00:04:47 you know set inside of a function that
  • 00:04:48 would be you know local to that function
  • 00:04:51 scope and what you know disappear
  • 00:04:53 instantly once that function is called
  • 00:04:55 instead this value is actually going to
  • 00:04:57 be you know stored on the blockchain so
  • 00:05:00 let's create a way to us just read this
  • 00:05:04 value so I can do this by writing a
  • 00:05:06 function and solidity like this we just
  • 00:05:08 start with the function keyword and I
  • 00:05:10 say I'm just gonna call the function get
  • 00:05:12 it's gonna get this value and we'll say
  • 00:05:15 I'll just open the braces for now I'm
  • 00:05:17 gonna write some more code here in a
  • 00:05:18 second we can see some warnings coming
  • 00:05:20 up but I'll fill this out more will say
  • 00:05:23 just return value okay and that's a
  • 00:05:29 really basic way to do this I'm gonna
  • 00:05:31 add some more to this I'm gonna say this
  • 00:05:33 is also public all right which basically
  • 00:05:37 is setting the visibility of this
  • 00:05:38 function that's that's called and
  • 00:05:39 solidity is setting the visibility so
  • 00:05:41 that knows this function can be called
  • 00:05:43 by anyone who has access to the smart
  • 00:05:45 contract on the blockchain not just
  • 00:05:47 inside this contract so I'm also going
  • 00:05:51 to set our term by you say returns a
  • 00:05:54 string all right that makes our warning
  • 00:05:58 disappear down here and I just tell
  • 00:05:59 their function that we're always gonna
  • 00:06:01 return a string datatype and we know
  • 00:06:04 that's the same data type as value
  • 00:06:06 because we declared it here now I'm also
  • 00:06:08 gonna look at this warning down here
  • 00:06:09 which says this function state
  • 00:06:11 mutability can be restricted so
  • 00:06:13 basically what this is saying is in
  • 00:06:15 solidity now in this newest version
  • 00:06:17 since we're not actually changing this
  • 00:06:19 value we're not changing anything inside
  • 00:06:22 this function we want to add additional
  • 00:06:24 modifier here which just says view all
  • 00:06:26 right and that makes that warning go
  • 00:06:28 away so now I'm gonna set a new function
  • 00:06:33 or write a new function that's called
  • 00:06:34 set let's say function it's gonna allow
  • 00:06:36 us to set this value say set
  • 00:06:39 as a string I'll save value all right
  • 00:06:43 and I'm also going to you know set the
  • 00:06:45 visibility of this function when it call
  • 00:06:48 it public which means that anyone will
  • 00:06:51 be able to set this value on the
  • 00:06:52 blockchain and I'll say value equals
  • 00:06:56 this value that we're passing in all
  • 00:06:58 right let me explain that I'm using
  • 00:07:02 value as an underscore here because I
  • 00:07:05 want to differentiate between this value
  • 00:07:07 that's getting passed in and the value
  • 00:07:09 that's referenced here now inside of
  • 00:07:13 here you know this value without an
  • 00:07:15 underscore is referencing this you know
  • 00:07:18 state variable that we set up earlier
  • 00:07:19 that's actually getting stored to the
  • 00:07:21 blockchain
  • 00:07:21 and this value solidity knows that it's
  • 00:07:24 just a local variable because I passed
  • 00:07:26 it in and I you know prepended it with
  • 00:07:28 an underscore like this and also notice
  • 00:07:31 that I declared the data type of the
  • 00:07:35 function argument we need that inside
  • 00:07:37 solidity because it needs to know the
  • 00:07:39 type of the data that's getting passed
  • 00:07:41 in all right and now we have a way to
  • 00:07:42 actually read this value and set it the
  • 00:07:47 next thing I'm going to show you is how
  • 00:07:49 to set this value whenever you read
  • 00:07:52 deploy the smart contract or whenever
  • 00:07:53 it's generated it for the first time and
  • 00:07:55 I'm going to do this with the
  • 00:07:57 constructor function so if you're
  • 00:07:59 familiar with other programming
  • 00:08:00 languages where you might write you know
  • 00:08:02 classes dozen like that you might be
  • 00:08:04 familiar with a constructor function
  • 00:08:06 which is basically a function that's
  • 00:08:08 called whenever this contract is
  • 00:08:10 instantiated or in this case it's gonna
  • 00:08:12 be whatever the contract is created or
  • 00:08:14 whenever the smart contract is deployed
  • 00:08:16 to the blockchain and in the newer
  • 00:08:18 versions of solidity we do that like
  • 00:08:21 this we say constructor and we just you
  • 00:08:24 know say it's public
  • 00:08:27 and we can you know write code inside of
  • 00:08:30 this constructor that gets called
  • 00:08:31 whenever this contract is created and
  • 00:08:34 we'll say you know value we'll just set
  • 00:08:37 it as you know my value
  • 00:08:41 all right there we go so that's a
  • 00:08:45 complete smart contract that you know
  • 00:08:47 sets this value whenever it's deployed
  • 00:08:49 so it will have some sort of default
  • 00:08:50 value and then we'll have you know some
  • 00:08:53 functions that allow us to read that
  • 00:08:55 value from the blockchain and then we'll
  • 00:08:57 have us a function that allows us to
  • 00:08:58 create that value as well so now I can
  • 00:09:02 go to this tab over here and remix and I
  • 00:09:05 can compile it we can select the
  • 00:09:07 compiler version and I'm gonna say let's
  • 00:09:11 see here 0.425 will start to compile it
  • 00:09:16 and see what happens
  • 00:09:18 all right and now what I'm gonna do is
  • 00:09:23 actually run the smart contract you'll
  • 00:09:25 see some options over here I'm just
  • 00:09:27 going to say JavaScript virtual machine
  • 00:09:31 which basically what that's gonna do is
  • 00:09:33 give us a test blockchain in the browser
  • 00:09:35 so we don't have to connect to a
  • 00:09:37 blockchain of any kind we can just you
  • 00:09:38 know compile this and run it and deploy
  • 00:09:40 it inside of our browser which is pretty
  • 00:09:42 cool and it's gonna give us some you
  • 00:09:45 know free aetherium accounts over here I
  • 00:09:47 don't worry if you don't quite
  • 00:09:48 understand what all these values mean
  • 00:09:50 just yet that's okay I can explain those
  • 00:09:52 more but for now we're gonna just keep
  • 00:09:55 my contract and we're gonna deploy it
  • 00:09:58 all right there we go it's deployed so
  • 00:10:02 now what I can do is click on this
  • 00:10:05 little down arrow here and we can see
  • 00:10:07 the functions that are available to us
  • 00:10:08 in the smart contract we can see the set
  • 00:10:11 function and the get function you know
  • 00:10:13 these are the functions that we created
  • 00:10:14 over here the set function is going to
  • 00:10:16 allow us to update it and the get
  • 00:10:18 functions allow us to get it
  • 00:10:19 so I'll click get and we should probably
  • 00:10:22 see the value that we created inside the
  • 00:10:25 constructor we set it here so let's get
  • 00:10:27 it all right there we go it's my value
  • 00:10:30 and we can see some activity in this log
  • 00:10:34 down here we can see that we it was a
  • 00:10:36 call to my contract get
  • 00:10:38 and now we'll set it so I'm going to
  • 00:10:41 enter the quotations for the string and
  • 00:10:45 I'm going to say new value
  • 00:10:48 alright let's click set
  • 00:10:51 all right and we can see that worked so
  • 00:10:54 you can see the transaction history over
  • 00:10:56 here let's get the value all right it's
  • 00:10:59 new value there we go that's a complete
  • 00:11:01 smart contract that allows you to get
  • 00:11:03 instead of value on the blockchain now
  • 00:11:06 let me explain what's going on here this
  • 00:11:08 is a list of transactions that are
  • 00:11:10 occurring on the etherium blockchain so
  • 00:11:12 the etherium blockchain is made up of
  • 00:11:15 you know bundles of records that are
  • 00:11:17 chained together into blocks that are
  • 00:11:20 make up the blockchain and you know the
  • 00:11:23 basic units of all those blocks are
  • 00:11:25 these transactions so we can see
  • 00:11:28 transaction receipts we can actually
  • 00:11:31 click the down arrow here we can see all
  • 00:11:33 the details of those transaction
  • 00:11:34 receipts and I've got other videos in
  • 00:11:36 this channel that kind of talk about
  • 00:11:37 that in detail if you want to know more
  • 00:11:39 about that just feel free to look for
  • 00:11:41 those all right that's it guys that's
  • 00:11:42 how you can write your first smart
  • 00:11:44 contract and the solidity programming
  • 00:11:45 language again that's supposed to be
  • 00:11:47 just a high-level overview of solidity
  • 00:11:49 and how to get started writing your
  • 00:11:51 first smart contracts and using the
  • 00:11:53 programming language itself and using
  • 00:11:56 the remix IDE in your browser to get
  • 00:11:58 started easily without having to
  • 00:12:00 download any tools or anything like that
  • 00:12:05 the first thing I want to do in this
  • 00:12:07 video is actually update the smart
  • 00:12:09 contract code that we wrote in the last
  • 00:12:11 video to support the newest version of
  • 00:12:13 the solidity programming language which
  • 00:12:15 is 0.5 dot 1 I believe now you can
  • 00:12:20 actually see the compiler version number
  • 00:12:22 has changed and in the last video you
  • 00:12:25 know we wrote this smart contract and
  • 00:12:27 there's some actually some compilation
  • 00:12:29 errors you can see over here on the
  • 00:12:31 right so you have to update this code to
  • 00:12:33 support the newest version of the
  • 00:12:35 solidity programming language and I want
  • 00:12:37 to go I wanted to go ahead and do this I
  • 00:12:39 wanted to go ahead and support the new
  • 00:12:40 version of solidity so that when I make
  • 00:12:42 more videos in this series they'll be up
  • 00:12:45 the day you'll have the newest version
  • 00:12:46 so let's do what we need to do we can
  • 00:12:49 read the error it says basically the
  • 00:12:53 data location must be memory and that's
  • 00:12:55 actually the error in both cases so all
  • 00:12:57 we need to do in this case is actually
  • 00:12:59 just return memory here and then
  • 00:13:03 whenever we're passing in the value a
  • 00:13:06 string we actually need to put memory
  • 00:13:10 here as well all right actually that
  • 00:13:13 needs to be before value my fault and
  • 00:13:16 now we there's go away so we will select
  • 00:13:20 the newest compiler version we'll say
  • 00:13:22 let's do the latest complete version is
  • 00:13:25 0.5 dot 1 start to compile and hopefully
  • 00:13:29 the compilation will work yeah it worked
  • 00:13:31 so let's go to run and we'll deploy and
  • 00:13:36 see if it worked
  • 00:13:37 let's look at the value yep it worked
  • 00:13:41 awesome
  • 00:13:42 now let's move on to the next part of
  • 00:13:44 this tutorial where we'll actually cover
  • 00:13:46 the content for this video which is
  • 00:13:47 gonna be you know the basic data types
  • 00:13:49 and data structures and solidity and how
  • 00:13:52 you can use them when you're developing
  • 00:13:53 your own smart contracts so I will
  • 00:13:56 change some values in here to kind of
  • 00:13:58 show you what that is the first thing I
  • 00:14:00 want to talk about are you know the
  • 00:14:02 different ways we can work with through
  • 00:14:03 state variables right so the first thing
  • 00:14:05 I want to mention is you know we had
  • 00:14:07 this string value here that we had
  • 00:14:10 declared and we set it here inside this
  • 00:14:12 set function and we had this getter
  • 00:14:15 function where we read that value right
  • 00:14:18 so solidity has a shortcut where I don't
  • 00:14:21 even need this function I can actually
  • 00:14:23 just read this string value for free I
  • 00:14:27 can actually get this function for free
  • 00:14:29 take this out and just declare this
  • 00:14:32 public that's that's declaring the
  • 00:14:34 visibility of the state variable here
  • 00:14:39 actually public goes before so I'll save
  • 00:14:41 that and I'll compile it you know run it
  • 00:14:47 and we'll see that a get getter function
  • 00:14:52 called value the same name is the state
  • 00:14:54 variable was exposed to us by the smart
  • 00:14:56 contract and we can see it works just
  • 00:14:58 the same way as the other function did
  • 00:14:59 so that's a really nice way to save
  • 00:15:02 yourself reminding extra code you can
  • 00:15:03 just get that function for free exposed
  • 00:15:06 to the public interface at the smart
  • 00:15:07 contract
  • 00:15:08 whenever you store your state variables
  • 00:15:10 that way okay so the next thing I want
  • 00:15:12 to talk about inside of here is actually
  • 00:15:15 you know we can set this as a default
  • 00:15:17 value we don't necessarily have to put
  • 00:15:19 you know my value here in the
  • 00:15:21 constructor you just say this alright I
  • 00:15:24 could take this away oops take this away
  • 00:15:29 and we will run that again let's just
  • 00:15:34 clear this out deploy all right we can
  • 00:15:39 actually sorry when you compile it first
  • 00:15:43 boil it again get the value we can see
  • 00:15:48 is my value we can also set this value
  • 00:15:49 as a constant if we don't want it to
  • 00:15:51 change
  • 00:15:52 you know if we don't want to allow a
  • 00:15:54 user to set this value we want to stay
  • 00:15:56 the same
  • 00:15:56 we can just say constant all right and
  • 00:16:00 that actually declares a constant we had
  • 00:16:01 to remove this function we can't update
  • 00:16:03 this value at all so let it you won't
  • 00:16:04 let us do that so we could just do that
  • 00:16:07 I'll deploy us again and we can still
  • 00:16:10 see our values here as my value so you
  • 00:16:13 know that's how you would add a constant
  • 00:16:14 to your state variables or make your
  • 00:16:17 state variables constant now let's look
  • 00:16:19 at some other data types and solidity
  • 00:16:21 let's just change this name we'll call
  • 00:16:24 this like string value
  • 00:16:26 alright let's just deploy that we can
  • 00:16:29 see this instance here again string
  • 00:16:32 value alright it's my value now let's
  • 00:16:34 explore some other data types we can say
  • 00:16:37 boolean type will say bool a public is
  • 00:16:41 equal to I'm gonna remove this constant
  • 00:16:43 for now my pool equals true alright so
  • 00:16:50 this just accepts true or false we can
  • 00:16:52 deploy that see this my pool all right
  • 00:16:56 it's true string value is my value say
  • 00:17:00 or my string just save that just for fun
  • 00:17:04 and we can also see that you know we
  • 00:17:07 support data types like integers so
  • 00:17:11 integers can be signed or unsigned so
  • 00:17:13 what does that mean well we can do a
  • 00:17:15 default int which would be public my int
  • 00:17:20 goes 1 right so we can deploy this and
  • 00:17:25 see let's actually just clear this out
  • 00:17:29 it's alright apply this and see my int
  • 00:17:33 equal to one so the difference between
  • 00:17:35 an int and au nth or an unsigned integer
  • 00:17:43 is that an integer can be signed in a or
  • 00:17:47 sorry yeah an integer can be signed and
  • 00:17:49 a you nth can be unsigned so what does
  • 00:17:51 that mean well basically an int can be
  • 00:17:54 negative so I can do minus one all right
  • 00:17:57 and deploy that that's a sign integer it
  • 00:18:00 has a sign in front of it it can be
  • 00:18:02 positive or negative and a unit can't so
  • 00:18:05 I'll deploy that clear these out and see
  • 00:18:09 my you int all right and my int all
  • 00:18:12 right yes a negative one can work for an
  • 00:18:14 int and they can't do negatives on
  • 00:18:17 unsigned integers you can also specify
  • 00:18:19 the amount of bytes or bits in an
  • 00:18:24 integer so we can say you int 8 blick
  • 00:18:28 goes in my unit 8 sorry because like all
  • 00:18:36 right oops
  • 00:18:38 let's do a small number yes
  • 00:18:42 and because he might you an eight
  • 00:18:44 alright and so if we do you int here it
  • 00:18:48 defaults to 256 I believe you meant 256
  • 00:18:52 public my unit okay so that gives you a
  • 00:18:56 basic overview of all those basic
  • 00:18:57 datatypes i don't want to bore you with
  • 00:18:59 all the pedantic details of this kind of
  • 00:19:01 stuff but this is helpful to know i feel
  • 00:19:03 like the next thing i want to show you
  • 00:19:04 is an enum so what is an enum and how do
  • 00:19:08 you use it well an enum is basically an
  • 00:19:11 enumerated list that's gonna allow us to
  • 00:19:14 keep track of you know set list of
  • 00:19:17 things in our contract so I'll show you
  • 00:19:18 what that means so basically I can
  • 00:19:20 declare anything like this they'll say a
  • 00:19:22 new state and I'll give it some options
  • 00:19:26 here I'll say waiting ready active
  • 00:19:30 alright and this is gonna allow us to
  • 00:19:32 keep a list of these three states inside
  • 00:19:35 the smart contract I'll be able to
  • 00:19:36 reference this to see what the current
  • 00:19:37 state is and it'll give us some options
  • 00:19:40 whenever we're updating this state and
  • 00:19:42 like checking against this so I'll show
  • 00:19:45 you an example it'll actually store this
  • 00:19:46 like this will declare state public
  • 00:19:49 state this alright so basically this is
  • 00:19:53 going to give us a public we're going to
  • 00:19:57 be able to access this publicly with
  • 00:19:59 that getter so inside of here I'll do a
  • 00:20:03 constructor I'll say constructor I was a
  • 00:20:07 public as a state equals state active
  • 00:20:15 all right so I'll go ahead and set the
  • 00:20:16 default state to active right so
  • 00:20:21 actually let's do this let's do waiting
  • 00:20:23 and then I'll say I'll create a function
  • 00:20:26 to activate it yes a activate
  • 00:20:31 as a public state equals state dot
  • 00:20:37 active so we can actually choose sorry
  • 00:20:41 we can choose this value out of this um
  • 00:20:44 list and update the state to that value
  • 00:20:47 all right so whenever we call this
  • 00:20:48 function it'll change the state to the
  • 00:20:49 active state and let's get a function
  • 00:20:52 that checks to see if the state is
  • 00:20:54 active will say function is active and
  • 00:20:59 we'll say public view returns bool all
  • 00:21:06 right we'll say return state equal to
  • 00:21:09 state active sense will do inside of
  • 00:21:12 here we'll just check to see if the
  • 00:21:13 state currently is active all right so
  • 00:21:15 let's see if you have any errors looks
  • 00:21:17 good
  • 00:21:18 I will compile this alright we run
  • 00:21:21 deploy and all right let's check the
  • 00:21:25 state so is to stay active all right no
  • 00:21:27 it's false so that's that's true it's
  • 00:21:31 not active it's in waiting
  • 00:21:33 so let's actually check the state so
  • 00:21:34 state is zero so zero corresponds to the
  • 00:21:37 first item in this list 0 which is
  • 00:21:40 waiting and I'll click activate alright
  • 00:21:44 so actually called the activate function
  • 00:21:46 which you know updates the state to
  • 00:21:49 active let's see if it's active yep
  • 00:21:51 it's true and let's check the state so
  • 00:21:53 now the state is 2 which is 0 1 2 all
  • 00:21:58 right so that's an idea of how you use a
  • 00:22:00 news let's look at the next concept that
  • 00:22:03 I want to show you the next comment I'll
  • 00:22:06 show you is let's do those two structs
  • 00:22:09 so structs are basically a way for you
  • 00:22:11 to define your own data structures
  • 00:22:14 inside solidity well do that like this
  • 00:22:16 we'll say what create a struct right
  • 00:22:20 here we'll call it person this is a
  • 00:22:21 struct person and we'll give it some
  • 00:22:26 look at some some characteristics with a
  • 00:22:29 string first name and string a last name
  • 00:22:37 okay so basically what's going on here
  • 00:22:40 is this allows us to basically model you
  • 00:22:42 know our own sort of arbitrary data I'm
  • 00:22:44 just gonna model a person with this
  • 00:22:45 truck that's gonna have a few attributes
  • 00:22:47 it's gonna have a first name attribute
  • 00:22:49 that's going to be a string and a last
  • 00:22:50 name attribute that's also going to be a
  • 00:22:52 string and we can put these struts
  • 00:22:54 inside of like a raise and mappings and
  • 00:22:56 things like that I'll show you that here
  • 00:22:57 in a minute inside this video but for
  • 00:23:00 now just know that we declare that we've
  • 00:23:02 defined this new data type person inside
  • 00:23:05 the smart contract with a struct that's
  • 00:23:07 gonna have a first name and a last name
  • 00:23:09 okay so let's actually create a function
  • 00:23:15 that allows us to instantiate a new
  • 00:23:17 person create a new one and we'll
  • 00:23:19 actually keep track of a bunch of these
  • 00:23:22 person's trucks inside of a people array
  • 00:23:26 all right so let's do that like this
  • 00:23:29 let's say let's keep track of a bunch of
  • 00:23:33 different persons that we can create new
  • 00:23:34 ones
  • 00:23:35 we'll say person so being array public
  • 00:23:39 people all right so let's see what's
  • 00:23:41 happening here people is the state
  • 00:23:43 variable here and its inner its public
  • 00:23:45 so we can read it outside the smart
  • 00:23:47 contract and it's an array here of these
  • 00:23:53 data type person all right so we declare
  • 00:23:56 the data type first justperson which we
  • 00:23:57 defined here the struct it's an array
  • 00:24:00 that you know contains these person's
  • 00:24:02 trucks it's public and it's stored up
  • 00:24:05 with the people very state variable so
  • 00:24:07 we can add people to this array or
  • 00:24:11 person's trucks to this array like this
  • 00:24:13 okay the function is a function add
  • 00:24:15 person it's a string this is a solidity
  • 00:24:19 5 update memory or 0.5 sorry a first
  • 00:24:24 name and then we'll say string memory
  • 00:24:30 last name was it public
  • 00:24:35 and we'll do this inside here so that
  • 00:24:37 people push so push is you know a
  • 00:24:42 function we can call an arrays to add a
  • 00:24:44 new item at the the 2d array and we'll
  • 00:24:48 push a new instance of this person
  • 00:24:51 struct so we can create an instance like
  • 00:24:56 this we just call person right and we
  • 00:24:59 pass in these attributes so I say first
  • 00:25:01 name
  • 00:25:03 oops sorry name and last name
  • 00:25:08 all right
  • 00:25:10 and what we can do here is y-yeah we've
  • 00:25:16 added it it's going to do it so let's
  • 00:25:19 just let's just compile this and run it
  • 00:25:22 deploy this all right so let's see what
  • 00:25:27 happens so people it's going to add the
  • 00:25:31 person
  • 00:25:32 and person I'm sorry see with first name
  • 00:25:37 the first name will be DAP last name
  • 00:25:39 would be University
  • 00:25:41 well a person okay awesome so that
  • 00:25:45 didn't give us any errors now how do we
  • 00:25:46 read this person out of here that's a
  • 00:25:48 good question so we added this person to
  • 00:25:51 an array but this function you know
  • 00:25:55 whatever we call this person or this
  • 00:25:57 artist people function it's not gonna
  • 00:25:59 return the entire array of people that's
  • 00:26:02 because this is you know we don't know
  • 00:26:05 the size of this arrays and solidity and
  • 00:26:07 we have an array of unknown size it's
  • 00:26:11 not going to return the entire array so
  • 00:26:13 basically we need to reference the
  • 00:26:15 person by ID so whenever we get this
  • 00:26:17 function it expects an argument which is
  • 00:26:19 gonna be the index of the person inside
  • 00:26:22 this array so for example we know we
  • 00:26:25 added one person in there so we will add
  • 00:26:27 people and we'll say zero all right
  • 00:26:29 there you go
  • 00:26:30 DAP University so what happens if we
  • 00:26:34 reference you know a person that doesn't
  • 00:26:36 exist a person you know a one people
  • 00:26:40 it's gonna give us an error it's an in
  • 00:26:43 doubt opcode error that's because
  • 00:26:44 there's no one in there at position one
  • 00:26:46 so how how do we like you know how do we
  • 00:26:50 fix that problem just keep track of that
  • 00:26:51 like this so basically we would keep an
  • 00:26:54 internal count of the people we'd say
  • 00:26:56 something like this you int 256 people
  • 00:26:59 count okay and that's gonna basically
  • 00:27:02 have this a counter cash that will
  • 00:27:04 increment every time a person to added
  • 00:27:06 so we can say something like people
  • 00:27:10 count plus equals 1 all right let me do
  • 00:27:17 that after I guess
  • 00:27:20 all right say then deploy
  • 00:27:24 and now we can actually make this public
  • 00:27:28 it was sighs before
  • 00:27:33 that's right again
  • 00:27:37 so now we can add the person tap
  • 00:27:40 University
  • 00:27:45 at person and we could see people count
  • 00:27:48 is one so we would do one – that people
  • 00:27:52 alright DAP University we do one – that
  • 00:27:56 because it's zero based index which
  • 00:27:58 means the first item you know as an
  • 00:28:00 index zero and the second ones that
  • 00:28:02 index one etc etc the next thing I want
  • 00:28:04 to show you is how to do a similar
  • 00:28:06 concept here but model it a little
  • 00:28:08 differently so instead of using an array
  • 00:28:10 we're actually gonna use a mapping so
  • 00:28:12 what is a mapping a mapping is basically
  • 00:28:15 like an associative array so instead of
  • 00:28:18 having you know just an array and we
  • 00:28:22 know the each index of the item in the
  • 00:28:23 array we can actually associate a – of
  • 00:28:25 values so that's let's look a hash table
  • 00:28:28 or a hash map or a hash and other
  • 00:28:31 programming languages that you might be
  • 00:28:32 familiar with or basically you can
  • 00:28:34 reference things by you know a key value
  • 00:28:37 its key key value pair so instead of
  • 00:28:39 having people like this instead of being
  • 00:28:43 an array I'm gonna change it down here
  • 00:28:48 I'm gonna say this is gonna be mapping
  • 00:28:52 mapping you int person
  • 00:28:58 okay so you int is going to be the key
  • 00:29:03 and the person's going to be a value so
  • 00:29:04 this mapping is like I said an
  • 00:29:05 associative array that's going to take a
  • 00:29:07 key value pair the key is going to be an
  • 00:29:09 unsigned integer we'll treat this like
  • 00:29:11 an ID so it'd be kind of like our
  • 00:29:12 database lookup where we have an ID and
  • 00:29:15 then a record so this is gonna return
  • 00:29:17 kind of like a record like a person
  • 00:29:19 record or a person struct in this case
  • 00:29:21 it'll be public and it'll be people and
  • 00:29:25 it is kind of a fair comparison to
  • 00:29:26 compare this to a database because you
  • 00:29:29 know we're watching kind of is a big
  • 00:29:31 database and these trucks are you know
  • 00:29:32 getting stored and storage on the
  • 00:29:34 blockchain with this mapping and is a
  • 00:29:36 person string first name last name it
  • 00:29:39 will do the add person so I mean you
  • 00:29:41 modify this function I will still think
  • 00:29:42 the first name will still look the last
  • 00:29:44 name but we're going to put this in the
  • 00:29:45 mapping instead of the array some of
  • 00:29:49 these people count back up here we're
  • 00:29:51 gonna want to keep track of this they're
  • 00:29:53 stuck we did earlier so we know how many
  • 00:29:55 people are in this mapping but we'll add
  • 00:29:57 it to the mapping like this we'll say
  • 00:29:59 people people count this will be your ID
  • 00:30:05 so if it starts at zero which it will do
  • 00:30:08 this all right people count plus equals
  • 00:30:11 one that'd be the new ID of the person
  • 00:30:15 that's going to go in here
  • 00:30:16 so this will be person number one the
  • 00:30:18 first time we add this we'll say this is
  • 00:30:20 going to be a new person it'll be we'll
  • 00:30:24 give it an we'll give this an ID we'll
  • 00:30:27 say you nth
  • 00:30:31 i D okay people count
  • 00:30:36 and then we'll say first name
  • 00:30:41 and then last name
  • 00:30:45 all right and we'll take this out all
  • 00:30:48 right let's see if that worked
  • 00:30:49 clear this out before the contract
  • 00:30:56 so
  • 00:30:59 first name is DAP University
  • 00:31:04 that person okay so see people count is
  • 00:31:09 one now and we'll go to people this time
  • 00:31:11 it won't be based on the array index
  • 00:31:14 it'll actually be on the ID so we can
  • 00:31:15 actually reference person 1 all right
  • 00:31:18 we'll do people 1 there we go
  • 00:31:22 deputy bursting so let me explain a
  • 00:31:25 couple more things about structs while
  • 00:31:26 we're here you know we need to keep
  • 00:31:28 track of this people count because
  • 00:31:30 there's no way to know the size this
  • 00:31:32 mapping and solidity basically any key
  • 00:31:35 that doesn't have a value set for it's
  • 00:31:37 going to turn a default value which is
  • 00:31:38 going to be like an empty struct here so
  • 00:31:42 if I do like you know person no 10
  • 00:31:46 or like 909 people seems you can return
  • 00:31:49 default values so or the default values
  • 00:31:52 for the struct person the ID for a you
  • 00:31:55 int is gonna be 0 and then these blank
  • 00:31:57 strings for first name and last name so
  • 00:31:59 that's why we need to have a person
  • 00:32:00 count so if you were gonna use this like
  • 00:32:01 as a data storage and you want to like
  • 00:32:04 show all the people or all persons and
  • 00:32:07 you're in your people mapping and your
  • 00:32:09 app or something like that ring for the
  • 00:32:10 smart contract you want to know that how
  • 00:32:13 many are actually in there and you want
  • 00:32:14 to do that with a counter cash so that
  • 00:32:17 way you know if you wanted to basically
  • 00:32:19 like the same thing with those variable
  • 00:32:22 sized arrays like if you wanted to get
  • 00:32:24 all the items you need to first make a
  • 00:32:26 call to find out how many there are and
  • 00:32:28 that's what to use the counter cash for
  • 00:32:30 and then you basically create a loop to
  • 00:32:32 read out each one one by one so yeah
  • 00:32:36 it's it can be kind of a pain but that's
  • 00:32:39 sort of just the state of things and
  • 00:32:40 what you have to do
  • 00:32:44 I'm gonna go ahead and pick up where we
  • 00:32:46 left off in the last video I'm gonna use
  • 00:32:48 this you know my contract that we built
  • 00:32:50 where we added a person person struct to
  • 00:32:53 this mapping of people if you haven't
  • 00:32:55 seen that last video go ahead check that
  • 00:32:56 out
  • 00:32:57 it's not necessary but you can probably
  • 00:32:59 follow along with this video if you want
  • 00:33:00 to what I'm gonna do now is show you a
  • 00:33:04 little more about these modifiers like
  • 00:33:05 public and things like that I think we
  • 00:33:08 talked about that in the first video but
  • 00:33:09 I'm gonna go just a little further so
  • 00:33:11 what I can do is you know show you
  • 00:33:13 another modifier which is basically like
  • 00:33:16 internal so that's different from public
  • 00:33:19 you know public is a function that can
  • 00:33:21 be called you know the public interface
  • 00:33:23 at the smart contract you can see you
  • 00:33:25 know the public functions listed here on
  • 00:33:26 the side so going create a private
  • 00:33:28 function or an internal function excuse
  • 00:33:30 me you'll say function we're just going
  • 00:33:32 to take this people count and wrap it in
  • 00:33:34 its own function to show you how that
  • 00:33:35 works
  • 00:33:36 we'll say increment counts we'll just
  • 00:33:42 make this a function and we can call it
  • 00:33:43 internal that's a different modifier I
  • 00:33:46 will just take this people account I'll
  • 00:33:48 paste it inside of here alright and
  • 00:33:51 we'll just say increment count call that
  • 00:33:54 function and we'll run it
  • 00:33:59 see the smart contract will add the
  • 00:34:00 person will say DAP oops
  • 00:34:02 DAP University
  • 00:34:07 and I'll click add person and there we
  • 00:34:10 go it incrementing the count you can see
  • 00:34:13 the people count has been changed you'd
  • 00:34:15 say Joe hello
  • 00:34:19 and the cow has changed so that's a way
  • 00:34:22 that you can you know use other
  • 00:34:23 functions that are gonna be internal and
  • 00:34:25 we can see this increment count function
  • 00:34:26 isn't added to this list over here it's
  • 00:34:29 not exposed to you know things outside
  • 00:34:32 of the smart contract external collars
  • 00:34:34 can't use it so that's an example of you
  • 00:34:37 know other you know types of visibility
  • 00:34:39 of functions and solidity let's talk
  • 00:34:41 about function modifiers so we can add
  • 00:34:44 you know more you know words and terms
  • 00:34:47 to the end of this function to change
  • 00:34:48 how it behaves and I'll show you an
  • 00:34:51 example so we're gonna create our own
  • 00:34:54 custom modifier actually inside of this
  • 00:34:56 smart contract so that only certain
  • 00:34:58 people can call this add person function
  • 00:35:01 all right so I'm gonna do is basically
  • 00:35:04 you know make this smart contract have
  • 00:35:08 an owner or like an admin and we're
  • 00:35:11 gonna say the only the owner can call
  • 00:35:13 this add person function and any other
  • 00:35:16 account connected to the network
  • 00:35:18 you know whenever they try to you know
  • 00:35:20 add a person they won't be able to and
  • 00:35:22 that'll show you how we can add an extra
  • 00:35:24 modifier to this function to make that
  • 00:35:26 happen basically we'll just add a
  • 00:35:27 modifier that's the only the owner of
  • 00:35:29 this smart contract can do that so first
  • 00:35:32 we need to actually it'll look like this
  • 00:35:35 will say only owner that's what the
  • 00:35:39 modifier would look like now this only
  • 00:35:41 owner motor fire doesn't exist yet so
  • 00:35:43 let's go ahead and create it alright so
  • 00:35:47 we'll do that like this first we need to
  • 00:35:49 keep track of an owner we'll say the
  • 00:35:51 owner is you know address
  • 00:35:55 I'm not sure if we talked about this in
  • 00:35:57 the data types video but you know an
  • 00:35:59 address is a data type in size of
  • 00:36:01 solidity you know I can address that's
  • 00:36:03 on the network and account so this will
  • 00:36:05 be the address we're just gonna declare
  • 00:36:07 it here we're not gonna set it just yet
  • 00:36:08 we can set it like this we can do it
  • 00:36:12 inside the constructor well actually
  • 00:36:13 nothing to that just yet so that will be
  • 00:36:16 the owner we basically just create a
  • 00:36:18 modifier like this we say modifier it's
  • 00:36:21 the only owner it's like a function and
  • 00:36:26 what we'll do inside of here is write
  • 00:36:28 the logic that makes sure that you know
  • 00:36:31 whoever you know calling the smart
  • 00:36:33 contract is the owner so that's what
  • 00:36:35 we're defining you're only owner and
  • 00:36:36 only owner so how do we do that how do
  • 00:36:39 we say the person who's calling this
  • 00:36:41 function is the owner of this smart
  • 00:36:43 contract well we're going to compare it
  • 00:36:44 to this owner right here but how do we
  • 00:36:47 know who's calling the function well
  • 00:36:49 solidity has a global keyword called MSG
  • 00:36:53 which basically stands for the function
  • 00:36:55 metadata that's passed in and we're not
  • 00:36:58 actually going to pass in any metadata
  • 00:36:59 here it's going to be implied so
  • 00:37:02 basically we have access to msu got
  • 00:37:05 sender and this is basically a special
  • 00:37:08 you know thing inside solidity that
  • 00:37:10 tells us the account you knows address
  • 00:37:13 who called the function and basically we
  • 00:37:18 can just say you know is this person the
  • 00:37:21 owner we can just you know compare
  • 00:37:23 equality of the person who's calling the
  • 00:37:25 function with this owner that we're
  • 00:37:26 gonna store here in a second all right
  • 00:37:28 and if they're not we actually want to
  • 00:37:31 throw an error all right so this is
  • 00:37:35 going to show you another concept and
  • 00:37:36 solidity about error handling so we can
  • 00:37:40 throw an error and solidity like you
  • 00:37:42 know I think earlier we had some errors
  • 00:37:44 happen like here's an error we actually
  • 00:37:45 want to trigger an error if this person
  • 00:37:48 you know it's not the owner we want to
  • 00:37:51 we want to revert the transaction now if
  • 00:37:53 you that like this we say require
  • 00:37:56 sorry Ms you got senders equal to the
  • 00:38:00 owner so basically anything inside of
  • 00:38:02 this require if it evaluates to true
  • 00:38:07 then this passes if it evaluates to
  • 00:38:10 false then this will throw an error we
  • 00:38:13 basically are saying require that
  • 00:38:15 whatever you put inside of here is true
  • 00:38:17 all right and then after this we can
  • 00:38:20 basically just do this all right
  • 00:38:23 and now our only only Audemar fire is
  • 00:38:27 complete
  • 00:38:28 it's basically now we have this modifier
  • 00:38:30 that's you know defined here and we add
  • 00:38:32 it here and basically say if the person
  • 00:38:35 who is you know calling this function is
  • 00:38:37 the owner then you know we can actually
  • 00:38:39 run this function and they're not we're
  • 00:38:41 going to you know create a failure we're
  • 00:38:44 going to revert the transaction and also
  • 00:38:46 whoever's doing this you know whatever
  • 00:38:48 like this code won't run so they won't
  • 00:38:51 pay this gas fee all right so now we
  • 00:38:54 need to set the owner and I'm just going
  • 00:38:56 to do that inside a constructor we'll
  • 00:38:58 use msg sender as well say function
  • 00:39:01 instructor
  • 00:39:03 and we'll just open this curly braces
  • 00:39:06 and we'll say owner equals to MSG SSRI
  • 00:39:10 msg sender sorry I don't need the
  • 00:39:14 function keyword here
  • 00:39:17 and we all seem to make this public all
  • 00:39:21 right so I'll save that so whenever you
  • 00:39:23 deploy the smart contract like that you
  • 00:39:25 know this constructor gets run and the
  • 00:39:26 MSG dot sender is actually the account
  • 00:39:28 that deploys the smart contract and
  • 00:39:30 they're gonna get set to this owner
  • 00:39:32 state variable write this address owner
  • 00:39:34 and basically that same you know address
  • 00:39:38 is the only person that's gonna be able
  • 00:39:39 to call this add person function all
  • 00:39:42 right and if they yeah this code will
  • 00:39:47 run and we will add a person and if we
  • 00:39:50 switch accounts to some other account
  • 00:39:51 over here then it won't work so let's
  • 00:39:53 just try that I deploy this
  • 00:39:58 we will you know deploy with this
  • 00:40:01 account so let's change to that account
  • 00:40:03 and we'll add the person actually I
  • 00:40:05 don't know if it deployed that a counter
  • 00:40:07 now let's try it again
  • 00:40:08 so make sure on the first account will
  • 00:40:10 deploy all right we'll add a person will
  • 00:40:13 say adapt University
  • 00:40:19 I had the person all right let's see
  • 00:40:21 here all right it worked
  • 00:40:22 your people count as one now we'll say
  • 00:40:26 you know Joe Blow and will change
  • 00:40:29 accounts to this one don't try to call
  • 00:40:32 it again add person and we see as fails
  • 00:40:36 and the people count has not changed at
  • 00:40:38 all so it worked we've kept track of the
  • 00:40:41 owner and we say only owner can do it
  • 00:40:43 now we're here I'm gonna show you a
  • 00:40:45 little code formatting that I like to do
  • 00:40:47 sometimes this is getting kind of long
  • 00:40:48 and sometimes you know if you like me
  • 00:40:50 and you have a text editor with a whole
  • 00:40:52 bunch of panes open I'd to keep my
  • 00:40:55 columns kind of short sometimes
  • 00:40:56 especially with solidity and it kind of
  • 00:40:58 makes it easier to maintain these
  • 00:40:59 functions when the arguments are getting
  • 00:41:01 long and using git and things like that
  • 00:41:03 sometimes I will just break these up
  • 00:41:05 like this
  • 00:41:07 all right and then I'll actually put the
  • 00:41:09 modifiers if you have a bunch of
  • 00:41:11 modifier sometimes this makes it easier
  • 00:41:12 to read and also when I'm writing my own
  • 00:41:15 salinity source code I don't use two
  • 00:41:17 spaces I don't use four but yeah that's
  • 00:41:21 sometimes how I break these functions up
  • 00:41:22 so I can see the function name and then
  • 00:41:24 the arguments then I can see the
  • 00:41:26 modifiers and then I can actually see
  • 00:41:28 the code that gets executed inside here
  • 00:41:30 and I can make your smart contracts kind
  • 00:41:32 of long but sick and me sometimes this
  • 00:41:36 will catch you if you have too many and
  • 00:41:38 if you have three visibility and
  • 00:41:40 modifiers on here sometimes it can be
  • 00:41:41 kind of tricky so I like doing this
  • 00:41:43 alright so next I'm gonna show you how
  • 00:41:46 to work with time and solidity okay so
  • 00:41:50 what we can do is basically compare time
  • 00:41:52 and solidity and instead of this saying
  • 00:41:56 only owner can do this let's say you can
  • 00:41:59 only call this function if a certain
  • 00:42:01 time has passed so let's pretend like
  • 00:42:04 you know this is a contract that is only
  • 00:42:07 open at a certain time so we'll say
  • 00:42:09 instead of only owner will say only
  • 00:42:11 while open only while open all right and
  • 00:42:15 this opening state is going to be
  • 00:42:19 determined by a time
  • 00:42:20 so once we've passed a certain time in
  • 00:42:22 history in the future then we'll let you
  • 00:42:26 call this function if it's before that
  • 00:42:27 we won't do it so that's really useful
  • 00:42:30 if like you're building a crowd sale or
  • 00:42:31 something like that that has an opening
  • 00:42:33 time ranking I see oh smart contract and
  • 00:42:35 you say hey we can only let you
  • 00:42:37 contribute ether like after you know the
  • 00:42:39 first of the month
  • 00:42:40 well you just figure out what time that
  • 00:42:41 is and say hey you know if you make a
  • 00:42:43 contribution before then we'll throw an
  • 00:42:44 error so I'll say only while open all
  • 00:42:50 right
  • 00:42:50 and instead of requiring that MSG that
  • 00:42:54 Center is the owner
  • 00:42:56 all right well actually gonna just take
  • 00:42:57 this out
  • 00:42:59 we're gonna say we're going to make sure
  • 00:43:04 that the current time is in the future
  • 00:43:08 beyond a certain like opening time so
  • 00:43:11 let's do you int 256 opening time all
  • 00:43:17 right
  • 00:43:19 so how do we do this well we need to set
  • 00:43:22 this opening times somehow alright so
  • 00:43:26 the opening time is actually expressed
  • 00:43:29 in seconds and you know inside solidity
  • 00:43:32 or these unit time Sandra expressed in
  • 00:43:34 seconds inside solidity and you know
  • 00:43:36 seconds of what well it's it's epic time
  • 00:43:39 which if you're not familiar with that
  • 00:43:40 it's a concept in computer science
  • 00:43:44 that's like the epic is a is a specific
  • 00:43:47 time I can't remember the actual date
  • 00:43:48 it's like a date in the sixties or
  • 00:43:50 seventies like that and basically we
  • 00:43:54 just add seconds since that point in
  • 00:43:56 time of history so this is the current
  • 00:43:58 epoch timestamp is this many seconds
  • 00:44:01 since the epoch time I guess she's
  • 00:44:03 probably a link here we can read more
  • 00:44:04 about that yeah the UNIX epoch clock
  • 00:44:07 yeah I'm sure you can get on Wikipedia
  • 00:44:10 and figure all that out but basically
  • 00:44:12 this is the current epoch time is
  • 00:44:14 changing so we use these seconds values
  • 00:44:16 so this is like the current you know
  • 00:44:18 epoch timestamp will say this alright so
  • 00:44:22 this is how we could set this and it's
  • 00:44:23 you know you went 256 it's gonna store
  • 00:44:25 this big number of seconds since that
  • 00:44:27 time stamp and that's gonna be our
  • 00:44:29 opening time and so how do we compare
  • 00:44:31 that to now so how do you get now and
  • 00:44:33 solidity well there's no perfect way to
  • 00:44:35 do it but the best way is to get the
  • 00:44:38 current blocks timestamp all right so
  • 00:44:42 how do we do that well just like MSG
  • 00:44:45 there's a global variable and getting's
  • 00:44:47 and solidity called block dot and we can
  • 00:44:51 say block timestamp all right and we can
  • 00:44:55 say is the block dot timestamp is it
  • 00:44:58 greater than or equal to the opening
  • 00:45:02 time
  • 00:45:03 all right so basically if it's after the
  • 00:45:09 opening time we're gonna let you call
  • 00:45:11 this function and if it's not then we
  • 00:45:15 won't all right so let's give this a try
  • 00:45:17 I'm gonna update this timestamp let's
  • 00:45:20 refresh it you'll see this is the
  • 00:45:21 current one I'll paste this in here so
  • 00:45:24 15 I'm gonna add 60 seconds so a minute
  • 00:45:29 so I'm gonna deploy this and it will be
  • 00:45:33 it shouldn't work right now so we can
  • 00:45:35 try to call add person now I will try to
  • 00:45:38 add DAP University
  • 00:45:42 okay well add person and we'll see that
  • 00:45:45 it reverted okay and now we can check
  • 00:45:47 the timestamp we're not there yet so
  • 00:45:49 I'll just pause the video wait for this
  • 00:45:50 to past 75 I think that's the actual
  • 00:45:52 time
  • 00:45:53 yeah 75 all right I'll pause the video
  • 00:45:56 pause the video give it as plenty of
  • 00:45:59 time or pass the timestamp you can see
  • 00:46:00 the 793 and we're at 775
  • 00:46:04 so let's deploy it again oh actually
  • 00:46:05 it's not deployed again let's just do
  • 00:46:07 Joe Blow a person and it worked so
  • 00:46:12 that's how you can use time and solidity
  • 00:46:16 you can get the current time and set a
  • 00:46:17 time value with seconds and so that's
  • 00:46:20 what I'm gonna call today guys
  • 00:46:21 I hope you all liked this video let me
  • 00:46:23 know how you're enjoying these solidity
  • 00:46:25 videos let me know if I missed anything
  • 00:46:26 or there something you want to learn or
  • 00:46:28 if you're just not liking then that's
  • 00:46:29 okay too just let me know down the
  • 00:46:31 comments section below
  • 00:46:35 let's use the same my contract example
  • 00:46:37 that we've been you know using
  • 00:46:38 throughout this tutorial series and you
  • 00:46:41 don't necessarily have to have been
  • 00:46:42 following along with every video in this
  • 00:46:43 series you can kind of pick back up with
  • 00:46:45 this one probably see yeah let's go
  • 00:46:47 ahead and jump in and start programming
  • 00:46:49 what I'm going to show you first is how
  • 00:46:52 to write a function that accepts ether
  • 00:46:55 so basically like how can we send ether
  • 00:46:58 in when we call function and do stuff
  • 00:47:01 with it so I'll show you how to do that
  • 00:47:02 first we'll create a function called by
  • 00:47:06 token say by token and this function is
  • 00:47:11 gonna kind of emulate what happens in an
  • 00:47:13 IC o—- like a crowd sale or a pre sale
  • 00:47:16 or something like that on aetherium when
  • 00:47:18 you're buying a RC 20 tokens I'm not
  • 00:47:20 gonna like code out an entire ERC 20
  • 00:47:22 tokens smart contract right here or you
  • 00:47:24 know cut out a crowd sale smart contract
  • 00:47:26 I've got several other tutorials that
  • 00:47:28 show you how to you know code romo
  • 00:47:29 cryptocurrency on etherium with you know
  • 00:47:31 bill ter c 20 tokens step-by-step and a
  • 00:47:33 real world crowd sale things like that
  • 00:47:35 so you can check out those other videos
  • 00:47:36 if you're interested but what we're
  • 00:47:39 gonna do is basically create this
  • 00:47:41 function called buy token it's gonna
  • 00:47:43 simulate what happens it's not gonna do
  • 00:47:45 everything but it'll least give you an
  • 00:47:47 idea of how that works so we'll call
  • 00:47:49 this you know buy token and inside here
  • 00:47:53 what we basically want to do is buy a
  • 00:47:56 token and we also want to send ether to
  • 00:48:01 the wallet so what's going to happen is
  • 00:48:05 someone's going to call this function
  • 00:48:07 and they're gonna send ether when they
  • 00:48:10 call this function and when they do
  • 00:48:11 we're gonna issue them a token and then
  • 00:48:14 the wallet is actually going to receive
  • 00:48:15 the ether that they sent and when they
  • 00:48:18 call this function so I'll show you how
  • 00:48:19 to do that first I'm gonna create a way
  • 00:48:23 to track the tokens and basically all
  • 00:48:25 this is gonna do is you know track this
  • 00:48:29 person's balance so why use a mapping
  • 00:48:31 for that I'll say mapping and again if
  • 00:48:35 you didn't check that out for the other
  • 00:48:35 videos the mapping is like an
  • 00:48:37 associative array where we have key
  • 00:48:38 value pairs
  • 00:48:39 so we'll say an address will be the key
  • 00:48:42 and the value will be an unsigned
  • 00:48:44 integer
  • 00:48:46 all right and this will be public all
  • 00:48:50 right this will give us a function for
  • 00:48:51 free that allow us to read this value
  • 00:48:53 and we'll just call this balances so
  • 00:48:56 what we'll do and when we buy a token
  • 00:48:59 and we'll basically just say balances of
  • 00:49:02 whoever called this function so if you
  • 00:49:04 remember from the last videos in this
  • 00:49:06 series we we get that value with MSG
  • 00:49:09 that's cinder this is the account that's
  • 00:49:11 calling this function and we will just
  • 00:49:14 increment this count by one now this is
  • 00:49:17 not really fancy you know we're
  • 00:49:20 basically just saying whenever you call
  • 00:49:21 this function you're gonna buy one token
  • 00:49:23 we're not using a rate of redemption or
  • 00:49:25 anything like that this is just to kind
  • 00:49:27 of show you how this works this is not
  • 00:49:28 very sophisticated but basically they'll
  • 00:49:32 just increment your balance you know
  • 00:49:34 inside this token contract by one
  • 00:49:38 whatever we do that we want to transfer
  • 00:49:41 funds that are sent in with this smart
  • 00:49:43 contract to a wallet all right so how do
  • 00:49:47 we do that well first we keep track of a
  • 00:49:49 wallet
  • 00:49:50 we'll just declare a state variable here
  • 00:49:52 it'll say address wallet
  • 00:49:55 and we want to send funds to this wallet
  • 00:49:58 okay it would do that like this and I'm
  • 00:50:01 actually gonna show you something in a
  • 00:50:02 minute that's gonna be a solidity 0.5
  • 00:50:04 update so if you've been using previous
  • 00:50:07 version solidity we have to make a
  • 00:50:08 change here I'll go back to that in a
  • 00:50:09 minute but anyways let's transfer funds
  • 00:50:12 to this wallet we'll do that like this
  • 00:50:14 so we'll say wallet transfer and we want
  • 00:50:19 to transfer in the funds that are sent
  • 00:50:22 by this function
  • 00:50:23 sorry transfer the funds that are sent
  • 00:50:25 in by this function to this wallet so
  • 00:50:27 how do we do that well how do we
  • 00:50:29 actually know what the the value is it's
  • 00:50:33 getting sent in well just like MSG
  • 00:50:35 that's sender you know MSG has another
  • 00:50:38 property called value so we can say MSG
  • 00:50:41 value alright and that's gonna tell us
  • 00:50:46 exactly how much ether or whey is sent
  • 00:50:50 in by the person who's sitting this
  • 00:50:52 function so I'll show you how that works
  • 00:50:55 so well that's going to do is take the
  • 00:50:58 each of that sent and with this you know
  • 00:51:00 function call and transfer it to this
  • 00:51:02 wallet right here now there's this is
  • 00:51:05 not complete there's a lot of things
  • 00:51:08 that actually need to change in order
  • 00:51:09 for this to work properly you can see
  • 00:51:11 these X's over here these you know
  • 00:51:13 compiler errors so the first thing we
  • 00:51:15 need to do is set a wallet let's do that
  • 00:51:19 inside a constructor we'll do that we'll
  • 00:51:20 ever to deploy the smart contract
  • 00:51:22 so we'll say constructor
  • 00:51:26 we'll say address wallet was a public
  • 00:51:34 and we'll say while it is just equal to
  • 00:51:37 the wall we pass in
  • 00:51:39 all right so we're still getting errors
  • 00:51:41 now let's see what they are the first
  • 00:51:43 thing you need to do in order to make
  • 00:51:45 this function except ether well actually
  • 00:51:49 first we need to make it public so that
  • 00:51:51 people can call it and I say public all
  • 00:51:54 right so now people will actually able
  • 00:51:56 to call this function and send ether n
  • 00:51:58 stuff like that and in order for people
  • 00:52:01 to in order for this to accept ether we
  • 00:52:04 have to add another modifier here which
  • 00:52:06 is payable all right and if we don't do
  • 00:52:10 that this won't allow us to send ether
  • 00:52:12 in with the transaction so this is how
  • 00:52:15 we declare that this function will
  • 00:52:16 accept ether okay now here's a new
  • 00:52:19 solidity update it requires explicitness
  • 00:52:22 whenever you're declaring an address
  • 00:52:25 that can accept either in setup smart
  • 00:52:27 contract and we do that like this we say
  • 00:52:30 address payable wallet all right
  • 00:52:35 and then likewise we say address payable
  • 00:52:42 wallet whenever we pass this function in
  • 00:52:45 the constructor all right so that should
  • 00:52:52 be a working implementation and we want
  • 00:52:53 to see so let's deploy a smart contract
  • 00:52:56 and try it out I will you know take one
  • 00:53:01 of these addresses and it will take the
  • 00:53:03 second address here in the list and
  • 00:53:05 we'll use that as the wallet will
  • 00:53:06 actually check its balance here a second
  • 00:53:09 so we'll deploy smart contract we need
  • 00:53:11 to pass in a wallet whatever deploy just
  • 00:53:13 paste this in good deploy all right
  • 00:53:15 looks like we're successful let's try to
  • 00:53:18 do the by token function okay she's
  • 00:53:20 gonna buy one token for us so I'll make
  • 00:53:22 sure we've got the first account the
  • 00:53:23 list selected by token all right looks
  • 00:53:26 like I'm successful let's check the
  • 00:53:28 balance of this oops actually paste it
  • 00:53:32 in the wrong address paste it in the
  • 00:53:34 second address so let's try the balance
  • 00:53:36 again all right it's one all right now
  • 00:53:38 all that did was actually transfer you
  • 00:53:41 know all that did was by token it didn't
  • 00:53:43 actually transfer ether to the wallet
  • 00:53:46 and that's we want to show so in order
  • 00:53:48 to like sand ether in and actually
  • 00:53:50 transfer funds to the wallet we would do
  • 00:53:52 that like this we change this to ether
  • 00:53:54 I'm gonna send one ether in with this
  • 00:53:57 transaction whenever we call the by
  • 00:53:58 tokens function we'll do after this
  • 00:54:00 account and we'll click by token all
  • 00:54:05 right boom so let's check the balance
  • 00:54:07 again all right the balances – now let's
  • 00:54:11 see what happened we can see that the
  • 00:54:12 ether value decreased here from you know
  • 00:54:15 ninety nine point nine to ninety eight
  • 00:54:16 point nine it actually took ether out of
  • 00:54:19 our wallet because we sent it in with
  • 00:54:21 the transaction and we can see that the
  • 00:54:24 second account in the list which I used
  • 00:54:27 as the wallet whenever deployed has been
  • 00:54:29 credited with that ether so our smart
  • 00:54:32 contract worked it actually sent you
  • 00:54:35 know the value that we sent in with this
  • 00:54:39 to that account alright so the next
  • 00:54:41 thing I want to show you is how you can
  • 00:54:45 create basically a fallback function and
  • 00:54:47 solidity this is what's called
  • 00:54:49 at least that's what I've seen it called
  • 00:54:50 I'm not sure what
  • 00:54:51 official name is but basically it's
  • 00:54:53 going to be like a default function that
  • 00:54:55 you can wrap this in so the whatever you
  • 00:54:59 just send either to the smart contract
  • 00:55:01 and execute the function you'll see that
  • 00:55:03 a lot like icos
  • 00:55:04 where you know they have an address
  • 00:55:06 posted and they say hey sent either to
  • 00:55:08 that smart contract and it'll actually
  • 00:55:10 buy the tokens for you now this is just
  • 00:55:13 a kind of pseudocode implementation
  • 00:55:15 again of like a purchase function you
  • 00:55:19 know real ico function functionality
  • 00:55:21 would be much more complex than this but
  • 00:55:23 you get the idea so do this fallback
  • 00:55:27 function will do like this will just say
  • 00:55:28 function alright and we'll say external
  • 00:55:33 payable and we'll just wrap this with
  • 00:55:37 the by token function alright so here's
  • 00:55:42 a new modifier I don't think we've used
  • 00:55:44 yet which is external so that's
  • 00:55:46 different from public because public can
  • 00:55:48 be called you know inside of our smart
  • 00:55:50 contract as well as outside but external
  • 00:55:53 can only be called outside so we don't
  • 00:55:55 want to like call this you know inside
  • 00:55:56 the smart contract somehow so now if we
  • 00:56:01 just compile this again and deploy I'm
  • 00:56:04 gonna paste you wallet in here deploy
  • 00:56:09 we'll see you have a new function just
  • 00:56:11 call fallback and we can basically just
  • 00:56:16 do that like this so we'll just send
  • 00:56:21 money either from a countable one call
  • 00:56:25 the fallback and it worked we can see
  • 00:56:27 our account balance went down this one
  • 00:56:29 up and the balance of first account
  • 00:56:33 should be one yep it's one so now I want
  • 00:56:37 to show you events inside of solidity so
  • 00:56:39 what do we use events for so events are
  • 00:56:41 a way for external consumers to kind of
  • 00:56:45 listen for things that happen on a smart
  • 00:56:47 contract
  • 00:56:47 so basically external consumers can
  • 00:56:50 subscribe to events on a smart contract
  • 00:56:53 and you know wait for something to
  • 00:56:55 happen
  • 00:56:56 so we'll do inside of this by token
  • 00:56:58 function watch e trigger an event that
  • 00:57:01 lets you know anyone connected to the
  • 00:57:03 blockchain really need to know that a
  • 00:57:06 token has been purchased from the smart
  • 00:57:08 contract if they were to listen for it
  • 00:57:10 so I'll create an event like this lit up
  • 00:57:15 here say event purchase and I'll just
  • 00:57:20 say I'll break this line right here
  • 00:57:26 oops I'll say address and know a buyer
  • 00:57:33 and I'll say you went 256 amount okay so
  • 00:57:40 that's how we declare an event just like
  • 00:57:42 that and inside of this function
  • 00:57:45 whenever the token is purchased will
  • 00:57:47 trigger it and we trigger the event like
  • 00:57:49 this I always say emit purchase and we
  • 00:57:54 pass it in the buyer we pass in the
  • 00:57:56 amount so the buyer in this case is MSG
  • 00:57:59 s sender you know the account that's
  • 00:58:00 calling this function and the value in
  • 00:58:03 this case is just one semicolon all
  • 00:58:08 right so there you go now another thing
  • 00:58:10 you can do with these events you can
  • 00:58:13 create an index like this indexed okay
  • 00:58:16 and that will allow you to filter events
  • 00:58:19 that are only from certain buyers so
  • 00:58:21 basically like if you wanted to only
  • 00:58:23 listen to events from the smart contract
  • 00:58:25 that you know a certain person did a
  • 00:58:29 certain account or maybe even just you
  • 00:58:31 you could subscribe based on a specific
  • 00:58:34 address it so I'll show you how that
  • 00:58:37 works
  • 00:58:38 all right let's compile this and run it
  • 00:58:41 I'm going to take the wallet paste it in
  • 00:58:46 here boy all right now I'll call the Buy
  • 00:58:51 its function will send in one either
  • 00:58:57 oops
  • 00:59:00 all right it worked now how we know the
  • 00:59:03 event worked so we can look inside of
  • 00:59:05 this transaction here see the log and we
  • 00:59:10 can see right here here's a log and
  • 00:59:13 inside this law we have some messages
  • 00:59:16 and this basically is is the event we
  • 00:59:19 can see the buyer listed it here and the
  • 00:59:22 amount see the buyer was this address
  • 00:59:24 and here's the amount so what are events
  • 00:59:28 for really there's there's two kind of
  • 00:59:30 main use cases for events you know
  • 00:59:32 because of the asynchronous nature of
  • 00:59:36 the blockchain you know if you're
  • 00:59:38 building an application that I mean if
  • 00:59:43 you're building an application that
  • 00:59:44 talks to the smart contract
  • 00:59:46 you know you might you you might call a
  • 00:59:50 function like by token like if you're
  • 00:59:53 writing a JavaScript application you
  • 00:59:55 might like call this function and then
  • 00:59:57 you know your application would just
  • 00:59:59 know that you called this function but
  • 01:00:01 you might want to actually wait for this
  • 01:00:02 to finish and execute wait for this
  • 01:00:04 event to be emitted so you can basically
  • 01:00:07 wait for that and subscribe to this
  • 01:00:09 event and filter for all the ones you
  • 01:00:13 know that are applicable to you and
  • 01:00:16 whenever that happens you know you can
  • 01:00:18 you can wait for that to happen and then
  • 01:00:19 you know reload your application state
  • 01:00:21 whenever that events been triggered so
  • 01:00:23 that's one popular use case another use
  • 01:00:26 case is you can get the entire event
  • 01:00:27 stream from a smart contract like this
  • 01:00:30 so you can basically see all of the
  • 01:00:33 purchase events because they're you know
  • 01:00:35 listed in these logs they're actually
  • 01:00:36 listed in the transactions you know that
  • 01:00:39 are contained in the blocks of the
  • 01:00:40 blockchain and that's a way to see all
  • 01:00:43 the purchases that have ever happened
  • 01:00:45 inside a smart contract like this
  • 01:00:51 I'll go ahead and use the smart contract
  • 01:00:53 that we were working on in the last
  • 01:00:54 video as the basis for this one so in
  • 01:00:57 this video we're gonna be talking about
  • 01:00:59 how to use multiple smart contracts in
  • 01:01:01 the sloan-c programming language you
  • 01:01:03 know two different aetherium smart
  • 01:01:04 contracts one that will talk to the
  • 01:01:06 other and also show you how to like
  • 01:01:09 write you know parent-child
  • 01:01:10 relationships with smart contracts so
  • 01:01:12 we'll have what about inheritance you
  • 01:01:15 know a contract that inherits from
  • 01:01:17 another one so I'm gonna go ahead and
  • 01:01:20 clear out some of this code from the
  • 01:01:22 last video to kind of get it where we
  • 01:01:24 want it to be the first thing I'll do is
  • 01:01:28 let's see here what basically what we'll
  • 01:01:30 do is well go ahead with this scenario
  • 01:01:33 where we're still gonna buy a token but
  • 01:01:35 instead of you know keeping track of the
  • 01:01:38 balance inside this smart contract will
  • 01:01:41 actually move it to a token contract to
  • 01:01:44 you know kind of separate concerns a
  • 01:01:46 little bit so actually tell you what
  • 01:01:48 let's go ahead and create the token
  • 01:01:49 smart contract like this and again this
  • 01:01:51 is gonna be a really just basic you know
  • 01:01:53 it's almost like pseudocode example it's
  • 01:01:55 not gonna be a full ERC 20 token but
  • 01:01:57 this will just give you an idea of
  • 01:01:58 what's gonna happen when you know two
  • 01:02:01 smart contracts call one another at a
  • 01:02:03 very simple example it'll say contract
  • 01:02:05 your c 20 token and again if you want to
  • 01:02:11 see more you know videos about you 20
  • 01:02:13 tokens i've got plenty on my channel so
  • 01:02:15 i say string name so we're gonna just
  • 01:02:18 have a state variable that keeps track
  • 01:02:19 of the name here it's actually make this
  • 01:02:21 public and we'll do a mapping it'll just
  • 01:02:27 basically move this balances mapping up
  • 01:02:29 here take it out of this contract and
  • 01:02:34 we're going to extract this balances you
  • 01:02:39 know increment here and put it into its
  • 01:02:41 own function so we'll just call this
  • 01:02:44 function mint say function mint i'll
  • 01:02:51 make this public and inside of here
  • 01:02:54 we're just going to do this we'll check
  • 01:02:56 this balances and increment it here just
  • 01:03:00 like that and also we can rewrite this
  • 01:03:02 just say plus plus which way
  • 01:03:05 increment by one refactor that a little
  • 01:03:07 bit make it a little nicer a little
  • 01:03:08 cleaner all right and now you should be
  • 01:03:14 able to clean up this a little bit take
  • 01:03:16 these comments out let's see here let's
  • 01:03:22 actually simplify this we'll take this
  • 01:03:25 purchase event out we don't need all
  • 01:03:26 this that was mostly to show you how
  • 01:03:28 events worked in the last video I just
  • 01:03:30 wanna simplify the code a little bit so
  • 01:03:31 that it's easier to follow along with
  • 01:03:35 alright so inside of here we want to
  • 01:03:40 mint tokens inside of this contract so
  • 01:03:44 you want to call the ERC 20 token from
  • 01:03:47 this by token function so how do we do
  • 01:03:50 that well you know you created this
  • 01:03:54 smart contract in the same file so
  • 01:03:56 because it's in the same file this
  • 01:03:58 contract knows that this contract is
  • 01:04:01 here before we compile it and deploy it
  • 01:04:04 so in the source code you know this one
  • 01:04:07 will know about this one so we can
  • 01:04:10 basically be explicit and tell this
  • 01:04:15 contract if you want to mint tokens in
  • 01:04:17 this contract so in order to do that
  • 01:04:20 there's a couple things that we need we
  • 01:04:23 need to know the address of this
  • 01:04:25 contract which is deployed because you
  • 01:04:28 know this will get deployed and that
  • 01:04:29 this will get deployed and they'll have
  • 01:04:30 two separate addresses and then my
  • 01:04:32 contract needs to know about the ERC 20
  • 01:04:34 token contract where it is and then
  • 01:04:37 we'll basically just reference that
  • 01:04:38 smart contract with the address and
  • 01:04:43 we'll basically get an instance of it
  • 01:04:45 kind of and then we'll just call the
  • 01:04:48 mint function on it all right so there's
  • 01:04:52 a two-step process the first is we need
  • 01:04:53 this contracts address and then we need
  • 01:04:55 to instantiate this contract and then I
  • 01:04:57 guess really the third step is we call
  • 01:04:59 the mint function
  • 01:05:00 so first we'll get access to this token
  • 01:05:04 address we'll say address token all
  • 01:05:08 right we'll make that public so we'll
  • 01:05:11 just say token it's equal to token let
  • 01:05:14 me pass in and we will keep track of it
  • 01:05:18 here
  • 01:05:19 say address token all right we can also
  • 01:05:22 make this public if you wanted to so now
  • 01:05:24 that we have the address we need to
  • 01:05:26 basically get a instance of it so we can
  • 01:05:32 do that like this like I said this
  • 01:05:33 contract source code knows about this er
  • 01:05:35 T 20 token here basically what I'll do
  • 01:05:38 is say I'll create a new one so I can
  • 01:05:43 say Yoshi 20 token all right we're gonna
  • 01:05:46 basically instantiate it and we do that
  • 01:05:50 by passing in the token address that
  • 01:05:52 we've kept track up here so it can be
  • 01:05:54 explicit which I think solidity 0.5
  • 01:05:57 requires us to be explicit so I'll say
  • 01:05:59 address and I'll say token all right so
  • 01:06:04 that will reference this er T 20 token
  • 01:06:08 it's actually deployed it knows about
  • 01:06:10 this source code and then it's going to
  • 01:06:11 get the deployed token from the
  • 01:06:13 blockchain like this and now we can use
  • 01:06:15 this token to call the mint function so
  • 01:06:19 I'll show you a couple ways of doing
  • 01:06:19 this the first one is we can store this
  • 01:06:22 to a variable like this you know when we
  • 01:06:25 create variables and solidity we you
  • 01:06:27 know or whatever declare them we must
  • 01:06:28 specify the type first so the type is
  • 01:06:31 gonna be a RC 20 token contract and this
  • 01:06:34 is a local variable it's on a state
  • 01:06:36 variable so I'm gonna prepend it with an
  • 01:06:37 underscore just kind of a convention
  • 01:06:39 token it's not necessary for solidity
  • 01:06:43 but I do this a lot you'll see this you
  • 01:06:46 know all over the place and now we can
  • 01:06:50 mint the token like this we just say
  • 01:06:52 token that mints
  • 01:06:55 all right so now we can call this by
  • 01:06:59 token function from this smart contract
  • 01:07:01 and it will you know use this you'll
  • 01:07:06 call them it function to from the smart
  • 01:07:07 contract so let's check it out all right
  • 01:07:10 we'll run it so first we have to deploy
  • 01:07:13 this in two steps since there's two
  • 01:07:15 smart contracts in this file we'll pull
  • 01:07:18 the ERC 20 token contract first we'll
  • 01:07:20 deploy it I'm sorry I'm actually going
  • 01:07:22 to use the JavaScript VM will deploy it
  • 01:07:27 all right and now we can just copy the
  • 01:07:30 address of the smart contract that was
  • 01:07:32 deployed
  • 01:07:32 down here from the transaction receipt
  • 01:07:35 let's say contract address and then now
  • 01:07:42 we'll go to my contract and deploy it so
  • 01:07:45 it's asking for a wallet and a token
  • 01:07:47 address let's paste in the token address
  • 01:07:49 the second argument the first argument
  • 01:07:51 I'll just get the wallet so I'll just
  • 01:07:53 copy the basically this is the second
  • 01:07:57 account from here so we'll do this
  • 01:08:01 account all right I deploy okay and now
  • 01:08:09 call the buy token function boom y'all
  • 01:08:15 see the token address here and now we'll
  • 01:08:18 say take the balance of our current
  • 01:08:21 address that we're connected with to see
  • 01:08:23 if it worked all right so why is that
  • 01:08:29 why is it zero well I'll show you why
  • 01:08:36 well first it was successful the buy
  • 01:08:39 tokens function works it actually did
  • 01:08:41 call them in function here but it's
  • 01:08:44 confusing because our you know balance
  • 01:08:48 from the account that we did it with is
  • 01:08:50 wrong so I'll show you what's right all
  • 01:08:54 right so let's go let's don't let you
  • 01:08:59 guess as to why this might be wrong well
  • 01:09:03 instead of leaving a suspense it's got
  • 01:09:06 to do with msg got sender here okay so
  • 01:09:10 this can be a real gacho in your
  • 01:09:13 programming and solidity and you want to
  • 01:09:15 call you know a smart contract function
  • 01:09:18 from another smart contract so earlier
  • 01:09:20 when we built this we call this meant
  • 01:09:22 function directly like if we go here and
  • 01:09:24 click mint right and now I click balance
  • 01:09:27 it goes up by one but if we do it from
  • 01:09:29 here you know we can click it again by
  • 01:09:34 token
  • 01:09:35 let me see it's working but our balance
  • 01:09:38 doesn't increase at all but if I could
  • 01:09:39 do mint here it does increase
  • 01:09:42 that's because msg got sender in this
  • 01:09:45 case is
  • 01:09:46 the address of the contract that called
  • 01:09:48 the function not who sent it
  • 01:09:51 okay now that's kind of tricky so we can
  • 01:09:58 actually get the address of the person
  • 01:09:59 who sent the initial transaction instead
  • 01:10:04 of MSG sender we also have access to TX
  • 01:10:06 top origin all right and that will
  • 01:10:10 actually meant functions for whoever
  • 01:10:13 initiate you know initiated this
  • 01:10:15 transaction so if we call MIT function
  • 01:10:17 direct on the smart contract
  • 01:10:18 it'll increment our balance and if we
  • 01:10:21 call from the smart contract it'll also
  • 01:10:22 increment the balance of the person who
  • 01:10:24 called this by token function so that's
  • 01:10:26 a real gotcha when you're you know
  • 01:10:28 programming multiple smart contracts and
  • 01:10:29 solidity that I wanted you to know about
  • 01:10:30 now I'll show you how this works
  • 01:10:32 so I'll deploy the token first we'll
  • 01:10:34 deploy it all right and I'm going to
  • 01:10:38 deploy you know my contract next say my
  • 01:10:43 contract and we'll paste in the address
  • 01:10:47 okay it'll be the wall address and we'll
  • 01:10:54 also paste in the smart contract address
  • 01:10:59 like this it would apply it now let's
  • 01:11:04 try to buy tokens again do buy token all
  • 01:11:11 right it worked so now let's go look at
  • 01:11:14 balances of this account we're connected
  • 01:11:17 to you know account three three see this
  • 01:11:20 is account three three see their balance
  • 01:11:23 and it's one if we mint tokens here as
  • 01:11:26 well it also increases so that's how TX
  • 01:11:30 origin works you know it's different
  • 01:11:32 from MSG dot sender it's always the
  • 01:11:33 person who originated the transaction
  • 01:11:36 even if a smart contract is calling the
  • 01:11:37 function so what are you all know that
  • 01:11:39 because if that's like definitely can be
  • 01:11:41 a gotcha whenever you are programming
  • 01:11:43 smart contracts that talk to one another
  • 01:11:46 all right so I'll show you one other
  • 01:11:50 thing before we move on just shorthand
  • 01:11:52 here so whenever we mint this token we
  • 01:11:56 can just do this
  • 01:11:59 we can we don't destroy this token as a
  • 01:12:00 variable we can just say he or she 20
  • 01:12:03 token and then just call them in
  • 01:12:05 function on it directly like this and
  • 01:12:08 that was the exact same thing so I want
  • 01:12:10 to walk you through all that but I just
  • 01:12:12 wanted to show you that that's also a
  • 01:12:14 nice clean shorthand valid way of
  • 01:12:17 writing this in one line instead of do
  • 01:12:20 okay so now I do want to talk about the
  • 01:12:24 next topic which is how to inherit smart
  • 01:12:26 contract so inheritance so having a
  • 01:12:29 parent and a child relationship now a
  • 01:12:33 creative token that inherits from this
  • 01:12:35 basic you know a 20 token to do two
  • 01:12:38 things so we'll keep this functionality
  • 01:12:39 the same and what we'll do is basically
  • 01:12:43 give out you know give this token you
  • 01:12:47 know the second token down here we'll
  • 01:12:50 give it its own characteristic so we'll
  • 01:12:52 give it its own name and we'll also
  • 01:12:54 override some of the minting functions
  • 01:12:57 to you know why aren't some of our own
  • 01:12:59 behavior in our own sort of pseudo token
  • 01:13:01 that we're gonna create so I'll say my
  • 01:13:04 token all right so that's what this will
  • 01:13:07 be and this mile thick them will inherit
  • 01:13:11 from this one and we do basic
  • 01:13:13 inheritance just like this we just say
  • 01:13:14 is ERC 20 token
  • 01:13:18 so let's customize this token I mean the
  • 01:13:21 first thing you could do is basically
  • 01:13:22 over I the name because a string a
  • 01:13:24 public name we could set it to something
  • 01:13:28 you know default like my token right and
  • 01:13:33 that would technically override this
  • 01:13:36 right it would be basic inheritance
  • 01:13:37 because we define it a second time it
  • 01:13:40 would override whatever's in this parent
  • 01:13:42 class or parent contract and that shows
  • 01:13:44 you how that works but it doesn't really
  • 01:13:46 give us a whole lot of extra
  • 01:13:47 functionality so let's show you how this
  • 01:13:49 works in a different way so I'll set the
  • 01:13:52 name inside this parent contract like
  • 01:13:53 this will say constructor say string
  • 01:13:58 memory name say public you know say name
  • 01:14:03 equals name right so basically we'll
  • 01:14:06 just have a constructor function that's
  • 01:14:07 realm of this corner just Boyd and we'll
  • 01:14:09 take a name and we'll set it to the
  • 01:14:11 state variable here
  • 01:14:13 so we can actually override this
  • 01:14:15 whenever we deploy our own smart
  • 01:14:17 contract like this we can say instead of
  • 01:14:20 just storing having the hard value we
  • 01:14:22 can also inherit that we do like this
  • 01:14:27 instructor string memory C name and I
  • 01:14:37 can say er C 20 token name and that'll
  • 01:14:42 pass the name in from this constructor
  • 01:14:44 along to this alright and we'll just say
  • 01:14:51 public let's say we also wanted our
  • 01:14:54 token to have a symbol just say string
  • 01:14:57 memory oops
  • 01:15:00 symbol okay and we'll just say symbol
  • 01:15:06 equals to symbol almost say a string
  • 01:15:12 public symbol all right so that's a way
  • 01:15:18 for us to accept our own you know
  • 01:15:20 argument into our own constructor but
  • 01:15:22 still take constructor parameters from
  • 01:15:25 the parent contract and I'm gonna break
  • 01:15:28 this up like this to make it a little
  • 01:15:29 easier to read I do this a lot in my you
  • 01:15:33 know production smart contracts I break
  • 01:15:35 up the lines like this so it's easier to
  • 01:15:37 read alright so that's how you you know
  • 01:15:42 override constructors like that now
  • 01:15:45 inside a few let's override this mint
  • 01:15:47 function as well okay let's basically
  • 01:15:50 add some extra behavior we want to
  • 01:15:52 preserve this mint function we still
  • 01:15:54 want to keep track of the balance but we
  • 01:15:56 also want to do some extra let's keep
  • 01:16:00 track of the people who actually own the
  • 01:16:02 tokens
  • 01:16:02 so first we'll say I will keep you an
  • 01:16:05 array of owners will say address some e
  • 01:16:08 array of addresses say public owners all
  • 01:16:11 right and then we'll say we'll keep it
  • 01:16:13 counter cash and the people who own the
  • 01:16:15 token this is a you int do 56 owner
  • 01:16:19 account all right and now we will update
  • 01:16:26 these values in our own men function so
  • 01:16:29 we're gonna create a mint function that
  • 01:16:30 basically preserves the functionality of
  • 01:16:32 this but add some additional behavior so
  • 01:16:37 we say that is going to be function mint
  • 01:16:41 so we're gonna go ride it say public
  • 01:16:43 okay so first thing we want to do is
  • 01:16:46 call this mint function so that this you
  • 01:16:49 know behavior is preserved we just say
  • 01:16:52 super mint all right and now we say
  • 01:16:57 owner count plus plus let me say owners
  • 01:17:03 push MSG descender okay and that is
  • 01:17:10 basically going to call the minute
  • 01:17:15 function the parent contract to update
  • 01:17:18 the balances then it'll increment the
  • 01:17:20 number of people who own the token right
  • 01:17:22 here and it will add them to the array
  • 01:17:26 of owners okay all right so now let's
  • 01:17:31 just compile it run it and so when we do
  • 01:17:33 this we don't have to deploy to smart
  • 01:17:35 contracts all we have to do is deploy
  • 01:17:36 this one so you can say you're actually
  • 01:17:39 20 token I'll see the string actually
  • 01:17:44 let's do this let's do my token so
  • 01:17:48 string name will be my token and symbol
  • 01:17:50 will be empty kay deploy all right
  • 01:17:56 so let's call them in function all right
  • 01:18:00 worked so let's see the balances of the
  • 01:18:04 current the current address that we're
  • 01:18:08 connected with will say balance is this
  • 01:18:12 okay all right we'll do the name my
  • 01:18:16 token and the owners of the first person
  • 01:18:20 and the lists is our address and a
  • 01:18:23 symbol okay alright and we can also make
  • 01:18:28 this public if you want to read the
  • 01:18:29 owner count all right guys so that's how
  • 01:18:33 you inherent smart contracts and
  • 01:18:35 solidity
  • 01:18:38 I'm gonna be talking about two different
  • 01:18:40 topics in this video that are somewhat
  • 01:18:41 related the first topic is libraries how
  • 01:18:45 to use them in solidity and the second
  • 01:18:47 topic is math you know how to do math in
  • 01:18:50 the solidity programming language and
  • 01:18:52 these are somewhat related because you
  • 01:18:54 know we use libraries to manage math
  • 01:18:58 functions all the time and solidity is a
  • 01:19:01 really common use case so I figured I
  • 01:19:03 would just record both of those in the
  • 01:19:04 same video so you know what is a library
  • 01:19:07 or if not familiar you know library is
  • 01:19:10 basically a way to organize code that
  • 01:19:13 can be reused in multiple places inside
  • 01:19:17 your project so that's one of the main
  • 01:19:18 reasons we use libraries is essentially
  • 01:19:20 to dry code up so if you're not familiar
  • 01:19:23 with dry Droy that's an acronym for
  • 01:19:26 don't repeat yourself alright so
  • 01:19:31 basically if we have a function that we
  • 01:19:32 want to use in multiple smart contracts
  • 01:19:34 we could define inside of a library and
  • 01:19:36 then use that library to use that
  • 01:19:38 function and that way if that function
  • 01:19:39 ever changes the change is absorbed by
  • 01:19:41 any place that we use it so how do we
  • 01:19:45 declare a library in solidity well like
  • 01:19:50 this first we use the library keyword
  • 01:19:53 state library and I'm gonna call this
  • 01:19:56 library just math
  • 01:19:58 okay
  • 01:20:00 so in the earlier videos you know I said
  • 01:20:03 that you know smart contracts are the
  • 01:20:05 main way that we organize code inside of
  • 01:20:07 solidity you know it's call it contract
  • 01:20:09 oriented language and that's true but we
  • 01:20:12 also have libraries where we can you to
  • 01:20:14 find functions inside of here and store
  • 01:20:16 variables and things like that and
  • 01:20:20 that's another way to organize code
  • 01:20:21 inside solidity but really at the end of
  • 01:20:23 the day a library belongs you know
  • 01:20:25 inside of the smart contracts that you
  • 01:20:27 can get called and used you know
  • 01:20:29 libraries don't have the full behavior
  • 01:20:31 of a smart contract they don't really
  • 01:20:33 have storage in and of themselves they
  • 01:20:36 don't you know you can't inherit from a
  • 01:20:39 library things like that so it's not
  • 01:20:41 really like a smart contract at the end
  • 01:20:43 of the day it's meant to be used inside
  • 01:20:44 of a smart contract so we can define a
  • 01:20:47 function inside a library like this I'm
  • 01:20:49 going to create my own divide function
  • 01:20:51 say function divided alright I'll just
  • 01:20:56 leave this here for a second so why we
  • 01:20:59 want to use divide function well I'll
  • 01:21:01 show you let's create an example inside
  • 01:21:04 of my contract where we would you know
  • 01:21:07 basically just do some math first of all
  • 01:21:09 will declare a value will say you at 256
  • 01:21:13 say public value will just store the
  • 01:21:17 value here and now actually store this
  • 01:21:19 value but will will compute it
  • 01:21:22 so we'll say function I'll say calculate
  • 01:21:26 and we'll take a value say you int will
  • 01:21:30 say value 1
  • 01:21:31 so you nth value to say public and
  • 01:21:38 inside of here well basically just set
  • 01:21:40 the value to whatever the calculated
  • 01:21:42 value is we'll say a value 1
  • 01:21:48 / by YouTube now this is a basic way to
  • 01:21:52 do division inside solidity we just use
  • 01:21:54 this operator here it's the division
  • 01:21:56 operator so why might we want to use a
  • 01:22:00 library for this
  • 01:22:01 well you might notice a problem it's
  • 01:22:04 value to what if value to is zero right
  • 01:22:09 there be a problem with this we get some
  • 01:22:12 sort of exception because you know we
  • 01:22:15 can't divide by zero
  • 01:22:18 and that's a really common use case for
  • 01:22:20 a library basically we can write some
  • 01:22:22 code inside of here that prevents this
  • 01:22:23 divided by zero error we can actually
  • 01:22:26 stop the function before that happens so
  • 01:22:30 basically we could do like this we could
  • 01:22:32 define this function and say divide is
  • 01:22:35 equal to you know the input say you int
  • 01:22:38 256 let's just say a you went 256
  • 01:22:42 B was a internal pure turns
  • 01:22:49 you had 256 it will do the division
  • 01:22:52 inside of here so you require that be
  • 01:22:56 it's greater than zero that's the first
  • 01:22:59 thing we'll do we'll take the
  • 01:23:00 denominator and make sure the
  • 01:23:02 denominator is greater than zero so that
  • 01:23:03 we don't divide by zero then we'll
  • 01:23:06 actually create a return value of C
  • 01:23:09 which is C divided by B or the numerator
  • 01:23:13 divided by the denominator and we'll
  • 01:23:15 just return that value return C sorry
  • 01:23:18 this is supposed to be a so now we can
  • 01:23:21 actually use this divide instead of this
  • 01:23:23 divide here as a safer way to do
  • 01:23:26 division inside of solidity that's a
  • 01:23:28 really common use case for a library so
  • 01:23:31 how do we do that how do we take this
  • 01:23:33 function out of this library and use it
  • 01:23:36 inside this calculate function like this
  • 01:23:38 well we can say math dot divide and then
  • 01:23:45 just pass in value one and value two and
  • 01:23:51 we store the output to value just like
  • 01:23:55 that all right now erase this all right
  • 01:24:00 now let's try to run this code
  • 01:24:02 so whenever we deploy this I'm gonna
  • 01:24:06 select my contract and instead of math
  • 01:24:08 and whenever this is to compiled and
  • 01:24:11 deployed it knows that my contract
  • 01:24:16 depends upon math so it's already gonna
  • 01:24:18 get compiled and deploy automatically we
  • 01:24:21 don't have to deploy this first and then
  • 01:24:22 this this and the whole compilation
  • 01:24:24 process of this file the math library at
  • 01:24:28 least the part that gets used is gonna
  • 01:24:29 get compiled because it's dependent upon
  • 01:24:31 right here so let's try this first we
  • 01:24:35 can see the value is zero and we'll say
  • 01:24:37 value one is I'll say 100 and value 2 is
  • 01:24:42 10 all right okay no successful you see
  • 01:24:46 the value is 10 now and as we try to
  • 01:24:49 divide by 0 calculate and we get an
  • 01:24:51 error just as we expect you know we can
  • 01:24:54 also do some other values just to make
  • 01:24:56 sure it works
  • 01:24:56 say 144 divided by 12 should also be 12
  • 01:25:01 yep all right so that's how you you know
  • 01:25:06 it's a basic example of how you import a
  • 01:25:07 math function from a library now there
  • 01:25:11 are a few other ways we can do this you
  • 01:25:14 know instead of having this library just
  • 01:25:17 at the top or a file here we can
  • 01:25:19 actually move it to another file and
  • 01:25:22 import it inside of this file so we can
  • 01:25:26 clean this up and we're only looking at
  • 01:25:27 one you know sort of unit at a time we
  • 01:25:31 like to look at the smart contract we
  • 01:25:32 don't have to look at this library so I
  • 01:25:34 should I to do that we'll go over here
  • 01:25:36 to our browser and we'll create a new
  • 01:25:37 file I'll call this math does so well
  • 01:25:40 all right now we'll just copy the math
  • 01:25:44 library from here I'll actually just
  • 01:25:47 copy it with the solidity programming
  • 01:25:49 language declaration and we'll paste it
  • 01:25:52 in here save
  • 01:25:55 now we can require the file just like
  • 01:25:59 this we say import we use double quotes
  • 01:26:05 math so well alright
  • 01:26:09 and unless I use the dot to indicate the
  • 01:26:12 current directory forward slash and the
  • 01:26:14 filename which is math the SOL followed
  • 01:26:17 by semicolon
  • 01:26:18 alright that'll be the same thing so we
  • 01:26:21 can go back to here and deploy this same
  • 01:26:23 math it's alright go to my contract and
  • 01:26:25 deploy and we'll see say one eighty
  • 01:26:29 eight and nine
  • 01:26:36 all right there we go that's doing an
  • 01:26:38 integer division that's why it's has no
  • 01:26:40 remainder
  • 01:26:42 okay so that's how you import a library
  • 01:26:45 from another file and slow to you now
  • 01:26:47 I'll show you one more thing I want to
  • 01:26:49 introduce you to a really common library
  • 01:26:52 which is called safe math which just put
  • 01:26:55 out by open Zeppelin you can go that
  • 01:26:58 repository here if you want to read more
  • 01:26:59 about it but basically it implements a
  • 01:27:02 lot of helpful functions that I use a
  • 01:27:06 lot but developing smart contracts and
  • 01:27:08 doing math they've you know they've
  • 01:27:12 solved a lot of problems like guarding
  • 01:27:14 from overflow and things like that and
  • 01:27:17 so I use this library a lot so it's
  • 01:27:21 really common use case from me I'm going
  • 01:27:23 to show you how to use that library
  • 01:27:24 inside of here because it works a little
  • 01:27:26 bit differently than how we implemented
  • 01:27:28 our math library so I'm going to delete
  • 01:27:30 this file and create a new one say safe
  • 01:27:36 math
  • 01:27:39 I'm just gonna copy the code here from
  • 01:27:41 github click Edit
  • 01:27:44 and go starting to edit I wanna do raw
  • 01:27:49 I'm just gonna copy all that go back to
  • 01:27:53 remix and paste this in here
  • 01:27:55 alright let's actually change the salute
  • 01:27:57 your programming language of version
  • 01:27:59 shows you're about 5.1 i think it should
  • 01:28:01 probably be up to date
  • 01:28:02 oops
  • 01:28:04 yeah looks good all right so let's save
  • 01:28:08 that go back to my contract instead of
  • 01:28:10 importing math we'll do safe math
  • 01:28:14 all right and instead of you know
  • 01:28:18 calling a library like this we're gonna
  • 01:28:21 do a different way all right and this is
  • 01:28:25 pretty cool so we can take all these
  • 01:28:29 functions in here like multiply and
  • 01:28:31 divide and subtract and add we can
  • 01:28:34 actually call them on unsigned integers
  • 01:28:36 directly all right and we developed this
  • 01:28:40 we can say using safe math for you int
  • 01:28:47 256 all right now that's pretty cool
  • 01:28:52 I'll show you what that does instead of
  • 01:28:55 doing this we can basically say value 1
  • 01:28:59 give value 2
  • 01:29:05 there you go that's pretty slick so
  • 01:29:09 let's try that
  • 01:29:10 go Mike on track with a ploy like this
  • 01:29:13 so he value wine will be 144 yeah I 2 is
  • 01:29:17 12
  • 01:29:18 all right value it's 12
  • 01:29:21 there we go say 13333 right by 11
  • 01:29:28 yeah 100/10
  • 01:29:35 there we go awesome all right so that's
  • 01:29:38 all I got for this video today guys
  • 01:29:39 again you can find all the code examples
  • 01:29:42 of this on my website over at DAP
  • 01:29:44 university comm ford slash article
  • 01:29:46 forward slash solidity tutorial I'll put
  • 01:29:48 a link to that down the description
  • 01:29:49 below and again if you're serious about
  • 01:29:51 becoming a blotching developer and you
  • 01:29:53 enjoy my free training on my website
  • 01:29:55 over at Dappy diversity comm forward
  • 01:29:57 slash bootcamp alright until next time
  • 01:29:59 thanks for watching dap University