Solidity Smart Contract Tutorial With Building Real-World DAPP — Part 2: Create Your First Contract

0
139

Solidity Smart Contract Tutorial With Building Real-World DAPP — Part 2: Create Your First Contract

In the first part of our series, we got familiar with the structure of the project that we are going to build. now it’s time to get your hands dirty with code and create your first contract.

Setting up your development environment

As you know, before starting a project, we need to set up our development environment. right?
We will use our favorite editor for coding, run a local blockchain in our system for deploy and testing the contracts and at the end, connect our front-end to the deployed contracts. but, for now, let’s start with an online IDE named Remix IDE.
Remix IDE is an open-source IDE for Solidity DAPP developers. you won’t need to install anything on your system. just click on the below link and you will get an IDE with some Ethereum account and a built-in blockchain for deploying with your contract.
https://remix.ethereum.org

I myself usually use this online IDE for my projects. not just for educational purposes, but for all of my projects! because it improves your development speed and helps you to debug your codes easily. most of the Ethereum DAPP developers start coding their contracts in this IDE and test the codes manually. after that, move the codes to VS Code or Atom for writing tests and deploy the contract in the Ethereum main net. we will use the same method in this tutorial.

Remix IDE

Open the IDE in the new tab. in the left vertical menu, below the remix icon, you can see two icons. one of them is File explorers and shows all of the project’s files and the other one shows the plugins. we will enable two plugins for our project: the Solidity compiler and Deploy & run transactions plugin. actually the only reason for speeding up the development on Remix IDE is these two plugins.
search these two names in the plugin manager search box and activate them.

Before start coding, I like to talk about the deploy and compile process.
well, the solidity itself is a human-readable language and it’s easy for us to just see the codes and figure out the code logic. but, that’s not the same version of code that the Ethereum network is going to use.
We need to compile our code. there are some outputs after compiling and two of them are very important for us. the bytecode and the ABI(Application Binary Interface)
See the below image. this is a JSON output of a compiled contract using truffle. we have not explained truffle yet. later in this tutorial, we will be familiar with these tools.

Bytecode: these codes are the same codes that will be deployed on the Ethereum network.
ABI: these codes help us to interact with our contract using Javascript. the Javascript itself cannot get connected with the Ethereum network and call the functions. instead, it uses some tools to connect the network and uses ABI to call the functions of our deployed contract.

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.

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.

Conclusion

I think that’s enough for this post. you now know about how to create your contract and have good information about variable and function types in solidity.
You have also got some general information about how solidity and Ethereum work behind the scene. later in this tutorial, we will deeply explain some theoretical stuff that you need to know as a blockchain developer.

Project source code

you can find the source code of the project in my GitHub repo:
https://github.com/bitnician/Delance-truffle

Every lesson will have its own branch.

My name is Behzad. I work as a blockchain developer and have experience with Ethereum development and Hyperledger fabric for the enterprise blockchain platforms. You can find me on twitter by my username: Bitnician.
Feel free to ask any questions here or on twitter.


Solidity Smart Contract Tutorial With Building Real-World DAPP — Part 2: Create Your First Contract was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.