app.ts


Overview

`app.ts` is the main entry point for the ShapeShift Arbitrum One API server. It sets up an Express-based HTTP server combined with WebSocket support to provide real-time blockchain data streaming and REST API endpoints. This file integrates several key components:

This file essentially acts as the glue between the blockchain data sources, business logic processing, API exposure, and real-time client communication.


Detailed Explanation

Imports and Configuration


Logger Initialization

export const logger = new Logger({
  namespace: ['unchained', 'coinstacks', 'arbitrum', 'api'],
  level: process.env.LOG_LEVEL,
})

Prometheus Metrics

const prometheus = new Prometheus({ coinstack: 'arbitrum' })

Express Application Setup

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

HTTP Routes


Blockchain Handlers

BlockHandler

const blockHandler: BlockHandler<NewBlock, Array<{ addresses: Array<string>; tx: evm.Tx }>> = async (block) => {
  // Fetch blockbook transactions and internal transactions in parallel
  // Process each transaction with internal traces and aggregate addresses involved
  // Return array of { addresses, tx } pairs for registry consumption
}

TransactionHandler

const transactionHandler: TransactionHandler<BlockbookTx, evm.Tx> = async (blockbookTx) => {
  // Process a single transaction with internal tracing
  // Extract unique addresses involved
  // Return { addresses, tx }
}

Registry Setup

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

WebSocket Client to Blockchain Indexer

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

HTTP and WebSocket Server Initialization

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

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

Usage Examples

Access Health Endpoint

curl http://localhost:3000/health

Returns:

{
  "status": "up",
  "asset": "arbitrum",
  "connections": 0
}

Connect WebSocket Client

Clients connect to the same server via WS, and receive real-time blockchain updates processed by `ConnectionHandler`.


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class App {
        +expressApp: Express
        +wsServer: WebSocket.Server
        +logger: Logger
        +prometheus: Prometheus
        +registry: Registry
        +blockbook: WebsocketClient
        +start()
    }

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

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

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

    class BlockHandler {
        +handleBlock()
    }

    class TransactionHandler {
        +handleTransaction()
    }

    App --> expressApp : uses
    App --> wsServer : creates
    App --> logger : logs
    App --> prometheus : collects metrics
    App --> registry : holds
    App --> blockbook : connects to indexer
    wsServer --> ConnectionHandler : on connection
    blockbook --> Registry : sends blocks/txs
    Registry --> BlockHandler : delegates block processing
    Registry --> TransactionHandler : delegates tx processing

Summary

`app.ts` is the core orchestration file for the ShapeShift Arbitrum One API, combining Express HTTP server with WebSocket blockchain streaming. It leverages modular handlers, middleware, and monitoring to deliver a robust API and real-time data service, serving as a vital backbone for interacting with Arbitrum blockchain data in both RESTful and streaming contexts.