Beginner
Solidity Basics: Your First Lines of Code — Hands-on
Know how a Solidity file is structured.
Developer Beginner
5/9 — Solidity Basics: Your First Lines of Code — Hands-on
Build and readWrite a tiny contract twice: once sloppily, once with explicit modifiers. Then compare the ABI and explorer view. Small differences (visibility, data locations) change the surface.
Trace the ABIUse an explorer or abi-to-interface tool to see what public state and functions became. Treat the ABI as the true public API.
// version A: sloppy defaults
contract CounterA {
uint count; // default internal, but public getter if marked public
function inc() public { count++; } // no mutability, no events
}
// version B: explicit, ABI-friendly
contract CounterB {
uint256 private _count;
event Incremented(uint256 newValue);
function getCount() external view returns (uint256) { return _count; }
function increment() external { _count += 1; emit Incremented(_count); }
}
Steps
- Create a Solidity file:With SPDX and pragma ^0.8.24. Add a contract with one public uint counter and a function increment() that mutates it.
- Compile and inspect the:ABI (e.g., Hardhat artifacts). Note the auto-generated getter for counter and the selector for increment().
- Refactor:Make counter internal, add a public getter getCounter(), mark increment() as external, and add event Incremented(uint256). Recompile and compare ABI length.
- Add a pure function:That returns a computed value (no storage). Mark it pure and see gas estimates drop.
- Swap increment() to accept:Calldata input and compare gas to a memory version; note that calldata avoids copies for external calls.
- Write a function that:Takes an array as calldata and the same as memory. Compare gas costs using a local gas reporter.
- Deploy to a testnet,:View the contract in an explorer, and observe which functions and getters appear. Call them to verify behavior.