Setup Project
Last updated
Last updated
After we got to know Hardhat, the next step we will go to install Hardhat.
The first step we need to install Node.js version 12 or higher
Then we will build our project Hardhat by opening Text Editor. Here we will select Visual Studio Code. When we open Visual Studio Code letβs select a directory. I will define the name Directory as HARDHAT-LAIKA-TUTORIAL
After that, We will open Terminal and we will use the command npx hardhat init
to create project hardhat.
When we run the Hardhat command. It will come up for us to select what kind of project we want to create. Here, We will select to create a basic sample project
. Next, we have to assign our projectβs root where we want to use it for installing hardhat, in this case, weβre going to select the default directory. And it will ask us do we want to have .gitignore or not. We want to use Git therefore I will answer Yes to add .gitignore into the project. Next, It will ask if we want to install any dependencies. We will answer yes and wait for an installing.
After installation, Hardhat will generate a folder structure for us. There will be a folder of contract, script, test, and file config.js which will be files that project Hardhat needs.
As I said, Hardhat can compile script, test, or deploy anything, and that's another feature that Hardhat has and we think is a cool feature. That is, we can run a local chain using Hardhat. So we let's run the local chain and we'll see how we can make our local chain connect to the MetaMask. When we want to run the local chain of hardhat, the command is simple.
Just use command
Here Hardhat will create a node on the local machine that runs these accounts and gives private keys to different accounts and it says: in the default setting It will run at port 8545, but there is nothing much here, just run it.
Next, we will configure the local chain that we run here so that it can connect with the MetaMask.
Letβs go to web.getlaika.app
connect to MetaMask in the top right corner.
After we connect to the MetaMask, we will be able to select the network. In this case, anyone who has never used a Hardhat before, the network will not have a Hardhat network to select. We have to click add network.
When we select Add Network, we'll be led to a page where we can enter our network information. We must write: How should we contact the Hardhat node on this page.
After that, weβll go to the New RPC URL. If anyone remembers after we run the Hardhat node, it will tell the RPC server.
Network Name : Hardhat Network (Local)
New RPC URL : http://127.0.0.1:8545/
Chain ID : 31337
Currency Symbol :
Block Explorer URL :
The Currency Symbol and Block Explorer URL sections can be left blank. After filling out the network information Click Save.
After we add a new chain to the MetaMask, we will see how we can connect to the new network, how to do it directly, here we will open the Laika website and connect the wallet and we click on the MetaMask wallet. And select the network chain that we want here is the hardhat Network that we have created.
Now Laika will pop up saying we goChain Testnet now we are already on hardhat network.
If we look at the wallet, we can see that we have 0 ETH, which means that we don't have gas in the transaction at all, so we will go to add more money to the wallet.
by adding gas money in our pocket We will go back to the vs code page and open a new terminal without tampering with the terminal that is running the Hardhat node.
In this terminal, we will use the command npx hardhat console --network localhost
. This command when we run it will open the console to communicate with the network chain we run and why we need flag --network in the command. To indicate that we want to contact the localhost network that the hardhat has run for.
After we've run the command npx hardhat console --network localhost
, the Hardhat will compile contracts and bring up the console to us.
Once the console is up, we can write code to connect to the network right here. Here we will use the command hre.network.provider.send(βhardhat_setBalanceβ,[array])
In this array, we will input 2 things: 1. Address that we want to put ETH into the wallet in 2. How much ETH we need We will get an order as hre.network.provider.send('hardhat_setBalance',['0x5C314eCB105F3e7A78BD19EE86823e44fd81D3DF','0xffffffffff'])
After that it returns a promise and we wait for it to run for a while.
And let's look at the terminal that is running localhost and see that there is setBalance.
And letβs look at our MetaMask, there will be an increase in the amount of ETH into our wallet. which we will use as our gas.