registry.ts


Overview

The **registry.ts** file implements a centralized **Registry** class designed to manage WebSocket client subscriptions to blockchain address events. It tracks which connected clients have subscribed to updates for specific blockchain addresses and forwards new transaction data relevant to those addresses directly to the subscribed clients.

This registry serves as a crucial component in a real-time blockchain event subscription system, enabling efficient and scalable delivery of transaction notifications over WebSocket connections. It maintains bidirectional mappings between clients and addresses and integrates with customizable handlers to process incoming block and transaction messages.


Detailed Explanation

Type Definitions




Internal Type Guard


Interface: RegistryArgs

interface RegistryArgs {
  addressFormatter?: AddressFormatter
  blockHandler?: BlockHandler
  transactionHandler: TransactionHandler
}

The constructor argument interface for the `Registry` class, allowing injection of:


Class: Registry

class Registry

Purpose

Manages WebSocket client subscriptions by tracking which clients have subscribed to which blockchain addresses and dispatching new transaction data to those clients based on their subscriptions.


Properties


Constructor

constructor(args: RegistryArgs)

Static Methods


Public Methods






Private Methods


Important Implementation Details


Interaction with Other System Components


Usage Example

import { Registry } from './registry'
import { ConnectionHandler } from './websocket'

const transactionHandler = async (tx: any) => {
  // Extract addresses and transaction data from raw tx
  return {
    addresses: tx.addresses,
    tx,
  }
}

const registry = new Registry({
  transactionHandler,
})

// WebSocket connection from a client
const connection = new ConnectionHandler()

// Client subscribes to addresses
registry.subscribe('client-uuid', 'sub-1', connection, ['addr1', 'addr2'])

// Incoming transaction from blockchain node
await registry.onTransaction(incomingTxMessage)

Visual Diagram

classDiagram
    class Registry {
        -clients: Record<string, Set<string>>
        -addresses: Record<string, Map<string, ConnectionHandler>>
        -handleBlock?: BlockHandler
        -handleTransaction: TransactionHandler
        -formatAddress: AddressFormatter
        -logger: Logger
        +constructor(args: RegistryArgs)
        +subscribe(clientId: string, subscriptionId: string, connection: ConnectionHandler, addresses: string[]): void
        +unsubscribe(clientId: string, subscriptionId: string, addresses: string[]): void
        +getAddresses(): string[]
        +onBlock(msg: unknown): Promise<void>
        +onTransaction(msg: unknown): Promise<void>
        -publishTransaction(addresses: string[], tx: unknown): void
        +static toId(clientId: string, subscriptionId: string): string
        +static fromId(id: string): { clientId: string; subscriptionId: string }
    }
    class ConnectionHandler {
        <<imported>>
    }
    Registry "1" -- "*" ConnectionHandler : manages

Summary

The **registry.ts** file implements the `Registry` class, a core component responsible for:

It achieves this through efficient bidirectional maps, address normalization, and pluggable handlers, enabling real-time and scalable blockchain event delivery over WebSocket connections. The Registry integrates tightly with WebSocket connection handlers and blockchain event sources to form a robust subscription delivery system.