app.ts


Overview

`app.ts` is the main entry point for the ShapeShift Gnosis API server application. It sets up an Express-based HTTP server integrated with WebSocket support for real-time blockchain event handling. The file configures REST API routes, middleware, metrics collection, Swagger API documentation, and the essential blockchain data processing pipelines for Gnosis chain transactions and blocks.

The primary functionality includes:

This file acts as the orchestrator connecting network, API, blockchain event processing, and monitoring layers for the Gnosis blockchain stack.


Classes, Functions, and Key Elements

1. Express Application Setup

const app = express()

2. Prometheus Metrics Instance

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

3. HTTP Endpoints


4. Blockchain Event Handlers

These handlers process blockchain data received from the WebSocket connection to the blockchain indexer.

blockHandler

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

transactionHandler

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

5. Registry Initialization

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

6. WebSocket Client Setup

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

7. HTTP and WebSocket Server

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

8. WebSocket Connection Handler

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

Important Implementation Details


Interaction with Other System Components


Usage Example

Starting the server after setting the environment variables:

export INDEXER_WS_URL=wss://gnosis-indexer.example.com/ws
export PORT=3000
export LOG_LEVEL=info
node dist/app.js

Mermaid Diagram - Structure of app.ts

classDiagram
    class App {
        +app: express.Application
        +prometheus: Prometheus
        +logger: Logger
        +blockHandler(block: NewBlock): Promise<{txs: Array}>
        +transactionHandler(tx: BlockbookTx): Promise<{addresses: Array, tx: evm.Tx}>
    }

    class Registry {
        +onBlock(block: NewBlock)
        +onTransaction(tx: BlockbookTx)
    }

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

    class Server {
        +listen(port: number)
        +on(event: string, handler: Function)
    }

    class ConnectionHandler {
        +start(connection: WebSocket, registry: Registry, blockbook: WebsocketClient, prometheus: Prometheus, logger: Logger)
    }

    App "1" *-- "1" Registry : uses
    App "1" *-- "1" Prometheus : uses
    App "1" *-- "1" Logger : uses
    App "1" *-- "1" WebsocketClient : creates
    App "1" *-- "1" Server : creates
    Server "1" *-- "*" WebSocketConnection : manages
    Server "1" o-- ConnectionHandler : uses on connection
    WebsocketClient "1" ..> Registry : calls handlers

Summary

`app.ts` is a critical integration point that bootstraps the Gnosis API server, combining HTTP serving, real-time WebSocket blockchain event processing, metrics collection, and API documentation. It leverages modular components like Registry and WebsocketClient to efficiently handle blockchain data streams and exposes RESTful endpoints for external clients. The file ensures observability via Prometheus and logging while maintaining extensibility through middleware and dynamic route registration.