Solidity Tutorial: Complete Step-by-Step Guide For Beginners 2019

In this Solidity tutorial you are going to learn how to code Solidity smart contracts by developing 5 smart contracts on Ethereum, with step-by-step instructions. This tutorial is for total beginners to Ethereum and Solidity, and does not require to install any development tool. You only need a web browser.

WORD OF CAUTION: Solidity changed a lot since its creation. The most recent version is Solidity 0.5.6. This tutorial covers the 0.5.x versions, i.e the most recent one. Be very careful with other tutorials as many of them teach older versions of Solidity (0.4.x) and are outdated.

Solidity is the most popular language to write Ethereum smart contracts. This is a high-level language that compiles to bytecode for the Ethereum Virtual Machine (EVM). Its syntax is similar to Javascript, but despite this superficial similarity this is a very different language:

  • This is a compiled language, which means you can’t just run it directly in a virtual machine like you do with Javascript. There is a separate compilation step before you can run the code.
  • This is a typed language, which means you need to declare the type of each variable
  • It’s much more limited than Javascript

Despite its shortcomings, that’s the best we have to write smart contracts on Ethereum. If you want to unleash the power of decentralized applications on Ethereum, you have to learn it!

This tutorial is part of Smart contract 30, a full course to learn how to develop Solidity smart contracts. In Smart contract 30, you go from beginner to expert level in Solidity by building 1 smart contract per day during 30 days, for a total of 30 smart contracts. This covers most of the aspects of the Solidity language, as well as many business applications: financial transfers, ERC20 tokens, DAO, governance / voting, private data on blockchain, etc…After you finish this course, you will be a competent Solidity developers, ready to develop smart contract at a professional level. For a full description of the course checkout the curriculum on EatTheBlocks Pro.

This tutorial gives away for free the first 5 Solidity tutorials of this course.

Contract 1: Simple smart contract

The first smart contract of this tutorial will be like its name implies, very simple. The focus will be on the tools and the workflow. We will:

  1. Discover Remix, the IDE for Solidity
  2. Code a Solidity smart contract
  3. Compile the smart contract
  4. Deploy the smart contract on a local blockchain

1. Discover Remix, the IDE for Solidity

First, go to your browser and load Remix, by going to https://remix.org:

It’s all online, so you don’t have anything to install. Remix is the most popular development environment for Solidity smart contract, and the easiest to get started with Ethereum development. So it’s a good investment to learn how to use it. Beside editing Solidity code, Remix also allows to:

  • Run a local Ethereum blockchain
  • Compile smart contracts
  • Deploy smart contracts on local blockchain
  • Interact with smart contracts with GUI

The fact that Remix manages a local Ethereum Blockchain is a huge win. Without this, you would need to use several separate tools and make them work together. Using Remix massively simplifies smart contract development.

Let’s see what we have in the UI of Remix. The screen is divided in 4 parts: the file editor, the code editor, the console and the toolbox on the right (click to enlarge):

Ethereum Remix IDE tutorial

  • File editor: to create Solidity files (for your smart contracts)
  • Code editor: to edit Solidity code. Has syntax highlighting.
  • Console: to see transaction results (success or error messages).
  • Toolbox: Multiple tabs to compile, deploy (the run tab), debug, smart contracts and more.

2. Code a Solidity smart contract

First, let’s create a file for our smart contract. On the top left corner, in the file editor, click on the “+” icon to create a new file. We will call it “SimpleContract.sol”. Usually, smart contract files start with an uppercase. The extension for Solidity is .sol.

Next, you will see the file appearing in the code editor at the center of the screen. Let’s write a pragma statement:

pragma solidity ^0.5.6;

This should always be written on the first line of your smart contract. This indicate to the compiler which version of Solidity this smart contract was written for. If you try to compile the smart contract with an older version of Solidity, the compiler will refuse to compile. This is a security mechanism. You will also notice that we use a semi-colon to terminate our pragma statement, like for all Solidity statements.

Next, we are going to define our smart contract with this line:

contract SimpleContract {}

In Solidity, to define a smart contract you need to use the contract keyword, followed by the name of the contract. What we have defined is the most simple smart contract possible. Between curly braces you can define variables and functions. In this tutorial, we will keep our smart contract very simple and focus on the workflow instead.

By the way, if you have errors, the syntax highlighting or Remix will show it to you: there will be a red line below it, and a red cross on the left gutter. If you hover on the error, Remix will show you an error message. This is extremely convenient, especially for learning.

3. Compile the smart contract

