Smart Contracts with SQL
Overview
Section titled “Overview”Amp turns contract events into SQL tables so you can query blockchain data instantly. This reference page documents the core concepts, configuration patterns, supported environments, and common workflows.
Example Use Cases
Section titled “Example Use Cases”- Cross-chain portfolio dashboard that aggregates wallet positions and liquidity using Amp token datasets
- Risk analytics or MEV monitor that visualizes transaction patterns or protocol surface exposure
- NFT trait liquidity explorer that ranks collections by floor depth and trading velocity using Amp NFT datasets
How Amp Works
Section titled “How Amp Works”Event to Table Pipeline
- Your contracts emit standard events.
- Amp automatically converts those events into SQL tables.
- No indexing logic, no schema management, no backend services required.
Query Anywhere
Section titled “Query Anywhere”Use SQL from:
- TypeScript / JavaScript
- Rust (via CLI)
- Python (via CLI)
- Amp CLI
- Amp Studio (browser)
Development Environments
Section titled “Development Environments”Amp integrates seamlessly with:
- Foundry (Anvil)
- Hardhat
- Any local Ethereum dev chain
Datasets
Section titled “Datasets”A dataset is a collection of SQL tables built from onchain data. Think of them as your data warehouse for onchain events.
Dataset Naming Convention
Section titled “Dataset Naming Convention”Format:
Section titled “Format:”namespace/name@versionExamples:
-
"_/counter@dev": your local dataset -
"_/anvil@0.0.1": chain-level data (blocks, txs, logs)
Conventions:
@devfor local development@latestor@1.0.0for published datasets_/is the personal local namespace
The dataset configuration is in amp.config.ts. It uses eventTables(abi) to automatically generate SQL tables from your contract events.
Raw Tables
Section titled “Raw Tables”Raw tables contain chain data and are populated automatically by Amp.
Example: anvil.blocks exposes block-level fields such as block number, hash, timestamp, and gas usage.
Raw tables also include the event tables Amp generates from your contract ABI using eventTables(abi).
These tables form the base layer you can use to build custom, derived datasets.
Derived Tables
Section titled “Derived Tables”Derived tables allow you to pre-compute filtered or transformed data for faster queries, similar to a materialized view.
Derived tables can pull from any dataset declared in dependencies, such as:
anvil.blocksanvil.logs
SQL Limitations (Streaming Model)
Section titled “SQL Limitations (Streaming Model)”Inside a derived table definition:
- No
GROUPBY - No
ORDERBY - No
LIMIT
For full details, see docs/streaming.md.
Example: Adding a Derived Table
Section titled “Example: Adding a Derived Table”import { defineDataset, eventTables } from "@edgeandnode/amp";// @ts-ignoreimport { abi } from "./app/src/lib/abi.ts";
export default defineDataset(() => { const baseTables = eventTables(abi);
return { namespace: "eth_global", name: "counter", network: "anvil", description: "Counter dataset with event tables and custom queries", dependencies: { anvil: "_/anvil@0.0.1", }, tables: { ...baseTables,
active_blocks: { sql: ` SELECT block_num, hash AS block_hash, timestamp, gas_used FROM anvil.blocks WHERE gas_used > 0 `, }, }, };});Amp automatically detects changes to amp.config.ts and generates the new active_blocks table.
Querying Tables
Section titled “Querying Tables”Query the derived table:
pnpm amp query 'SELECT * FROM "_/counter@dev".active_blocks LIMIT 10'Filter logs for a specific contract:
pnpm amp query 'SELECT * FROM anvil.logs WHERE address = 0xYOUR_CONTRACT_ADDRESS LIMIT 10'Project Structure
Section titled “Project Structure”amp-demo/├── amp.config.ts # Dataset configuration├── contracts/src/Counter.sol # Contracts with events├── app/ # React frontend│ ├── src/components/ # Components that query Amp datasets│ └── src/lib/ # Amp client utilities├── infra/│ ├── amp/ # Providers, data, generated artifacts│ └── docker-compose.yaml # Local services└── justfile # Task runner commandsClient Libraries
Section titled “Client Libraries”Amp supports multiple languages:
- TypeScript/JavaScript
- Rust (via CLI)
- Python (via CLI)
All clients use the same SQL query engine.
Supported Chains
Section titled “Supported Chains”- Foundry Anvil
- Hardhat
Hosted (https://playground.amp.thegraph.com/)
Section titled “Hosted (https://playground.amp.thegraph.com/)”- Ethereum mainnet
- Arbitrum mainnet
- Base mainnet
- Base Sepolia
Roadmap includes additional major chains.
Hosted Environment Development
Section titled “Hosted Environment Development”Amp transitions smoothly from local datasets to published datasets hosted by Edge & Node.
See:
- docs/hosted-env.md
- docs/streaming.md
- docs/troubleshooting.md
Common Questions
Section titled “Common Questions”-
Do I need indexing code? No. Amp auto-generates SQL tables from contract events.
-
Can I use Hardhat? Yes. Amp works with any EVM local dev chain.
-
Raw vs Derived tables? Raw tables mirror events. Derived tables transform your data using SQL for faster queries.
-
Why
@devin queries? Local datasets use the@devtag. Published datasets use semantic versions.