models.ts


Overview

The [models.ts](/projects/291/68901) file defines TypeScript interfaces that model the fundamental data structures and API response formats used throughout the application. These interfaces primarily represent error responses, account details, transaction information, pagination structure, and JSON-RPC request/response formats. The file serves as a centralized type definition module that ensures consistent data handling and validation across various components interacting with blockchain data, transactions, and API communications.

Key functionalities include:

This file acts as a foundational layer for data integrity and type safety in the system.


Interfaces and Their Details

1. BadRequestError

Represents a 400 Bad Request HTTP error response.

interface BadRequestError {
  error: string;
}

2. NotFoundError

Represents a 404 Not Found HTTP error response.

interface NotFoundError {
  message: string;
}

3. ValidationError

Represents a 422 Validation Error response indicating failed input validation.

interface ValidationError {
  message: 'Validation failed';
  details: { [name: string]: unknown };
}

4. InternalServerError

Represents a 500 Internal Server Error HTTP response.

interface InternalServerError {
  message: string;
}

5. BaseAccount

Represents basic account details for a blockchain address or an extended public key (xpub).

interface BaseAccount {
  balance: string;
  unconfirmedBalance: string;
  pubkey: string;
}

6. BaseInfo

Represents basic information about the running blockchain stack or network.

interface BaseInfo {
  network: string;
}

7. Pagination

Represents pagination data for API responses supporting paged results.

interface Pagination {
  cursor?: string;
}

8. SendTxBody

Represents the body of a request to send a serialized raw transaction.

interface SendTxBody {
  hex: string;
}

9. EstimateGasBody

Represents the transaction data used to estimate gas cost for a transaction.

interface EstimateGasBody {
  data: string;
  from: string;
  to: string;
  value: string;
}

10. BaseTx

Represents basic transaction details.

interface BaseTx {
  txid: string;
  blockHash?: string;
  blockHeight: number;
  timestamp: number;
}

11. BaseTxHistory<T = BaseTx>

Represents paginated transaction history for a given public key.

interface BaseTxHistory<T = BaseTx> extends Pagination {
  pubkey: string;
  txs: Array<T>;
}

12. RPCRequest

Represents a JSON-RPC 2.0 request structure for blockchain node communication.

interface RPCRequest {
  jsonrpc: '2.0';
  id: string | number;
  method: string;
  params?: Array<unknown>;
}

13. RPCResponse

Represents a JSON-RPC 2.0 response structure.

interface RPCResponse {
  jsonrpc: '2.0';
  id: string | number;
  result?: unknown;
  error?: {
    code: number;
    message: string;
    data?: unknown;
  };
}

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram: Class Diagram Representing Interfaces and Their Relationships

classDiagram
    class BadRequestError {
        +error: string
    }
    class NotFoundError {
        +message: string
    }
    class ValidationError {
        +message: 'Validation failed'
        +details: { [name: string]: unknown }
    }
    class InternalServerError {
        +message: string
    }
    class BaseAccount {
        +balance: string
        +unconfirmedBalance: string
        +pubkey: string
    }
    class BaseInfo {
        +network: string
    }
    class Pagination {
        +cursor?: string
    }
    class SendTxBody {
        +hex: string
    }
    class EstimateGasBody {
        +data: string
        +from: string
        +to: string
        +value: string
    }
    class BaseTx {
        +txid: string
        +blockHash?: string
        +blockHeight: number
        +timestamp: number
    }
    class BaseTxHistory {
        +pubkey: string
        +txs: Array~BaseTx~
        +cursor?: string
    }
    class RPCRequest {
        +jsonrpc: '2.0'
        +id: string | number
        +method: string
        +params?: Array~unknown~
    }
    class RPCResponse {
        +jsonrpc: '2.0'
        +id: string | number
        +result?: unknown
        +error?: ErrorObject
    }
    class ErrorObject {
        +code: number
        +message: string
        +data?: unknown
    }

    BaseTxHistory --|> Pagination
    RPCResponse "1" o-- "1" ErrorObject : error

Summary

The [models.ts](/projects/291/68901) file provides a well-organized set of TypeScript interfaces modeling error responses, blockchain accounts, transactions, pagination, and JSON-RPC communication. It ensures type safety and consistent data structures across the application, supporting blockchain-related workflows such as transaction handling, account management, and node communication. This foundational module integrates closely with API layers, blockchain service components, and UI modules that display or manipulate blockchain data.