app.ts


Overview

`app.ts` implements the Litecoin-specific API server leveraging Express.js and WebSocket technologies. It provides REST endpoints and WebSocket real-time subscriptions to serve blockchain data, integrating deeply with a Blockbook indexer via WebSocket to receive and process live blockchain events.

This server exposes health and metrics endpoints, interactive Swagger API documentation, and manages live client connections that subscribe to blockchain events. The file configures middleware, registers REST routes, and orchestrates event processing through handlers and a central subscription registry.

Key functionalities include:

This file acts as the entry point to the Litecoin API server, tying together middleware, controllers, event handling, and client interaction layers.


Detailed Explanation

Imports and Constants

Constants:


Express App Setup

const app = express()
app.use(...middleware.common(prometheus))

REST Endpoints


Route Registration

RegisterRoutes(app)

Blockchain Event Handlers


Subscription Registry

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

Blockbook WebSocket Client

const blockbook = new WebsocketClient(
  INDEXER_WS_URL,
  {
    apiKey: INDEXER_API_KEY,
    blockHandler: registry.onBlock.bind(registry),
    transactionHandler: registry.onTransaction.bind(registry),
  },
  { resetInterval: 5 * 60 * 1000 }
)

HTTP and WebSocket Server Setup

const server = app.listen(PORT, () => logger.info('Server started'))
const wsServer = new Server({ server })

WebSocket Client Connection Handling

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

Usage Examples

Starting the Server

Set environment variables and run:

export INDEXER_WS_URL=wss://litecoin-blockbook.example.com/socket
export INDEXER_API_KEY=your-api-key
export PORT=3000
node dist/app.js

Query Health

GET /health

Response:

{
  "status": "up",
  "network": "litecoin",
  "connections": 2
}

Subscribe to Address Updates (WebSocket Client Example)

Client connects to WS server and sends a message like:

{
  "type": "subscribe",
  "addresses": ["LcHKV1x2Z4Tz7E9Gz7Wb5AqT2Na1hT6k1S"]
}

Subsequent new blocks or transactions affecting these addresses will be pushed to the client.


Important Implementation Details


Interaction with Other System Components


Visual Diagram

classDiagram
    class App {
        +app: Express
        +wsServer: WebSocket.Server
        +start()
    }
    class Registry {
        +addressFormatter()
        +onBlock()
        +onTransaction()
        +manageSubscriptions()
    }
    class WebsocketClient {
        +connect()
        +blockHandler()
        +transactionHandler()
        +resetInterval
    }
    class ConnectionHandler {
        +start(connection, registry, blockbook, prometheus, logger)
        +manageClientConnection()
    }
    class Service {
        +handleBlock(hash)
        +handleTransaction(tx)
    }

    App "1" --o "1" Registry : uses
    App "1" --o "1" WebsocketClient : connects to
    App "1" --o "1" ConnectionHandler : handles WS connections
    Registry "1" --> "1" Service : processes data via
    WebsocketClient "1" --> "1" Registry : forwards events to
    ConnectionHandler "1" --> "1" Registry : subscribes clients

Summary

`app.ts` is the Litecoin blockchain API server entry point, implementing a robust, extensible server that:

It acts as the critical middleware layer between the Litecoin blockchain indexer and client applications, enabling real-time and on-demand blockchain data delivery.


End of Documentation for app.ts