Setting Up a Development Environment and Using Hardhat/Truffle

In this lesson, you'll learn to set up your local development environment for building Web3 applications. We'll explore installing and using either Hardhat or Truffle, two popular frameworks that simplify smart contract development and deployment. By the end, you'll have a functioning environment ready to test your smart contracts.

Learning Objectives

  • Install Node.js and npm (Node Package Manager).
  • Choose and install either Hardhat or Truffle as your development framework.
  • Configure your development environment to connect to a local blockchain (e.g., Hardhat Network).
  • Deploy a simple smart contract to your local blockchain.

Lesson Content

Prerequisites: Node.js and npm

Before diving into Web3 development frameworks, you need to ensure you have Node.js and npm installed. Node.js is a JavaScript runtime environment, and npm is the package manager that helps you install and manage dependencies for your projects.

Installation:

  1. Check if you have them: Open your terminal or command prompt and type node -v and npm -v. If you see a version number, you're good to go. If not, proceed to installation.
  2. Download: Go to the official Node.js website (https://nodejs.org/) and download the LTS (Long Term Support) version, which is generally recommended for stability.
  3. Install: Run the installer and follow the on-screen instructions. Make sure to check the box that automatically installs the necessary tools (e.g., npm) during the installation process.

Once installed, verify the installation by typing node -v and npm -v again in your terminal. You should see version numbers displayed. If you are having trouble you can also use your operating systems package manager for a more simplified approach like sudo apt install nodejs npm on Ubuntu based systems.

Example: (Verifying Node.js and npm versions)

node -v
v18.16.0  # (Example version - yours might be different)
npm -v
9.5.1  # (Example version - yours might be different)

Quick Check: What is the primary function of npm (Node Package Manager)?

Choosing Your Framework: Hardhat vs. Truffle

You can choose between Hardhat and Truffle. Both are excellent frameworks, offering features to streamline the development process. Here's a brief comparison:

  • Hardhat: A more modern framework, known for its flexible configuration, debugging features, and powerful task runner. It's often preferred for its clear error messages and enhanced development experience.
  • Truffle: A more mature framework, well-established in the Ethereum ecosystem. It includes features like built-in contract compilation, deployment, and testing. It also includes a robust suite of testing utilities.

For this lesson, let's focus on setting up Hardhat, as it is more modern and user-friendly.

Note: Truffle installation and setup are similar; the core concepts remain the same. The main difference lies in the project structure and specific commands.

Quick Check: Which of these is a popular Ethereum development framework?

Setting Up Hardhat

Let's create a new Hardhat project:

  1. Create a Project Directory: In your terminal, create a new directory for your project:
    bash mkdir my-web3-project cd my-web3-project
  2. Initialize the Project: Use npm to initialize the project and install Hardhat. Answer the prompts as follows:
    bash npm init -y npm install --save-dev hardhat
    • npm init -y creates a package.json file to manage project dependencies. The -y flag answers 'yes' to all prompts.
    • npm install --save-dev hardhat installs Hardhat as a development dependency.
  3. Create a Hardhat Project: Run the following command. You will be prompted with a series of questions. Select 'Create a basic sample project'.
    bash npx hardhat
  4. Explore the Project Structure: Hardhat will create a project directory structure similar to this:
    my-web3-project/ ├── contracts/ # Where you'll put your Solidity smart contracts ├── scripts/ # Scripts to deploy and interact with your contracts ├── test/ # Tests for your contracts ├── hardhat.config.js # Hardhat configuration file ├── package.json # Project dependencies └── .gitignore # Files to ignore in your Git repository

Quick Check: What is the purpose of `hardhat.config.js`?

Connecting to a Local Blockchain with Hardhat Network

Hardhat comes with a built-in local blockchain called Hardhat Network. It's an in-memory blockchain designed specifically for development and testing. It's pre-configured and ready to use.

  1. Hardhat Network Configuration (Default): The hardhat.config.js file configures your project. By default, Hardhat will use Hardhat Network. You don't need to change anything for the initial setup. Open the hardhat.config.js to see the configuration file and confirm the network configuration.
    ```javascript
    /**

    • @type import('hardhat/config').HardhatUserConfig
      */
      require('@nomicfoundation/hardhat-toolbox');

    module.exports = {
    solidity: "0.8.19",
    };
    `` 2. **Deploying to Hardhat Network:** Hardhat provides sample scripts for deploying and testing contracts. You can find them in thescripts/directory. Deploying the sample contract involves: * Opening thescripts/` directory and find the deployment script. The scripts typically deploy a sample contract to the Hardhat network.

Quick Check: What is Hardhat Network?

Deploying Your First Smart Contract

Let's deploy a simple smart contract to the local Hardhat Network. We will use the sample contract provided by Hardhat.

  1. Navigate to your Project Directory: Open your terminal and navigate to your my-web3-project directory.
  2. Run the Deployment Script: Execute the deployment script using the following command:
    bash npx hardhat run scripts/deploy.js --network localhost

    • npx hardhat run: Runs a Hardhat task.
    • scripts/deploy.js: The path to the deployment script. (The default script deploys the sample contract).
    • --network localhost: Specifies that we want to deploy to the localhost network (Hardhat Network in this case).

    The output in your terminal will show the contract being compiled, deployed, and the contract's address (where it's deployed on the blockchain). This address is important if you want to interact with your contract from other applications.

Quick Check: Which command is used to deploy a smart contract using Hardhat?

Progress
0%