Solidity Smart Contract Development

intermediatev1.0.0tokenshrink-v2
# Solidity SC Development

## Solidity Language Fundamentals

Solidity is a statically-typed, contract-oriented language targeting the EVM. Every SC is deployed at an address and contains state variables (stored on-chain), functions (logic), events (indexed logs), and MRs (reusable preconditions).

Value types: `uint256` (most common, default for integers — always use explicitly sized types), `int256`, `address` (20 bytes, use `address payable` for ETH transfers), `bool`, `bytes32` (cheaper than `string` for fixed-length data). Reference types: arrays, mappings, structs. Mappings cannot be iterated — if you need iteration, maintain a parallel array.

Visibility: `public` (external + internal, auto-generates getter), `external` (only callable from outside — cheaper than public for large calldata), `internal` (this contract + derived), `private` (this contract only — NOT truly private, data is readable on-chain).

State mutability: `view` (reads state, no gas when called externally), `pure` (no state access), `payable` (can receive ETH). Unmarked functions can read and write state.

Key patterns:
```solidity
// MR pattern — reusable preconditions
modifier onlyOwner() {
    require(msg.sender == owner, "Not owner");
    _;
}

// Event emission — always emit after state changes
event Transfer(address indexed from, address indexed to, uint256 value);

// Error handling — custom errors save gas vs require strings
error InsufficientBalance(uint256 requested, uint256 available);
if (balance < amount) revert InsufficientBalance(amount, balance);
```

Custom errors (Solidity 0.8.4+) save significant gas compared to `require` with string messages. A `require` with a 32-character string costs ~500 more gas than a custom error `revert`.

## OZ Patterns & Libraries

OZ provides audited, battle-tested SC components. Always inherit rather than writing from scratch.

**AC patterns**: `Ownable` for single-owner (simple but centralized). `AccessControl` for role-based (define roles like MINTER_ROLE, PAUSER_ROLE). `AccessControlDefaultAdminRules` adds two-step admin transfer with delay — use this for production.

**Token standards**: `ERC20` (fungible tokens — include `ERC20Permit` for gasless approvals via EIP-2612), `ERC721` (NFTs — use `ERC721Enumerable` only if on-chain enumeration needed, it's expensive), `ERC1155` (multi-token — single contract for fungible + non-fungible, batch transfers save gas).

Showing 20% preview. Upgrade to Pro for full access.

2.7K

tokens

13.0%

savings

Downloads0
Sign in to Download