Connecting Hardhat to MetaMask allows you to interact with your smart contracts directly from your web browser. This is useful for testing your decentralized applications (dApps) and for deploying contracts to the Ethereum network. Below is a detailed guide on how to set this up, along with sample code.
Prerequisites
- Ensure you have Hardhat installed and set up in your project.
- Install MetaMask in your web browser.
- Create a wallet in MetaMask and fund it with test Ether (if using a test network).
Setting Up Hardhat
- Open your Hardhat project and navigate to the
hardhat.config.js
file. You need to configure it to use a network that MetaMask can connect to. For this example, we will use the Rinkeby test network.
require("@nomiclabs/hardhat-ethers");
const INFURA_PROJECT_ID = "your_infura_project_id"; // Replace with your Infura Project ID
const PRIVATE_KEY = "your_wallet_private_key"; // Replace with your wallet private key
module.exports = {
solidity: "0.7.3",
networks: {
rinkeby: {
url: `https://rinkeby.infura.io/v3/${INFURA_PROJECT_ID}`,
accounts: [`0x${PRIVATE_KEY}`]
}
}
};
Deploying a Smart Contract
- Create a smart contract in the
contracts
directory. For this example, we'll use a simpleGreeter.sol
contract:
pragma solidity ^0.7.0;
contract Greeter {
string greeting;
constructor(string memory _greeting) {
greeting = _greeting;
}
function greet() public view returns (string memory) {
return greeting;
}
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
}
- Next, create a deployment script in the
scripts
directory nameddeploy.js
:
async function main() {
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, world!");
console.log("Greeter deployed to:", greeter.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Deploying the Contract
- Run the deployment script to deploy your contract to the Rinkeby test network:
npx hardhat run scripts/deploy.js --network rinkeby
Connecting MetaMask to Hardhat Network
- Open your MetaMask wallet and switch to the Rinkeby network.
- To add the Hardhat network to MetaMask, go to the "Networks" section in MetaMask and select "Add Network."
Fill in the following details:
- Network Name: Hardhat Local
- New RPC URL:
http://127.0.0.1:8545
- Chain ID: 31337
- Currency Symbol: ETH
- Block Explorer URL: (leave blank)
Interacting with Smart Contracts
Now that you have deployed your smart contract and connected MetaMask to the Hardhat network, you can interact with your smart contract using a front-end application. Here’s a simple example using JavaScript and ethers.js:
const { ethers } = require("ethers");
async function main() {
// Connect to MetaMask
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
// Replace with your deployed contract address
const greeterAddress = "your_deployed_contract_address";
const greeterContract = new ethers.Contract(greeterAddress, [
"function greet() view returns (string)",
"function setGreeting(string memory _greeting)"
], signer);
// // Call the greet function
const greeting = await greeterContract.greet();
console.log("Current greeting:", greeting);
// Set a new greeting
const tx = await greeterContract.setGreeting("Hello, Ethereum!");
await tx.wait();
console.log("Greeting updated!");
}
main().catch((error) => {
console.error(error);
});
Conclusion
By following these steps, you can successfully connect Hardhat to MetaMask, allowing you to deploy and interact with your smart contracts directly from your browser. This setup is essential for developing and testing decentralized applications on the Ethereum network.