Folks we have a lot to set up before starting our project. But don’t worry, I’ve got you! Just follow the steps and you will be all ready!Let’s get to setting up the environment:
- We will install and set up our Metamask account.
- We will set up the Sepolia testnet environment.
- We will fetch some fake money.
- We will set up Hardhat project.
- We will set up environment variables.
Your metamask wallet is your key to the web3 world. It is your digital wallet to store, swap and buy cryptocurrencies, tokens, NFTs, and other amazing things in the web3 world.
MetaMask has a mobile application and a Chrome browser extension too. I personally use MetaMask on my Chrome browser extension because I do most of the work on my laptop. Anyways,
let’s go!
Here are the installation steps:
- Go to MetaMask.ioClick
- Download and Install the Chrome Extension.
- Setup your secure password.
- The next step is the most important one.Secure your 12-word phrase properly, never share it with anyone.
- Write it somewhere because if you lose it, you will never have a way to recover your account. So be mindful of that.
- Congrats! your account is created and you must have received a public address.
- The public address will be in the pattern of: Example: 0x12r45...6HJ9
-
Use the provided information (Network name, RPC URL, Chain ID, Currency Symbol) to add the network manually.
Network name : Sepolia test network
RPC URL : https://rpc2.sepolia.org
chain ID : 11155111
Currency : ETH -
Open MetaMask.
-
Click the circle icon at the top right, then select "Settings." Navigate to "Networks" and click "Add Network."
-
If your desired network isn't listed, scroll to the bottom and choose "Add a Network Manually."
-
Input the Network Name, RPC URL, Chain ID, and Currency Symbol. The Blockchain Explorer field can be left blank.
-
Paste the values from the provided information and click "Save" to add the network.
-
You'll receive a success message prompting you to switch to the newly added network. Switch to it, and you'll see it displayed at the top middle of the app.
You can follow the steps given below:
- Head over to the site: https://sepoliafaucet.com/.
- Create an account there.
- Follow the steps if they ask you any questions.
- Select “Learning” as your project
- Select Ethereum as your blockchain you want to work with.
- After the questions, faucet will re-appear.
- Paste your account address and check captcha.
- Click on “Send me ETH” button.
After waiting for some time, you will receive 0.5 Sepolia ETH.
First make a project directory where you will work and we will install all the dependencies and hardhat there.
Now open your terminal and run the following commands one by one.
Just copy and paste one by one.
mkdir Hello-World
cd Hello-World
npm init --yes
npm install --save-dev hardhat
Now write the following command on the terminal.
npx hardhat
You will see something like this.
Please follow the guide and create a JavaScript project. Just follow through the questions and answer them yes. For me it was something like below: just press Enter/Yes to All.
You need to install some more dependencies.
npm install --save-dev @nomiclabs/hardhat-ethers ethers --force
npm install @nomiclabs/hardhat-ethers ethers hardhat @nomicfoundation/hardhat-toolbox --force
Let’s open your project in VSCode or Sublime or any other IDE your prefer. The structure will look something like this.
You can even install a Solidity extension there.
- Open the metamask extension to find your private key.
- Click on Account.
- Click on 3 dots on right side and go to “Account Details”.
- Click on “Show private key”.
- Enter Metamask password.
- Copy your private key.
Now if that private key is made visible, the hacker can gain access to your account and then the rest will be history. In order to avoid this issue. Let’s do the following.
npm install dotenv --save --force
Now go ahead and create .env file if you haven’t at the base of your project. Open your .gitignore file and write .env there.
Open the .env file you have just created. Replace YOUR_PRIVATE_KEY with your MetaMask Private Key.
PRIVATE_KEY="YOUR_PRIVATE_KEY"
- Login to your Alchemy account.
- Go to this link: https://dashboard.alchemy.com/apps
- Create new app.
- Name your app and write description.
- Choose “Ethereum Sepolia” as your Network.
- After creating the app, click on “API Key”, and copy HTTPS link.
Open your hardhat.config.js file and paste the following code.
Don’t forget to update with your Alchemy HTTPS link.
require("dotenv").config();
require("@nomiclabs/hardhat-ethers");
const { PRIVATE_KEY } = process.env;
module.exports = {
solidity: "0.8.9",
defaultNetwork: "sepolia",
networks: {
hardhat: {},
sepolia: {
url: "<YOUR-HTTPS-LINK>",
accounts: [`0x${PRIVATE_KEY}`],
},
},
};
run the following command to compile the smart contract and Create a .env file.
npx hardhat compile
You’ll get the following output.
Downloading compiler 0.8.9
Compiled 1 Solidity file successfully
Now, run the script file using the following command.
npx hardhat run scripts/deploy.js --network sepolia
If all goes well, your contract will be deployed and you will be able to see the address of the contract on the terminal. Copy that and save in your .env file with variable name as CONTRACT_ADDRESS, we will use it later to interact with the contract.
Now your .env file should look something like this.
PRIVATE_KEY="YOUR_PRIVATE_KEY"
CONTRACT_ADDRESS="YOUR_DEPLOYED_CONTRACT_ADDRESS"
Your contract is deployed.