Placeholder Image

字幕列表 影片播放

  • everybody.

  • This is Gregory from Dad University.

  • Some so excited to be on free code camp today to teach you how to become a Blockchain developer.

  • I'm gonna take you over the shoulder and teach you how to write Ethereum smart contracts with a solidity programming language for the block chain.

  • And you don't even have to know anything about solidity or Blockchain to follow along.

  • So I've compiled this multi part tutorial Siri's into one massive video for free code camp and you can follow along with the code examples step by step on my website.

  • There's an article that I'm gonna put a link to down the description below.

  • And finally, if you're serious about becoming a Blockchain developer needed or my free training on my website over a Daph university dot com forward slash boot camp and I also put a link to that description.

  • Below solidity is the main programming language for writing smart contracts on the ethereum block.

  • Jame, it's a contract or read a language which basically means that you know, smart contracts are the main way that you organize code and like stored data and write all of your programming logic and you know it's a high level language for implementing these smart contracts.

  • It looks a lot like Java script and, you know, python and C plus plus, as you see right here and it's used to run on the Ethereum virtual machine, which is basically, you know, the thing that runs the code on the ethereum Blockchain.

  • So solidity is statically typed a supposed to a dynamically type language and its support stuff like inheritance libraries and lots of other stuff.

  • So, enough of talking about solidity.

  • Let's actually jump in and start writing some solidity code.

  • This is a Web site that allows you to write solidity smart contracts in your browser and you know, it has a text editor here.

  • It allows you to compile them and deploy them and run them.

  • And that's exactly what I'm gonna use for this tutorial.

  • You won't have to down with anything in your computer.

  • You won't have to install anything.

  • You could just visit this website, um, to use remix, and we'll write the smart contracts that way.

  • So in order to do that, I'm gonna go over here to this browser folder.

  • Um, you can see the file browser over here.

  • This is basic smart contracts that come preloaded inside of this.

  • Ah, I d I'm just going to create a new one here.

  • I think I couldn't create a new file.

  • This will be a solidity file.

  • I will call it my contract, and we're gonna write a smart contract inside of here.

  • I'll do this by first declaring the version of solidity that we want to use you.

  • To do that like this was a Prague MMA solidity enemies a carrot to specify aversion greater than the woman want to use.

  • And I'm gonna say version zero dot for us for 24 And I'm gonna end each line like this with a semi colon, and next, I'm actually gonna declare the smart contract.

  • So before I do that, I'll explain a little bit more about what a smart contract is.

  • Um, you know, smart contract is code that gets executed on the block chain.

  • In this case is gonna be the ethereum Blockchain.

  • That's for using, you know, solidity for this Ethereum smart contract.

  • And this smart contract is kind of be kind of like a micro service out on the web.

  • It's gonna be accessible to everyone on the block chain.

  • They'll be able to, you know, see, this smart contract will be able to use it.

  • They'll be able to read and write data with it and actually execute any code that we write inside of here.

  • So it's going to be public.

  • And that's kind of why I called like a micro service.

  • It's more.

  • It's more than just a class, like in an eye, Victorian and system or something like that.

  • It's actually something that's publicly accessible.

  • So it's gonna look something like a class, though, because it's how we're gonna actually, you know, uh, keep all the code and side of here.

  • So she had to do that.

  • We'll call.

  • We'll start by saying Contract, say my contract and I'm opening with some curly races and it will notice that the editor closed this automatically for us, which is really nice.

  • And what I'm going to do first is just show you how to read and write a value for variable inside of here.

  • Now, like I said earlier, solidity is a statically type language, so we're gonna actually have to declare the type the debt a type of the variable you wanna store.

  • So in this case, we're just gonna store string and we'll declare string will say value.

  • Now, let me explain something about this value this value is gonna represent, you know, a variable that belongs to this entire smart contract.

  • So this isn't like a local variable.

  • It's actually a variable that this entire contract will know about.

  • And what that means with a smart contract is that data is actually gonna get stored on the block chain in storage.

  • So if we set this value were actually be riding data to the Blockchain.

  • That's different from a local variable that I might just know set instead of a function that would be, you know, local to that function scope on.

  • But you know, disappear instantly once that function is called.

  • Instead, this values actually could be stored on the Blockchain.

  • So let's create a way to us.

  • Just read this value so I can do this by writing a function and solidity like this.

  • We start with the function key word I say we call the function, get it's gonna get this value, and we'll say We'll just open the braces for now and write some more code here in a second season.

  • Warnings coming up.

  • But I'll fill this out more will say, just return about you.

  • Okay?

  • And that's a really basic way to do this.

  • I'm gonna add some more to this.

  • I'm gonna say this is also public, all right?

  • Which basically is setting the visibility.

  • This function that's that's called solidity is setting the visibility.

  • So that knows this function could be called by anyone who has access to the smart contract on the block chain, not just inside this contract.

  • So I'm also going to set a return value returns string.

  • All right, that makes our warning disappear down here.

  • And I just tell their function that we're always gonna return.

  • Ah, string data type.

  • And we know that's the same type as value because we declared it here.

  • Now, I'm also gonna look this warning down here which says this function state mutability could be restricted to basic.

  • What this is saying is in solidity.

  • Now, in this newest version, we're not actually changing this value.

  • We're not changing anything inside this function.

  • We want to add additional modifier here, which is says of you, all right.

  • And that makes that warning go away.

  • So now I'm gonna set a new function or write a new function that's called set Say function.

  • It's gonna louse to set this value set as a string.

  • It's a value, all right.

  • And 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 block chain.

  • And I'll say value equals this value that were passing him.

  • All right, let me explain that I'm using values and underscore here because I want to differentiate between this value that's getting passed in and the value that's referenced here.

  • Now, inside, if you're you know, this value without underscore is referencing this, you know, state variable that we set up earlier that's actually getting store to the block chain.

  • And this value slitting knows that it's just a local variable because I pass it in and I, you know, pretended it with an underscore like this and also noticed that I declare 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 said it.

  • The next thing I'm going to show you is how to set this value.

  • Ah, whenever you we deploy the smart contract or whatever it's generated for the first time.

  • And I'm gonna do this with the constructor function.

  • So if you're familiar with other programming languages where you might write, you know, classes doesn't like that you might be familiar with a constructor function, which is basically a function that's called whenever this contract isn't Stan she hated or in this case is gonna be won over the contracts is created whenever the smart contract is deployed to the Blockchain.

  • And in the newer versions of solidity, we do that like this was a 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.

  • Um, well, say 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, set this value whatever is deployed, so it will have some sort of default value.

  • And then we'll have some functions that allow us to read that value from the block chain.

  • And then we'll have a function that allows us to create that value as well.

  • So now I can go to this tab over here and remix, and I can compile it.

  • We can, ah, slipped the compiler version.

  • And I'm gonna say this a year 04 to 5 will start to compile it and see what happens.

  • All right?

  • And now what I'm gonna do is actually run this mark contract.

  • You see some options over here.

  • I'm just going thio say JavaScript virtual machine, which basically what that's gonna do is give us a test Blockchain in the browser so we don't have to connect to a Blockchain of any kind.

  • We could just, you know, compile this and run it and deploy it inside our browser, which is pretty cool.

  • And it's gonna give us some, you know, free ethereum accounts over here.

  • I don't worry if you don't quite understand why all these values mean just yet.

  • That's okay.

  • I can explain those more.

  • Um, but for now, we're going to just keep my contract and we're gonna deploy it.

  • Alright, here we go.

  • It's deployed.

  • So now what I can do is click on this little down air here and we see the functions that are available to us in the smart contract.

  • We can seethe set function and the get function.

  • You know, these are the functions that we created over here.

  • The set function is gonna allow us to update it and they get functions allows to get it.

  • So I click get and we should probably see the value that we created inside the constructor.

  • We set it here, so let's get it.

  • Alright, there we go.

  • It's my value.

  • And we can see some activity in this log down here.

  • We can see that there was a call to my contract I get and now we'll set it.

  • So I'm gonna enter.

  • Ah, the quotations for the string and I'm gonna say new value.

  • All right, let's look set.

  • All right, then we can see that worked so you can see the transaction history over here.

  • Let's get the value.

  • All rights, new value.

  • There we go.

  • That's a complete smart contract that allows you to get instead of value on the block chain.

  • Now, let me explain what's going on here.

  • This is a list of transactions that are occurring on the ethereum Blockchain.

  • So the ethereum Blockchain is made up of, you know, bundles of records that are chained together into blocks that make up the block chain and the basic unit of all those blocks are these transactions so we can see transaction receipts?

  • Um, we can actually cook the downers here.

  • We could see all the details, those transaction receipts.

  • And 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 in the solidity programming language again, that's supposed to be just a high level overview of solidity and how to get started riding your first mark contracts and using the programming language itself and using the remix I t e in your browser to get started easily without having to download any tools or anything like that.

  • The first thing 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 um dot one.

  • I believe you can actually see the compiler version overhears changed in the last video.

  • You know, we wrote this smart contract and there's, um, actually Cem copulation errors You can see over here on the right.

  • You have to update this code to support his version of the solidity programming language.

  • And I want to go.

  • I want to go ahead and do this.

  • I wanted to go ahead and support the new version of solidity so that when I make more videos in this series, we'll be up the day.

  • You'll have the newest version.

  • So let's see what we need to do.

  • We could read the air, it says, Um, basically, the data location must be memory, and that's actually the air in both cases.

  • So all we need to do in this case is actually just return.

  • Ah, memory here.

  • And then whenever we're passing in the value a string we actually need Thio put memory here as well.

  • All right, actually, that need to be before value.

  • My fault.

  • And now we there's go away.

  • So we will select the newest compiler version Will say, uh, do.

  • The latest complete version is you're not fired at one started, compile.

  • And hopefully the accomplishment work.

  • Yes, it worked.

  • So let's go to run and we'll deploy.

  • Yes, you've worked.

  • So get the value.

  • Yep, It worked.

  • Awesome.

  • Now let's move on to the next part of this tutorial where we're 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 develop your own smart contracts.

  • So I would change in values in here to kind of show you what that is.

  • The first thing I want to talk about our, you know, the different ways we can work with the state variables.

  • Right?

  • So the first thing I want to mention is, you know, we had this string value here.

  • Ah, that we declared we set it here inside this set function, and we had this getter function.

  • Uh, well, we read that value, right?

  • So slow today has a shortcut.

  • Where I don't even need this function.

  • I can actually just read.

  • Ah, this string value for free.

  • I can actually get this function for free.

  • Take this out and just declare this public.

  • That's that's declared the visibility of the, uh, state variable here.

  • She publicly before.

  • So save that, you know, compiling, you know, run it And we'll see that a, uh, get get her function called value the same names 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.

  • Says a really nice way, Thio.

  • Save yourself.

  • Reminding extra code.

  • You could get that function for free exposed to the public interface with smart contract whenever you store your state variables that way.

  • Okay, so the next thing I want to talk about inside of here, um, is actually, you know, we could set this as a default value.

  • We don't necessarily have to put my value here.

  • The instructor, You just say this all right?

  • We could take this away.

  • Hoops, take this away, and we will, um, run that again.

  • Let's just clear this out.

  • Deploy.

  • All right, again.

  • Ah, actually, everybody.

  • Good pilot.

  • First Boyd again value my value.

  • We can also said this value as a constant if we don't want to change.

  • You know, if we don't want to allow user to set this value, we want to stay the same.

  • We could just say Constant.

  • All right, that actually declares a constant We had to remove this function.

  • We can't update this value at all.

  • Solidity won't let us do that.

  • So we could just do that, Um, the place again, Because still see our values here as my value.

  • So, you know, that's what you would add a constant to your state variables or make your stay variables constant.

  • Now, let's look at some other data types and solidity.

  • Um, let's just change this name.

  • We'll call this like string value.

  • All right, let's just deployed that we can see this instance here again.

  • Um, string value writes my value.

  • Now it's explore some other data types.

  • Weaken.

  • Say, uh, bullion type will say boule public is equal to men through this constant.

  • For now, um, my bull equals true.

  • All right, so this just accept your false we can deploy that.

  • See this?

  • My bull writes true string values.

  • My value it's a, uh, or my string.

  • Just say that just for fun.

  • And we could also see that, you know, we support that a types like, um integers so interest could be signed or unsigned.

  • So what does that mean?

  • Well, we could do a default int which would be Public Myint one.

  • All right, We can deploy this and see sexy.

  • Just clear this out.

  • Sorry.

  • Deploy this and see my end table.

  • The one so the difference train.

  • Ah, end and a U.

  • N.

  • Or an unsigned integer is that an integer can be signed A Sorry, Uh, yeah, energy could be signed and a u N could be unsigned.

  • So what does that mean?

  • Well, basically, an end can be negative, so I can do minus one.

  • All right.

  • And deploy that.

  • That's a sign into dirt.

  • Has a sign in front of it be positive or negative in a u.

  • N.

  • Can't.

  • So deploy that.

  • Clear the zone and see my You went all right And my end.

  • All right.

  • Yes.

  • A negative one can work for an end, and, uh, they can't do negatives on unscientific er's.

  • You also specify the amount of bites or bids, um, in a ninja Germs.

  • So we can say you, Aunt eight.

  • Blake goes my yuet eight.

  • Sorry.

  • You girls like All right.

  • Oops to a small number.

  • And I could see my you ate All right.

  • And so we do you aunt here it defaults to 2 56 A belief you had 2 56 public My you.

  • So that gives you a basic overview of all those basic data types.

  • I don't want to bore you with all the pedantic details of this kind of stuff, but this is helpful to know if you'll like.

  • The next thing I want to show you is an e newme.

  • So what is an E?

  • Neuman?

  • How do you use it?

  • Well, an E newme is basically an Inu Marie did list that's gonna allow us to keep track of, you know, set list of things in our contract, so I'll show you what that means.

  • So basically, I can declare anything like this.

  • They'll say a new MME Ah ST and I'll give it some options here.

  • I'll say waiting.

  • Ready, active.

  • All right.

  • And this is gonna allow us to keep a list of these three states inside.

  • The smart contract will be able to reference this to see what the current state is, and it will give us some options whenever up there in the state and like checking against this.

  • So a shining example.

  • Watch the store.

  • There's like this will declare state public.

  • Ah, state this right.

  • So basically, this is going to give us, um, a public.

  • Ah Rubio.

  • Access this publicly with that.

  • Get her so inside of here.

  • I'll do a constructor.

  • I'll say, Constructor, I was a public.

  • It's a state equals state active.

  • All right, So go and set the default State Thio active, right?

  • So, actually, let's do this.

  • Let's do waiting and then I'll say I create a function to activate it.

  • It was a activate as a public state equals ST dot active so we can actually choose Sorry, we can choose this value out of his Indian list and update the state to that value.

  • All right, so now we call this function, it will change the state to the active state and let's get a function that checks to see if the state is active was a function is active and was a public view returns boom.

  • All right, well, say return state equal to state that active.

  • That will do inside 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.

  • Um, I will compile us.

  • All right, Run, Deploy, and all right, let's check the state.

  • So is the stay active all right?

  • No, it's false.

  • So that's that's true.

  • It's not active.

  • It's in waiting, so it's actually check the state.

  • So status 00 corresponds to the first item on this list.

  • Zero, which is waiting and I'll squawk.

  • Activate.

  • All right.

  • So actually called the activate function, Um, which, you know, updates the state to active.

  • Let's see if it's active.

  • Yep, it's true.

  • Let's check the state.

  • So now the state is, too, which is 012 All right, so that's an idea of how you use E new me's.

  • Um, let's look at the next concept that I want to show you.

  • The next guy.

  • Don't always show you is ah, stools destructs.

  • So structure basically a way for you to define your own data structures inside solidity.

  • We'll do that like this will say, uh, credit structure right here.

  • We call it a person obstruct person, and we'll give it some Look, it's, um, some characteristics was a string.

  • Ah, first name and string.

  • 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.

  • I'm just gonna model a person with Destruct is gonna have a few attributes.

  • 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 thes Struck's inside of, like, a raise and mapping.

  • And things like that will show you that here in a minute and saw this video.

  • But for now, just know that we declare that we've defined this new data type person inside the smart contract with Destruct that's gonna have a first name and last name.

  • Okay, so let's actually ah, create a function that allows us to in Stansky a new person create a new one and want to keep track of a bunch of these persons Struck's inside of a people array.

  • All right, so what's it like this Let's say, Let's keep track of of a bunch of different persons that we can create new ones.

  • Person.

  • So being a ray public people All right, so we'll see what's happening here.

  • People is just a variable here in its in its public, so you can read it outside of smart contract.

  • And it's an array here of these data type person, right?

  • So we declare the data type for just person, which we define here.

  • Destruct.

  • It's an array that you know, contains these person Struck's.

  • It's public and it's started with the people.

  • Stay variable so we can add people to this array or persons trucks to this ray like this'll could've function is a function add person String isa solidity five updates Ah, memory or 05 Sorry, first name and they will say string.

  • Ah, memory.

  • Last name was a public, and we'll do business out here.

  • Sick people not push.

  • So push is, you know, a functioning Conner Reyes to add a new item at the to be Ray.

  • Um and we'll push a new instance of this person struck, so we can, um, creating new instance like this.

  • We just call person right and we pass in these attributes.

  • So it's a first name.

  • Oops.

  • Sorry.

  • Name and last name.

  • All right.

  • And what we can do here is why.

  • Yeah, we've added it was going to do it.

  • Let's just let's just compile this room on it.

  • Um, we'll play this.

  • All right?

  • So see what happens of people.

  • Um it's gonna add the person.

  • Ah, person.

  • I'm sorry.

  • Sue.

  • First names.

  • The first name will be dap last night with the university.

  • We'll add person.

  • Okay, Awesome.

  • So that didn't give us any errors.

  • Now, how do we read this person out of here?

  • That's good question.

  • So we added this person to an array.

  • Um, but this function, you know, whatever we call this person or the starters people function.

  • It's not gonna return the entire ray 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 gonna return the entire ray.

  • So basically, we need to reference the person by i d.

  • So whenever you get this function, it expects an argument, which is gonna be the index of the person inside this Marais.

  • So, for example, we know he added one person in there.

  • So we will add.

  • People will say zero.

  • All right, there we go.

  • Dap University.

  • So what happens if we reference, you know, a person that doesn't exist?

  • It's a person, you know, One, people.

  • It's gonna give us an error.

  • It's an endowed topcoat here.

  • That's because there's no one in there at position one.

  • So how do we How do we like, you know, how do you fix that problem?

  • Keep track of that like this.

  • So basically, we would keep an internal count of the people we'd say something like this.

  • You end to 56 people, count.

  • Okay.

  • And that's gonna basically have a counter cash that will increment everytime person who added So we can say something like, um, people count plus equals one.

  • All right.

  • We'll do that after I guess.

  • All right.

  • Say, then deploy.

  • And now we can ah actually make this public.

  • They have size before.

  • Let's try it again.

  • Now we can add a person university, that person, and you see people count is one.

  • So we would do one minus that, people.

  • All right, Dad University.

  • We do one minus that, because zero based in next.

  • Which means the first item you know as an index zero and the second one's that index one, etcetera, etcetera.

  • The next thing I want to show us how to do a similar concept here but model a little differently.

  • So instead of dizzying array, were actually use a mapping.

  • So what is it?

  • Mapping?

  • A mapping is basically like an associative array.

  • So instead of having you know, just in array, um, and we know the each index of the item in the ray, we can actually associate to a values that's that's like a hash table or a hash map or a hash, um, and other programming languages that you might be familiar with.

  • We're basically you can reference things by, you know, Ah, key value.

  • It's Kiki Value pair.

  • So instead of ah, having people like this and seven are being array, um, I'm gonna change it down here.

  • I'm gonna say, uh, see me mapping, mapping, you rent person.

  • Okay, so eunt is going to be the key, and the person's going to be values.

  • This mapping is like it's an associative array, and it's gonna take a key value pair.

  • That key is going to be an unsigned imager.

  • We'll treat this like an I d would be kind of like our database.

  • Look up, what we have and I d And then a record to this is gonna return.

  • Kind of like a record, like a person record or a person struck in this case and the public and the people.

  • And it is kind of fair comparison to compare this to, ah of database because, you know, we're watching Kind of is a big database, and these trucks are getting stored in storage on the block chain.

  • With this mapping, there is a person string first name, last name.

  • It will do you add person so many A modified its function.

  • I also think the first name listed with the last name, but we're going to put this in the mapping instead of theory.

  • A.

  • Some of these people count back up here.

  • We're gonna want to keep track of this your second earlier.

  • So we know how many people are in this mapping, but add it to the mapping like this will say people, um people count.

  • So your i d so if it starts at zero, which it will do this.

  • All right, people, count plus equals one.

  • They'll be the new idea of the person's gonna go in here.

  • So this is the person number one.

  • The first time we add this, we'll say this is going to be a new person.

  • And the ah, we'll give it and will give us an I D.

  • Well, say, Ah, you went 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.

  • See that worked.

  • Clear this out.

  • Um, by the contract.

  • So we'll do.

  • First name is stabbed University.

  • That person.

  • Okay, so see, people count is one.

  • Now we'll go to people.

  • This time it won't be.

  • Ah, based on the array Index will actually be on the i.

  • D.

  • So we can actually reference person one.

  • All right, what do people?

  • One there?

  • We go to university.

  • So let me explain.

  • A couple more things about Struck's while we're here.

  • Um, you know, we need to keep track of this.

  • People count because there's no way to know the size.

  • That's mapping and solidity.

  • basically any key that doesn't have a value set for it's gonna turn a default value, which is gonna be like an empty struck here.

  • Um, so if I do like, you know, a person no.

  • 10 or like 909 people so you can return default values.

  • So war that he fell values for the struck person I d for you, ent is gonna be zero.

  • And then these blank strings, her first name and last name.

  • So that's what we need to have a person counts.

  • So if you're gonna use this, like as a data storage and you wanted, like, show all the people are all persons and you're in your people mapping in your app or something like that ring for the smart contract.

  • You want to know how many are actually in there, and you want to do that with a counter cash?

  • Um, so that way, you know, if you wanted to basically like same thing with me but those variable size of Ray's, like if you wanted to get all the items you need to first make a call to find out how many there are.

  • That's what use the counter cash four and they basically credible loop to read out each 11 by one.

  • So, yeah, it's it can be kind of pain, but ah, that's sort of just the state of things and what you have to do.

  • I'm gonna go ahead and pick up where we left off in the last video.

  • I'm gonna use this.

  • My contract that we built where we added a person person struck to this mapping of people.

  • You haven't seen that last video.

  • Go and check that out.

  • It's not necessary.

  • But you can probably follow along with this video if you want to.

  • What I'm gonna do now is show you a little more about thes modifiers like public, um, things like that.

  • I think we talked about that.

  • The first video, but I'm gonna go a little further.

  • So what I can do is, you know, show you another modifier, which is basically like internal.

  • Um, so that's different from public Public is a function that could be called Ah, you know, the public interface with smart contract.

  • You can see you know, the public functions listed here on the side.

  • So going create a private function are an internal functions skews male, say function.

  • We're just gonna take this people count and wrap it in its own function to show you how that works.

  • Well, say, uh, increment count.

  • We'll just make this a function and we can call it internal.

  • That's a different modifier.

  • We'll take this.

  • People count will pay.

  • Sit inside of here.

  • All right, well, just say increment count.

  • Call that function and we're running.

  • See the smart contract?

  • We'll add The person was a dap groups Yup, university and I'll click Add person And there we go It a committed the count You can see the people count has been changed It's a Joe hello and the cows changed So that's the way that you can, you know, use other functions they're going to internal And we could see this increment count function isn't added to this list over here is 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 types of visibility, of functions and solidity.

  • Let's talk about function modifiers so we can add you no more.

  • You know words In terms of the end of this function to change how it behaves, not show 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 ad person function.

  • All right, so 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 ad 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 will show you how we can add an extra modified to this function to make that happen.

  • Basically, we'll just add a modified it.

  • So the only the owner of this smart contract could do that.

  • So first we need Thio.

  • Well, actually, a little like this will say only owner.

  • That's what the modifier would look like Now there's only under mortar fire doesn't exist yet, so it's going created.

  • All right, so we'll do that like this first round to keep track of the owner will say the owner is, um, you know, address.

  • I'm not sure if we talked about this in the data types video.

  • But, you know, an address is a data type inside solidity.

  • You know, the address that's on the network and account.

  • So this will be the address we're gonna declare here.

  • We're not gonna set it just yet.

  • We can set it like this.

  • We can do inside the constructor.

  • Well, actually, listen to that shit.

  • So that will be the owner.

  • We basically just create a modifier like this.

  • We say modifier, it's the only owner.

  • Good function.

  • And we'll do inside, if you're is right.

  • The logic that makes sure that you know whoever you know, calling the smart contract is the owner.

  • So that's what we're finding here.

  • Only owner and on Lee 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?

  • We're gonna compare it to this owner right here, But how do we know who's calling the function?

  • Well, solidity has global keyword called MSG, which basically stands for the function metadata that's passed in.

  • We're not actually gonna pass in any metadata here.

  • It's gonna be implied.

  • So basically we have access to initiate a sender, and this is basically a special Ah, nothing inside of solidity that tells us the account in his address, Who called the function?

  • And basically, we can just say, you know, is this person the owner we could 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 won a throwing error.

  • All right, so this is going to show you another consequence solidity about error handling so we can throw a nearer and solidity like, you know, I think earlier we had some errors happen.

  • Like, here's an ear.

  • We actually trigger an air.

  • If this person, um, you know, is not the owner we want, we want to revert the transaction.

  • You that, like this is that require, um sorry.

  • MSU sender is equal to the owner.

  • So basically anything inside of this require, um if if it evaluates to true than this passes, if it evaluates to false thin, 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, um only on only onem.

  • Our fire is complete.

  • It's basically Now we have this modifier that's defined here, and we added here and basically say, if the person who is called dysfunction is the owner Ah, then you know, we can actually run this function, and they're not.

  • We're going to create a failure.

  • We want to revert the transaction.

  • And also whoever doing this, you know, whatever.

  • Ah, like this go bone runs.

  • They won't pay this gas fee.

  • All right, so now we need to set the owner, and I'm gonna do that inside of instructor will use MSG.

  • Does senders well say, ah, function, constructor.

  • And we'll just open this curly braces and will say owner equals to MSG as our MSG did sender.

  • So I only the function key word here, and we all seem to make this public.

  • All right, so I'll say that so never deployed the smart contract like that.

  • You know, this constructor gets run and the MSG does sender is actually account that deploys a smart contract, and they're gonna get set to this owner state variable, right?

  • This address owner and basically that same you know, addresses.

  • The only person is gonna be able to call this ad person function, All right.

  • And if they, um yeah, they go to run, and we'll add a person.

  • And if we switch accounts of some other cow over here, then it won't work.

  • So let's just try that.

  • I deploy this, we will.

  • Ah, deployed.

  • But this account.

  • So it's changed that account at the person.

  • Actually, I don't have a deployed that account.

  • Now, let's try it again.

  • So make sure on the first count will deploy.

  • All right, Well, at a person will say DAP University at the person.

  • All right, let's see here.

  • All right.

  • It worked.

  • Your people count is 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 his sales and the people count has not changed at all.

  • So it worked.

  • We've kept track of the owner and we say only the owner could do it.

  • Now we're here.

  • I'm gonna show you a little code formatting that I'd like to do.

  • Sometimes it's getting kind of long.

  • And sometimes, you know, if you're like me, you have a text editor with the whole bunch of pains open.

  • I had to keep my columns kind of short sometimes especially solidity.

  • And it kind of makes it easier to maintain these functions when the arguments are getting long and using get and things like that, sometimes I will just break these up like this, all right?

  • And then I'll actually put the modifiers.

  • You have a bunch of modifier.

  • Sometimes it makes it easier to read.

  • And also, when I'm running my own solidity source code, I only used two spaces I don't use for Yeah, that's sometimes how break these functions up so I can see the function name and then the arguments, um, that I can see the modifiers, and then I could actually see the code that gets executed inside here.

  • And I can make your smart contract kind of long, but it can be sometimes this will catch you.

  • They have too many.

  • If you have three visibility and modifiers in here, sometimes it can be kind of tricky.

  • So am I doing this.

  • All right, So next I'm gonna show you how to work with time and solidity.

  • Okay, so we can do is basically compare time and solidity and instead of, um, 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 Ah, a contract is only open in a certain times, we'll say, instead of on Lee, Owner will say only while open only while open.

  • All right.

  • And this opening ah, state is gonna 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 of like, you're building a crowd sale or something like that that has an opening time again.

  • I seo smart contract and you say, Hey, we can only let you contribute ether like after you know, the first of the month.

  • Will 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 I'll say only while open, all right.

  • And instead of requiring that, uh, MSG that center is the owner, right?

  • Actually gonna just take this out?

  • We're gonna say, Ah, we're gonna make sure that the current time is ah in the future beyond a certain, like opening time.

  • So it's two u N ce to 56 opening time.

  • All right, so how do we do this?

  • Well, when you just set this opening time somehow, all right, so the opening time is actually expressed in seconds, you know, inside solidity or these time stamps are expressed in seconds inside solidity and, you know, seconds of what?

  • Well, it's epic time, which for not familiar with that.

  • It's, um, concept and computer science.

  • That's like the epic is a specific time.

  • I can remember the actual date.

  • It's like a date in the sixties or seventies shouldn't like that.

  • And basically, we just add seconds since that point in time in history.

  • So this is the current epic time stamp.

  • Is this many seconds since the every time.

  • And I guess she was probably a link here.

  • We can read more about that.

  • Yeah, the UNIX epic clock.

  • Um, yeah.

  • I'm sure you can get on Wikipedia and figure all that out, but Basically, this is the current epic.

  • Time is changing, so we use these seconds values.

  • So this is like the current, you know, epic time stamp will say this.

  • All right, this is how we could set this.

  • And it's, you know, you went to 56 gonna stroll 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 time stamp.

  • All right, So how do we do that?

  • Well, just like MSG is a global variable and solidity called block dot and we could say, blocked out time stamp.

  • All right.

  • And we can say is the block dot timestamp is a greater than or equal thio 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, um, then we won't.

  • All right, let's get this try.

  • I'm gonna update this time stamp.

  • Let's refresh it.

  • Will see, this is the current one paces in here s 0 15 I'm gonna add 60 seconds.

  • So a minute.

  • Um, So I'm gonna deploy this, and it will be.

  • Shouldn't work right now, so we can try to call add person.

  • Sort of add DAP University.

  • Okay, We'll add person and will see that it reverted.

  • Okay.

  • And now we can check the time stamp.

  • We're not there yet, so I'll just pause the video.

  • Wait for this to pass.

  • 75.

  • Think this actual time?

  • Yes, 75.

  • All right.

  • Composite video pause.

  • The video given us plenty of time or passed the time stamp 73.

  • And we're 7 75 So is deployed again.

  • Oh, actually, you know, it's not deployed again.

  • Let's just do Joe blow a person, and it worked.

  • So that's how you can use, um, time and saluted team.

  • You get the current time and set a time value of seconds.

  • And so that's I'm gonna call today.

  • Guys, I hope you all like this video.

  • Let me know how you're enjoying these solidity videos.

  • Let me know if I've missed anything or there's, um you wanna learn or if you're just not liking them, that's okay to just let me know down the comment section below.

  • Let's use the same my contract example that we've been using throughout this tutorial Siri's.

  • 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.

  • Um, c l squared and jump in and start programming.

  • What I show you first is how to right a function that accepts ether.

  • So, basically, like, how can we send ether in when we call function and do stuff with it?

  • So she had to do that first creative function, Uh, called by token, Say, by token, And this function is going to kind of emulate what happens in an I c e o like a crowd sale or a pretty sale or something like that on a theory.

  • Um, where you're buying E r.

  • C 20 tokens.

  • I'm not gonna, like code out an entire e r c 20 token smart contract right here or, you know, cut out a crowd sale.

  • Smart contract.

  • I've got several other tutorials.

  • Show you howto coachman Cryptocurrency on a theory.

  • Um, well, you know, building YRC 20 took and step by step and in a real world crowd sale things like that so you can check out the other videos if you're interested.

  • But, ah, what we're gonna do is basically create this function called buy tokens going to simulate what happens.

  • It's not going to do everything but Italy's give you an idea of how that works.

  • So well, call this, um, you know, by token and inside here, well, we basically want to do is buy a token.

  • And we also want to send either to, uh, the wallet.

  • So what's gonna happen is someone's going to call this function, and they're gonna send either 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 in when they call this function.

  • So she had to do that.

  • First, I'm gonna create, um, a way to track the tokens.

  • And basically all this is going to do is, um, you know, track this person's balance.

  • So he's a mapping for that same mapping. 00:48:34.11

everybody.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

Solidity教程--關於Ethereum、區塊鏈開發、智能合約和EVM的完整課程。 (Solidity Tutorial - A Full Course on Ethereum, Blockchain Development, Smart Contracts, and the EVM)

  • 1 2
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字