Skip to content

Comments

feat: add TON blockchain indexing, RPC client, and worker support#39

Open
vietddude wants to merge 10 commits intomainfrom
feat/ton
Open

feat: add TON blockchain indexing, RPC client, and worker support#39
vietddude wants to merge 10 commits intomainfrom
feat/ton

Conversation

@vietddude
Copy link
Collaborator

@vietddude vietddude commented Feb 6, 2026

TON Indexer

Why this design

TON execution is asynchronous/sharded, so this indexer tracks a wallet list and polls each account directly from LiteServers.
Per-wallet cursor (LastLT, LastHash) is stored in KV for restart-safe resume.

Main components

  1. TonPollingWorker
    File: internal/worker/ton/worker.go
  • Runs poll loop with worker-pool concurrency.
  • Wallet source on startup: KV first, DB fallback.
  • Emits matched transactions to event bus.
  1. TonAccountIndexer
    File: internal/indexer/ton/core.go
  • Polls account transactions using cursor.
  • Parses TON native transfer + Jetton transfer patterns.
  • Validates transaction phases and ignores bounced/invalid messages.
  1. CursorStore
    File: internal/indexer/ton/core.go
  • KV key: ton/cursor/<address>.
  • Stores LastLT, LastHash, UpdatedAt.
  1. RedisJettonRegistry
    File: internal/indexer/ton/registry.go
  • Runtime snapshot + config fallback.
  • Redis keys:
    • ton/jettons/<chain>/masters
    • ton/jettons/<chain>/wallet_to_master
  1. TON RPC Client
    File: internal/rpc/ton/client.go
  • Uses connection pool to multiple LiteServers.
  • Caches masterchain info for 1s to reduce duplicate upstream calls.

Startup flow

  1. Manager builds TON worker from chain config.
  2. Worker starts polling loop.
  3. Worker calls ensureWalletsLoaded():
    • ReloadWalletsFromKV()
    • if empty and DB exists: ReloadWalletsFromDB() and persist back to KV.
  4. For each wallet:
    • load cursor from KV
    • poll account with retry
    • save advanced cursor
    • emit matched txs.

Data and keys

  • Wallet list KV: ton/wallets/<chain>
  • Cursor KV: ton/cursor/<address>
  • Jetton metadata Redis:
    • ton/jettons/<chain>/masters
    • ton/jettons/<chain>/wallet_to_master

Runtime APIs

Base URL: http://localhost:${INDEXER_PORT:-8080}

Reload TON wallets

POST /ton/wallets/reload

Query params:

  • source: kv (default) or db
  • chain: optional chain filter (example: ton_mainnet)

Examples:

# default source=kv
curl -s -X POST "http://localhost:${INDEXER_PORT:-8080}/ton/wallets/reload" | jq

# force reload from DB
curl -s -X POST "http://localhost:${INDEXER_PORT:-8080}/ton/wallets/reload?source=db" | jq

# one chain only
curl -s -X POST "http://localhost:${INDEXER_PORT:-8080}/ton/wallets/reload?source=kv&chain=ton_mainnet" | jq

Reload TON jettons

POST /ton/jettons/reload

Query params:

  • chain: optional chain filter

Examples:

curl -s -X POST "http://localhost:${INDEXER_PORT:-8080}/ton/jettons/reload" | jq
curl -s -X POST "http://localhost:${INDEXER_PORT:-8080}/ton/jettons/reload?chain=ton_mainnet" | jq

Config example

chains:
  ton_mainnet:
    network_id: "ton_mainnet"
    internal_code: "TON_MAINNET"
    type: "ton"
    start_block: 0
    poll_interval: "4s"
    nodes:
      - url: "https://ton.org/global-config.json"
    client:
      timeout: "15s"
    throttle:
      rps: 10
      burst: 20
      batch_size: 1
      concurrency: 4
    jettons:
      - master_address: "EQCxE6mUtQJKFnGfaROTgt1lZbDiiX1Gw83iAV82Cn_0opUb"
        symbol: "USDT"
        decimals: 6

@vietddude vietddude requested a review from anhthii February 6, 2026 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant