1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Smart Contract Development with Solidity

Your First Smart Contract

Start course
Overview
Difficulty
Intermediate
Duration
3h
Students
49
Ratings
5/5
starstarstarstarstar
Description

This course provides a deep dive into the Solidity programming language which is used to implement smart contracts on blockchain platforms. This course will give you a practical understanding of Solidity and how to develop your own smart contracts.

Transcript

In this lecture, we're going to develop your first smart contract which really write  something and read  something. We already we're working with this Hello World example before, but this is really just a simplistic example, you can't really do anything with it. And, in order to do  something with  Solidity, we are going to write two functions, and actually writing function and we're going to remove this public keyword here or you can leave it here and we're writing a get function, because the two have a different meaning in  Solidity. Now there are two ways how you can do this, you can either follow along here, then at the end of this lecture there's a little coding challenge, or if you don't want to do the coding challenge and you don't want to follow along then as promised in our code repository on the S01, C01-Hello world, you can find this start and end files. So, C01HelloWorld_start will be empty. There's nothing in there. If you don't want to follow along, then you can just directly go to C01Hello World _end Solidity register file code. And I'm going to close this now and I'm going to start writing and commenting what I'm doing here. Now, we want to write into this Hello World string. And in order to do that we need  some kind of a function or an interface to the outside world to let  Solidity or the Compile Solidity write to the storage position of this Hello World string. And this usually works as with all the other programming languages, it's nothing different here with function. And functions start with a keyword, 'function' and then you have the function name and that is in mixed case style. So, the first letter is lowercase and let's call this write, Hello World for example. And then the other words would be capitalized,  so hello would be capitalized, world would be capitalized, "writeHelloWorld."  According to the style guide. Now obviously if you want to have your own style guide, you can make these all capitalized letters or you can make it all lowercase or you can make it with underscores. So, this is just according to the official style guide from Solidity. And then in round brackets you can have arguments that this function will take, and in our case it'll take a string, and I'm going to leave this in there with an error now. So, I'm going to have a string which is called, and I give string, give arguments, and underscore, and I will tell you in a second why? So, a string, Hello world and then you have a function visibility specified at the end and this function visibility specifier tells Solidity if this function can be called from outside, inside, other smart contracts, extended smart contracts that extend the smart contracts. So, with inheritance or just inside the smart contract and there are four different visibility specifiers for this. That one is public and which we're going to use and then you have the curly brackets. The other visibility specifiers are private. And private would mean that you can call this function only from within this smart contract. This Hello World smart contract. Then there is internal, which means that you can call this function only from within this smart contract or every smart contract that extends this smart contract. We are going to talk about this with inheritance later a little bit more. And then there is another visibility specifier called external. And this one specifies that you can call this function only from outside. So, only from any interface from outside smart contracts. So, no other smart contract could call this function here. Now, we are going to leave this at Public for now. It's the easiest one in our case and we see here an error and errors are always popping up on the left side with this little 'x' here. And when you hover it then you see the error message. And mostly it's an informative error message telling you that you have to do something or what you have to do in order to fix this error. And in our case we have here a string as an argument and this string as you might have heard me saying before it's a reference type. That I will explain it to you later, but in general, since Solidity 0.5.0, you have to tell  Solidity where these reference types are stored and there are free storage locations and this is  also  something for later in more detail but I want to give you a heads up now, their free storage locations that one is called data which is basically over here, the transaction data field. The second one is memory which is like your work host, like your RAM on your computer. And then the third one is storage and that would be like your hard drive on your computer,  something that is stored forever. And if you keep it in memory, which we're going to do here,  so we say string memory, Hello world. Then we can access this _helloworld argument which is then a Hello World variable inside this Hello World, bright Hello World function and now we can simply take this Hello World and write it into this Hello World variable. So, we say hello world = _helloworld and I use this underscore here even though I think it's not in the official style guide, but I use this underscore in these arguments because when you access it later with JavaScript then you have a clear distinction between JavaScript variables and variables that belong to a smart contract  so I just use this for myself to keep an eye on how I use my smart contracts. Okay, then we could give this a try and I, from our previous lecture, I still have my smart contract here and one thing that you might know or might not know due to the inevitability of this blockchain, we cannot update any smart contracts. So, our Hello World contract here still stays the same. But in order to use the updated version we have to deploy a new version. So, every time you change  something and you want to see the updated functionality here, then you have to hit 'Deploy', and what happens next is once you hit 'Deploy', new transaction is sent off to this internal Blockchain to this JavaScript virtual machine and you have your deployed contract over here on the bottom. And now when I hit the 'Hello World' over here then I have this Hello World here from this string and when I update this string, and I have to put this in double quotes. I think single quotes work as well. But I'm going to put it in double quotes. And I call it "Hello my World!" And then I write Hello World. And you see on the left side the transaction happened and the transaction went through this little check mark here, green one, and when I hit then 'Hello World' here then it updates Hello my World. Now again, one thing that you can observe and I want to highlight it. You write Hello World. So, you indirectly view smart contract. But in order to read the updated value, you have to execute another transaction or another call against your Blockchain node. So, it doesn't stream through, it doesn't update or cascade any updates to your clients or it's like you read data from an API and then on that server this things the data is updated but on your client there's still the old data  so you have to read the data again. So, this is  something that I want to highlight at this point. Now obviously this Hello World is our automatic get function that was generated by  Solidity by this public keyword here. But now I want to write a get function by hand and I'm going to call this getHelloWorld. So, again we start with the keyword function getHelloWorld and this time we have no arguments and I have a public keyword here and I have a returns keyword, and inside this returns keyword I have to give  Solidity the types that are returned from this function. In our case it's going to be a string. And again because it's a reference type, I have to tell Solidity where the string is stored and it's going to be stored in memory for now. And then, I have to have the curly brackets and inside the curly brackets there's the function body and here I simply return Hello world. Now, there is one important thing here that I want to highlight. It's  also in this exclamation mark on the left side which is not an error, it's a warning and let me read out the warning for you, Warning: function state mutability can be restricted to view. What does it mean? We are not changing any information here. We're just giving back what is already known on the blockchain. So, we're not writing to this storage variable. We' re just reading from the storage variable. And there are two ways to read from storage variables without sending a transaction that actually costs  something. Now if you think of a blockchain, if you think of how the blocks are distributed then every node, every client from this blockchain has a copy of the data. And that means, now when we don't need to write anything we don't have to inform all the other nodes, there's no mining or nothing, we just have to read from the local copy of that database which  also means we don't have to send off a writing transaction. We can send off a reading transaction and reading transactions are called 'call' which you see here highlighted in this transaction log window, here's a call and the other one needs mining and once this is mined and which happens instantaneously on our little JavaScript virtual machine blockchain here in the browser, then the checkmark output is here. So, whenever you read  something you want to actually have the call. Now let me deploy this without the keyword view over here and we will see what happens. So, I see here in my new instance that getHelloWorld is in pink which means it's a writing transaction and it needs mining. Now if you're going to make this view function with the view modifier here then obviously we have to redeploy this smart contract and I'm going to remove all of my smart contracts here by clicking on this 'X'. I can  also just click on this little trash icon here but usually I just removed them with the 'X', the ones that I don't need and then I redeploy and then guess what? If you click 'getHelloWorld' then this one will first of all be in this bluish color and it will output you HelloWorld, famous HelloWorld itself. But that's not the point here. Now here is the coding challenge. I'm going to add another variable. It's going to be a uint256 which is an unsigned integer ranges from 0 to 2^(256-1) which is a very very large number. And I call it myUint, I don't have to initialize this because every variable in  Solidity is initialized with this default value which is zero for uint,  more on that later. And I'm going to make this public but the challenge here is go and write a setter function,  so we can set this uint to a specific value. If you want to do this coding challenge then pause the video now and in about five seconds or so, I will show you how this is done. All right,  so this is how you write to this uint, it's actually pretty straightforward and I hope you could finish that. You need the function. And I just call this function setMyUint and obviously you need one argument here which is again uint256, you can also just call it uint, its in alias  and I call this myUint, for me it's going to have an underscore here and it's going to be public function and this is going to write to myUint is myUint. So, what I'm going to do next is I take this code and I'm going to put it into the GitHub repository. So, you have the exact same code in the GitHub repository. And in case  something changes, and in case there are code updates necessary, then I'm going to comment this code updates in the GitHub repository. So, in case the code in the GitHub repository doesn't look exactly the same, check out the comments. Now before I do this obviously you want to try this. I'm going to deploy this smart contract, scroll down to my latest instance here. myUint is 0, I set myUint to 5. And I have to click this myUint pattern again, and myUint is 5. In the next lecture we are talking about starting, stopping, and interacting with smart contract. It's going to be a very interesting lecture about the AVI arrays, about addresses, and about instances. So, I'll see you in the next one.

 

About the Author
Students
58
Courses
7

Tom is a CTO, senior back-end developer, and systems architect with over twenty years of hands-on development experience in a variety of languages and systems. He has a CS master's degree and has been working with Ethereum and blockchain technologies since 2016.

Covered Topics