today I'm gonna show you how to write your first aetherium smart contract will create this simple counter smart contract with the solidity programming language we'll do it all in your browser so you don't have to install anything in your computer and you don't have to know anything about aetherium solidity or blockchain in order to get started I'm gonna walk you through step by step so before we get into that be sure to subscribe to this channel if you haven't already and click the like button down below and if you're serious about learning blockchain then you
need to join my free training on my website over at dap university comm for it slash booking in order to follow along with me head on over to remixed etherium order to get started so remix is a browser-based ide that allows you to start writing aetherium smart contracts without having to set anything up on your development environment you can do everything your browser you don't have to get anything installed okay so I'm gonna go and get started by clicking solidity that's the programming language that we're gonna use to write etherium smart contracts today so close
solidity and this is gonna give you a fully fledged development environment ready to go out of the box with solidity even comes the blockchain that's gonna run in your web browser so you don't have to connect to a public blockchain and you don't have to install a blockchain on your machine at all although I will show you how to do that at the end of this tutorial so stick around okay so I'm gonna go ahead and click on this little file icon right here this is the file tree that shows you where you can actually
create new theorem smart contracts in this browser-based IDE so there's already a file here called untitled that Sol you can create a new file if you don't have one I'm just gonna do this right so here's we're gonna actually start writing the code for the etherium smart contract so I'll start off by creating the Declaration of the solidity programming language that we're gonna use excuse me I'm gonna declare their version of solidity just like this say pragma eye solidity version 0.5 dot 1 alright some people do this with a caret but I'm actually going to
lock their version of solidity that's so that we know the exact version that we want to use and sometimes this is actually a security vulnerability because people could use higher versions of solidity that might actually have a security vulnerability that you don't want your app to be exposed to so that's just a little tidbit for you and we can create the smart contract like this we just say contract it's a key word and solidity and we give it a name I'm gonna call it counter and then we follow that with some curly braces open and
close all right it automatically close the curly brace for us whenever I hit enter so all the solidity source code for the smart contracts going to go inside of these curly braces the first thing that I want to do is create a variable for this counter smart contract I'm gonna call it u int count all right so let me explain what this means this is a variable in solidity and this is how we declare them we give it a type followed by the variable name that's because solidity is a statically typed language that means that
we have to declare the datatype that's going to be stored in the variable and it can't change so if you come from some dynamically typed programming language maybe like Ruby or JavaScript something like that where you know this could be a string it could be an array it could be whatever whatever it always change that's not how it works in solidity you have to only use the same data type anytime you you know assign this variable of value so in this case it's an unsigned integer that just means that it's a number that does not
have a sign so a signed integer could be negative for example like minus 2 would be a sign integer for the sign in front of it this is an unsigned integer does not have a sign in front of it so it's always positive that's what an unsigned integer is now there's different kinds of unsigned integers in solidity this could be you in 256 you can see all the different possibilities here it's gonna be you int eight right eighty eight eighty one eighty four forty eight to forty eight all right and this is just referring to
the number of bits that the integer can contain which is going to determine the size so by default there are 256 you can customize this to be smaller if you want but for now I'm just going to use you int 256 and I'm gonna use you int for short so this is gonna be the count inside the smart contract and I saw some note that this is a state variable excuse me so the value this variable is going to get written to the blockchain so anytime we update this value it's gonna get stored on the
blockchain like a database this is really similar to like an object relational mapper or something like that and another programming language or framework where you might be reading and writing from the database and have an object that you know is responsible for updating a certain column in the database that's how this state variable works inside of solidity essentially this council is gonna get stored in the blockchain and this is the variable that's in charge of reading and writing that value of count on the blockchain so let's create a way to set this value we're gonna
do it like this we'll save function increments alright so this is a credit function assign solidity use the function keyword followed by the name of the function and then you follow it with an open and closed parenthesis if it had some arguments we could put some arguments inside of here but in this case we don't and then you use curly braces open and closed and you put all the function code inside of here all right we're a lot like any other programming language that has functions you should be pretty familiar with this if you come
from any other language that has them ok so we can ascend we can change this value pretty easily right we could say count equals count plus 1 right that would just increment the count or change it by one we would just access this value and then at you know add it back into itself we can also do this with a shortcut we can say count is plus equal one just like that so that will change the count like this ok so I want to do one other thing while I'm here that will successfully increment the
count but I want to show you something that solidity smart contracts have in addition to functions and you know these data types like variables all that kind of stuff it also has events alright so I'll show you what that is so we can create an event inside the smart contract like this event increment and we will do this say you int value all right I can call this event inside here just like this say emits increment and then pass in account all right followed by a semicolon so in all these lines and solidity need to
be followed by semicolons just like this too let's load you know that you're going to a new line obviously we don't have to do those functions because the curly braces are self-explanatory okay so what is this event doing well solidity and aetherium allow you to subscribe to events to smart contracts so essentially we can call this function and anyone on the blockchain can subscribe to this increment event and find out that the count actually went up okay so basically anytime this increment function happened we can listen for it maybe inside of an app or any
other service on the web or something like that that's going to listen to the blockchain and know that this function count happened right and it doesn't matter whether we called it or not anyone can listen for this and we can also find you know the history of these events on the blockchain from an event stream and stuff like that's pretty cool so it's pretty powerful just to add this to your functions and of your programming smart contracts I do a lot do it a lot in professional projects for especially for histories and things like that
good so we can also create a function to decrease the count this they functioned decrement all right and we can you know say count equals count minus one all right or we could do the shorthand just like we did a minute ago and say count - equals one and that will also dicker with the count and we can create an event for that just like this say event decrement and say you int value and that's going out anyone to subscribe to this decrement value in order to you know do the same thing I did with
increment if anyone wants to find out that this value went less they can subscribe to this event it doesn't matter whether they call the function or not okay so we can omit decrement all right and then pass in the count all right perfect so now we want to be able to call these functions on the smart contract from our IDE and there's a problem currently these functions can only be called inside the smart contract cuz they don't have any visibility set to them so what do I mean by that well we can't call this function
from outside the smart contract because it doesn't have a visibility of public all right we could do that just like this all right so these visibilities are things we can add two functions to tell solidity where the function can be called you know inside the smart contract out so the smart contract etc etc alright so if we want to be able to change the count we have to add public here and likewise we have to add public here all right perfect so now what I want to do is set a default value for this count
right so I can do this with something called a constructor function all right so the constructor function is the function that gets run whenever this smart contract is initialized for the first time or when the smart contract is created that's the same thing okay whenever it's initialized it's created it's deployed it's put on the blockchain remember smart contracts are code that live on the blockchain and anytime we deploy them we're actually updating the blockchain state and putting the code on there so that I can get run on the etherium virtual machine okay so I can
create the constructor function like this this is the function that's going to get Rome and Everest deployed just call it a constructor okay we don't use a function name or anything like that which use constructor and we create the function like that and then we also set the visibility to public just like the other functions and now we can set the value of this count like this we can say sorry count equals zero all right and that's gonna set the default value for this count whenever we deploy it to the blockchain now I want to
show you one more refactoring that shows you you know how to create a constructor function how to do stuff inside of it while we're here I want to refactor right this is a little long-winded but it does show you how the constructor function works so I'm gonna set the default value of the count right here boom we can do it inline without having to do it inside the constructor function both of these are fine and I wanted to show you how the constructor function worked but this is a simpler way to be explicit about how
you're setting the count and you could go ahead and change it to anything you want it to be 10 everyone it could be you know a thousand really fit to zero for now okay so let's go ahead and run this smart contract and test it out I'm gonna deploy it to the blockchain that's connected to this remix browser IDE already okay so I'll go to this solidity icon here and I'm going to choose the version of solidity that I want so I chose 0.5 dot one I'm gonna say 0.5 dot one right here okay this
is going to match the version that we said in the file and solidity is the language EVM compiler default this is just the version of the etherium virtual machine we don't need to change this all right so we'll compile it and see what happens all right no warnings that's great if you see some warnings down here it should have either some error messages or some warning messages just read those and fix your smart contract code to fit whatever this says now I'm going to I'll see actually deploy this so I'm gonna deploy it to the
blockchain that comes with this IDE alright so what you can do is click this icon right here this deploy and run transactions this is just an ethereal logo and it's gonna give you several different environments so you can use the JavaScript virtual machine basically what that is is it gives you a JavaScript based blockchain that runs in your web browser and that way you don't have to install anything you don't have to connect to a test network on aetherium you're gonna do it right here okay so I'm gonna use that and it also has five
different accounts with ether right RT so you don't have to like request aetherium from a faucet or anything like that you can just use all these accounts that have funds in them and though this is not worth anything on the real aetherium network sorry yes so let's go ahead and employ it I'm gonna click counter click deploy and there we go the smart contract is a deploy to the blockchain so we can see the functions that we created are exposed down here so I can increment looks like this all right yeah well actually tell you
what I just realized that I forgot one essential component we forgot to create a way to read the count all right so let's go ahead and do that I'm just gonna create a function that says get count function get count and this function is going to read the count back save you public and so view here is telling the solidity that it's just going to return a value so view public and then I'm gonna say return count all right and also I need to specify the return value s a return you int oops sorry had
a little problem there all right so my bad guys I forgot to add this so now we have a function that says get count all right and I'm also going to show you that we can use the get count like this or we can also declare the state variable public like this you went public count equals zero solidity will expose a public function just like this that's gonna be called count so we'll see two functions the two ways to do it get count you can run it out long ways like this we're gonna do this
shortcut where you can just read the count from the smart contract with a shortcut like this all right so let's go back to the compile make sure it compiles correctly all right no problems go back to deploy I'm gonna click the trashcan on this cuz I don't want to use that smart contract anymore I'm gonna keep this JavaScript virtual machine instance click deploy alright now we see all the functions that we want increment decrement count get count all right so increment will go and increment it and we'll get the count boom it's 1 and also
we can you know call the count function that was created by default like this count but I'm also 1 so we'll increment it again counts to get count us to increment it one more time count you know alright works now let's try to decrease it with decrement decrement alright decrement 3 decrement boom all the way down to 0 perfect alright so both these functions work awesome so congratulations you just written your first aetherium smart contract with this little programming language in your browser alright so as I promised earlier in the video to show you how
to do this with a different blockchain so what you can do is use ganach if you've seen any of my other tutorials or you're maybe you've tried aetherium development before you have seen ganache so ganache is a personal blockchain that runs on your computer you know on your desktop it's got a nice graphic usually your excuse me graphical user interface you don't have to run from the command line or anything like that I'll pull up on my screen here so ganache is a blockchain a lot like the one that was in your brow and it
has a whole bunch of free accounts in here that have ether and things like that so we can actually connect to the ganache blockchain like this let's just do here still web 3 provider and say yes the local host port 75 45 and then we'll deploy the smart contract just like this and there we go bone you can increment it counts increment accounts boom there we go we can see all the accounts here just like that so that's how you connect to your ganache personal blockchain now you can also do this with an injected web
3 provider that would allow you to connect with meta mask alright so meta mask is the etherium wallet that you know runs in your Chrome browser and right here and this would allow you to connect two different networks so you could connect to this with the cove on test network you do the main aetherium network let's see your stress switching networks sorry I had a little hiccup in my browser here so you could deploy the different networks like the main aetherium Network I asked oh here let's do injected provider all right account open meta mask
sorry I have to open and set my password so you could use any you know metric you wanted to you could deploy the smart contract the main aetherium network just like this you could do a test Network like Kevon right and that's a way that you could you know access some other network to put the smart contract on alright so I hope you all liked this video again subscribe to the channel if you haven't already click the like button down below and if you were to learn more about how to build blocking technology you know
if you're serious about becoming a blockchain developer you should join my free training on my website over add a pin your bursary com4 it slash bootcamp alright until next time thanks for watching DEP University