app.ts


Overview

`app.ts` is the main entry point for the **ShapeShift BNB Smart Chain API** server application. It sets up an Express-based HTTP REST API augmented with WebSocket support to serve blockchain data related to Binance Smart Chain (BNB Smart Chain). The file orchestrates:

This file acts as the glue between the HTTP interface, WebSocket event listening, blockchain data processing services, and monitoring infrastructure.


Classes, Functions, and Methods

Express App Setup


blockHandler

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

transactionHandler

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

Registry

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

WebSocket Client Setup

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

WebSocket Server Setup

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

Logger

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

Prometheus Metrics

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

Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Usage Example

To start the BNB Smart Chain API server:

INDEXER_WS_URL=wss://indexer.example.com/ws \
INDEXER_API_KEY=yourapikey \
PORT=3000 \
LOG_LEVEL=info \
node dist/app.js

Then:


Mermaid Diagram: Class Diagram of Key Components

classDiagram
    class app {
        +use(middleware)
        +get(route, handler)
        +listen(port, callback)
    }
    class Registry {
        +onBlock(block)
        +onTransaction(tx)
        -addressFormatter
        -blockHandler
        -transactionHandler
    }
    class WebsocketClient {
        +constructor(url, options)
        +connect()
        +blockHandler
        +transactionHandler
    }
    class Server {
        +constructor(options)
        +on(event, callback)
    }
    class ConnectionHandler {
        +start(connection, registry, blockbook, prometheus, logger)
    }
    class Prometheus {
        +register
        +constructor(options)
    }
    class Logger {
        +info(message)
        +error(message)
    }

    app --> Registry : uses
    app --> Server : creates
    Server --> ConnectionHandler : on connection calls start
    WebsocketClient --> Registry : calls onBlock, onTransaction
    app --> Prometheus : uses for metrics
    app --> Logger : logs info/errors

Summary

This file acts as the core runtime for the BNB Smart Chain API service, bridging HTTP REST, WebSocket blockchain event streaming, transaction processing, and monitoring. It leverages external libraries and internal controllers to provide a scalable, observable, and real-time blockchain data API tailored for ShapeShift's ecosystem.