Hashtag Web3 / Updated
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.
Deploying your first smart contract represents a significant milestone for anyone aspiring to become a Web3 developer. While the process may initially appear intimidating, modern tools make it straightforward. This guide provides a detailed walkthrough of creating and deploying a basic smart contract using Remix, a popular web-based Integrated Development Environment (IDE).
Requirements
To get started, you will need:
- A web browser, preferably Chrome or Firefox.
- The MetaMask browser extension wallet.
This setup allows you to interact with the Ethereum test network, so there’s no need for real cryptocurrency during this initial phase.
Step 1: Set Up Your Wallet and Acquire Test ETH
- Install MetaMask: Download the MetaMask extension from the official site if you haven’t done so already.
- Switch to a Test Network: Open MetaMask and select the network dropdown at the top. Choose the "Sepolia" test network.
- Obtain Test ETH: You will need test Ether to cover gas fees on the Sepolia network. Visit a Sepolia faucet (for example,
sepoliafaucet.com), enter your wallet address, and request some funds. The test ETH may take a few minutes to arrive.
Step 2: Write Your Smart Contract in Remix
- Access Remix: Open your browser and work through to
remix.ethereum.org. - Create a New File: In the file explorer on the left, create a new file named
HelloWorld.sol. - Insert the Code: Copy and paste the following basic smart contract code into the newly created 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 specifies the Solidity compiler version to use.contract HelloWorld { ... }: This defines the smart contract.string public message;: This declares a public state variable namedmessageof typestring. Its public visibility ensures that Remix generates a "getter" function for reading its value.constructor(): This function runs once when the contract is deployed, initializing themessage.function updateMessage(...): This public function allows anyone to change themessagevariable's value.
Step 3: Compile Your Contract
- Work through to the Compiler Tab: Click the Solidity compiler icon on the left side of Remix.
- Select the Compiler Version: Ensure the compiler version matches the one specified in your code (e.g.,
0.8.20). - Compile the Contract: Click the "Compile HelloWorld.sol" button. A green checkmark next to the compiler icon indicates successful compilation.
Step 4: Deploy to the Sepolia Testnet
- Go to the Deploy Tab: Click the "Deploy & Run Transactions" icon on the left.
- Choose Environment: In the "ENVIRONMENT" dropdown, select "Injected Provider - MetaMask." This instructs Remix to use your MetaMask wallet for deployment. A MetaMask popup will prompt you to connect your account; approve it.
- Deploy the Contract: Make sure your
HelloWorldcontract is selected in the "CONTRACT" dropdown. Click the orange "Deploy" button. - Confirm in MetaMask: A MetaMask popup will appear for you to confirm the deployment transaction. This 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.
- Read the Message: Click the blue button labeled
message. This action retrieves the value of themessagevariable, displaying "Hello, Web3 World!". - Update the Message: In the
updateMessagefield, type a new message, such as "My first dApp!", and click the orangetransactbutton. - Confirm the Transaction: Another MetaMask popup will appear, as this is a state-changing transaction that requires gas. Click "Confirm."
- Read the New Message: After the transaction confirms, click the
messagebutton again. You will see the updated value, now reflecting "My first dApp!".
Congratulations on successfully writing, compiling, and deploying your first smart contract to a public blockchain. This achievement opens the door to exploring more complex contract types and developing frontends that interact with them, advancing your journey as a Web3 developer.
