Sub topic 1
How on-chain data is structured: blocks, transactions, logs and traces
Prerequisites
This chapter assumes you know basic SQL (SELECT, WHERE, GROUP BY, JOIN), how transactions and smart contracts work, and what an ERC-20 token is. You also need a free Dune account.
The data model
A blockchain is an ordered chain of blocks. Each block records a number, a timestamp, and the transactions it contains.
A transaction is a signed message sent from one address to another. It carries a native-token value, input data, gas fields, and a success or failure status. Executing a transaction can produce two further kinds of records:
- Logs are event records that a smart contract emits during execution. An ERC-20 transfer emits a
Transferlog. - Traces record every internal call made during execution, including value transfers that emit no log.
Getting around Dune
Dune is a web platform that runs SQL queries against indexed blockchain data. The query editor lets you write, run, and save queries. Each run uses compute from your account's credit allowance, so test on a short date range before running a query over the full history.
DuneSQL basics
DuneSQL is Dune's SQL dialect, built on the Trino engine. Addresses are stored as binary values and written as hex literals with a 0x prefix. Timestamps are in UTC. Most large tables include a block_date column, and filtering on it limits how much data the engine scans.
Raw, decoded and curated tables
Dune organizes data in three layers:
- Raw tables, such as
ethereum.transactions, hold data as indexed from the chain. - Decoded tables, named after a contract, such as
erc20_ethereum.evt_Transfer, turn log bytes into named columns. - Curated tables, such as
dex.trades, combine many sources into one standard schema.
The query below counts yesterday's Ethereum transactions from the raw layer:
SELECT
COUNT(*) AS tx_count,
SUM(value) / 1e18 AS eth_moved
FROM ethereum.transactions
WHERE block_date = current_date - INTERVAL '1' DAYExercise
Why does the query above divide value by 1e18?
Show answer
Native-token values are stored in the smallest unit (wei on Ethereum). One ETH equals 10^18 wei, so dividing by 1e18 converts the sum to ETH.