Hello World Smart Contract: A Step-by-Step Guide

ยท

Estimated completion time: ~15 minutes

If you're new to blockchain development or simply want to learn how to deploy and interact with smart contracts, this guide is for you. We'll walk you through creating and deploying a simple smart contract on the Sepolia test network using tools like Metamask, Solidity, Hardhat, and Alchemy.


Prerequisites

Before diving into the smart contract code, ensure you complete these installations (Steps 1โ€“9):

  1. Alchemy Account: Sign up for a free Alchemy account to interact with the Ethereum blockchain.
  2. Virtual Wallet: Install Metamask to manage your Ethereum address.
  3. Test ETH: Use the Sepolia faucet to fund your wallet with test ETH.

Step-by-Step Deployment

Step 1: Set Up Your Alchemy App

  1. Navigate to your Alchemy Dashboard and create a new app.
  2. Name it Hello World, select Ethereum as the chain, and choose Sepolia as the network.

Step 2: Initialize Your Project

Create a project folder and run:

npm init -y

Install Hardhat:

npm install --save-dev hardhat

Step 3: Write the Smart Contract

  1. Create a file HelloWorld.sol in the contracts/ folder.
  2. Use this Solidity code:

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    
    contract HelloWorld {
     string public message;
     
     constructor(string memory initMessage) {
         message = initMessage;
     }
     
     function update(string memory newMessage) public {
         message = newMessage;
     }
    }

Step 4: Configure Environment Variables

Create a .env file with:

Example:

PRIVATE_KEY="YOUR_PRIVATE_KEY"
API_URL="https://eth-sepolia.g.alchemy.com/v2/YOUR_API_KEY"

Step 5: Deploy the Contract

  1. Run npx hardhat compile to ensure your code works.
  2. Execute the deploy script:

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

FAQs

Q1: How do I get test ETH for Sepolia?

A: Use the Sepolia faucet while connected to the Sepolia network in Metamask.

Q2: Why is my contract deployment failing?

A: Verify your .env file has the correct private key and API URL. Also, ensure you have sufficient test ETH.

Q3: Whatโ€™s the difference between Sepolia and Mainnet?

A: Sepolia is a test network for developers, while Mainnet is the live Ethereum blockchain.

๐Ÿ‘‰ Explore advanced smart contract interactions


Next Steps

  1. Interact with Your Contract: Call the update function to change the stored message.
  2. Publish to Etherscan: Verify your contract for public transparency.

For feedback or questions, reach out via Alchemy Support.

Happy coding! ๐Ÿš€


### Key SEO Keywords  
- Smart contract deployment  
- Ethereum Sepolia testnet  
- Hardhat tutorial  
- Solidity Hello World