Skip to content

Mohammed-Saleh-Ishaq/Solidity-Smart-Contract-on-Ethereum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 

Repository files navigation

Solidity-Smart-Contract-on-Ethereum

setting up the environment

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:

  1. We will install and set up our Metamask account.
  2. We will set up the Sepolia testnet environment.
  3. We will fetch some fake money.
  4. We will set up Hardhat project.
  5. We will set up environment variables.

Creating MetaMask wallet

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:

  1. Go to MetaMask.ioClick
  2. Download and Install the Chrome Extension.
  3. Setup your secure password.
  4. The next step is the most important one.Secure your 12-word phrase properly, never share it with anyone.
  5. Write it somewhere because if you lose it, you will never have a way to recover your account. So be mindful of that.
  6. Congrats! your account is created and you must have received a public address.
  7. The public address will be in the pattern of: Example: 0x12r45...6HJ9

Manually add Sepolia to your MetaMask

  1. Use the provided information (Network name, RPC URL, Chain ID, Currency Symbol) to add the network manually. m-step-4 ffba6a8a

    Network name : Sepolia test network
    RPC URL : https://rpc2.sepolia.org
    chain ID : 11155111
    Currency : ETH

  2. Open MetaMask.

  3. Click the circle icon at the top right, then select "Settings." Navigate to "Networks" and click "Add Network."
    m-step-5 e9128667

  4. If your desired network isn't listed, scroll to the bottom and choose "Add a Network Manually." m-step-6 209228f9

  5. Input the Network Name, RPC URL, Chain ID, and Currency Symbol. The Blockchain Explorer field can be left blank. m-step-7 b0a24dd6

  6. Paste the values from the provided information and click "Save" to add the network.

  7. 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.

Get some Sepolia ETH $$

You can follow the steps given below:

  1. Head over to the site: https://sepoliafaucet.com/.
  2. Create an account there.
  3. Follow the steps if they ask you any questions.
    1. Select “Learning” as your project
    2. Select Ethereum as your blockchain you want to work with.
  4. After the questions, faucet will re-appear.
  5. Paste your account address and check captcha.
  6. Click on “Send me ETH” button.

After waiting for some time, you will receive 0.5 Sepolia ETH.

Set up HardHat project

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.
HardHat-output

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. hardhat-output-2

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

Project structure

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.

If you don't see a script, just add it manually. In the script, add a new file name, deploy.js

Get your private MetaMask key

  1. Open the metamask extension to find your private key.
  2. Click on Account.
  3. Click on 3 dots on right side and go to “Account Details”.
  4. Click on “Show private key”.
  5. Enter Metamask password.
  6. Copy your private key.

Setting up the environment variables

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"

Get Alchemy HTTP link

  1. Login to your Alchemy account.
  2. Go to this link: https://dashboard.alchemy.com/apps
  3. Create new app.
  4. Name your app and write description.
  5. Choose “Ethereum Sepolia” as your Network.
  6. After creating the app, click on “API Key”, and copy HTTPS link.

Updating Hardhat Config file

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.

About

Write Solidity Smart Contract on Ethereum

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published