Skip to content
LogoLogo

Lithe by Example

Short, complete programs that show Lithe's syntax and idioms, in the spirit of Solidity by Example. Each one is a whole contract that you can compile with lithe compile. The docs build compiles every example, so none of them can fall out of date.

New to Lithe? Read Introduction and The transition model first, then work through the Basics in order.

Basics

  • Hello world: The smallest useful Lithe contract.
  • Counter: Read, increment, and decrement a stored number.
  • Primitive types: Booleans, integers of every width, addresses, bytes, strings, and semantic types.
  • Variables: Locals, storage, transient storage, and execution context.
  • Constants: Named constants as pure functions, and built-in bounds.
  • Reading and writing state: Views read storage; stateful functions write it.
  • Ether and time units: wei, gwei, eth, and durations, with typed arithmetic.
  • If / else: Conditionals, nested branches, and choosing a value.
  • Loops: For loops over lists and ranges, with until and bound.
  • Recursion: Recursive algorithms written as bounded loops.

Data

  • Maps: Mappings, nested mappings, defaults, and deletion.
  • Lists: Dynamic and fixed-length lists in storage and memory.
  • Variants: Closed sets of cases with payloads, matched exhaustively.
  • Records: Structs: declaring, constructing, storing, and updating fields.
  • Tuples: Grouping values by position, returning several values, and destructuring.
  • Options: Optional values with Some and None.
  • Nominal types: New types over existing representations, for domain safety at no runtime cost.

Functions

  • Functions: Parameters, results, blocks with with, mutable parameters, and tuples.
  • Overloading: Functions that share a name, picked by their arguments.
  • View and pure: Declared effects, checked by the compiler.
  • Visibility: External, internal, and private functions.
  • Errors: Declaring errors, raising them, and declaring what a function raises.
  • Guards: Access control shared by groups of entrypoints, instead of modifiers.
  • Events: Declaring, indexing, and emitting events.
  • Initialization: Setting up a contract as it is deployed.

Ether and calls

Contracts and modules

Encoding and crypto

Applications

  • Ether wallet: Anyone can deposit; only the owner can withdraw.
  • ERC-20 token: A complete ERC-20 with owner minting and ERC-6093 errors.
  • ERC-721 NFT: Ownership, approvals, operators, transfers, mint, and burn.
  • Multi-sig wallet: Submit, confirm, revoke, and execute ether payments with 2-of-3 approval.
  • Iterable set: Enumerate members without loops.
  • Escrow: A buyer, a seller, and an arbiter.
  • Crowdfund: Pledge toward a goal, then claim or refund.
  • English auction: Ascending bids with pull-based refunds.
  • Dutch auction: A price that falls over time.
  • Time lock: Queue payments with a delay before they can execute.
  • Vesting: Linear release of ether to a beneficiary.
  • Order executor: Swap, then deposit the proceeds, with chained continuations.