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.
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:
node -v
and npm -v
. If you see a version number, you're good to go. If not, proceed to installation.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)
You can choose between Hardhat and Truffle. Both are excellent frameworks, offering features to streamline the development process. Here's a brief comparison:
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.
Let's create a new Hardhat project:
bash
mkdir my-web3-project
cd my-web3-project
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.bash
npx hardhat
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
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.
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
/**
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 the
scripts/directory. Deploying the sample contract involves:
* Opening the
scripts/` directory and find the deployment script. The scripts typically deploy a sample contract to the Hardhat network.
Let's deploy a simple smart contract to the local Hardhat Network. We will use the sample contract provided by Hardhat.
my-web3-project
directory.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.
Explore advanced insights, examples, and bonus exercises to deepen understanding.
Congratulations on setting up your Web3 development environment! Now that you have the basics down, let's explore some more advanced concepts and alternative perspectives to solidify your understanding. This extended lesson will delve deeper into the choices you made, introduce best practices, and offer practical applications.
You've chosen either Hardhat or Truffle. While both frameworks excel in their respective areas, understanding their nuances can help you choose the best fit for future projects.
Configuring your environment to connect to a local blockchain (like Hardhat Network) is crucial. Beyond the default settings, consider:
If you chose Hardhat, explore some of the popular Hardhat plugins. Install and configure a plugin like hardhat-etherscan
or hardhat-waffle
and experiment with their capabilities. Try using the debugger to debug your contract deployment process.
If you chose Truffle, explore the Truffle Boxes available. Create a project based on a pre-built box (e.g., a "pet shop" or "token" example) and analyze the project structure. Modify the existing smart contract and re-deploy it to your local blockchain.
Setting up a robust development environment isn't just a technical exercise; it's the foundation for building real-world Web3 applications. Think about how these skills translate:
Modify your configuration files (Hardhat or Truffle) to deploy your "Hello World" smart contract (or the one you deployed on Day 3) to a public testnet (e.g., Goerli, Sepolia, or Mumbai). You'll need to obtain some testnet ETH/tokens and configure your environment to connect to the testnet. This often involves setting up an RPC URL for the testnet and configuring your wallet address in the configuration file.
Here are some topics for your continued exploration:
Navigate to your project directory. Inspect the `package.json` file. What dependencies are listed? Inspect `hardhat.config.js`. What does it configure?
Examine the output of your contract deployment (from the previous steps). What is the contract address? Where does the deployment occur? Is there an output of any kind?
Modify the sample contract's logic (e.g., change a variable name or add a function). Redeploy the contract using the same command as before. Observe how the deployment process changes.
In the terminal, run `npx hardhat help`. Explore some of the available Hardhat tasks (e.g., `compile`, `test`). Experiment with a few tasks and observe their outputs. Also experiment with different network configurations (e.g. going to a testnet).
Build a simple decentralized application (dApp) that allows users to send and receive messages. Start with a simple smart contract that stores messages and a basic front-end interface that allows to submit the messages and retrieve them.
In the next lesson, we'll dive deeper into writing Solidity smart contracts, focusing on data types, functions, and contract structure. We'll also cover writing unit tests to ensure your contracts behave as expected.
We're automatically tracking your progress. Sign up for free to keep your learning paths forever and unlock advanced features like detailed analytics and personalized recommendations.