Box

Box

3BoxTech Founder / Web3Box Founder/ solidity dev./Several years of experience in Solidity and full-stack development.
twitter

Web3 Enterprise Engineering - Beginner's Guide: 2. Solidity Environment

Introduction#

This article will introduce several commonly used Solidity development environments and recommend the most suitable development environment while installing the necessary content.

Environment Introduction#

Currently, Solidity development environments can be roughly divided into two categories:

  • Online: These are browser-based integrated development environments that allow for easy debugging, managing account balances, configuring environments, and other operations.

  • Offline: These are environments suitable for local development.

Online#

The main online product is Remix https://remix.ethereum.org/.

Remix is an online development environment provided by ETH, and it is a powerful integrated cloud environment development IDE. It provides file management, online compilation, static analysis, deployment, transaction execution, testing, and other functions. It also has a powerful plugin system.

Remix is powerful in all aspects and can even directly modify and read local files. The only drawback is that it does not support multiple version compilers. It is very suitable for simple development or temporary analysis or data calling operations on the chain.

Offline#

Truffle Suite#

Website: https://trufflesuite.com/

Truffle Suite is a suite of tools that includes Truffle, Ganache, Drizzle, and more.

Truffle: A development environment for EVM that includes a suite of testing tools.

Ganache: A local EVM node for testing and development, used for contract deployment, development testing, and other functions.

Drizzle: A frontend component based on Redux that facilitates interaction on the frontend.

Hardhat#

Website: https://hardhat.org/

Formerly known as Builder, Hardhat is a feature-rich Solidity development framework that covers almost all the features needed for Solidity development. It comes with smart contract testing, compilation, and local node capabilities. It is a highly integrated development framework with a powerful plugin system that makes development easier.

Foundry#

Website: https://getfoundry.sh/

Foundry is a Solidity development framework developed based on Rust, focusing on speed. It also covers development, testing, and local node functions. Unlike Hardhat, Foundry's test code is written in Solidity, but it can also interact with the local environment with the help of VM cheat code. However, it still lacks some functionality compared to Hardhat.

There are also some frameworks developed using Python, which will not be discussed in this article. Interested friends can search for them on their own.

Setting up the Hardhat Environment#

Among the three mainstream development environments, Hardhat's powerful features make it the benchmark for development frameworks. If someone still recommends Truffle as your development framework, proudly bring out Hardhat and let it join the Hardhat family.

Of course, this does not mean that Hardhat has no shortcomings. It is just that after weighing the pros and cons of the advantages and disadvantages, the shortcomings seem insignificant. Of course, Foundry is also a very powerful framework, but its functionality is still not as comprehensive as Hardhat. However, using Hardhat and Foundry together can have unexpected effects.

However, we will not do this for now because Hardhat is currently sufficient for most situations.

Without further ado, let's move on to the next step.

Creating a Project and Installing Hardhat#

Find a directory you like, create a folder, and open it with VSCode. You can also use the terminal and enter code \\path\\to\\your\\project to open the project directory with VSCode in one click.

After opening VSCode, press Command+J to open the Terminal pane. Here, we will install Hardhat.

Enter npx hardhat and wait for the prompt to press Enter, and wait a moment (if it is slow, please use a VPN).

Hardhat Installation

Here, press the down arrow key and select Create a TypeScript project. Of course, you can choose Create a Javascript project, but it is not recommended because TypeScript has more advantages than JavaScript in multi-person collaboration.

Complete Initial Setup

Follow the prompts to complete the initial setup, but be careful to enter n when asked Do you want to install this sample project's dependencies with npm because we need to use Yarn to manage dependencies.

Then, enter yarn add "hardhat@^2.11.1" @nomicfoundation/hardhat-toolbox @nomicfoundation/hardhat-network-helpers @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers @nomiclabs/hardhat-etherscan chai ethers hardhat-gas-reporter solidity-coverage @typechain/hardhat typechain @typechain/ethers-v5 @ethersproject/abi @ethersproject/providers to install the dependencies.

Note⚠️#

Dependency Installation

If you encounter this error, don't panic. This is a common occurrence in the JS world, and you will encounter various problems in the future, such as missing Python compilation environment or incorrect architecture.

The problem here is that the current version of Node does not meet the requirements. As you can see, Node version ^14, ^16, ^18 is required. So here, we need to reinstall Node to a version that meets the requirements. This problem may be caused by an incorrect Node version or if you used brew install nvm. If so, you can enter brew uninstall nvm and then use the following command to install: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash. Then restart the terminal.

Installation Complete#

image

After the installation is complete, your directory structure should look like this.

Installing Other Necessary Dependencies#

Of course, our tutorial aims to create an enterprise-level project, so installing only Hardhat is not enough. We need a feature for the project to be upgraded continuously.

Typescript#

Since we created a TypeScript project, we need to install the dependencies for TypeScript.

yarn add ts-node typescript chai @types/node @types/mocha @types/chai

Hardhat Deploy#

This is a necessary plugin for Hardhat project engineering. This plugin helps manage code deployment, allowing you to easily obtain the addresses of the contracts you deployed in your code and decide which contracts need to be deployed based on the context. It also allows you to set contracts that can only be deployed once, similar to migrations. It also provides convenient functions for testing, such as deploying contracts with certain tags.

To install Hardhat Deploy, simply enter the following command:

yarn add hardhat-deploy @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers ethers

Then add import "hardhat-deploy" to the hardhat.config.ts file.

At this point, the deployment is complete. We create a deploy folder in the root directory, and all future deployment files will be placed in this folder.

Dotenv#

Dotenv is a JavaScript package that imports the .env file into the system environment variables for program use. This package is very useful because when developing code in an engineering project, you will definitely use Git or other version control tools. For private projects, uploading some confidential variables to the version control repository may not be a big problem, but for public projects, there may be some security issues. Therefore, we need to put some variables in a file that will not be uploaded to the version control.

Enter

yarn add dotenv

Then modify hardhat.config.ts and add import 'dotenv/config' at the top.

At this point, our necessary project engineering packages have been installed, and you can save this package as a template for future use.

Let's run the test command to see if our integrated package is working properly.

yarn hardhat test

Normal Execution

When you see the above content, congratulations, you have set up an environment that meets engineering standards.

VSCode Plugins (Optional)#

Of course, installing some plugins for our VSCode can better help us with development. Here are a few recommended plugins.

  • Hardhat Solidity - This is a Solidity plugin developed by the Hardhat team, which is very useful.

  • Copilot - GitHub's intelligent code completion plugin, also very useful.

  • Material Theme Icons - A plugin for beautification.

  • Material Theme - Another beautification plugin.

The final installation effect is as follows.

image

Of course, everyone has their own definition of beauty, so if you decide it doesn't look good, feel free to change it.

Summary#

This chapter explains how to install the Hardhat development environment. In the subsequent tutorials, we will develop a program that interacts with Uniswap to continue with the rest of the content.

subscribe://

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.