types.ts
Overview
The [types.ts](/projects/291/69057) file defines TypeScript interfaces representing essential blockchain-related data structures. It primarily focuses on providing type-safe contracts for pagination cursors and detailed blockchain block information. These interfaces are foundational for interacting with blockchain data within the application, ensuring consistent data shapes for pagination control and block data handling.
Interfaces
1. Cursor
Represents pagination information for managing cursor-based pagination.
Properties:
Name | Type | Description |
|---|---|---|
`page` | `number` | The current page number in the pagination sequence. |
Usage:
This interface is typically used when requesting or returning paginated data, keeping track of the current page index.
const cursor: Cursor = { page: 2 };
// Use cursor.page to fetch the corresponding data page
2. NodeBlock
Represents a detailed blockchain block with all relevant metadata fields typically returned by a blockchain node.
Properties:
Name | Type | Description |
|---|---|---|
`hash` | `string` | The block hash (unique identifier). |
`confirmations` | `number` | Number of confirmations for this block. |
`size` | `number` | The block size in bytes. |
`strippedsize` | `number` | Size of the block without witness data. |
`weight` | `number` | The block weight as defined by BIP141. |
`height` | `number` | The block height (position in the blockchain). |
`version` | `number` | The block version number. |
`versionHex` | `string` | The block version in hexadecimal format. |
`merkleroot` | `string` | The Merkle root of the transactions in the block. |
`tx` | `Array` | List of transaction IDs (hashes) included in the block. |
`time` | `number` | The block timestamp (Unix epoch time). |
`mediantime` | `number` | Median time of the block and its ancestors. |
`nonce` | `number` | The nonce used for the proof-of-work algorithm. |
`bits` | `string` | The encoded difficulty target for this block. |
`difficulty` | `number` | The difficulty of the block. |
`chainwork` | `string` | Total work in the chain up to and including this block (hex). |
`nTx` | `number` | Number of transactions in the block. |
`previousblockhash` | `string` | Hash of the previous block in the chain. |
`nextblockhash` | `string` | Hash of the next block in the chain (if available). |
Usage:
This interface models the structure of block data retrieved from a blockchain node RPC or API call. It can be used to type-check block objects to guarantee that all required fields are present before processing or displaying block information.
function printBlockSummary(block: NodeBlock): void {
console.log(`Block ${block.height} (${block.hash})`);
console.log(`Transactions: ${block.nTx}, Time: ${new Date(block.time * 1000).toLocaleString()}`);
}
const exampleBlock: NodeBlock = {
hash: "0000000000000000000abcdef1234567890...",
confirmations: 100,
size: 123456,
strippedsize: 120000,
weight: 400000,
height: 680000,
version: 536870912,
versionHex: "20000000",
merkleroot: "abcdef1234567890...",
tx: ["txid1", "txid2", "txid3"],
time: 1617181723,
mediantime: 1617181700,
nonce: 1234567890,
bits: "170d9d3a",
difficulty: 21000000000000,
chainwork: "0000000000000000000000000000000000000000000000000000000000000000",
nTx: 3,
previousblockhash: "0000000000000000000abcdef1234567890...",
nextblockhash: "0000000000000000000abcdef1234567890..."
};
printBlockSummary(exampleBlock);
Implementation Details and Algorithms
Both interfaces are plain data structures without behavior or methods.
The
NodeBlockinterface follows the canonical structure of a blockchain block as returned by typical Bitcoin node RPC calls (e.g.,getblock).Cursorencapsulates simple pagination state, enabling consistent handling of page-based navigation.No algorithms are implemented in this file; it serves purely as a type definition module to enforce data integrity across the application layers interacting with blockchain data.
Interaction with Other Parts of the System
These interfaces are expected to be imported and used by services or modules that:
Fetch blockchain data from node APIs or third-party services.
Manage paginated lists of blockchain data (blocks, transactions).
Provide typed data to UI components for rendering block information.
They act as contracts ensuring data consistency between data retrieval layers and presentation/business logic layers.
Changes to these interfaces would directly affect any module expecting blockchain block data or pagination info, so they form a critical part of the system's type safety.
Diagram: Flowchart of Functions Using these Interfaces
Since this file contains only interfaces (data types) and no functions or classes, the most relevant diagram is a simple flowchart showing how these interfaces are typically used in the system workflow:
flowchart TD
A[Fetch Block Data from Node/API] --> B[Parse JSON Response]
B --> C[Validate & Type as NodeBlock]
C --> D[Process Block Data]
D --> E[Display Block Info in UI]
F[Handle Pagination] --> G[Use Cursor Interface]
G --> A
style A fill:#f9f,stroke:#333,stroke-width:1px
style C fill:#bbf,stroke:#333,stroke-width:1px
style G fill:#bbf,stroke:#333,stroke-width:1px
The flowchart shows that data fetched from a blockchain node or API is parsed and validated against the
NodeBlockinterface.Pagination in the UI or service layer uses the
Cursorinterface to keep track of pages.The validated and typed block data is then processed and displayed.
Summary
types.ts defines two key interfaces:
Cursorfor pagination andNodeBlockfor blockchain block data.These interfaces ensure strong typing and data consistency across modules handling blockchain data.
They are foundational for blockchain data retrieval, pagination, and UI integration.
No business logic or algorithms reside here; it serves as a type contract.
Changes here affect any component or service consuming blockchain-related data.
This documentation equips developers with a clear understanding of the purpose, structure, and usage of the data types defined in this file.