Deploying smart contracts to the Ethereum mainnet using Hardhat involves several steps, including setting up your project, writing your contract, configuring your deployment script, and finally executing the deployment. This guide will walk you through the entire process.

1. Setting Up Your Hardhat Project

If you haven't already set up a Hardhat project, follow these steps:

mkdir my-hardhat-project
cd my-hardhat-project
npm init -y
npm install --save-dev hardhat

Next, initialize Hardhat:

npx hardhat

When prompted, choose to create an empty Hardhat configuration file.

2. Writing Your Smart Contract

Create a contracts directory in your project root and add a simple contract file, for example, MyToken.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyToken {
string public name = "MyToken";
string public symbol = "MTK";
uint256 public totalSupply;

constructor(uint256 _initialSupply) {
totalSupply = _initialSupply;
}
}

3. Configuring Your Deployment Script

Create a scripts directory in your project root and add a deployment script, for example, deploy.js:

const hre = require("hardhat");

async function main() {
const initialSupply = 1000000; // Specify the initial supply
const MyToken = await hre.ethers.getContractFactory("MyToken");
const myToken = await MyToken.deploy(initialSupply);

await myToken.deployed();

console.log("MyToken deployed to:", myToken.address);
}

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

4. Configuring Network Settings

To deploy to the Ethereum mainnet, you need to configure your Hardhat project to connect to the mainnet. Open your hardhat.config.js file and add the following configuration:

require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-ethers");
require("dotenv").config();

module.exports = {
solidity: "0.8.0",
networks: {
mainnet: {
url: process.env.INFURA_URL, // Replace with your Infura/Alchemy URL
accounts: [process.env.PRIVATE_KEY], // Replace with your wallet's private key
},
},
};

Make sure to create a npx hardhat0 file in your project root and add your Infura/Alchemy URL and your wallet's private key:

npx hardhat1

5. Deploying Your Contract

Once everything is set up, you can deploy your contract to the Ethereum mainnet. Open your terminal and run the following command:

npx hardhat2

This command will execute your deployment script and deploy your contract to the mainnet. If successful, you will see output similar to:

npx hardhat3

6. Verifying Your Contract on Etherscan (Optional)

After deploying your contract, you may want to verify it on Etherscan. You can use the Hardhat Etherscan plugin for this purpose. First, install the plugin:

npx hardhat4

Then, add the plugin to your hardhat.config.js file:

npx hardhat6

Finally, verify your contract with the following command:

npx hardhat7

Conclusion

Deploying contracts to the Ethereum mainnet using Hardhat is a straightforward process that involves setting up your project, writing your smart contract, configuring your deployment script, and executing the deployment. By following the steps outlined in this guide, you can successfully deploy your smart contracts and interact with the Ethereum blockchain. Always ensure that you test your contracts thoroughly on a test network before deploying to the mainnet to avoid costly mistakes.