In the toolbox panel on the right, go to the “Compile” tab. In the dropdown menu, select the same version of Solidity as our pragma statement (i.e Solidity 0.5.6). Next click on the button “Start to compile”. You should see a green box appearing below in case of success. In the next tutorials, make sure to tick “auto-compile” so that you don’t have to manually compile every time you change the source code.

4. Deploy the smart contract on a local blockchain

In the toolbox panel, go to the “Run” tab. This is used to deploy and interact with smart contracts (click to enlarge):

remix ethereum run tab tutorial

For the environment, make sure to choose “Javascript VM”. That means that Remix will deploy your smart contract to a local Ethereum blockchain. This blockchain is not connected to the real Ethereum blockchain (mainnet), and does not use real Ether. You will be given access to 10 Ethereum addresses pre-funded with 100 (fake) Ether each. You can use this Ether like you want: send it to other addresses or use it for running transactions. This is basically a safe sandbox for local development.

The account dropdown show you a list of the 10 pre-funded addresses I just mentioned. You can leave the first one selected. That’s the address that will deploy or interact with the smart contract.

For “gas limit” you can ignore that for the moment. The default value is enough for us.

You can also leave the “value” field to 0. That’s the value of Ether in Wei (10^ -18 Ether) that will be sent to the smart contract for the next transaction.

Below, you will find the deployment panel. That’s where you can deploy smart contract to the Blockchain. You need to first select in the dropdown the smart contract you want (in our case we just have one). Then you click on the “deploy” function.

And finally in the “deployment panel” you will find you deployed smart contract. That’s where you normally have a visual interface to interact with it. In our case, we haven’t defined any function, so there is no interaction possible. Lastly, you can delete a contract instance by clicking on the trash icon next to each contract instance.

That’ it for our first smart contract. Now you understand better the workflow of developing a smart contract with Remix, congrats!

Contract 2: Hello World

For our second smart contract, we will do a simple smart contract HelloWorld smart contract. This contract will be able to return a ‘helloworld’ string. We will learn:

  • how to write a read-only function in Solidity
  • how to specify returns type of a Solidity functions
  • pure and public function modifiers
  • how to call a read-only function from outside the smart contract

Let’s get started!

Go to your Remix editor, and add the pragma statement:

pragma solidity ^0.5.6;

Then, define your smart contract:

contract HelloWorld {}

In Solidity, like with many other programming languages, we define code inside functions. Solidity functions need to be declared inside a contract. Let’s add a function hello():

contract HelloWorld {
    function hello() {}
}

We use the function keyword, followed by the name of the function. Exactly like in Javascript. Inside the parenthesis, we can define function arguments, but for this function we don’t need any.

Between the parenthesis and the curly braces, we can define so-called ‘function modifiers’ and other meta information about the function. This is intended to be used by both the Solidity compiler and for programmers. We will add 2 function modifiers:

  • pure, to indicate that this function does not modify the blockchain.
  • public, to indicate that it can be read from outside the smart contract

And we also need to indicate the return type of the function, with the returns keyword:

returns(string memory)

Because strings are a non-scalar types(i.e a complex type composed of several values. Strings are represented as a dynamic array of bits), we also need to specify the memory location. I don’t want to go too deep on this so early in the tutorial, but basically there are different memory locations, and you need to be explicit for memory locations of non-scalar types like strings). One of these memory locations is just called memory (confusing, I know…), which is a temporary memory location, not stored on blockchain. That’s what we specify here.

In the end, the complete function signature will look like this:

contract HelloWorld {
    function hello() pure public returns(string memory) {}
}

Next, let’s define the function body. Inside the curly braces, we’ll add a return statement:

contract HelloWorld {
    function hello() pure public returns(string) {
        return 'Hello World';
    }
}

This function will return the string ‘Hello World’ to the caller.

Next, let’s go try out our function.

Go to the run tab of Remix, and click on the ‘deploy’ button. It will deploy the HelloWorld smart contract to a local Ethereum blockchain, like we did for the previous smart contract. Once this is done, you should see a rectangle appearing at the bottom of the run tab. This represents the deployed instance of your smart contract. You can use that to interact with your smart contract (i.e call its functions). Click on this rectangle, it will expand a new section. Inside, you will find a green button hello. This allow you to call the hello function of the smart contract. Click on it. You should see Hello World appearing next to it.

Congrats, you just called your first Solidity function!

We just scratched the surface of what is the possible to do in a Solidity function. We can also declare variables of many different types, manipulate them and change the data of the blockchain. We’ll see this in the next smart contract.

Contract 3: Simple storage

@TODO

Contract 4: Advanced storage

@TODO

Contract 5: Crud

@TODO

Leave a Reply

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