Smart Contracts

Definition

A smart contract is a program stored on a blockchain that automatically executes when predetermined conditions are met. It encodes the rules of an agreement and enforces them programmatically — no intermediary (bank, lawyer, notary) is needed.


Core Ideas

Properties

  • Deterministic — given the same inputs and state, always produces the same output
  • Immutable — once deployed, code cannot be changed (upgradeable patterns exist as workarounds)
  • Transparent — code is publicly visible on-chain
  • Trustless — execution is enforced by the network, not a central party
  • Permissionless — anyone can deploy or interact with a contract

Ethereum & Solidity

Ethereum is the primary platform for smart contract development in the source notes.

Solidity basics:

pragma solidity ^0.8.0;
 
contract SimpleStorage {
    uint256 private value;
 
    function set(uint256 _value) public {
        value = _value;
    }
 
    function get() public view returns (uint256) {
        return value;
    }
}

Key concepts:

  • address type for Ethereum accounts and contracts
  • mapping for key-value storage
  • event for emitting logs (cheaper than storage)
  • modifier for access control
  • payable for receiving ETH

Token Standards

StandardTypeUse Case
ERC-20Fungible tokenCryptocurrencies, governance tokens
ERC-721Non-fungible (NFT)Digital art, collectibles, ownership
ERC-1155Multi-tokenGames (items that are both fungible and non-fungible)

Security

Critical vulnerabilities in source notes:

  • Re-entrancy — attacker re-enters the contract before balance update; fix with checks-effects-interactions pattern or ReentrancyGuard
  • Integer overflow/underflow — use Solidity 0.8+ (built-in checks) or SafeMath
  • Access control — use OpenZeppelin Ownable or role-based access
  • Front-running — MEV (Miner Extractable Value) and commit-reveal schemes

Development Tooling

  • Hardhat — local development environment, testing, deployment
  • Truffle — older alternative; Ganache for local chain
  • OpenZeppelin — audited contract library (tokens, access, governance)
  • Etherscan — block explorer; contract verification

Relationships


References

  • Solidity development tutorials and notes