Chapter 34: Key Takeaways

Blockchain Fundamentals for Prediction Markets


Why Blockchain for Prediction Markets

  • Blockchain-based prediction markets offer censorship resistance, global access, trustless settlement, transparency, and composability --- advantages that centralized platforms cannot fully replicate.
  • These benefits come with trade-offs: higher transaction costs (gas), slower execution (block times), a steeper learning curve, and smart contract risk.
  • The choice between centralized and decentralized prediction markets depends on your priorities: if trust minimization and global access matter most, blockchain is compelling; if speed and low cost are paramount, centralized platforms may be preferable.

Blockchain Fundamentals

  • A blockchain is a distributed, append-only data structure maintained by a network of nodes following a consensus protocol.
  • Blocks contain transactions and are linked by cryptographic hashes, making the chain tamper-evident: altering any historical block invalidates all subsequent blocks.
  • Merkle trees organize transactions within blocks, enabling efficient verification with O(log n) proof sizes.
  • Proof of Work secures the chain through computational effort; Proof of Stake secures it through economic collateral (staked tokens). Ethereum uses PoS since "The Merge" in September 2022.
  • Finality determines when a transaction is irreversible. Ethereum's PoS provides epoch-based finality (~12.8 minutes), which is critical for prediction market settlement.

Ethereum and the EVM

  • Ethereum extends blockchain with a Turing-complete programming environment (the Ethereum Virtual Machine, or EVM), enabling arbitrary smart contract logic.
  • Ethereum has two account types: Externally Owned Accounts (EOAs) controlled by private keys, and Contract Accounts controlled by their code.
  • Every transaction specifies a gas limit and fee parameters (base fee + priority fee under EIP-1559). Gas is the unit of computational work; the base fee is burned, and the priority fee goes to validators.
  • Typical prediction market operations cost between 65,000 gas (token transfer) and 2,000,000 gas (market creation). At 20 gwei base fee and $3,000 ETH, a 200,000-gas trade costs approximately $12 on L1.

Smart Contracts

  • Smart contracts are immutable programs deployed on the blockchain that hold state, enforce rules, and interact with other contracts.
  • Solidity is the dominant smart contract language. Key constructs include state variables, functions (external/public/internal/private), events, modifiers, and constructors.
  • The ABI (Application Binary Interface) defines how to encode function calls and decode return values. Python's web3.py handles ABI encoding automatically.
  • Contract interactions follow the pattern: build transaction, sign with private key, broadcast, wait for confirmation.

web3.py --- Python's Blockchain Library

  • Providers connect web3.py to blockchain nodes: HTTPProvider (most common), WebsocketProvider (real-time), and IPCProvider (local nodes).
  • Reading data (balances, contract state, events) is free --- it runs locally on the node without consuming gas.
  • Writing data (sending transactions, calling state-changing functions) costs gas and requires a private key for signing.
  • Events/logs are the primary mechanism for tracking on-chain activity. They are cheaper to emit than storage writes and are indexed for efficient querying.
  • Pagination is essential when querying historical events, as RPC providers limit the block range per query.

Token Standards

  • ERC-20 defines fungible tokens (every unit is identical). Most prediction market collateral and simple outcome tokens use ERC-20.
  • ERC-721 defines non-fungible tokens (each unit is unique). Less common in prediction markets but used for unique positions or governance rights.
  • ERC-1155 is a multi-token standard that manages both fungible and non-fungible tokens in a single contract. Polymarket's Conditional Token Framework uses ERC-1155.
  • The split/merge mechanism is fundamental: 1 unit of collateral splits into 1 YES + 1 NO token; merging reverses the operation. This ensures YES price + NO price approximates 1.

Layer 2 Solutions

  • Layer 2 (L2) networks process transactions off Ethereum mainnet while inheriting its security. They reduce costs by 10-1000x and increase throughput.
  • Optimistic Rollups (Arbitrum, Optimism, Base) assume validity and allow fraud proof challenges during a 7-day window.
  • ZK-Rollups (zkSync, StarkNet) generate cryptographic validity proofs, enabling faster withdrawals but requiring more computation.
  • Sidechains (Polygon PoS) have their own consensus but lower security guarantees than rollups.
  • Polymarket operates on Polygon because the low fees ($0.001 per trade) and fast confirmations (2 seconds) make small bets economically viable.
  • Connecting to L2 from Python uses the same web3.py API --- only the RPC endpoint URL changes.

Wallets and Key Management

  • Ethereum uses elliptic curve cryptography (secp256k1). Address = last 20 bytes of keccak256(public key).
  • Never hardcode private keys in source code. Use environment variables or dedicated secrets management.
  • Minimize hot wallet funds and use separate accounts for trading, testing, and cold storage.
  • Monitor token approvals and revoke unused ones. Unlimited approvals are a security risk.

On-Chain Data Reading

  • Direct contract queries (view functions) provide current state data for free.
  • Event logs provide historical activity data (trades, market creation, settlement).
  • The Graph indexes blockchain data and exposes it via GraphQL, enabling efficient historical queries without block scanning.
  • Data pipelines for serious analysis combine contract queries, event logs, and indexed data sources.

Transaction Lifecycle

  • Transactions move through: construction, signing, broadcasting (mempool), inclusion (validator selection), execution (EVM), and confirmation (finality).
  • Gas price strategies range from "slow" (25th percentile, save money) to "urgent" (high priority fee, time-sensitive).
  • Transaction monitoring includes checking confirmation, handling reverts, and implementing retry logic with escalating gas prices.
  • Simulating transactions via eth_call before sending catches reverts early and saves gas on failed transactions.

Security

  • Reentrancy: External calls before state updates allow attackers to drain funds. The Checks-Effects-Interactions pattern prevents this.
  • Front-running / MEV: Visible mempool transactions enable sandwich attacks. Mitigations include private mempools (Flashbots), commit-reveal schemes, and slippage limits.
  • Oracle manipulation: The most critical risk for prediction markets. Decentralized oracle networks, optimistic oracles, and dispute mechanisms mitigate this risk.
  • Approval risks: ERC-20 approvals grant contracts permission to spend your tokens. Use exact amounts, not unlimited approvals.
  • Always verify contract addresses, read audit reports, start small, and use hardware wallets for significant positions.

Quick Reference

Concept Key Number
Ethereum block time ~12 seconds
Polygon block time ~2 seconds
PoS finality ~12.8 minutes (2 epochs)
Simple transfer gas 21,000
ERC-20 transfer gas ~65,000
Prediction market trade gas 100,000-300,000
1 ETH 10^18 wei
1 gwei 10^9 wei
EIP-1559 base fee Burned
EIP-1559 priority fee Goes to validator
Polygon chain ID 137
Ethereum chain ID 1