Solidity Smart Contract Tutorial With Building Real-World DAPP — Part 2: Create Your First Contract
By: Behzad Pournouri
Tags: Solidity, Smart Contracts, Ethereum, Dapps, Blockchain

Your first contract

It’s time to write your first contract. go back to the Remix IDE. in the file explorers, there is a plus icon. Click on the icon, create a new file and name it Delance.sol or any other name that you like. the only important thing here is your created file should end with .sol.

New contract

The first thing is defining the version of solidity. we can define any version of solidity, but it’s always good to use the latest stable version. it enables new features and functionality of solidity for you. at the time of writing this article, the latest version is 0.6.9:

pragma solidity 0.6.9;

Basically, defining a contract is almost like defining a class in an object-oriented programming language. in solidity, we use contract keyword to define a contract.

In contracts, we have some public or private methods and properties. the contracts also can inherit from each other.

Now, let’s define a contract named Delance:

pragma solidity 0.6.9; contract Delance{



}

As we see in the first part of this tutorial, our contract needs the freelancer address and deadline. we should also deposit some amount of ether to our contract. the ether balance of the contract will be our project fee.

let’s create some state variables in our contract:

pragma solidity 0.6.9; contract Delance{



address public freelancer;

uint public deadline;

}

As you see, solidity is a strongly typed language. it means you need to define the type of variable.

You can also define how you can access the variable: if you want to access the variable from outside of the smart contract, you need to add the public keyword, otherwise, it will consider as a private variable and can be visible just inside the contract.

Types

There are some important types in the solidity that I like to mention them here:

string, bool, int, uint, and address

There are also some other types that we will mention later in this tutorial.

If you have experience with any other programming language, you may know about the above types. but, one of them seems to be totally new here: the address type!

In the Ethereum world, every account or contract is identified by the Ethereum addresses. All of the identities in the network can find each other using Ethereum addresses. an Ethereum address is simply a 64 character hex string: 0xE349fBbeDA6642BF459f4957b6D6AbcCBA8bA74c

In this post, we will see how we get our contract address after deploying it.

let’s back to our contract. As you saw, we define a public variable named freelancer and its type is address. it means it will store something like 0xE349fBbeDA6642BF459f4957b6D6AbcCBA8bA74c 🙂

Functions

Now, we need to write some functions to set the value of the freelancer and the deadline:

pragma solidity 0.6.9; contract Delance{



address public freelancer;

uint public deadline;



function setFreelancer(address _freelancer) public {

freelancer = _freelancer;

}



function setDeadline(uint256 _deadline) public {

deadline = _deadline;

}

}

As you see, we have to define the visibility type for functions as well as variables. The available types to define a visibility type of function are public, private, internal, and external.

Public: can be called from inside and outside of the contract

Private: only can be called from inside the smart contract. private functions are not available in derived contracts.

Internal: can only be called within the contract itself and any derived contracts.

External: can be called from outside the smart contract, but cannot be called from inside.

All of the above types can be used for variables except the External type.

constructor

If you know about OOP, you should know it’s good practice to define our necessary variables in our constructor. our contract cannot be useful without the freelancer and deadline!

As I told you, the contract is like a class in an object-oriented programming language. one of these similarities is the constructor method. Here in our contract, we can delete the last two functions and then, define a constructor and expect two properties: the freelancer address and the deadline. So, when we want to deploy the contract, we should define these two properties. an address type and a uint type. otherwise, an error will be generated.

pragma solidity 0.6.9; contract Delance{



address public freelancer;

uint public deadline;



constructor(address _freelancer,uint _deadline) public {

freelancer = _freelancer;

deadline = _deadline;

}

}

Notice the type of constructor function should always be public. we also don’t need to use the `function` keyword to define a constructor.

***Update (October 2020): In the next version of solidity (Solidity 7), visibility for a constructor is ignored unless you want to create an abstract contract. abstract contracts are just a non-deployable contract.

Our contract is not abstract, so it’s not necessary to use public keyword if we change the version of solidity to 7!

Global variables

In Solidity, there are some variables and functions which already exist globally and are mainly used to provide information about the blockchain.

The msg global variables are special global variables that contain properties that allow access to the blockchain. For example, msg.sender can return the address who has sent the transaction(let’s say sending a transaction is like calling a function, later we explain about transactions).

So, in our contract, we can define a state variable named employer with a type of address. then, in the constructor, we can get the address who has sent the transaction (the address who calls the constructor method) and store it in the employer variable:

pragma solidity 0.6.9; contract Delance{



address public employer;

address public freelancer;

uint public deadline;



constructor (address _freelancer, uint _deadline) public {

employer = msg.sender;

freelancer = _freelancer;

deadline = _deadline;



}

}

We have stored all of the important initial information in our contract. there is only one thing left. the project fee! we will explain the deposit process later in the tutorial. for now, let’s deploy our contract.

Compile

Now it’s time to use the activated plugins. first, we compile the contract and then deploy it.

let’s click on the solidity compiler icon. change the compiler version to the 0.6.9. you can also enable the auto compile option. so, Remix IDE will automatically compile the contract while you typing.

When you were ready, hit the compile button.

Deploy

after compiling the contract, click on the deploy plugin.

- Select Javascript VM for the environment.

- There are some built-in accounts with 100 ether of balance. you can select one of them.

- The gas limit has been set to 3000000. it’s good for our transaction(later we explain about gas price and gas limit)

- Leave the value to be zero, as I told, in the next post we will write a function in the contract to accept ether.

Select your contract, and fill the required arguments. here we need an address type and uint type.

_freelancer can be: 0xE349fBbeDA6642BF459f4957b6D6AbcCBA8bA74c

and _deadline can be: 123456789

Congrats, You just deployed your first contract.🥳

Now you can read the stored data from the blockchain.

Every time you define a public state variable in the contract, the solidity creates a getter function for you.

For instance, for reading the value of the `freelancer` variable, you don’t need to create a getFreelancer() function to return the freelancer value. you have already access a function named freelancer that returns the freelancer value.

In the below image, you can see the employer address is the same address that we use to send the transaction.