controller.ts


Overview

The `controller.ts` file is responsible for configuring and initializing the core service layer that interacts with Dogecoin blockchain data. It sets up connections to blockchain indexer and RPC (Remote Procedure Call) endpoints, configures the `Blockbook` client for blockchain data access, and exposes a unified `Service` instance tailored for Dogecoin UTXO (Unspent Transaction Output) management.

The file also integrates environment variable configurations, handles API key management depending on the provider (Liquify or Nownodes), and assigns the initialized service to the shared `UTXO` controller class. This enables other parts of the Dogecoin module to leverage the same service instance for querying blockchain data and performing UTXO-related operations.


Classes, Functions, and Methods

1. isXpub(pubkey: string): boolean

Checks if a given public key string corresponds to an extended public key (xpub) format specific to Dogecoin.

const key = "dgub8abc123...";
console.log(isXpub(key)); // true

2. formatAddress(address: string): string

Formats a Dogecoin address string. This implementation currently returns the address unchanged but acts as a placeholder for any future address formatting logic.

const rawAddress = "D7Y55FqQp2h...";
const formatted = formatAddress(rawAddress);
console.log(formatted); // "D7Y55FqQp2h..."

Constants and Variables


Main Objects

logger

An instance of the `Logger` class scoped to the Dogecoin API namespace. It logs messages at a level determined by the `LOG_LEVEL` environment variable.


blockbook

An instance of the `Blockbook` client class configured with:

This client interfaces with blockchain indexer services to retrieve blockchain data such as blocks, transactions, and addresses.


service

An instance of the `Service` class that acts as the main interface for Dogecoin blockchain operations. It is configured with:

The `service` instance abstracts communication with both the indexer and RPC endpoints and provides UTXO-related functionality.


Integration with UTXO Controller

The file assigns the created `service` instance to the static `service` property of the `UTXO` class:

UTXO.service = service

This makes the service globally accessible within all `UTXO` controller instances, centralizing blockchain interaction logic and avoiding redundant initializations.


Important Implementation Details


Interaction with Other System Parts


Usage Example

import { service, logger, formatAddress } from './controller'

// Check if a public key is an xpub
const pubkey = 'dgub8xyz...'
if (service.isXpub(pubkey)) {
  logger.info('Received an extended public key')
}

// Format an address
const addr = formatAddress('D7Y55FqQp2h...')
logger.info(`Formatted address: ${addr}`)

// Use the service to fetch UTXOs, transactions, or other blockchain data
const utxos = await service.getUtxos(addr)
logger.info(`Fetched ${utxos.length} UTXOs`)

Mermaid Class Diagram

classDiagram
    class Blockbook {
        +constructor(options)
        +methods...
    }

    class Service {
        +constructor(options)
        +getUtxos(address)
        +sendTransaction(tx)
        +other blockchain methods...
    }

    class UTXO {
        <<static>>
        +service: Service
        +methods...
    }

    class Logger {
        +constructor(options)
        +info(message)
        +error(message)
        +debug(message)
        +other logging methods...
    }

    controller.ts  -->  Blockbook : instantiates
    controller.ts  -->  Service : instantiates with Blockbook
    controller.ts  -->  Logger : instantiates
    controller.ts  ..>  UTXO : assigns static service property

Summary

The `controller.ts` file is a critical configuration and bootstrap component for Dogecoin blockchain interaction within the application. It initializes the blockchain client, manages environment-based configuration, and provides a well-structured `Service` instance used by the UTXO controller and other modules. The file ensures seamless integration with different blockchain data providers while maintaining a consistent interface for Dogecoin blockchain operations.