Deploy Your First Smart Contract

·

Estimated completion time: 15 minutes

New to blockchain development? This guide walks you through deploying a simple smart contract on the Goerli testnet using Metamask, Solidity, Hardhat, and Alchemy. No prior expertise required—we’ll explain each step!

👉 Ready to dive deeper into blockchain?


Step-by-Step Smart Contract Deployment

Step 1: Connect to Ethereum via Alchemy

Use Alchemy’s developer platform to interact with Ethereum without running your own node.

Step 2: Create an App & API Key

Generate an API key to access the Goerli testnet:

  1. Navigate to "Create App" in your Alchemy Dashboard.
  2. Name it "Hello World," select "Staging," and choose "Goerli" as the network.
    Critical: Verify Goerli is selected!

Step 3: Set Up Metamask

Create an Ethereum account using Metamask.

Step 4: Get Test Ether

Use the Goerli faucet to request fake ETH. Network delays may occur (~30 mins).

Step 5: Check Balance

Confirm your ETH balance via Alchemy’s composer tool:

{"jsonrpc":"2.0","id":0,"result":"0x2B5E3AF16B1880000"}

Note: Results are in wei (1 ETH = 10¹⁸ wei).

Step 6: Initialize Project

mkdir hello-world && cd hello-world
npm init

Follow prompts to create package.json.

Step 7: Install Hardhat

npm install --save-dev hardhat

Step 8: Create Hardhat Project

npx hardhat

Select "Create an empty hardhat.config.js."

Step 9: Organize Folders

mkdir contracts scripts

Step 10: Write the Contract

  1. Create HelloWorld.sol in contracts/.
  2. Paste this Solidity code:

    pragma solidity >=0.7.3;
    contract HelloWorld {
     event UpdatedMessages(string oldStr, string newStr);
     string public message;
     constructor(string memory initMessage) {
         message = initMessage;
     }
     function update(string memory newMessage) public {
         string memory oldMsg = message;
         message = newMessage;
         emit UpdatedMessages(oldMsg, newMessage);
     }
    }

Step 11: Secure Environment Variables

Store keys in .env:

API_URL="https://eth-goerli.alchemyapi.io/v2/your-api-key"
PRIVATE_KEY="your-metamask-private-key"

Install dotenv:

npm install dotenv --save

Step 12: Add Ethers.js

npm install --save-dev @nomiclabs/hardhat-ethers "ethers@^5.0.0"

Step 13: Update Hardhat Config

Edit hardhat.config.js:

require('dotenv').config();
require("@nomiclabs/hardhat-ethers");
module.exports = {
  solidity: "0.7.3",
  networks: {
    goerli: {
      url: process.env.API_URL,
      accounts: [process.env.PRIVATE_KEY]
    }
  }
};

Step 14: Compile Contract

npx hardhat compile

Step 15: Create Deploy Script

Save as scripts/deploy.js:

async function main() {
  const HelloWorld = await ethers.getContractFactory("HelloWorld");
  const hello_world = await HelloWorld.deploy("Hello World!");
  console.log("Contract deployed to:", hello_world.address);
}
main().catch(error => console.error(error));

Step 16: Deploy to Goerli

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

Success Output:
Contract deployed to: 0xCAFBf889bef0617d9209Cf96f18c850e901A6D61

👉 Explore advanced blockchain tools


FAQs

What’s a testnet?

A simulated blockchain for developers to test applications without real funds.

Why use Hardhat?

It simplifies compiling, testing, and debugging Ethereum contracts locally.

How do I debug deployment issues?

Check Alchemy’s Explorer tab for transaction logs or join the Alchemy Discord.