Chapter 4: Deploying solidity Smart Contracts using Hardhat

Chapter 4: Deploying solidity Smart Contracts using Hardhat

In simple terms: Chapter teaches you how to take your smart contract from your computer and put it on a blockchain. It covers testing it in a safe environment first, then deploying it to a live network where people can use it. Ethers.js is the tool that makes this process manageable, and the FAQ section answers common questions you might have along the way.

1. Local Deployment (Hardhat Network):

Imagine you’ve got constructed a fab new smart contract. Before unleashing it on the sector, you want to check it very well. That’s where local deployment comes in. Hardhat provides a built-in, simulated blockchain environment. Think of it as a practice ground. You deploy your contract to this local network, which doesn’t cost you any real money. You can interact with your contract, test all its functions, and debug any issues in a controlled setting. It’s like a dress practice session earlier than the principle overall performance.

2. Testnet Deployment (Goerli, Sepolia, Mumbai):

Once you are confident your agreement works domestically, you want to test it on a extra realistic, but nonetheless safe, surroundings. Testnets are like scaled-down versions of the actual blockchain. They use “take a look at” cryptocurrency, which has no actual-global value. Deploying to a testnet lets you enjoy the real deployment procedure—sending transactions, paying fuel fees (in check crypto), and interacting with your contract on a stay, albeit test, blockchain. Goerli, Sepolia, and Mumbai are examples of testnets. Each is associated with a different blockchain (e.g., Goerli with Ethereum). It’s like a tech demo before the full product launch.

3. Mainnet Deployment (Ethereum, Polygon, Base):

This is the actual deal. Mainnets are the stay, functioning blockchains like Ethereum, Polygon, and Base. Deploying to a mainnet way your settlement is now stay and available to everybody. Users can interact with it using real cryptocurrency. This step requires careful planning, as any bugs or vulnerabilities can have serious consequences. It’s like launching your product to the market.

4. Ethers.js for Deployment:

Ethers.Js is a JavaScript library that makes interacting with blockchains much less difficult. It’s like a toolkit for developers. When it comes to deployment, Ethers.js helps you:

  • Connect to Networks: It allows you to connect to the local Hardhat network, testnets, or mainnets.
  • Interact with Contracts: It lets you take your compiled contract (ABI and bytecode) and create a “contract instance” in your JavaScript code, which you can then interact with.
  • Send Transactions: It helps you send the deployment transaction to the network, which actually puts your contract on the blockchain.

1. Deploying Locally (Hardhat Network)

Before deploying to real networks, we use Hardhat’s built-in local blockchain.

Step 1: Start the Hardhat Network

Run:

npx hardhat node

This launches a local blockchain with fake ETH accounts.

Step 2: Deploy Your Smart Contract

Modify scripts/deploy.js:

const { ethers } = require("hardhat");

async function main() {
  const Contract = await ethers.getContractFactory("MyContract");
  const contract = await Contract.deploy();
  await contract.deployed();
  
  console.log(`Contract deployed at: ${contract.address}`);
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});

Run the deployment script:

npx hardhat run scripts/deploy.js --network localhost

2. Deploying to Testnets (Goerli, Sepolia, Mumbai)

Step 1: Configure Networks in Hardhat

Modify hardhat.config.js:

require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();

module.exports = {
  networks: {
    goerli: {
      url: process.env.GOERLI_RPC_URL,
      accounts: [process.env.PRIVATE_KEY],
    },
    sepolia: {
      url: process.env.SEPOLIA_RPC_URL,
      accounts: [process.env.PRIVATE_KEY],
    },
    mumbai: {
      url: process.env.MUMBAI_RPC_URL,
      accounts: [process.env.PRIVATE_KEY],
    },
  },
};

Step 2: Deploy to a Testnet

Run:

npx hardhat run scripts/deploy.js --network goerli
npx hardhat run scripts/deploy.js --network mumbai

💡 Testnet Faucets:
🔹 Goerli: Alchemy Faucet
🔹 Mumbai: Polygon Faucet


3. Deploying to Mainnets (Ethereum, Polygon, Base)

Step 1: Add Mainnet Configuration

Modify hardhat.config.js:

module.exports = {
  networks: {
    ethereum: {
      url: process.env.ETH_MAINNET_RPC,
      accounts: [process.env.PRIVATE_KEY],
    },
    polygon: {
      url: process.env.POLYGON_MAINNET_RPC,
      accounts: [process.env.PRIVATE_KEY],
    },
    base: {
      url: process.env.BASE_MAINNET_RPC,
      accounts: [process.env.PRIVATE_KEY],
    },
  },
};

Step 2: Fund Your Wallet

Ethereum: Buy ETH
Polygon: Use Bridge
Base: Use Base Bridge

Step 3: Deploy to Mainnet

Run:

npx hardhat run scripts/deploy.js --network ethereum
npx hardhat run scripts/deploy.js --network polygon
npx hardhat run scripts/deploy.js --network base

4. Using Ethers.js for Deployment

Step 1: Install Dependencies

npm install ethers dotenv

Step 2: Write an Ethers.js Deployment Script

Create deploy.js:

require("dotenv").config();
const { ethers } = require("ethers");
const contractABI = require("./MyContract.json"); // Compiled ABI

const provider = new ethers.JsonRpcProvider(process.env.GOERLI_RPC_URL);
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);

async function deploy() {
  const factory = new ethers.ContractFactory(contractABI.abi, contractABI.bytecode, wallet);
  const contract = await factory.deploy();
  await contract.waitForDeployment();

  console.log("Contract deployed at:", contract.target);
}

deploy().catch(console.error);

Run:

node deploy.js

5. Frequently Asked Questions (FAQ)

1. How do I get testnet ETH?

Use faucets like:

2. What’s the difference between local, testnet, and mainnet?

TypePurposeGas Fees?
Local (Hardhat)Testing No
Testnet (Goerli, Sepolia)Pre-production Yes (Fake ETH)
Mainnet (Ethereum, Polygon)Real Transactions Yes (Real ETH)

3. What’s the cheapest blockchain to deploy on?

Polygon & Base have lower gas fees than Ethereum.

4. How can I verify my contract on Etherscan?

Use:

npx hardhat verify --network goerli <contract_address>

5. How can I automate multi-chain deployments?

Use a script to deploy to multiple networks:

for network in goerli mumbai polygon; do
  npx hardhat run scripts/deploy.js --network $network
done

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *