models.ts


Overview

The [models.ts](/projects/291/68901) file defines TypeScript classes and interfaces that represent the core data structures used in the Blockchain Data Indexing module. These models provide a strongly-typed, normalized representation of blockchain-related entities such as transactions, addresses, blocks, tokens, and errors. This standardization enables consistent interaction with blockchain data, regardless of underlying chain specifics (e.g., Bitcoin vs Ethereum).

The file also includes an error class (`ApiError`) which wraps and extends Axios errors that occur during HTTP communication with the Blockbook backend. This enhances error handling by providing meaningful messages extracted from API responses.

Overall, [models.ts](/projects/291/68901) serves as the foundational schema for blockchain data throughout the system, facilitating clear data contracts between the Blockbook API client, business logic, and any consumers of blockchain data.


Detailed Descriptions

Class: ApiError

A custom error class extending `AxiosError` to represent failures when interacting with the Blockbook API.

Constructor

constructor(err: AxiosError | Error)
try {
  // axios request that may fail
} catch (error) {
  throw new ApiError(error)
}

Interfaces

The interfaces define the shape of blockchain-related data objects used throughout the system.


Address

Represents an address with associated balance and transaction details.


BackendInfo

Contains metadata about the connected backend blockchain node.


BalanceHistory

Represents historical balance data at a specific time point.


Block

Describes a blockchain block and its transactions.


BlockbookInfo (internal interface)

Information about the Blockbook service instance.


BlockIndex

Minimal info about a block, typically for referencing by hash.


Info

Aggregates Blockbook and backend node info.


EthereumSpecific

Ethereum-specific transaction fields.


Erc20Contract

Details of an ERC20 token contract.


Paging

Pagination parameters common to address, block, and xpub queries.


SendTx

Result object for a transaction submission.


Token

Information about tokens held by an address.


MultiTokenValue

Represents a single multi-token ID and its value.


TokenTransfer

Details a single token transfer within a transaction.


Tx

Represents a blockchain transaction with normalized fields.


Utxo

Data about a single unspent transaction output (UTXO).


Vin

Details about a single transaction input.


Vout

Details about a single transaction output.


Xpub

Type alias for `Address` representing extended public keys.


BlockbookArgs

Arguments to configure a Blockbook client instance.


Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram: Class Diagram of Key Entities

classDiagram
    class ApiError {
        +constructor(err: AxiosError | Error)
    }
    class Address {
        +address: string
        +balance: string
        +totalReceived?: string
        +totalSent?: string
        +unconfirmedBalance: string
        +unconfirmedTxs: number
        +txs: number
        +transactions?: Tx[]
        +txids?: string[]
    }
    class Tx {
        +txid: string
        +vin: Vin[]
        +vout: Vout[]
        +blockHeight: number
        +confirmations: number
        +blockTime: number
        +value: string
        +tokenTransfers?: TokenTransfer[]
        +ethereumSpecific?: EthereumSpecific
    }
    class Vin {
        +n: number
        +addresses?: string[]
        +isAddress: boolean
    }
    class Vout {
        +n: number
        +addresses: string[] | null
        +isAddress: boolean
        +spent?: boolean
    }
    class Token {
        +type: string
        +name: string
        +transfers: number
        +balance?: string
    }
    class TokenTransfer {
        +type: string
        +from: string
        +to: string
        +contract: string
        +value: string
    }
    class EthereumSpecific {
        +status: number
        +nonce: number
        +gasLimit: number
        +gasUsed: number | null
        +gasPrice: string
    }
    class Erc20Contract {
        +contract: string
        +name: string
        +symbol: string
        +decimals: number
    }
    class Utxo {
        +txid: string
        +vout: number
        +value: string
        +confirmations: number
    }

    Address "1" o-- "*" Tx : transactions
    Tx "1" o-- "*" Vin : vin
    Tx "1" o-- "*" Vout : vout
    Tx "1" o-- "*" TokenTransfer : tokenTransfers
    Tx "1" o-- "0..1" EthereumSpecific : ethereumSpecific
    Address "1" o-- "*" Token : tokens
    TokenTransfer "1" o-- "0..1" MultiTokenValue : multiTokenValues
    Token "1" o-- "0..1" MultiTokenValue : multiTokenValues

Summary

The [models.ts](/projects/291/68901) file plays a critical role in the Blockchain Data Indexing module by defining the data contracts and error handling mechanisms essential for consistent and reliable blockchain data retrieval. Its well-structured, strongly-typed interfaces enable multi-chain support and token complexity handling, while the custom error class facilitates robust API error management. These models underpin the entire interaction layer with the Blockbook backend and serve as the foundation for blockchain data services in the application.