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