In January 2022, I did a 30-day project on “Why Your Business Needs to Adapt to Blockchain”. This post is a part of it. To know what I covered, learned, and executed in the project, visit this page.
In Part 2 of this series, I explained how I set up my system. In this part, I elaborate on what the smart contract actually looked like.
Writing the actual Hello World dApp
First, I opened a new tab on Terminal. The working directory was the project we had created ‘react-dapp’ by default. I viewed the files in the project by using the command ls.
I got a basic contract created by Hardhat for us.
vi contracts/Greeter.sol
The sol extension is used for Solidity – the language used to create dApps or smart contracts in Ethereum.
This is what the basic smart contract looks like:
The first line tells the version of Solidity the dApp is written for. The import “hardhat/console.sol” allows us to use the command console.log used inside the constructor. This command logs things to the console when it is running.
The contract command is a set of rulesets to be deployed on the blockchain. The rules are verified by verifiers on the blockchain. There are two sets of rules in this contract:
- The read rules: Denoted by function greet(). It simply returns the current greeting stored as a string on the blockchain.
- The write rules: Denoted by function setGreeting. It assigns the value of the greeting to the blockchain, i.e., it changes the value of the greeting irrespective of what it was previously.
Blockchain is a database and the contract holds the rules that decide what would go into the blockchain and what would come out of it. If you are able to write something on the blockchain, you are honoring a contract. Secondly, since there are codes as rules associated with it, it is a smart contract.
The contract in this example is that I can add some string to this blockchain and I can retrieve it. I can write whatever I want. The only rule is that it has to be a string.
This file was already created by hardhat. I simply deployed this contract without changing anything. So, I quit the editor using :q! and was back on the react-dapp.
Next, I had to compile this solidity contract into a format that could go onto the blockchain. This compilation also created some APIs so that I could interact with this contract using my node or Javascript code.
I used npx hardhat compile to compile the Solidity code into a format that the blockchain could run, the verifiers could understand, and it was going to give me some javascript APIs.
The compiled file can be seen by using: vi src/artifacts/contracts/Greeter.sol/Greeter.json
This is the compiled code or the compiled version of the smart contract which will go to the blockchain. I had already started a node for the blockchain that we created using hardhat. Next, I needed to import one of the node accounts into Metamask. I imported the first account because, during deployment, it will use the first account by default.
Part 4 of this series describes how I imported the first node account into Metamask.