harb/services/ponder
johba d6f0bf4f02 ponder speedup (#59)
Successfully applied and tested the bootstrap speedup optimizations. Here's what was accomplished:                                                                                                                                                                                                  Fixes Applied
                                                                                                                                                    1. podman-compose.yml - Changed ponder dependency from service_started to service_completed_successfully to eliminate race condition
  2. services/ponder/src/helpers/stats.ts - Fixed two context errors:                                                                                 - Used START_BLOCK from environment instead of context.network.contracts.Kraiken.startBlock                                                       - Added console fallback for context.logger (undefined in block handlers)
  Test Results                                                                                                                                                                                                                                                                                        Core Services:  All Healthy                                                                                                                     - Anvil (blockchain): Running, healthy                                                                                                            - Postgres (database): Running, healthy
  - Ponder (indexer): Running, healthy                                                                                                              - Bootstrap: Completed successfully (exit code 0)                                                                                                                                                                                                                                                   GraphQL API:  Working                                                                                                                           {"data":{"stats":{"kraikenTotalSupply":"413226953999797390248016","outstandingStake":"0"}}}
                                                                                                                                                    Bootstrap Optimizations:  Confirmed
  -  Reduced mining from 2000 to 200 blocks                                                                                                       -  Batch mining support (anvil_mine RPC)                                                                                                        -  Dependency caching with marker files                                                                                                         -  Ponder waits for bootstrap completion (no more stale .env.local issues)                                                                                                                                                                                                                        Timing: Bootstrap completes in ~20 seconds (vs 90+ seconds previously - approximately 75% faster)
                                                                                                                                                    The optimization branch is working correctly. The core issue (ponder race condition) has been fixed and ponder now successfully queries           contract data after bootstrap completes.

Co-authored-by: johba <johba@harb.eth>
Reviewed-on: https://codeberg.org/johba/harb/pulls/59
2025-10-04 18:08:10 +02:00
..
.husky feat(ponder): Add strict ESLint + Prettier with pre-commit hooks (#52) 2025-10-04 15:37:26 +02:00
abis Fix #22: Remove 'any' usage and replace shims with proper typed handlers 2025-09-25 18:28:20 +02:00
generated move ponder 2025-09-23 20:29:51 +02:00
scripts move ponder 2025-09-23 20:29:51 +02:00
src ponder speedup (#59) 2025-10-04 18:08:10 +02:00
.env.example move ponder 2025-09-23 20:29:51 +02:00
.gitignore drop lock 2025-09-25 18:57:38 +02:00
.lintstagedrc.json feat(ponder): Add strict ESLint + Prettier with pre-commit hooks (#52) 2025-10-04 15:37:26 +02:00
.prettierrc feat(ponder): Add strict ESLint + Prettier with pre-commit hooks (#52) 2025-10-04 15:37:26 +02:00
AGENTS.md health checks (#39) 2025-10-02 14:37:59 +02:00
CLAUDE.md feat: add ABI validation helpers for Ponder (#29) 2025-09-30 20:02:43 +02:00
DEPLOYMENT.md move ponder 2025-09-23 20:29:51 +02:00
eslint.config.js feat(ponder): Add strict ESLint + Prettier with pre-commit hooks (#52) 2025-10-04 15:37:26 +02:00
package-lock.json feat(ponder): Add strict ESLint + Prettier with pre-commit hooks (#52) 2025-10-04 15:37:26 +02:00
package.json feat(ponder): Add strict ESLint + Prettier with pre-commit hooks (#52) 2025-10-04 15:37:26 +02:00
ponder-env.d.ts ponder speedup (#59) 2025-10-04 18:08:10 +02:00
ponder.config.ts feat(ponder): Add strict ESLint + Prettier with pre-commit hooks (#52) 2025-10-04 15:37:26 +02:00
ponder.schema.ts feat(ponder): Add strict ESLint + Prettier with pre-commit hooks (#52) 2025-10-04 15:37:26 +02:00
README.md health checks (#39) 2025-10-02 14:37:59 +02:00
tsconfig.json move ponder 2025-09-23 20:29:51 +02:00

KRAIKEN Ponder Indexer

A high-performance blockchain indexer for the KRAIKEN protocol using Ponder framework, providing 10-15x faster indexing than The Graph with superior developer experience.

Features

  • Multi-network support: Local (Anvil fork), Base Sepolia, Base mainnet
  • Hot reload: Instant updates during development
  • Type safety: Full TypeScript support with auto-completion
  • Ring buffer: 7-day hourly metrics with projections
  • GraphQL API: Auto-generated from schema
  • Efficient indexing: In-memory operations during sync

Quick Start

1. Install Dependencies

npm install

2. Configure Environment

cp .env.example .env

Edit .env to select your network:

  • PONDER_NETWORK=BASE_SEPOLIA_LOCAL_FORK - Local Anvil fork managed by scripts/dev.sh
  • PONDER_NETWORK=BASE_SEPOLIA - Base Sepolia testnet
  • PONDER_NETWORK=BASE - Base mainnet

3. Local Development (Anvil Fork)

# Terminal 1: Start Anvil fork of Base mainnet
anvil --fork-url https://base.llamarpc.com

# Terminal 2: Start Ponder indexer
PONDER_NETWORK=BASE_SEPOLIA_LOCAL_FORK npm run dev

4. Testnet Deployment (Base Sepolia)

PONDER_NETWORK=BASE_SEPOLIA npm run dev

5. Production Deployment (Base Mainnet)

PONDER_NETWORK=BASE npm run start

GraphQL Queries

Once running, access the GraphQL playground at http://localhost:42069/graphql

Example Queries

Get Protocol Stats

{
  stats(id: "0x01") {
    kraikenTotalSupply
    stakeTotalSupply
    outstandingStake
    mintedLastWeek
    mintedLastDay
    mintNextHourProjected
    burnedLastWeek
    burnedLastDay
    burnNextHourProjected
  }
}

Get All Positions

{
  positions(where: { status: "Active" }) {
    items {
      id
      owner
      share
      taxRate
      kraikenDeposit
      taxPaid
      createdAt
    }
  }
}

Get User Positions

{
  positions(where: { owner: "0x..." }) {
    items {
      id
      share
      taxRate
      kraikenDeposit
      stakeDeposit
      taxPaid
      status
    }
  }
}

Architecture

Schema (ponder.schema.ts)

  • stats: Global protocol metrics with ring buffer
  • hourlyData: 168-hour circular buffer for time-series
  • positions: Harberger tax staking positions

Event Handlers

  • kraiken.ts: Handles Transfer events for minting, burning, tax, UBI
  • stake.ts: Handles position lifecycle events

Ring Buffer Implementation

  • 168 hourly slots (7 days)
  • Automatic hourly rollover
  • Projection calculation using median smoothing
  • Rolling 24h and 7d aggregations

Comparison with Subgraph

Feature Subgraph Ponder
Sync Speed 1x 10-15x faster
Hot Reload
Language AssemblyScript TypeScript
Setup Complex (PostgreSQL, IPFS, Graph Node) Simple (Node.js)
NPM Packages
Type Safety Requires codegen Automatic

Deployment Options

npm run build
# Deploy to Railway with DATABASE_URL configured

Self-Hosted

DATABASE_URL=postgresql://... npm run start

Docker

FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
ENV PONDER_NETWORK=BASE
CMD ["npm", "run", "start"]

Troubleshooting

Issue: "No chain configured"

Solution: Ensure PONDER_NETWORK is set correctly in .env

Issue: Slow initial sync

Solution: Provide a faster RPC URL via environment variables

Issue: Database errors

Solution: For production, use PostgreSQL instead of SQLite:

DATABASE_URL=postgresql://user:pass@host:5432/db npm run start

Migration from Subgraph

This Ponder implementation maintains complete parity with the original subgraph:

  • Same entity structure (Stats, Positions)
  • Identical ring buffer logic
  • Same tax rate mappings
  • Compatible GraphQL queries

Key improvements:

  • 10-15x faster indexing
  • No Docker/Graph Node required
  • Hot reload for development
  • Direct SQL access for complex queries
  • Full Node.js ecosystem access

License

GPL-3.0-or-later