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


Interaction with Other Parts of the System


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

Summary

This documentation equips developers with a clear understanding of the purpose, structure, and usage of the data types defined in this file.