app.ts


Overview

`app.ts` implements the core API server for the **Optimism** blockchain, leveraging Express.js for REST endpoints and WebSocket connections for real-time event streaming. It acts as a bridge between client applications and the Optimism blockchain indexer (via Blockbook WebSocket API), providing:

This file orchestrates the server lifecycle, middleware setup, API routing, WebSocket event handling, and interaction with the blockchain data services specifically tailored to Optimism's EVM-compatible chain.


Detailed Explanation

Constants and Environment Variables

Name

Description

`PORT`

Server listening port (default 3000 if not set)

`INDEXER_WS_URL`

WebSocket URL of the blockchain indexer service (must be set)

`INDEXER_API_KEY`

Optional API key for authenticating with the indexer

`IS_LIQUIFY`

Boolean flag indicating if the indexer URL contains "liquify"

`IS_NOWNODES`

Boolean flag indicating if the indexer URL contains "nownodes"

Logger

`logger` is an instance of `Logger` configured with namespaces relevant to the Optimism API stack. It manages structured logging throughout the server.

Prometheus Metrics

`prometheus` collects metrics tagged with the chain identifier `"optimism"`. It exposes metrics via `/metrics` endpoint for monitoring.


Express Application Setup (app)


Blockchain Event Handlers

These handlers process incoming blockchain data from the WebSocket client connected to the Blockbook indexer.

addressFormatter

const addressFormatter: AddressFormatter = (address) => evm.formatAddress(address)

blockHandler

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

transactionHandler

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

Registry Initialization

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

Blockbook WebSocket Client Setup

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

HTTP Server and WebSocket Server

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

WebSocket Connection Handling

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

Important Implementation Details


Interaction With Other Components


Usage Example

Starting the server:

INDEXER_WS_URL=wss://example-indexer.com/ws INDEXER_API_KEY=secretkey PORT=3000 node dist/app.js

Visual Diagram

classDiagram
    class App {
        +express app
        +Prometheus prometheus
        +Logger logger
        +Registry registry
        +WebsocketClient blockbook
        +Server httpServer
        +Server wsServer
        +addressFormatter()
        +blockHandler(block)
        +transactionHandler(tx)
    }

    class Registry {
        +onBlock(block)
        +onTransaction(tx)
        +addressFormatter
        +blockHandler
        +transactionHandler
    }

    class WebsocketClient {
        +connect()
        +blockHandler[]
        +transactionHandler
    }

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

    App --> Registry : uses
    App --> WebsocketClient : creates
    App --> ConnectionHandler : manages WS connections
    Registry ..> WebsocketClient : subscribes to events
    WebsocketClient --> Registry : calls onBlock/onTransaction

Summary

`app.ts` is the entry point of the Optimism blockchain API server, implementing a REST API and WebSocket server that:

This design ensures real-time, scalable access to Optimism blockchain data for client applications while providing robust monitoring and developer-friendly interfaces.