app.ts


Overview

[app.ts](/projects/291/68852) serves as the main entry point and orchestrator for the Solana blockchain API server built by ShapeShift. It sets up an Express-based HTTP server along with a WebSocket server to provide real-time transaction data streaming and RESTful API endpoints. This file integrates several middleware components, including Prometheus metrics, Swagger API documentation, and custom transaction processing logic tailored for Solana blockchain data.

Key functionalities include:

This file ties together various modules such as transaction models, route definitions, utilities, and WebSocket clients to provide a cohesive API service.


Detailed Breakdown

Constants and Environment Variables

Throws an error if either `WS_URL` or `WS_API_KEY` is not set, preventing the app from starting without essential configuration.


Logger

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

Prometheus Metrics

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

Express Application Setup

const app = express()

app.use(...middleware.common(prometheus))

HTTP Endpoints


Swagger UI Options

const options: swaggerUi.SwaggerUiOptions = {
  customCss: '.swagger-ui .topbar { display: none }',
  customSiteTitle: 'ShapeShift Solana API Docs',
  customfavIcon: '/public/favi-blue.png',
  swaggerUrl: '/swagger.json',
}

Route Registration

RegisterRoutes(app)

Transaction Handler

const transactionHandler: TransactionHandler<Logs, Tx> = async (log) => {
  const tx = await getTransaction(log.signature, true)
  const addresses = tx.accountData.map((key) => key.account)
  return { addresses, tx }
}

**Parameters:**

Name

Type

Description

`log`

`Logs`

Solana blockchain transaction log containing signature and metadata

**Returns:**

Type

Description

`Promise<{ addresses: string[], tx: Tx }>`

Returns an object containing the involved addresses and the transaction data

**Usage Example:**

const log: Logs = ...; // obtained from Solana WebSocket feed
const result = await transactionHandler(log);
console.log(result.addresses, result.tx);

Registry Initialization

const registry = new Registry({
  addressFormatter: (address: string) => address,
  transactionHandler,
})

WebSocket Client Setup

const helius = new WebsocketClient(WS_URL, {
  apiKey: WS_API_KEY,
  transactionHandler: registry.onTransaction.bind(registry),
})

Server 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, helius, prometheus, logger)
})

Implementation Details and Algorithms


Interactions with Other Modules


Usage Example

  1. Start server:
    Ensure environment variables WS_URL and WS_API_KEY are set, then run:

    node dist/app.js
    
  2. Access health check:

    curl http://localhost:3000/health
    
  3. Access API docs:
    Open browser at http://localhost:3000/docs

  4. Connect via WebSocket:
    Use the WebSocket URL exposed by this server (e.g., ws://localhost:3000) to receive real-time transaction updates.


Mermaid Class Diagram

classDiagram
    class app {
        +PORT: number|string
        +WS_URL: string
        +WS_API_KEY: string
        +logger: Logger
        +prometheus: Prometheus
        +app: express.Application
        +transactionHandler(log: Logs): Promise<{addresses: string[], tx: Tx}>
        +registry: Registry
        +helius: WebsocketClient
        +server: http.Server
        +wsServer: WebSocket.Server
    }

    class Logger {
        +namespace: string[]
        +level: string
        +info(msg: string)
        +error(msg: string)
    }

    class Prometheus {
        +coinstack: string
        +register: any
        +metrics(): Promise<string>
    }

    class Registry {
        +addressFormatter(address: string): string
        +transactionHandler: Function
        +onTransaction(tx: any): void
    }

    class WebsocketClient {
        +constructor(url: string, options: object)
        +apiKey: string
        +transactionHandler: Function
        +connect(): void
    }

    class ConnectionHandler {
        +start(conn: WebSocket, registry: Registry, client: WebsocketClient, prometheus: Prometheus, logger: Logger): void
    }

    app --> Logger
    app --> Prometheus
    app --> express.Application : app
    app --> Registry
    app --> WebsocketClient
    app --> ConnectionHandler

Summary

The [app.ts](/projects/291/68852) file is the central bootstrapper of the ShapeShift Solana API service. It configures HTTP and WebSocket servers, integrates middleware for observability and error handling, registers REST endpoints and Swagger documentation, and orchestrates the processing of real-time Solana blockchain transactions via WebSocket streams. The modular design leverages abstractions like `Registry` and `ConnectionHandler` to decouple concerns and improve maintainability. This file is critical for initializing and running the Solana API server within the overall ShapeShift infrastructure.