Hello World Smart Contract

·

Creating your first smart contract is an exciting milestone in blockchain development. Whether you're new to Ethereum or expanding your decentralized application (dApp) toolkit, this guide walks you through deploying a simple "Hello World" smart contract on the Sepolia test network using industry-standard tools like Solidity, Hardhat, and Metamask. By the end, you’ll understand how to write, compile, and deploy a contract—laying the foundation for more complex dApps.

This tutorial emphasizes clarity and hands-on learning. We'll use Alchemy, a powerful blockchain developer platform, to interface with Ethereum without running your own node. Alchemy also provides real-time monitoring and analytics, giving you visibility into your contract’s deployment process.


Step 1: Connect to the Ethereum Network

To interact with Ethereum, you need access to its network. Running a full node is resource-intensive, so developers often use API services like Alchemy. It offers reliable, scalable access to Ethereum testnets and mainnets.

👉 Get started with Alchemy-powered development today.

Sign up for a free Alchemy account if you haven’t already. Once registered, you can generate API keys and connect to Ethereum networks instantly.


Step 2: Create Your App and API Key

After logging into Alchemy, navigate to the Apps section and click Create New App.

The Sepolia testnet allows you to experiment without spending real ETH. After creating the app, copy the HTTP API URL—you’ll use it later in your configuration.


Step 3: Set Up Your Ethereum Wallet

To send transactions, you need an Ethereum wallet. Metamask is the most popular browser-based wallet for development.

  1. Install the Metamask extension.
  2. Create or import an account.
  3. Switch to the Sepolia Test Network (enable test networks in settings if not visible).

Your wallet address will be used to deploy and interact with your smart contract.


Step 4: Get Test Ether from a Faucet

Deploying contracts requires gas, paid in ETH. On Sepolia, you can get free test ETH from a faucet.

Visit the Sepolia faucet, connect your Alchemy app, enter your Metamask address, and request funds. Depending on network traffic, this may take a few minutes.


Step 5: Verify Your Balance

Use Alchemy’s Composer tool to check your balance via the eth_getBalance method.

  1. Open Alchemy Composer.
  2. Select eth_getBalance.
  3. Enter your wallet address and set blockTag to "latest".
  4. Send the request.

The result appears in wei (1 ETH = 10¹⁸ wei). A response like 0x2B5E3AF16B1880000 equals 5 ETH, confirming your test funds are ready.


Step 6: Initialize Your Project

Open your terminal and create a project directory:

mkdir hello-world
cd hello-world
npm init -y

This generates a package.json file to manage dependencies.


Step 7: Install Hardhat

Hardhat is a development environment for compiling, testing, and deploying Ethereum software.

Run:

npm install --save-dev hardhat

Hardhat simplifies local development and integrates seamlessly with tools like Ethers.js.


Step 8: Create a Hardhat Project

In your project folder, run:

npx hardhat

Select "Create an empty hardhat.config.js". This generates the configuration file needed for setup.


Step 9: Organize Project Structure

Create two folders for better organization:

mkdir contracts scripts

Step 10: Write the Smart Contract

Navigate to contracts/ and create HelloWorld.sol. Paste the following 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;
    }
}

This contract:


Step 11: Secure Your Keys with Environment Variables

Never expose private keys or API endpoints in code. Use the dotenv package:

npm install dotenv

Create a .env file in the root:

PRIVATE_KEY=your_metamask_private_key
ALCHEMY_API_URL=https://eth-sepolia.g.alchemy.com/v2/your-api-key

👉 Learn how to securely manage blockchain credentials.

Retrieve your Metamask private key from settings → Account DetailsExport Private Key.
Get the Alchemy URL from your app dashboard under “HTTP API Key.”


Step 12: Install Ethers.js

Ethers.js simplifies Ethereum interactions:

npm install --save-dev @nomiclabs/hardhat-ethers ethers

This plugin enables clean contract deployment and transaction handling within Hardhat.


Step 13: Configure Hardhat

Update hardhat.config.js:

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

module.exports = {
  solidity: "0.8.0",
  networks: {
    sepolia: {
      url: process.env.ALCHEMY_API_URL,
      accounts: [process.env.PRIVATE_KEY]
    }
  }
};

This config tells Hardhat where to deploy and how to authenticate.


Step 14: Compile the Contract

Compile your Solidity code:

npx hardhat compile

You may see a warning about missing SPDX license identifiers—this is safe to ignore for learning purposes.


Step 15: Write the Deployment Script

In scripts/deploy.js, add:

async function main() {
  const HelloWorld = await ethers.getContractFactory("HelloWorld");
  const helloWorld = await HelloWorld.deploy("Hello, World!");
  await helloWorld.deployed();

  console.log("Contract deployed to:", helloWorld.address);
}

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

This script:


Step 16: Deploy to Sepolia

Run:

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

On success, you’ll see:

Contract deployed to: 0x...

Save this address—you’ll need it for future interactions.

Verify deployment on Sepolia Etherscan by searching the contract address.


Frequently Asked Questions (FAQ)

Q: Why use the Sepolia testnet instead of the mainnet?
A: Sepolia allows risk-free testing with free ETH. It mirrors Ethereum’s mainnet behavior without financial risk.

Q: What is Hardhat used for?
A: Hardhat provides a local Ethereum environment for compiling, testing, and deploying smart contracts efficiently.

Q: How do I fix "Error HH8: There are one or more errors in your config file"?
A: Double-check your .env file formatting and ensure both PRIVATE_KEY and ALCHEMY_API_URL are correctly copied without extra spaces or quotes.

Q: Can I redeploy the same contract?
A: Yes. Each deployment creates a new contract instance with a unique address.

Q: Is Solidity hard to learn?
A: If you know JavaScript or C++, Solidity’s syntax will feel familiar. Start small—like this Hello World—and build up gradually.

Q: Why use Ethers.js over Web3.js?
A: Ethers.js offers a lighter, more secure, and easier-to-use API with better TypeScript support—ideal for modern dApp development.


With your contract live on Sepolia, you’ve taken a major step in blockchain development. From here, you can expand functionality, add events, or integrate with frontends.

👉 Accelerate your Web3 journey with powerful development tools.