Skip to main content

Overview

Dune is an onchain analytics platform for exploring historical blockchain data, building SQL queries, and turning the results into dashboards or API-backed application views. While live contract reads tell you what’s happening right now, Dune adds the historical context that helps you explain growth, usage, and network health.

The End-State

By the end of this guide, you will have a functional Onchain Saved Views implementation where:
  1. Users save their preferred analytics view (e.g., Bridge Volume) to a smart contract on an Initia rollup.
  2. The Frontend reads that onchain preference and fetches matching historical data from Dune via a secure proxy.
  3. The Developer maintains full control over API keys and allowed queries without exposing secrets to the browser.

Initia App

The UI built with InterwovenKit for wallet connection and onchain actions.

Onchain Registry

A VM-specific contract or module that stores user-specific analytics preferences.

Secure Backend

A lightweight proxy that manages your DUNE_API_KEY and filters requests.

Dune Analytics

The SQL engine that processes historical data and serves indexed results.

Choosing the Right Tool

Use the Initia Indexer for live app state and Dune for historical analysis, reporting, and dashboards.

Prerequisites

  • Dune API Key: Obtained from your Dune settings.
  • Saved Query IDs: The numeric IDs for the queries you want to display in your app.
  • Node.js Environment: To run the backend proxy and frontend application.
  • Foundry: To deploy the preference registry contract to your Initia rollup.
  • Registry Contract Address: The deployed address for your saved-view contract.

Step 0: Build Your Dune Queries

Before wiring anything together, create the SQL queries your app will display. Run these example templates in the Dune Query Editor and save each one to get a numeric query ID. You will plug those IDs into the backend allowlist and the frontend .env in the next steps.
You can also browse public examples on the query page when signed in. For table-specific ideas, search for Initia in Dune’s data catalog.

1. Daily Transaction Volume

Step 1: Backend Implementation

Your backend acts as a secure proxy, ensuring your DUNE_API_KEY is never exposed to the frontend.
Never expose DUNE_API_KEY in client-side code. Keep the key on the backend and proxy all Dune requests through your server.

1. Setup and Configuration

Create an api/ directory and install the required dependencies:
Create api/.env and store your backend configuration there:
api/.env
Update api/package.json with these scripts:

2. Proxy Server

This server validates requests and only forwards allowed query IDs to Dune.
api/src/server.js

Step 2: Onchain Registry

This example uses a MiniEVM Solidity contract to store user view preferences. The saved-view pattern is VM-agnostic, so you can implement the same registry on MiniMove or MiniWasm by using the equivalent Move or CosmWasm syntax.
Deploy the contract to your rollup with Foundry, then set VITE_REGISTRY_ADDRESS in your frontend .env to the address it prints.

Step 3: Frontend Implementation

The frontend uses InterwovenKit to manage onchain preferences and the proxy for fetching historical data.

1. Dune Helper

Map stable application keys to your numeric Dune query IDs.
frontend/src/lib/dune.ts
frontend/.env
These examples use local-development defaults so you can run the guide end-to-end on your machine. Replace the URLs with your deployed frontend and backend origins when you publish the app.

2. Contract Helper

This helper encodes the MsgCall write for your registry contract and reads saved views back through a JSON-RPC eth_call. Both paths reuse the same ABI.
frontend/src/lib/contract.ts

3. Rendering Results

Use a reusable table component to format Dune’s tabular data.
frontend/src/components/DuneTable.tsx

4. Integration Example

Use useInterwovenKit for the write path and useHexAddress for the read. The component loads the user’s saved views on mount, lets them open any saved view to see the matching Dune result, and refreshes the list after a new preference is saved.
frontend/src/App.tsx

Troubleshooting and Security Tips

  • API Key Security: Never expose DUNE_API_KEY in your frontend. It must remain server-side in your proxy.
  • Source of Truth: Dune is an analytics layer. Keep critical app logic and state validation onchain.
  • Access Control: Always use the DUNE_ALLOWED_QUERY_IDS allowlist on your backend to prevent unauthorized proxying.
  • Data Freshness: Dune’s results endpoint returns the latest saved execution. For real-time state, use the Initia Indexer.

Common Dune Tables for Initia