app.ts


Overview

`app.ts` serves as the main entry point for the Avalanche blockchain API server within the ShapeShift "unchained" project ecosystem. This file sets up an Express-based HTTP server combined with WebSocket support to provide real-time blockchain data indexing and querying capabilities. It integrates core components such as block and transaction handlers, metrics collection with Prometheus, API documentation via Swagger UI, and WebSocket client connections to an external blockchain data provider (Blockbook).

The file orchestrates how new blocks and transactions are processed, parsed, and exposed through REST and WebSocket interfaces, enabling clients to interact with the Avalanche blockchain data efficiently and in near real-time.


Key Functionalities


Classes, Functions, and Methods

1. Express app instance (app)

app.get('/health', async (_, res) => res.json({ status: 'up', asset: 'avalanche', connections: wsServer.clients.size }))

2. blockHandler

const blockHandler: BlockHandler<NewBlock, Array<{ addresses: Array<string>; tx: evm.Tx }>> = async (block) => { ... }

3. transactionHandler

const transactionHandler: TransactionHandler<BlockbookTx, evm.Tx> = async (blockbookTx) => { ... }

4. registry

const registry = new Registry({ addressFormatter: evm.formatAddress, blockHandler, transactionHandler })

5. blockbook

const blockbook = new WebsocketClient(INDEXER_WS_URL, {
  apiKey: INDEXER_API_KEY,
  blockHandler: [registry.onBlock.bind(registry), gasOracle.onBlock.bind(gasOracle)],
  transactionHandler: registry.onTransaction.bind(registry),
})

6. WebSocket Server (wsServer)

const wsServer = new Server({ server })
wsServer.on('connection', (connection) => {
  ConnectionHandler.start(connection, registry, blockbook, prometheus, logger)
})

7. Prometheus Metrics (prometheus)


8. Logger (logger)


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Usage Example

Starting the server (assuming environment variables set):

INDEXER_WS_URL=wss://blockbook.indexer.example.com/ws \
INDEXER_API_KEY=apikey123 \
PORT=3000 \
LOG_LEVEL=info \
node dist/app.js

Clients can:


Mermaid Diagram: Structure of app.ts

classDiagram
    class App {
        +express app
        +start()
    }

    class Registry {
        +onBlock()
        +onTransaction()
    }

    class WebsocketClient {
        +constructor(url, options)
        +connect()
    }

    class BlockHandler {
        <<function>>
    }

    class TransactionHandler {
        <<function>>
    }

    class Prometheus {
        +register
        +metrics()
    }

    class Logger {
        +info()
        +error()
    }

    class ConnectionHandler {
        +start(connection, registry, blockbook, prometheus, logger)
    }

    App --> "1" Registry : uses
    App --> "1" WebsocketClient : connects to
    App --> "1" Prometheus : monitors
    App --> "1" Logger : logs
    App --> "1" ConnectionHandler : manages WS connections
    Registry --> "1" BlockHandler : processes blocks
    Registry --> "1" TransactionHandler : processes transactions
    WebsocketClient --> "many" BlockHandler : invokes on block
    WebsocketClient --> "1" TransactionHandler : invokes on transaction

Summary

`app.ts` is a comprehensive server bootstrap file that integrates Express and WebSocket servers with blockchain data handlers, metrics, logging, and API documentation. It acts as the core runtime environment for the Avalanche coinstack API in the ShapeShift "unchained" ecosystem, enabling clients to query and subscribe to blockchain data efficiently and reliably. The file demonstrates a clean separation of concerns, modular handler usage, and real-time data streaming integration.