Query Blockchain Datasets
Overview
Section titled “Overview”This guide walks you through how to query the edgeandnode/ethereum_mainnet@0.0.1 dataset using:
- Amp Playground (interactive)
- The Amp gateway (programmatic, including dapps)
You’ll learn how to:
- Explore blocks, transactions, token transfers, and contract events
- Filter by addresses, tokens, and time ranges
- Join onchain tables for deeper analytics
- Authenticate via Privy and CLI access tokens
- Run optimized SQL queries from your dapps against live data
Prerequisites
Section titled “Prerequisites”- Access to Amp Playground
- Signed in via wallet
- Amp CLI installed (for generating access tokens)
Dataset Schema
Section titled “Dataset Schema”Core Tables
Section titled “Core Tables”| Table | Description | Key Columns |
|---|---|---|
| blocks | Block headers | number, hash, timestamp, miner, transaction_count, gas_limit, gas_used |
| transactions | Transaction details | hash, from_address, to_address, value, gas (limit), gas_price, block_number, block_timestamp |
| receipts | Transaction receipts | transaction_hash, receipt_gas_used (actual), status, contract_address |
| token_transfers | ERC-20 token transfers | token_address, from_address, to_address, value, block_number, transaction_hash |
| decoded_events | Decoded contract events | contract_address, event_name, block_number, transaction_hash, log_index |
Important Notes
- All addresses (wallet, contract, token) are stored in lowercase
- Transaction hashes and other hex values are also lowercase
- Timestamps are in UTC timezone
- Gas prices are in wei (divide by 1e18 for ETH)
- Tables are partitioned by
block_numberandblock_timestampfor optimal performance
Get Started
Section titled “Get Started”To get started in the playground:
- Open Datasets → edgeandnode → ethereum_mainnet → 0.0.1. When you click the dataset card, the Playground editor opens automatically.
Authentication
Section titled “Authentication”Amp supports two main ways to query datasets through the gateway:
- Privy Access Token (handled automatically in UI/CLI)
- CLI Access Token (
amp auth token)
1. Privy Access Token
Section titled “1. Privy Access Token”This is the default path for most users.
- Automatically handled in the Amp Playground UI
- Automatically handled in the Amp CLI after signing in
To establish a CLI session:
amp auth loginNo additional setup needed for interactive queries.
2. Dapp Access Token
Section titled “2. Dapp Access Token”For backend services and dapps, generate an access token:
amp auth tokenYou can also set a custom duration using the --duration flag, which accepts values like “30 days”.
This command:
- Issues an access token bound to your Amp account / workspace
- Can be used in any dapp or backend that needs to query datasets through the gateway
- Store this token securely (environment variable, secret manager, etc.).
Gateway Request Pattern
Section titled “Gateway Request Pattern”Once you have a token (Privy-backed CLI session or a token from amp auth token), you can call the gateway:
Copy codecurl -X POST "https://gateway.amp.staging.edgeandnode.com/api" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "dataset": "edgeandnode/ethereum_mainnet@0.0.1", "query": "SELECT number, hash FROM blocks ORDER BY number DESC LIMIT 5" }'Example Response
Section titled “Example Response”{ "data": [ { "number": 18500000, "hash": "0xabc123..." }, { "number": 18499999, "hash": "0xdef456..." } ], "metadata": { "row_count": 5, "execution_time_ms": 145 }}Use the same pattern in your dapps (Node.js, Python, etc.).
Query Interface (Playground)
Section titled “Query Interface (Playground)”Basic Query Structure
Section titled “Basic Query Structure”In the Amp Playground:
-- Basic transaction querySELECT hash AS transaction_hash, from_address, to_address, value / 1e18 AS value_eth, -- Convert wei to ETH gas AS gas_limit, gas_price / 1e9 AS gas_price_gwei -- Convert wei to gweiFROM "edgeandnode/ethereum_mainnet@0.0.1".transactionsWHERE block_number > 18000000LIMIT 100;Query Builder Features
Section titled “Query Builder Features”- Auto-completion – start typing a table/column and suggestions appear
- Syntax highlighting – SQL keywords, functions, and identifiers are colorized
- Query validation – obvious syntax errors are surfaced before execution
- Execution metrics – after running a query you’ll see duration and row count
Query Your First Dataset
Section titled “Query Your First Dataset”1. Block Analysis
Section titled “1. Block Analysis”SELECT number, hash, timestamp, miner, transaction_count, gas_limit, gas_used, (gas_used * 100.0 / gas_limit) AS gas_utilization_pctFROM blocksWHERE timestamp > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 HOUR)ORDER BY number DESCLIMIT 10;2. Address Activity
Section titled “2. Address Activity”Addresses are stored lowercase; always normalize inputs:
SELECT block_number, block_timestamp, hash AS transaction_hash, CASE WHEN from_address = LOWER('0xYourAddress') THEN 'sent' WHEN to_address = LOWER('0xYourAddress') THEN 'received' END AS direction, value, gas, gas_priceFROM transactionsWHERE from_address = LOWER('0xYourAddress') OR to_address = LOWER('0xYourAddress')ORDER BY block_timestamp DESCLIMIT 100;Replace 0xYourAddress with the address you care about.
3. Token Transfers (ERC-20)
Section titled “3. Token Transfers (ERC-20)”SELECT block_timestamp, block_number, transaction_hash, token_address, from_address, to_address, value AS raw_value -- Note: decimal places depend on tokenFROM token_transfersWHERE token_address = LOWER('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48') -- USDC AND block_timestamp > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 24 HOUR)ORDER BY block_timestamp DESCLIMIT 100;4. Gas & Fee Analysis
Section titled “4. Gas & Fee Analysis”SELECT TIMESTAMP_TRUNC(t.block_timestamp, HOUR) AS hour, AVG(r.receipt_gas_used) AS avg_gas_used, AVG(t.gas_price / 1e9) AS avg_gas_price_gwei, SUM(r.receipt_gas_used * t.gas_price) / 1e18 AS total_fees_eth, COUNT(*) AS tx_countFROM transactions tJOIN receipts r ON t.hash = r.transaction_hashWHERE t.block_timestamp > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 24 HOUR)GROUP BY hourORDER BY hour DESC;Note: receipt_gas_used is the actual gas consumed, while gas in transactions table is the gas limit.
5. Smart Contract Events
Section titled “5. Smart Contract Events”SELECT block_timestamp, block_number, transaction_hash, log_index, event_name, contract_addressFROM decoded_eventsWHERE contract_address = LOWER('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48') AND event_name = 'Transfer' AND block_timestamp > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 HOUR)ORDER BY block_timestamp DESCLIMIT 100;Note: decoded_events contains standard events (ERC-20 Transfer, Approval, etc.) that have been decoded. Custom events may require additional processing.
Advanced Querying
Section titled “Advanced Querying”Join Operations
Section titled “Join Operations”SELECT t.hash AS transaction_hash, t.from_address, t.to_address, t.value, t.block_number, b.timestamp AS block_timestamp, b.miner, b.gas_used AS block_gas_usedFROM transactions tJOIN blocks b ON t.block_number = b.numberWHERE b.timestamp > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 HOUR)ORDER BY b.timestamp DESCLIMIT 200;Window Function
Section titled “Window Function”SELECT block_timestamp, block_number, hash AS transaction_hash, value, SUM(value) OVER ( ORDER BY block_number ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS running_inflowFROM transactionsWHERE to_address = LOWER('0xYourAddress')ORDER BY block_numberLIMIT 200;Aggregation Queries
Section titled “Aggregation Queries”SELECT DATE(block_timestamp) AS date, COUNT(*) AS total_transactions, SUM(receipt_gas_used * gas_price) / 1e18 AS total_fees_eth, AVG(value) AS avg_valueFROM transactionsWHERE block_timestamp > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)GROUP BY dateORDER BY date DESC;Query Optimization
Section titled “Query Optimization”1. Use Time Filters
Section titled “1. Use Time Filters”WHERE block_timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)2. Avoid SELECT *
Section titled “2. Avoid SELECT *”SELECT block_timestamp, number, miner, gas_used, transaction_countFROM blocksWHERE block_timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 DAY)ORDER BY block_timestamp DESC;3. Always Use LIMIT
Section titled “3. Always Use LIMIT”SELECT hash, from_address, to_address, valueFROM transactionsWHERE block_number > 18000000LIMIT 1000;4. Normalize Addresses
Section titled “4. Normalize Addresses”WHERE from_address = LOWER('0xMixedCaseInput') OR to_address = LOWER('0xMixedCaseInput')Error Handling
Section titled “Error Handling”| Error type | Cause | Fix |
|---|---|---|
| TIMEOUT | Wide scan, no filters | Add block_timestamp / block_number filters, use LIMIT |
| SYNTAX_ERROR | Invalid SQL | Check commas, function names, column names |
| NO_DATA | Query returns zero rows | Check address case, contract/token address, table name |
| PERMISSION | Token or login issue | Re-auth with Privy / regenerate amp auth token |
Quick Reference
Section titled “Quick Reference”Useful Functions
Section titled “Useful Functions”CURRENT_TIMESTAMP ()TIMESTAMP_SUB (ts, INTERVAL n HOUR)TIMESTAMP_TRUNC (ts, HOUR|DAY)DATE(block_timestamp)`COUNT(*), SUM(), AVG()LOWER(address)