React Project Setup Using Hardhat & Truffle — part 2

0
32

React Project Setup Using Hardhat & Truffle — part 2

Computer screen with a notebook in front
Photo by Kevin Canlas on Unsplash

Now that we have our project set up from part 1, we will have to do some configurations before we start writing our contract.

Configuration Setup

Each project should now have a configuration file in the root directory for Truffle it should be named.

truffle-config.js

For Hardhat it should be named

Hardhat-config.js

Let’s configure our complier version for Solidity as well as the network we will be deploying our contract to

Networks

This is referring to where we are planning to have our contract deployed to and interact with.

In this example, we will use a local blockchain for our deployment since we will be provided with accounts, private keys and already be pre-funded with test Ethers

Local — running a version of a blockchain on our computer

When using a testnet option, a wallet will have to be created and you will have get fake ethers for testing from a faucet of the chosen testnet. Metamask is one option for a wallet and fairly easy to use

Testnet — a simulation of the Ethereum mainnet where you use fake ethers to pay for contract interactions and gas fees below is a list of testnest:

The real thing and uses real ether for interaction

Mainnet — the Ethereum blockchain

Truffle config

First we will set up our Solidity compiler to the version that we will be using in our smart contracts.

Open truffle-config.js file and under the compiler object go to solc: and set it to version 0.8.6 for this example.

You have the option to leave the other parameters commented out or they can be deleted since this example will not utilize them. It is left commented out to match as close to the example that you might see.

The next step is to set up our networks.

Truffle provides two ways to create a local blockchain on our computer using Ganache.

We will go the route of using Ganache CLI but feel free to try out Ganache GUI as well.

Install Ganache CLI locally in the project.

npm i ganache-cli

or globally on your computer

npm i -g ganache-cli

Once the installation is complete, we will set it up in our truffle-config.js by uncommenting the development object, host, port and network_id.

More can information on Truffle config can be found Here.

https://gist.github.com/Shih-Yu/440d791557eb0e1ce8c47baba9fe0680

Now that Ganache is configure, let’s run it by going to our terminal.

This should be running when interacting with the smart contract.
ganache-cli

Now we have a blockchain on our computer that has 10 accounts pre-funded with 100 ethers and their private keys, along with the Mnemonic seed phrase.

Hardhat config

First we will set up our Solidity compiler to the version that we will be using in our smart contracts.

Open hardhat.config.js file and under module.exports go to solidity: and set it to version 0.8.6 for this example.

Hardhat already has it set to 0.8.4

The next step is to set up our networks.

Since there’s no format provided, we will set it up like this:

More can information on Hardhat config can be found Here.

Now that Hardhat is configured, let’s run it by going to our terminal.

npx hardhat node

Now we have a blockchain on our computer that has 20 accounts pre-funded with 1000 ethers and their private keys

In part 3 we will write a simple smart contract

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also Read


React Project Setup Using Hardhat & Truffle — part 2 was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.