Mastering Blockchain Development: First Step to Implement Ethereum Hardhat

Mastering web 3 Part 1 — Hello dApp Contract

Arista Indrajaya
Better Programming

--

Freepik Image from @natanaelginting

This article series has been created to show the differences between web 2.0 and web 3.0.

I will explain the differences between them by building the same application called Hello App and Hello DApp step by step. This is a very basic tutorial the part 1 and will use the Hello World use case for the example but I also add another part to testing a contract (Part 2) and deploying (part 3) on a local network.

Or you can see my GitHub repo for this project if you want direct to show the code.

Pre-Requisites

If you follow this tutorial I assume you have at least programming experience, and also it would be better if you have any experience in using Typescript. And also I assume that you know nothing about smart contract development, but I will try to explain each step to build a Hello dApp smart contract.

Getting Started

1 . Set up your local environment

$ mkdir your-project
$ cd your-project
$ git init
$ yarn init -y
$ yarn add -D hardhat
$ npx hardhat

It will show this console:

2 . Basic Structure Folder

Create a Contract

Create the first contract, create a file in the contracts folder named HelloContract.sol. And the .sol is mean that solidity, which is the programming language that is used for developing smart contracts in Ethereum.

For the file contract is up to you, you can name it as you want because the name file of your contract has no meaning on how its compiled as long as it ends with .sol.

The first thing that you need to do when you write a solidity file is you need to tell the compiler that is used to compile your contract because you need to tell the solidity what the compiler that you need to expect to run or compile your contract. And then you write pragma, it’s just kind of precompile command.

You can write this one:

Code 1 : It means that you expect at least this version of compiler will run/compiler your contract, just same thing when you write the version of the libraries on the package.json.

And then the next one you also define the name contract, it’s just the same structure code when we write a class in javascript. The code will be like this:

Code 2: The simple explanation about it — the code is just like a state container with some function to mutate it (we will write later), and it’s just like a class, it also has the constructor that only execute only once when you deploy the contract (or one time executed).

The next code, you can add this:

Seem familiar with these lines of codes? For sure, it’s the first code that is usually written when we learning a new programming language, and this is the Solidity that should be written when declaring Hello, World from Web 3, because Solidity has its style.

But if you feel confused about another command, that’s okay, maybe you’re not familiar with the command pure and memory, and that’s the differences (we will discuss later).

Code 3: The line codes means, a hello() function hanging out the HelloContract contract, the public means anyone can call it, and returns to string and the Hello, World is the string that we expect to return.

Ooops, I think we forget to add the license version of our contract. You can add at the top of your code:

Code 4: What’s the reason? it just make us, as the developer can identifier specific license that used in our solidity file. The complete explanation you can see here

Okay, the next one we should compile our contract, so we can know what’s happened. Write this command to your command-line interface:

$ npx hardhat compile

Have you met this error:

So, the error code is about the Solidity version pragma statement that doesn’t meet any of the configured compilers.

It can be the compiler that you use to compile your smart contract is different from the default compiler that hardhat use.

Open the hardhat.config.js and then you can solve by this solution:

But if you still see another warning, that’s fine. And then, congrats, you compile your first contract. Wohooo!!

But… What’s next?

Testing? Oh yeah, that’s it. You should do testing before running it or deploying it to the network (test or main). You can go through part 2 of this tutorial series.

Conclusion

For this article, we already know how to create a simple contract using Solidity and then make sure that the compiler that we used was the same as our Hardhat compiler, and I also explain every single line of code that Solidity use to make a Hello World decentralized application.

It still much room to explore, and perhaps at the end of this tutorial series, we can know how the real differences between web 2.0 development and also web 3.0 development. Thank you!!

--

--