app.ts


Overview

`app.ts` serves as the main entry point for the Dogecoin API server within the ShapeShift ecosystem. It sets up an Express-based HTTP server with integrated WebSocket support to handle real-time blockchain data streaming and API requests. The file orchestrates connections to an external blockchain indexer (via WebSocket), processes new blocks and transactions, exposes REST endpoints (including health checks, metrics, and API documentation), and manages client WebSocket connections for live updates.

Key responsibilities include:


Classes, Functions, and Methods

1. blockHandler: BlockHandler<NewBlock, Array<BlockbookTx>>

const blockHandler: BlockHandler<NewBlock, Array<BlockbookTx>> = async (block) => {
  const txs = await service.handleBlock(block.hash)
  return { txs }
}

2. transactionHandler: TransactionHandler<BlockbookTx, utxo.Tx>

const transactionHandler: TransactionHandler<BlockbookTx, utxo.Tx> = async (blockbookTx) => {
  const tx = service.handleTransaction(blockbookTx)
  const addresses = getAddresses(blockbookTx)
  return { addresses, tx }
}

3. Registry

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

4. WebsocketClient

const blockbook = new WebsocketClient(
  wsUrl,
  {
    apiKey,
    blockHandler: registry.onBlock.bind(registry),
    transactionHandler: registry.onTransaction.bind(registry),
  },
  { resetInterval: 5 * 60 * 1000 }
)

5. Express Application Setup (app)


6. WebSocket Server and Connection Handling

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)
})

Important Implementation Details


Interaction With Other System Components


Usage Example

Once started, the server will:

Clients can subscribe to this WebSocket server to receive live updates about Dogecoin blocks and transactions, while developers can explore the REST API via Swagger UI.


Visual Diagram

classDiagram
    class app {
        +express: Express.Application
        +server: HTTP.Server
        +wsServer: WebSocket.Server
    }

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

    class WebsocketClient {
        +connect()
        +resetConnection()
    }

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

    class Prometheus {
        +register
        +metrics()
    }

    app --> Registry : uses
    app --> WebsocketClient : instantiates
    app --> ConnectionHandler : on wsServer.connection
    app --> Prometheus : integrates
    Registry <-- WebsocketClient : callbacks (onBlock,onTransaction)

Summary

`app.ts` is the core server bootstrap file for the Dogecoin API, integrating HTTP REST endpoints, WebSocket streaming for blockchain data, Prometheus metrics, and Swagger-based API documentation. It leverages a modular design with a registry pattern and external libraries to manage blockchain event subscriptions, client connections, and data processing, providing a robust and scalable backend service for Dogecoin blockchain interactions.