Skip to content

Getting Started

Installation

go get github.com/smallyunet/etherflow

Basic Usage

Here is a simple example of how to setup an indexer to listen for Transfer events.

package main

import (
    "context"
    "log"
    "time"

    "github.com/smallyunet/etherflow"
    "github.com/smallyunet/etherflow/pkg/config"
    "github.com/smallyunet/etherflow/pkg/core"
    "github.com/smallyunet/etherflow/pkg/spi/eth"
    "github.com/smallyunet/etherflow/pkg/spi/store/sqlite"
)

func main() {
    // 1. Configure
    cfg := &config.Config{
        RPCURL:          "https://mainnet.infura.io/v3/YOUR_KEY",
        DBDriver:        "sqlite",
        DBPath:          "etherflow.db",
        PollingInterval: 2 * time.Second,
        StartBlock:      0,
        SafeWindowSize:  128,
    }

    // 2. Setup dependencies
    client, err := eth.NewClient(cfg.RPCURL)
    if err != nil {
        log.Fatal(err)
    }
    store, err := sqlite.NewStore(cfg.DBPath)
    if err != nil {
        log.Fatal(err)
    }

    // 3. Create Indexer
    indexer := etherflow.New(cfg, client, store)

    // 4. Register Handlers (topic hash routing)
    const TopicTransfer = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" // Transfer(address,address,uint256)
    indexer.On(TopicTransfer, func(ctx *core.EventContext) error {
        log.Printf("Transfer detected in block %d", ctx.Block.Number)
        return nil
    })

    // 5. Run
    if err := indexer.Run(context.Background()); err != nil {
        log.Fatal(err)
    }
}

Configuration

Most settings are configured via config.Config, environment variables, or a YAML config file.