Skip to content

Query Smart Contracts with SQL

Amp automatically transforms your smart contract events into SQL-queryable datasets the moment those events are emitted.

This quickstart template walks you from install to your first on-chain SQL query in minutes. Perfect for prototypes, and production applications that need fast access to on-chain data.

You can:

  • Query blockchain data using SQL (both from the CLI and in your app)
  • Create custom datasets by combining and transforming on-chain data
  • Build a React app that displays live blockchain data

Benefits:

  • There’s no backend to deploy, no indexing code to maintain, and no configuration overhead.
  • Deploy your contract, trigger events, and query the resulting tables immediately using standard SQL.
  • Build real-time dashboards, analytics pipelines, and data-driven applications with the tooling and workflows you already use.

To understand the specifics, see Smart Contract Reference

Make sure Docker is running. Then install:

  • Node.js (v22+)
  • Pnpm (v10+)

Verify with

Terminal window
node --version
pnpm --version

Older versions may cause issues.

For smart constract development

Terminal window
curl -L https://foundry.paradigm.xyz | bash && foundryup
Terminal window
cargo install just
Terminal window
curl --proto '=https' --tlsv1.2 -sSf https://ampup.sh/install | sh
Terminal window
# Clone with submodules
git clone --recursive <repository-url>
cd amp-demo
Terminal window
just install
Terminal window
just up

4. Start Development serves (frontend + Amp)

Section titled “4. Start Development serves (frontend + Amp)”
Terminal window
just dev

In a new terminal, query your dataset:

Terminal window
# See the events your contract emitted
pnpm amp query 'SELECT * FROM "_/counter@dev".incremented LIMIT 5'

Amp automatically created a incremented SQL table from your smart contract’s Incremented event. No indexing code required.

5. Create Your First Derived Table (Optional)

Section titled “5. Create Your First Derived Table (Optional)”

To understand specifics to datasets, see (Ampup Basics)[]

Raw tables (example: anvil.blocks) contain the chain data and are populated for you; with them you can create and deploy derived datasets that pre-compute transformations for faster queries (like a materialized view).

Edit amp.config.ts to add a custom table:

import { defineDataset, eventTables } from "@edgeandnode/amp";
// @ts-ignore
import { 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", // Access to blocks, transactions, logs
},
tables: {
...baseTables, // Your contract's event tables
// Add a custom derived table
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 your new active_blocks table.

Terminal window
pnpm amp query 'SELECT * FROM "_/counter@dev".active_blocks LIMIT 10'
Terminal window
pnpm amp query 'SELECT * FROM anvil.logs WHERE address = 0xYOUR_CONTRACT_ADDRESS LIMIT 10'

The frontend (app/src) shows how to query Amp datasets from TypeScript. See app/src/components/IncrementTable.tsx for a complete example.

Amp easily transitions from local development to developing on datasets located in a hosted environment.

  • Follow the hosted environment to transition Amp from local datasets to published datasets hosted by Edge & Node.

Prototype with Amp Studio:

Terminal window
just studio
Terminal window
just logs
Terminal window
pnpm amp query 'SELECT * FROM "_/counter@dev".decremented LIMIT 10'

You’re now ready to build dashboards, analytics tools, agent workflows, or data-driven apps—powered by SQL on on-chain events.

Local

  • Foundry Anvil (local development)

On hosted playground

  • Ethereum mainnet
  • Arbitrum mainnet
  • Base mainnet
  • Base Sepolia