app.ts


Overview

[app.ts](/projects/291/68852) is the main entry point for the Ethereum API server implemented using Express.js. It establishes a RESTful API and WebSocket server that provide access to Ethereum blockchain data and services. This server acts as a bridge between client applications (such as wallets and dApps) and blockchain indexers (like Blockbook or other indexers), exposing blockchain data through HTTP endpoints and real-time event subscriptions over WebSocket.

Key functionalities include:

This file integrates various components such as controllers, middleware, WebSocket clients, and service logic to deliver a cohesive API server tailored for the Ethereum blockchain.


Detailed Explanation

Constants and Environment Variables

Constant

Purpose

`PORT`

The port number on which the Express server listens (defaults to 3000 if not set).

`INDEXER_WS_URL`

WebSocket URL for connecting to the blockchain indexer (required).

INDEXER_API_KEY

Optional API key for authenticating with the indexer’s WebSocket service.

`IS_LIQUIFY`

Boolean flag indicating if the indexer URL contains "liquify" (affects internal tx fetching).

`IS_NOWNODES`

Boolean flag indicating if the indexer URL contains "nownodes" (affects API key usage).

Logger


Express Application Setup


Block and Transaction Handlers

These asynchronous handlers process blockchain events received from the indexer WebSocket client.

blockHandler

transactionHandler


Registry


WebSocket Client and Server

WebSocket Client (blockbook)

WebSocket Server (wsServer)


Server Startup


Usage Example

import { app, wsServer, logger } from './app'

// Server is already listening on configured PORT.
// Clients can:
// - Access HTTP API (e.g., GET /health, /metrics, /docs).
// - Connect to WebSocket for real-time blockchain events.

// Example: REST API call to check health
fetch('http://localhost:3000/health')
  .then(res => res.json())
  .then(console.log)

// Example: WebSocket client subscribing to new blocks and transactions
const ws = new WebSocket('ws://localhost:3000')
ws.onmessage = (event) => {
  console.log('Received event:', event.data)
}

Important Implementation Details


Interaction with Other System Components


Diagram

classDiagram
    class App {
        +express app
        +WebSocket server wsServer
        +startServer()
    }

    class BlockHandler {
        +handleBlock(block: NewBlock): Promise<{txs: Array<{addresses: string[], tx: evm.Tx}>}>
    }

    class TransactionHandler {
        +handleTransaction(tx: BlockbookTx): Promise<{addresses: string[], tx: evm.Tx}>
    }

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

    class WebsocketClient {
        +connect()
        +blockHandler: Array<Function>
        +transactionHandler: Function
    }

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

    App --> Registry : uses
    App --> WebsocketClient : creates
    App --> ConnectionHandler : uses for WebSocket connections
    Registry --> BlockHandler : processes blocks
    Registry --> TransactionHandler : processes transactions
    WebsocketClient --> Registry : calls onBlock/onTransaction
    ConnectionHandler --> Registry : manages subscriptions

Summary

The [app.ts](/projects/291/68852) file is a well-structured API server tailored for Ethereum blockchain data, bridging blockchain indexers and client applications. It combines REST endpoints, WebSocket real-time subscriptions, enriched transaction processing, monitoring, and documentation in one cohesive server implementation. Its modular design leverages common libraries and controllers, ensuring extensibility and maintainability within the broader ShapeShift API ecosystem.