How To Create Your First Ethereum Smart Contract – Live Coding

– Hey guys, welcome to this webinar on writing your first smart contract on the Ethereum Blockchain. If you are not familiar
with Ethereum or Blockchains I encourage you to read
our awesome articles and courses online at where you can learn more
about the Blockchain and Ethereum and the details
of its inner workings. But in this webinar,
we’re gonna take a look at how to write a very simple smart contract using a language called solidity. So a smart contract is just a program deployed on to the Blockchain. And this program can contain a set of variables or
fields and set of methods that manipulate those fields to achieve the task of the smart contract. So in this webinar, we’re
gonna take a look at a very simple hello world type example and just get a very
gentle intro to solidity and how you would go about developing smart contracts
on your own machine. So to get started, we are just gonna open up our browsers. I am just using chrome over here. And we are gonna use a tool called remix, which is in open source IDE specifically for solidity smart contracts. And you can just get it by
going to and it will just load up this webpage which presents an IDE. Now my favorite part about remix is that you just have to go to and you don’t have to
install anything else and you have a full-fledged IDE where you have an editor, a debugger as well as the test blockchain
where you can test out your smart contracts. So I really highly recommended, It’s open source, you can go to the get hub
and downloaded as well and run it locally if you like. But this is a very convenient
option to get started, just in your browser without having any sort of complicated set up. So let’s just give you
a brief intro to the UI. On the left hand side
we have this menu bar, where we can select which of our contracts you wanna open. So this is just a little file explorer, you can also add a new file
and open an existing one. In the middle over here,
we have our text editor where we type in our solidity code. When you go to the page there is a sample voting smart contract that
is given as an example. I encourage you to through it, but this is little more complicated then what we are gonna
see in this webinar. So on the right hand side, we also have another menu bar that has some tabs for us. We have a compile tab, where we can select which smart contract we wanna compile and we can also get some more details about the compile smart contract like its bytecode and its interface. On the run tab, which is probably we’re gonna be using most you can you know set up which environment you want to deploy your smart contract to. And we are gonna use JavaScript VM and if you just mouse
over each of these options it tells you more detail
about what it does. But JavaScript VM all it really means is it will spin up a test blockchain
inside this browser tab. And you can use this test Blockchain to deploy your smart contract and the benefit of that is obviously you don’t need to
actually pay it to deploy a transaction to the
production Ethereum network and you just test and develop your contracts for free. So we are just gonna click JavaScript VM. These other two options
allow you to connect to other instances outside of the browser, but we are gonna get into that. We just gonna keep it really simple and click JavaScript VM. And when we click that we see that this accounts
drop down also generated some values for us. So we see five accounts
that were automatically generated for us with a
hundred test ether each. And the first one is
selected for us by default and we can copy the address if we like. Gas limits you can specify
you know how much gas you want to sort of provide
for your transactions. And values also how much
ether you wanna provide with your transactions and you can specify that in way or ether and all of these are just smaller denominations of ether. And down here we can also select which smart contract we wanna run. So if we have multiple open, we can select them from this drop down and we can either create a new instance of this smart contract or we can connect to an existing instance by
using the add address button. So in order to create
a new ballot contract, I would actually have to put in an integer to designate the number of proposals and this lines up with
this parameter over here being passed into the constructor and I would just enter
some value and click create which we will see in a bit
with our hello world contract. And just one last thing
with UI at the bottom. We have this terminal which is gonna print out some useful logs
and transaction details for us to help us debug and
understand what is going on while we are developing. So all that being said
there was a quick intro to remix in the UI, Now let’s create our first smart contract and we will create a new file by clicking this plus button and we’ll just call our contract instead of calling a hello world, we are gonna make it a
little bit more interesting and we are gonna call a code word. So the first line at the top of any solidity smart contract
is the pragma statement where you declare which version of solidity you will be using. So I am just going with point four dot 19 where you can see that this
specific version of remix is actually using a slightly newer version point four dot two two But it’s upto you whatever you wanna use and this caret sign this upwards arrow indicates that any version above point four dot 19 is also okay as well. Now to declare our smart contract, we use the contract keyword. Very similar to how you
will use the class keyword to define the class in Java if you have the Java background and we are just gonna
call our contract outdoor. So we’ve defined the structure
of our contract by using the contract keyword and providing a name for it, awesome. And we also gonna define a
variable in our contract. We need to find the string
variable called reading, which we’ll just store a string value and the next thing we are gonna define is a constructor. So smart contracts can have a constructor, that is invoked only once and that’s only when the smart contract is first deployed to the Blockchain. So if you are deploying it
to the production Blockchain, we’re gonna call then but as we will see in this example when we deployed to our test Blockchain
it will get invoked. And the way we declare our constructor is by saying function using the function keyword and then using the same name as we did for the contract name that’s how the compiler can tell that this is the constructor and in our constructor, we also want to provide
the first initial greeting that should be set and we’ll pass that in as a parameter and just set it to our
field, our greeting field to be the initial value. One more thing we are gonna do is we are gonna declare
this variable public by just adding the public keyword in front of the variable name and what this does is aside from making this variable public, it automatically generates
a getter function for this variable. So if I wanted to provide a public getter for the greeting variable you know normally I would
have to do something like this and explicitly declare it that it returns string and I would have to say
something like this. But just by adding this public key word, this function is
automatically generated for me so I don’t need to explicitly declare. So one thing we are gonna add to this smart contract is a
way to change the greeting. And we’re gonna do that by
declaring another function and we’re gonna call our
functions set greeting. And we also gonna pass in the new greeting as a parameter into the function and we are gonna do the same thing, we are just gonna set our variable to whatever parameter was passed in. And there we have a very
simple smart contracts, very simple hello world smart contract that gets initialized
with the greeting, right and anyone can read this greeting from the smart contract, because we declared the variable public. And anyone can also set the greeting on the smart contract. So we wanna provide a
bit more access control to the set greeting function. So how would we do that. Let’s take a look So I wanna update this
set greeting function so that only the owner of this contract only the person who deployed this contract we can call the set greeting function. So the first thing I
need to do is key track of who created this contract. And the way I can do that
is in the constructor, I can declare an owner
variable which is just going to keep track of the person
who has sent the message. So when I invoke message
dot sender it’s referring to the account where the address that invoked this constructor. And you can only invoke the constructor where you are deploying it. So this owner filed is
of the address data type and we’ll also make it public but it’s gonna store whoever
was the person sending the deployment transaction
for the smart contract and we’ll report that over here. Now to make sure that
only the owner can call the set greeting function. We are gonna use the require function and the require function all it does is it checks in condition and
if it evaluates to false then it throws an exception and the transaction is reverted. So the execution does not
continue beyond that point. So inside the set greeting function, we are gonna make sure that owner equals message dot sender. So when I use message dot
sender in this function, it’s actually different from this message dot sender, right. Because in this function
this variable is referencing whoever called the set greeting function whereas in the constructor
message dot sender is referencing whoever
called the constructor. So using this require statement, I can ought check that says hey make sure that the
person calling this method is actually the owner and if it’s not then throw an exception and don’t continue going down this function any longer. And one more thing that
I wanna demonstrate is just a way to broadcast any
you know event of interest that occurred during execution and the way we do that is using something called solidity events. And we can declare a new event by just using the event keyword and giving it a name and
you know you can also pass parameters to the event to provide extra information about the event. So in our case all we are gonna do is provide the old greeting as
well as the new greeting. So that anytime the greeting is changed, whoever is listening for
that event is notified and typically who ever the
person or the application that is listing for this events are front ends which need to you know update their state based on changes to the smart contract. And so we have declared
our greeting changed event at the top using the event keyword and the way we fired the event is just by calling the name of the event and also passing in the parameters, but if we pass in the parameter after we have changed our greeting then we’ll lose track, we’ll lose reference to the old greeting so we just gonna move this line below the event so that we can print out the old greeting as well
as the new greeting. So there we have a simple smart contract that allows you to
change and set a greeting and it fires an event whenever that greeting has changed and tells you what the
value was changed to, okay awesome. So we have written the solidity code, you might notice, it looks
very similar to JavaScript so it shouldn’t be you know too different, but you know its
JavaScript with data types and some other extra features as well. But we just saw a very simple example of how to use some of those features. So now let’s see how we
can actually run this code inside the browser on a test Blockchain. So like we said we select JavaScript VM from the environment drop down. We are just gonna keep it selected to the first account, any account will do. And in this dropdown, we
are gonna select outdoor. And as you can see to
create a new instance or to deploy a new instance
of the smart contract, we need to provide the initial greeting. So the initial greeting
will just be outdoor and if I click create we see that a new square
pops up towards the bottom of the run tab telling us that a new
instance has been deployed to the test Blockchain
notified by this JavaScript VM. And we also saw you know
some output printed out in our terminal over here below and it basically printed
out a log of the transaction that was invoked to
deploy the smart contract. So to deploy the smart contract, we took this solidity code compiled it down to EVM bytecode which is just you can take
as low level machine code And we send that machine
code to the Blockchain, so in this case to our test Blockchain running inside our browser and that deployment is sent using something called the transaction. You can take up a just message being sent to the Blockchain. And in this message we, let’s take a look at the
details of this message that we sent to deploy this smart contract to the test Blockchain. So if I click the details button, you know it’ll tell me the status that you know the transaction succeeded. It will also tell me the
address of this smart contract. So whenever you deploy
a new smart contract, it is written, it is assigned an address, when it is successfully deployed and any further interaction
with the smart contract is done using this contract address. We also see from tab
which is just from field which is just our address CA35B, which matches up with what
we have over here, right, because this is the account
that we selected to deploy. And the to address is left empty and it just says that
since we are not sending this transaction to
any particular address, we are deploying a new smart contract to the Blockchain. And it also indicates
how much gas we provided. So gas is just a unit
of competition, right. So each of these lines in solidity translates to some EVM
bytecode and each of those EVM bytecode operations requires
a certain amount of gas. And it varies depending
on what the operation is. But you basically have
to provide enough gas for your transaction to complete And after you have
deployed the transaction it will tell you, you know,
how much gas it cost to actually send the data to the Blockchain which is the transaction cost. And how much gas did it cost
to actually run the logic on the Blockchain which
is the execution cost and the execution cost in
this case would just be you know running the constructor and initializing our greeting
and our owner variables. We are also given a hash
for this transaction. So this hash references
this particular transaction and the from address, the to address as well as any data provided with it. And the data I am just
gonna increase this a bit, the data is provided in this input and this is the compiled EVM bytecode that is sent to the test Blockchain. So this is what the compiled solidity code is translated into and this is the data that is actually sent to the Blockchain. And we also we since we provided a greeting input parameter this is also included
in our transaction log and since we didn’t
provide any ether with it this just indicates the zero way. This logs field also will print out any events or other things of interest first and these are, these logs
will be forever stored on the Blockchain so we can come back and re-use them and revisit them to extract information. And we’ll see how these logs get printed out in a second. But this is just the details
of one transaction to deploy our smart contract to the
test Blogchain, awesome. Okay and you’ll also see
that the buttons that we have available inside
our smart contract instance line up with the functions
that we have available to invoke on the smart contract. So we have the set greeting for the set greeting function,
we have this greeting function as well which
is auto generated for us using this public keyword as
well as the owner function which is auto generated for us using this public keyword. So if I just click owner,
right, it’ll tell me the address of whoever is the owner and it matches up with the address that we have selected over here and so calling the greeting
or the owner function doesn’t require any gas. And that’s because all
we’re doing in the greeting and the owner function is reading data from the Blockchain and we are just retrieving
data from the Blockchain. We are not actually executing
any code or any logic, which is what gas is okay. So gas is just competition, right, and if you are running some competition, then you need to cover
the cost of that gas. But in this case if you are just retrieving the owner address or if you are just retrieving
a string greeting value it doesn’t actually cost any gas. And we can see that in the calls that are being printed out
in the terminal over here, right so when I clicked greeting let’s take a look at the details
of this transaction, right, and it will indicate to us that you know this cost only applies
when called by a contract, but what we are seeing over here is that you know it’s slightly
different from the transaction that was required to
deploy this smart contract, which is where we are writing some data to the Blockchain. But over here we have a
little less information and we see that it didn’t
actually cost any gas to retrieve this string value. Yeah and the output of this invocation was provided in our decoded output field. So okay now let’s see how we can actually set the greeting. So first let’s test our
require statement over here to make sure that only
the owner of the contract can call set greeting. So if I change my address
that I am using, right let’s use our second address and just one thing before I do that you will notice that our
balance for the first address changed from 100 ether to 99.99999. And that balance was reduced
as a result of the gas cost of deploying this smart
contract to the test Blockchain. So it’s taken directly
out of the first contract. But let’s quickly change
it to the second account and let’s try this set the
greeting to something else. Right I am just gonna
clear my terminal over here so we can get the fresh output and if I try to click set
greeting let’s see what happens. So when I click details, as well as look at the
output of my transaction log, but when I click details it tells me that the transaction failed but it doesn’t tell me exactly
on which line it fails. So this is one of the limitations of exception handling
and solidity right now. You can tell that an exception was thrown, but you can’t really
get any more information about the exception like
some sort of string message or what line it was on, but this is feature that is
currently in development, but right now there is
no easy way to figure out like what line exactly
the exception was thrown. But we can also see more details about this transaction. We can see that it cost us gas, so we have to provide gas. And even though our
transaction failed, right, we still have to cover the
cost of the gas ,right, because it’s still running competition, it’s still needs to go
through the competition to figure out that hey
there is exception over here so there is some cost
for this transaction. And yeah we can see our input, which is just our encoded string, which is outdoor and also this input contains
the information about which function we are calling
on our smart contract. And we can see that the to
address is the same address as our smart contract, which is 692A and that lines up with the address, which is also shown over here, 692. And we can see that our input parameter was also included. But we can see that the transaction failed and which is will be expected because we are not the
owner of the contracts. So we shouldn’t be able
to set the greeting. And in the logs, we can
see that the logs are empty so we know that no events were fired. Okay, so now let’s try
the same thing again, by using the actual owner address, right. So I’ll switch back to the owner account, and now if I try to click set greeting, let’s see what happens, you gonna clear the terminal, so we have it fresh and
click set greeting, right and we see our transaction
log being printed out and this time we see that
our transaction was mind and it succeeded, right, so the to address again is
just our contract address and the input contains information about which function we’re calling as well as any input parameters and we also have hash
for this transaction, which uniquely identifies
this transaction. As well as information about the gas cost of running this set greeting function. And since the execution succeeded and the greeting change
event was fired on line 17, we can see that it was
printed out in the logs field of our transaction, right. And we can see that you know it printed out the event name which is greeting changed and it printed out the arguments which was the old greeting
as well as the new greeting. And we can verify that
the greeting was updated by just clicking the greeting button again and we get the new string that was passed into the smart contract. So this is just a very simple example of a hello world type smart contract where we can read some data from it like a string reading and we can also write some data to it and provide some access control around that ability to write data
to the smart contract. So that brings me to
the end of my example. This is just a very gentle intro
to solidity smart contracts just to get you used to the syntax as well as this interface of remix, which is a really great interface for developing your smart contracts. And if you want to learn more and get into more detailed examples about smart contracts and some other codes and limitation of them, definitely check out our online courses as well as our online articles. One of the courses eth 101 is the course that I teach on Ethereum and how to build solidity smart contracts as well as how to built
more complicated dApps including the front end to interact with the smart contract. So I highly recommend checking those out at and
yeah thanks for tuning in to this very simple webinar on how to write your first smart contract. And we look forward to
see on

Leave a Reply

Your email address will not be published. Required fields are marked *