Hashtag Web3 Logo

How to Create and Deploy Your First Smart Contract

A beginner-friendly guide to writing, compiling, and deploying a basic smart contract on the Ethereum blockchain using Remix.

How to Create and Deploy Your First Smart Contract - Hashtag Web3 article cover

Deploying your first smart contract is a rite of passage for any aspiring Web3 developer. It can seem daunting, but with modern tools, it's more accessible than ever. This guide will walk you through the entire process using Remix, a web-based IDE that's perfect for beginners.

What You'll Need

  • A web browser like Chrome or Firefox.
  • The MetaMask browser extension wallet.

That's it! We'll be working on an Ethereum test network, so you won't need any real cryptocurrency.

Step 1: Set Up Your Wallet and Get Test ETH

  1. Install MetaMask: If you don't have it, install the MetaMask extension from their official website.
  2. Switch to a Test Network: Open MetaMask and click on the network dropdown at the top. Select the "Sepolia" test network.
  3. Get Test ETH: You'll need some test Ether to pay for gas fees on the Sepolia network. Go to a Sepolia faucet (like sepoliafaucet.com), enter your wallet address, and request some funds. It may take a few minutes to arrive.

Step 2: Write Your Smart Contract in Remix

  1. Open Remix: Go to remix.ethereum.org in your browser.
  2. Create a New File: In the file explorer on the left, create a new file named HelloWorld.sol.
  3. Write the Code: Paste the following simple smart contract code into the file:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract HelloWorld {
    string public message;

    constructor() {
        message = "Hello, Web3 World!";
    }

    function updateMessage(string memory newMessage) public {
        message = newMessage;
    }
}

Code Breakdown:

  • pragma solidity ^0.8.20;: This line specifies the version of the Solidity compiler to use.
  • contract HelloWorld { ... }: This defines our smart contract.
  • string public message;: This declares a public state variable named message of type string. "Public" means Remix will automatically create a "getter" function for us to read its value.
  • constructor(): This is a special function that runs only once when the contract is first deployed. Here, we initialize our message.
  • function updateMessage(...): This is a public function that allows anyone to call it and change the value of the message variable.

Step 3: Compile Your Contract

  1. Go to the Compiler Tab: On the left-hand side of Remix, click the Solidity compiler icon.
  2. Set the Compiler Version: Make sure the compiler version matches the one in your code (e.g., 0.8.20).
  3. Compile: Click the "Compile HelloWorld.sol" button. If everything is correct, you'll see a green checkmark next to the compiler icon.

Step 4: Deploy to the Sepolia Testnet

  1. Go to the Deploy Tab: On the left, click the "Deploy & Run Transactions" icon.
  2. Set the Environment: In the "ENVIRONMENT" dropdown, select "Injected Provider - MetaMask". This tells Remix to use your MetaMask wallet to deploy. A MetaMask popup will ask you to connect your account; approve it.
  3. Deploy: Ensure your HelloWorld contract is selected in the "CONTRACT" dropdown. Click the orange "Deploy" button.
  4. Confirm in MetaMask: A MetaMask popup will appear asking you to confirm the deployment transaction. It will show the estimated gas fee (in Sepolia ETH). Click "Confirm".

Step 5: Interact With Your Deployed Contract

Once the transaction is confirmed, your contract will appear under "Deployed Contracts" at the bottom of the Remix panel.

  1. Read the Message: Click the blue button labeled message. This will instantly read the value of your public message variable and display it below: "Hello, Web3 World!".
  2. Update the Message: In the updateMessage field, type a new message like "My first dApp!", and click the orange transact button.
  3. Confirm the Transaction: Another MetaMask popup will appear, as this is a state-changing transaction that requires gas. Click "Confirm".
  4. Read the New Message: After the transaction confirms, click the message button again. You will see that the value has now been updated to "My first dApp!".

Congratulations! You have successfully written, compiled, and deployed your first smart contract to a public blockchain. You can now explore more complex contract types, build frontends that interact with them, and continue your journey as a Web3 developer.

Looking for a Web3 Job?

Get the best Web3, crypto, and blockchain jobs delivered directly to you. Join our Telegram channel with over 60,000 subscribers.