✍️ Writing Solidity: Your First Smart Contract Code
Learn basic Solidity syntax for state variables, constructors, and functions
Your Progress
0 / 5 completed📝 Writing Your First Contract
Let's write a simple smart contract that stores a number on the blockchain. We'll break down every line so you understand what's happening!
🎮 Interactive: Line-by-Line Code Breakdown
Step through the code to understand each part of the contract
1// SPDX-License-Identifier: MIT2pragma solidity ^0.8.0;34contract SimpleStorage {5 uint256 public storedData;67 function set(uint256 x) public {8 storedData = x;9 }10}
Specifies the license. MIT is common for open-source. Required by Solidity compiler.
🔍 Key Solidity Concepts
Variables stored permanently on blockchain. Cost gas to write, free to read.
Code that can be called externally or internally. Can modify state or just read.
Control who can access functions and variables.
🎯 What Does This Contract Do?
Call set(42) to store 42 on blockchain
Call storedData() to read current value
Writing costs ~25,000 gas. Reading is FREE (doesn't modify state)
Anyone can read or write. No access control (for simplicity)
💡 Common Data Types
| Type | Description | Example |
|---|---|---|
| uint256 | Unsigned integer (0 to 2^256-1) | uint256 age = 25; |
| int256 | Signed integer (negative allowed) | int256 temp = -10; |
| address | Ethereum address (20 bytes) | address owner = 0x... |
| bool | Boolean (true/false) | bool isActive = true; |
| string | Text (UTF-8 encoded) | string name = "Alice"; |
| bytes32 | Fixed-size byte array | bytes32 hash = 0x... |
✏️ Try It Yourself: Remix IDE
Go to remix.ethereum.org in your browser
File Explorer → contracts folder → New File → Name it SimpleStorage.sol
Copy the SimpleStorage contract code and paste it into your file
Ctrl+S (Windows) or Cmd+S (Mac) to save. Remix auto-compiles!