Sub topic 12
Common pitfalls: decimals, double counting and prices
This chapter assumes basic SQL, blockchain transactions, ERC-20 tokens, and decentralized exchanges. You should know how to read a table schema and use SELECT, WHERE, GROUP BY, and JOIN.
On-chain data records token amounts as integers. Analysts must convert those integers, decide which events represent the activity they want, and choose a price source. A query can run successfully while producing a misleading number.
Token decimals
An ERC-20 token usually stores its balance in the smallest unit. The token's decimals metadata tells you how many decimal places the displayed amount uses.
For example:
| Token | Raw value | Decimals | Displayed amount |
|---|---|---|---|
| USDC | 1,250,000 | 6 | 1.25 USDC |
| WETH | 1,000,000,000,000,000,000 | 18 | 1 WETH |
The conversion is:
human_amount = raw_amount / 10^decimals
Do not assume that every token uses 18 decimals. USDC uses 6. Some tokens use 0, 8, or other values. Read the token metadata or use a trusted token dimension table.
This query converts USDC transfer values from raw units:
SELECT
date_trunc('day', evt_block_time) AS day,
SUM(CAST(value AS DECIMAL(38, 0))) / DECIMAL '1000000' AS usdc_transferred
FROM erc20_ethereum.evt_Transfer
WHERE contract_address = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
AND evt_block_date >= current_date - INTERVAL '7' DAY
GROUP BY 1
ORDER BY 1;The partition filter on evt_block_date limits the scanned data. The contract address identifies USDC on Ethereum. Always verify the address and chain before interpreting the result.
Double counting events
A single user action can create many blockchain records. A swap may emit transfers for the input token, output token, pool reserves, a router, and a fee recipient. Summing every Transfer event may count the same trade several times.
For DEX volume, prefer a curated trade table when it matches your question. Curated tables usually combine related events into one trade record and expose fields such as amount_usd, project, and blockchain.
Transfer tables remain useful for questions such as:
- How many USDC tokens moved?
- Which addresses received a token?
- How much was minted or burned?
- What activity occurred for a particular contract?
Before aggregating, identify the table's grain. The grain is what one row represents. If one row represents one token leg, it is not automatically one swap.
Price assumptions
A USD column depends on a price source and a timestamp. Two systems can report different values because they use different prices, time windows, token coverage, or filtering rules.
Common sources of disagreement include:
- Spot price versus a time-weighted price.
- Trade-time prices versus end-of-day prices.
- Different treatment of stablecoins and depegged assets.
- Excluding wash trading, failed trades, or suspicious pools.
- Different treatment of fees and multi-hop swaps.
- Different protocol labels or chain coverage.
DefiLlama metrics come from protocol-specific adapters and defined methodology. Dune results depend on the selected table, query logic, and price data used by that table. Neither number is automatically the universal answer.
A practical validation routine
For each metric, record:
- The table and chain.
- The row grain.
- The decimal rule.
- The price source and timestamp.
- Exclusions for mints, burns, routers, or suspicious activity.
- Whether the metric counts token legs, swaps, or unique users.
Then compare a small sample manually. Pick one transaction, inspect its logs, and confirm that your query counts the intended economic action once.
Exercise
Exercise
A query sums USDC value from evt_Transfer and returns 250000000 for one day. What is the displayed USDC amount, and why might it still not equal DEX volume?
Show answer
USDC has 6 decimals, so the amount is 250 USDC. It may include ordinary wallet transfers, minting, burning, liquidity movements, and multiple transfer legs from the same swap. A DEX-volume query should use an appropriate trade grain and filtering.