Sub topic 1
How on-chain data is structured: blocks, transactions, logs and traces
This chapter explains the four layers of data that a blockchain produces. Every table you query later in Dune, and every number you read on DefiLlama, is built from these layers. Knowing what each layer records, and what it leaves out, tells you which table to reach for and why two tools can report different numbers.
Prerequisites
This chapter assumes you already know:
- Basic SQL: SELECT, WHERE, GROUP BY and JOIN.
- Blockchain basics: what a transaction, a block, an address and a smart contract are.
- What an ERC-20 token is and what a decentralized exchange (DEX) does.
You do not need to run any code to follow this chapter. The queries are previews of what you will write in later chapters. The examples use Ethereum, but the same model applies to other chains that run the Ethereum Virtual Machine (EVM).
The layered model
A blockchain is an ordered list of blocks. Each block holds an ordered list of transactions. Executing a transaction runs smart contract code, and that execution produces two more kinds of data: logs and traces.
Each layer answers a different question:
| Layer | What it records | Typical question it answers |
|---|---|---|
| Block | Number, timestamp, miner or validator, gas used | How many blocks were produced today? |
| Transaction | Sender, recipient, value in ETH, gas, input data, status | Who called which contract, and did it succeed? |
| Log | Events a contract chose to emit | How many tokens moved in a Transfer? |
| Trace | Every internal call, including ETH sent between contracts | Where did ETH move inside a complex transaction? |
Blocks
A block is a batch of transactions with a header. The header fields you will use most are:
- Number: the block's height. It increases by one for each block.
- Time: the timestamp the block was produced. Every transaction and log in the block shares this time.
- Hash and parent hash: the block's identifier and a link to the previous block.
- Gas used and gas limit: how much of the block's capacity was consumed.
- Base fee: the minimum price per unit of gas, burned after the EIP-1559 upgrade.
Block time is the only clock the chain has. When you later group activity "by day", you are grouping by block time, not by when you ran the query. Because a block holds many transactions, blocks are rarely the right table for business questions. They are useful for time ranges and for gas analysis.
Transactions
A transaction is a signed message from an externally owned account (EOA). An EOA is an address controlled by a private key, as opposed to a contract address. Only EOAs can start a transaction. Important fields:
- from and to: the sender and the recipient. For a contract creation,
tois empty. - value: the amount of ETH sent, in wei. One ETH is 10^18 wei.
- data (input): the encoded call. The first 4 bytes are the function selector, which identifies the function being called. The rest are its arguments.
- gas used and gas price: what the sender paid for execution.
- success: whether execution finished or reverted.
Two edge cases matter in practice.
First, a failed transaction is still included in the block, and the sender still pays gas. Its state changes are rolled back, and its logs are discarded. If you count transactions without checking the success flag, you count failures as activity.
Second, the transaction's to field only shows the first contract called. A swap through an aggregator has to set to the aggregator's router. The pool that actually executed the trade appears only in the traces and logs.
Logs and events
A log is a record that a contract writes during execution using an event. Logs are cheap to store and easy to search, so contracts use them to announce what happened. Most token and DEX analytics are built from logs.
A log has these parts:
- Contract address: the contract that emitted the event.
- topic0: the hash of the event signature. For the ERC-20
Transfer(address,address,uint256)event, this is a fixed value shared by every ERC-20 token. - topic1 to topic3: up to three indexed parameters. For
Transfer, these are the sender and the recipient. - data: the non-indexed parameters packed together. For
Transfer, this is the amount. - Log index: the position of the log within the block.
Here is the Transfer event as it appears in Solidity:
event Transfer(address indexed from, address indexed to, uint256 value);The indexed keyword moves a parameter into a topic, which makes it filterable. The value is not indexed, so it sits inside data as raw bytes.
A raw log is hard to read. A decoder uses the contract's ABI (application binary interface, the description of its functions and events) to turn the bytes into named columns. Dune does this for many contracts, so you can write from, to and value instead of unpacking hex. You will see these tables in the chapter on raw, decoded and curated tables.
Logs have two limits you should remember:
- A contract must choose to emit an event. A contract that moves tokens without emitting a standard event leaves no log trail for that movement.
- Logs cannot be read by other contracts. They exist for off-chain consumers such as analysts, and that is the reason they are cheap.
Traces
A trace is a record of one call made during a transaction. When a contract calls another contract, or sends ETH to one, that is an internal call, and it gets its own trace entry. The traces of one transaction form a tree. The top-level call from the EOA is the root, and each nested call is a child.
Each trace row typically includes:
- from and to: caller and callee for that step.
- call type:
call,delegatecall,staticcall, orcreate. - value: ETH moved in that step.
- input and output: the encoded arguments and return data.
- trace address: the path in the call tree, for example
[0, 2, 1]. - success: whether this step succeeded. A child call can fail while the parent still succeeds, because the parent may catch the error.
Traces are the only place to see ETH sent from one contract to another. A plain ETH transfer between contracts emits no log. For example, a multi-sig wallet paying out ETH shows up in traces and not in the transaction's value, which only covers the top-level call.
Traces are much larger than logs. A single complex transaction can have hundreds of trace rows. Querying them over a long period is slow and expensive, so use them only when logs and curated tables cannot answer the question.
Logs versus traces
| Question | Use |
|---|---|
| How much USDC was transferred? | Logs (the Transfer event) |
| How much ETH did a contract receive from other contracts? | Traces |
| Which function was called, with which arguments? | Transactions or traces |
| Which pool executed a routed swap? | Logs (Swap event) or traces |
| Did an internal step fail inside a successful transaction? | Traces |
A worked example: following one swap
Suppose a user swaps USDC for ETH through a DEX aggregator. Walk through each layer.
- Block: the transaction is included in block N, with a block timestamp. That timestamp is the time you will report.
- Transaction: the
fromis the user's EOA and thetois the aggregator router. The input starts with the selector for the router's swap function. Thevalueis 0 because the user is paying in USDC, not ETH. - Traces: the router calls a pool, and the pool calls the USDC contract and sends ETH back. The ETH payout appears here as a trace with a non-zero value.
- Logs: USDC emits a
Transferevent for the payment. The pool emits its ownSwapevent with the amounts in and out.
A curated table such as dex.trades reads the pool's Swap log, prices the tokens, and writes one row per trade. That is the reason you can answer "how much volume did Uniswap do yesterday" in one short query. The raw layers hold the same information, but you would have to decode and price it yourself.
Here is a preview of a query against a decoded log table. You will learn the details of the syntax later.
SELECT
date_trunc('day', evt_block_time) AS day,
COUNT(*) AS transfers
FROM erc20_ethereum.evt_Transfer
WHERE contract_address = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
AND evt_block_date >= current_date - INTERVAL '7' DAY
GROUP BY 1
ORDER BY 1This counts USDC Transfer events per day. It uses the log layer, so it sees transfers made through routers, pools and any other contract, not just direct calls.
Common errors
- Counting reverted transactions. Filter on the success flag when you count activity.
- Reading the transaction
toas the final destination. Routers, proxies and wallets hide the real counterparty. Look at logs or traces. - Treating
valueas the full ETH movement. It covers only the top-level call. Internal ETH moves are in traces. - Assuming every token movement emits a Transfer log. Standard tokens do, but a nonstandard contract may not, and native ETH never does.
- Ignoring units. Amounts are integers in the token's smallest unit. ETH uses 18 decimals and USDC uses 6. Divide by the right power of ten before summing.
- Using traces for everything. Traces are accurate but heavy. Start from logs or curated tables.
Summary of the model
- Blocks give time and order.
- Transactions give the entry point: who called what, and whether it succeeded.
- Logs give the events contracts announce, which is the basis of most token and DEX analytics.
- Traces give the full call tree, including internal ETH movement.
- Decoded and curated tables sit on top of these layers and make them easier to query. The next chapters show how.
Exercise
A user sends a transaction to a DEX router. The transaction succeeds, and value is 0. Inside it, the router calls a pool, and the pool sends 2 ETH to the user. Which layer shows the 2 ETH payout, and why does the transaction's value field not show it?
Show answer
The traces show it. The transaction's value records only the ETH attached to the top-level call from the user to the router, which was 0. ETH sent between contracts during execution is an internal call, and a plain ETH transfer emits no log, so the trace row with a non-zero value is the only record of it.
Exercise
You count rows in a transactions table to measure how many swaps a router handled last week, and the number is higher than the count of Swap events. Name two likely reasons.
Show answer
First, the transactions table includes failed (reverted) transactions, whose logs are discarded, so they appear as transactions but produce no Swap events unless you filter on the success flag. Second, a transaction to the router may not be a swap at all, because it can call other functions such as approvals or liquidity management. A single swap transaction can also route through several pools and emit several Swap events, which pushes the event count the other way, so the two counts measure different things.
Exercise
Why does the query in the worked example use the Transfer log table instead of the transaction table to measure USDC transfers?
Show answer
Most USDC transfers happen inside calls to other contracts, such as routers, pools and lending protocols. The transaction table only shows the top-level call and its to address, so it misses those. The Transfer event is emitted by the USDC contract on every transfer, so the log table captures all of them regardless of who made the call.