Solidity 101: Writing Your First Smart Contract

Tutorials · 2026-05-29 · 比特三棱镜编辑部
Ask AI

Five steps from zero to your first HelloWorld contract:

  1. Open Remix—develop right in the browser, no install.
  2. Write a tiny Solidity contract.
  3. Compile and look at the build output.
  4. Switch to a testnet, fund it from a faucet, deploy.
  5. Interact with the deployed contract from MetaMask or Remix itself.

No advanced math, no Linux, no prior blockchain expertise required. You just need one evening and a willingness to type along. (/uploads/20260529/1780066678858-99585.png)

Remix browser IDE with the first Solidity HelloWorld contract on screen

Why Start With Solidity

Solidity is the dominant smart-contract language for Ethereum and every EVM chain (Polygon, Arbitrum, Base, BSC, Avalanche C-Chain). Learn it and your contracts can theoretically deploy to dozens of chains—a footprint nothing else (Move, Rust) currently matches.

The syntax is JavaScript-flavored, so the curve isn’t steep. But three things make it fundamentally unlike normal programming:

  • Every line costs gas—storage and computation are paid in real money.
  • Deployed code can’t be modified—you can only deploy a new version.
  • The contract’s state is the user’s truth—bugs become exploits, not patches.

These three constraints shape the engineering mindset around smart contracts. After getting the basics, the blockchain development roadmap gives the bigger picture.

Step 1: Open Remix

Head to remix.ethereum.org. It’s a full Solidity IDE in the browser—editor, compiler, deployer, debugger included. Beginners shouldn’t bother with Hardhat or Foundry yet; Remix is enough.

The layout:

  • Left File Explorer: project tree.
  • Left sidebar icons: Solidity Compiler, Deploy & Run, debugger, plugins.
  • Center editor: where you type.
  • Bottom terminal: compile errors and tx logs surface here.

First-launch sample files (1_Storage.sol, 2_Owner.sol, 3_Ballot.sol) can be deleted or kept as reference.

Step 2: Write the Code

Create HelloWorld.sol and paste:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract HelloWorld {
    string public message;

    constructor(string memory _initialMessage) {
        message = _initialMessage;
    }

    function setMessage(string memory _newMessage) public {
        message = _newMessage;
    }
}

Line by line:

  • SPDX-License-Identifier: open-source license tag. Compiles without it, but it’s a good habit.
  • pragma solidity ^0.8.20: requires compiler ≥ 0.8.20. The 0.8 line has built-in overflow checks and is the current default track.
  • contract HelloWorld: like a class.
  • string public message: a state variable that lives on-chain forever. public auto-generates a getter.
  • constructor(…): runs once at deploy time, initializing message.
  • setMessage(…) public: any caller can rewrite message.

Under 10 lines, but already containing the state variable, constructor, and write function trinity—most contracts are just permutations of those.

Step 3: Compile

Switch to the Solidity Compiler tab:

  • Compiler version: 0.8.20 or later.
  • Click Compile HelloWorld.sol.
  • Green check = pass.

Two outputs matter:

  • ABI (Application Binary Interface): the function manifest. Frontends, wallets, and Etherscan use the ABI to call functions.
  • Bytecode: the actual code deployed on-chain.

Click “ABI” to copy the JSON. This is the plug between your contract and the outside world—Etherscan’s Read/Write Contract panels read this same ABI.

Step 4: Deploy to Testnet

MetaMask wallet popup switching to the Sepolia testnet before deploying a contract

Mainnet deployment costs real money (a few to tens of dollars in gas). Beginners must practice on testnet first. The active one is Sepolia.

Prep:

  1. Switch MetaMask to Sepolia: top network dropdown.
  2. Claim test ETH: any Sepolia faucet (sepoliafaucet.com, Alchemy Faucet) takes your address and drips 0.05–0.5 ETH—enough for dozens of deployments.
  3. Switch Remix environment: in Deploy & Run, set ENVIRONMENT to “Injected Provider - MetaMask”. MetaMask will ask permission.

Then:

  • Pick HelloWorld from the CONTRACT dropdown.
  • Type Hello, Web3 into the _initialMessage field.
  • Click Deploy.
  • MetaMask confirms → wait 10–30 seconds.
  • Once confirmed, your contract appears under “Deployed Contracts” with its address.

Paste that address into Sepolia Etherscan and you’ll see your deployment tx—a record that persists forever, available years from now.

Step 5: Interact

Back in Remix, expand the contract under “Deployed Contracts”:

  • message (blue): a view call, free, returns "Hello, Web3" instantly.
  • setMessage (orange): state-changing, requires a tx with gas.

Run through it:

  1. Type "Hello from <your name>" into the setMessage input.
  2. Click the button → MetaMask confirms.
  3. A few seconds later, click message again → value updated.

You just rewrote a state variable on a shared, globally consistent copy of state. Every node in the network agrees on the new value. That’s the magic part of smart contracts—no server, but state is globally consistent.

(/uploads/20260529/1780066723209-22519.png)

Beginner Traps

Gas estimate too low—Remix usually estimates fine but occasionally fails. Bump the Gas Limit manually.

Wrong network in MetaMask—deploying half an hour only to realize you were on mainnet wastes real gas. Glance at the network indicator every time.

Faucet runs dry—Sepolia faucets are rate-limited. Use multiple addresses.

Edited code without recompiling—Remix doesn’t auto-rebuild; you’d deploy the old version. Get into the habit of Ctrl+S after every change to trigger compile.

Want to change deployed code? Can’t. Bytecode is immutable. “Upgradable contracts” use a Proxy Pattern, which is beyond this tutorial.

How It Differs From Regular Programming

Dimension Regular backend Solidity contract
Deployment cost ~0 Paid per bytecode size
Modification Edit and restart Immutable; deploy new
State storage Your own DB On-chain storage, paid per byte
Concurrency Threads, locks Serial execution, no race
Debugging Logs and restart Events + on-chain traces

Burn those five rows into your brain and you’ll stop assuming the flexibility of a normal backend.

Where to Go Next

Once HelloWorld is deployed, typical next steps:

FAQ

  • Need JavaScript first? Not required, but the syntax overlap helps.
  • Can I deploy straight to mainnet? Technically yes, no one stops you—but no mainnet contract should ship without testing and audit. See contract audit basics.
  • Is Solidity obsolete? No. EVM chains hold 70%+ of the smart-contract market and won’t lose that soon.
  • Solidity or Rust? For mainstream dApps, Solidity first. For Solana / high-perf infra, add Rust later.

From Reading to Writing, Just One Deploy Away

The barrier to entry is lower than people think—an IDE, a wallet, a coffee’s worth of time, and you can push bytecode to Ethereum. The real bar comes after: writing exploit-free code, optimizing gas, designing upgradeable architecture, surviving audit. Those are the chapters that follow HelloWorld.

But tonight, just walk through the five steps.

From reading contracts to writing one, it’s just the courage to hit deploy.