Solidity Smart Contract Tutorial With Building Real-World DAPP — part 1: Introduction

Behzad Pournouri
6 min readJun 18, 2020

--

In this article series, I want to teach you how to develop a real project on the Ethereum blockchain using solidity 6. this project will help you to get familiar with DAPPs (Decentralized Applications) and have a clear understanding about their use cases. In my opinion, one of the difficulties for developers who want to migrate on the blockchain development is decentralized thinking model. I have tried to make this mindset clear to you as well as development skills🔥

This project that I call it Delance (Decentralized Freelance), is just for education purpose and no more. After a walk through all of these series, you will be able to build your next great idea on the blockchain. (Of course, if blockchain is necessary for you and could improve your business.) Delance is not a perfect project and I could add more features to it but, If you keep adding features, your project will never deploy! plus, this project is not going to fix all of the centralized platform issues but, you will see how it can improve the process.

What you’ll learn

  • Creating a smart contract using solidity 6
  • Writing test for your smart contract in Javascript using Truffle
  • Running you own local blockchain with Ganache
  • Deploy your contract on the Ethereum test network using Infura, you can use the same process for deploying on the Ethereum main network.
  • Connect your Solidity codes to your Interface using web3js

Requirements

  • Good understanding of javascript.
  • As we are going to create a full-stack Ethereum app, It’s better for you to get familiar with to react but, it’s not necessary

What this project is all about?

In this project, we are going to build a freelancing platform. our participants in the project are the freelancer and employer.
All of the freelancing projects have two important attributes: deadline and price. the employer usually likes to get the project before the deadline and the freelancer needs to get the project fee once he delivered the project. the freelancer may also want to break the project to a couple of parts and request for payment after finishing each part. On the other hand, the employer would like to have some guarantee to make him sure the project will be delivered before the deadline.

How blockchain can help?

First, we need to know what is the smart contract? according to Investopedia, “A smart contract is a self-executing contract with the terms of the agreement between buyer and seller being directly written into lines of code.”

It means you can bring almost every paper contract on the blockchain using Ethereum smart contracts. I know it seems a little unclear. let’s back to our Delance project and see how we define a smart contract between freelancers and employers.

Delance introduction

Suppose the freelancer and employer get familiar together on twitter and make some agreement about the project price and the deadline. the employer doesn’t want to deposit all of the payments before getting the project and the freelancer doesn’t want to start the project before getting any payment. He doesn’t even know about the financial situation of the employer. Does the employer have enough money to pay? Here we can use a smart contract: a self-executing contract with the terms of the agreement 😎

Delance lifecycle

1. The employer will create a smart contract that accepts three arguments: the freelancer address, the deadline, and the project fee. the important thing here is the project fee is not just a number. the employer should deposit some ether in the smart contract. the balance of the smart contract will be the project fee. the interesting thing here is the employer cannot withdraw any amount of ether unless in some situations that we explain later.
the image below shows the HTML form that the employer used to create a contract. notice that we don’t have an input for price. the employer should deposit some ether through his/her wallet while creating the contract. if no ether is sent, an Error will throw in the contract codes.

ethereum/solidity 6 tutorial

2. The freelancer can check the contract properties to see if the deadline and the project fee are matching with their agreements or not(the freelancer can find the contract on the blockchain. the contracts are also can be visible in our interface). if everything was right, the freelancer start the project.
When the project reached a good point, the freelancer can create a request and ask for some amount of ether.
the image below shows the HTML form that the freelancer used to create a request for payment.

ethereum/solidity 6 tutorial

3. We show all of the project details in our interface. the employer will see the freelancer requests and can unlock each of them. after that, the freelancer can withdraw the ether in just one click. the payments will be in his wallet up to 1 minute later.
Below image shows you all of the project details🤓

Delance rules

Imagine the employer have a web development project. the project fee is 1 ether:

1. The freelancer has done the frontend stuff and creates a request with an amount of 0.3 ether. after the creation of a request, the freelancer should wait for the employer to unlock the request. then, the freelancer can withdraw ethers. freelancers cannot withdraw any amount of ether that is in the smart contract without the employer’s permission.

2. If the project ends before the deadline, the employer can mark the project as a completed and all of the remaining amounts of ethers that exist in the contract will be withdraw to the freelancer wallet.
for example, the freelancer has got 0.8 ether so far. 0.3 for frontend and 0.5 for the backend. once the project is done, the employer change project status to complete and then, 0.2 ether will automatically transfer to the freelancer address.

3. If the deadline reached and the freelancer wasn’t able to deliver the final version, the employer has two option:
- The employer can increase the deadline: in this case, the freelancer would have extra time for delivering the project.
- The employer can cancel the project: in this case, all of the remaining ethers in the smart contract will be withdraw to the employer address.
for example, the employer has unlocked 0.3 ether for the ‘frontend request’ and get the front-end codes. the freelancer got stuck in the backend and cannot deliver the project. If the deadline reached, the employer may cancel the project and 0.7 ether will automatically transfer to the freelancer address.

Conclusion

As you saw, Ethereum enables some new features for us. we can define a new contract with our own logic. the logics are immutable and the participant cannot circumvent any rule or restriction.
we also don’t need any middle-man. the business will run with our defined logics. the example could be the withdrawal logics in our contract. the contract will hold all of the ethers and participants can withdraw them within some rules. If we don’t have some functions or logic to withdraw the ethers, nobody can get back the ethers from the contract😀

Well, I hope you understand the Delance structure. If for some reason, It’s still not crystal clear for you, there is no problem. you are here to learn. we will build it step by step and I’m sure after that, you will have a very deep understanding of the smart contract development.

In the next post, we are going to create our first solidity smart contract.

Project source code

I hope this project has caught your attention. 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. we first create our contract and then, connect this contract to our UI that created with react.

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.

--

--

Behzad Pournouri

I build smart contracts and distributed applications | Blockchain developer | Hyperledger Fabric | Ethereum