Creating your first smart contract in solidity
If you have not installed Nodejs yet then You have to install Nodejs. To follow along this article we will need an code editor for this I am using VsCode.
To setup environment locally we will be using a tool called Hardhat. Hardhat is all in one tool for developing smart contract and debugging. Okay! enough talk let's build.
Initializ project
Create HelloWorld directory and initialize empty nodejs project by running npm init -y.
Install hardhat package as dev dependencies by running this command
Initialize hardhat project into it
run this command to initialize a hardhat project this will ask you some question.
After running this command your terminal will look something like this-
Choose Create a Javascript project option then press enter, enter and enter. It will create an .gitignore file and install some dependencies. After doing this step your terminal will look something like this
In your terminal if you write code . then it will open current directory inside the VsCode. After opening inside vscode your folder structure will look something like this.
Let's talk about each directory first-
- contracts
- All of your solidity smart contract files will live here.
- ignition
- It contains scripts file to deploy your smart contracts.
- test
- It contains test files of your smart contracts
- hardhat.config.js
- this is the most importaint file in your smart contract. If you find some error on deploying first of all you have to check this file. It contains configuration for deploying your contract.
Clean project
delete default contract file Lock.sol and it's test file Lock.js and it's deployement file inside ignition/modules folder.
Let's create your first smart contract
Inside contracts directory creating a file named HelloWorld.sol In every smart contract first line of file is has to be SPDX License Identifire it tells the license type of the code. In second line it has pragma solidity solidity_version. It tells the solidity compiler, that which version of solidity to use when compiling this code.
We define the contract by keyword contract itself and followed by name of the contract. Now we have created an contract named HelloWorld.
event - by using event keyword we declare an event. Here we are declaring an event called MessageChanged which takes to argument first oldMessage which is string and other one is newMessage which is also a string.
We are creating an public state variable named message which is typeof string.
constructor - constructor runs only once when deploying the contract. We can use to set some initial values of variables. Here we are setting the value of message varialbe.
changeMessage - changeMessage function is taking one argument newMessage to set and setting value of variable message. In this function first thing we are storing current value of message into oldMessage to use in emiting event and setting value of message to newMessage and then we are emiting the event named MessageChanged by passing oldMessage and newMessage into it.
Writing deploying scrit
In the ignition directory create an modules directory inside it create HelloWorld.js file.
Configure hardhat.config.js file
Deploy on testnet
thanks!