models.ts


Overview

The [models.ts](/projects/291/68901) file defines TypeScript types and interfaces that model the core data structures related to blockchain transactions, accounts, tokens, and fees within the application. It serves as a foundational data schema layer that standardizes the shape and expected properties of entities involved in transaction processing and account management. This file also includes an interface to abstract API interactions related to fetching transaction details.

By combining base types from a common API and enriched transaction data from an external SDK (`helius-sdk`), the file ensures consistent and enriched data representation across the application. These models facilitate type safety, code clarity, and easier integration between the UI, business logic, and API communication layers.


Detailed Descriptions

Type Aliases

Tx

export type Tx = BaseTx & EnrichedTransaction
async function printTx(txid: string, api: API) {
  const transaction: Tx = await api.getTransaction(txid);
  console.log(transaction.signature, transaction.slot, transaction.type);
}

TxHistory

export type TxHistory = BaseTxHistory<Tx>
function processHistory(history: TxHistory) {
  history.txs.forEach(tx => console.log(tx.signature));
}

Interfaces

Address

export interface Address {
  balance: string
  pubkey: string
}
const addr: Address = {
  balance: "1000000",
  pubkey: "F7Yd..."
};

Token

export interface Token {
  id: string
  decimals: number
  name: string
  symbol: string
  type: string
}
const solToken: Token = {
  id: "So11111111111111111111111111111111111111112",
  decimals: 9,
  name: "Solana",
  symbol: "SOL",
  type: "native"
};

TokenBalance

export interface TokenBalance extends Token {
  balance: string
}
const userTokenBalance: TokenBalance = {
  id: "TokenMintAddress",
  decimals: 6,
  name: "USD Coin",
  symbol: "USDC",
  type: "spl-token",
  balance: "1500000"
};

Account

export interface Account extends BaseAccount {
  tokens: Array<TokenBalance>
}
const account: Account = {
  // BaseAccount fields like address, pubkey, etc.
  address: "SomeAddress",
  pubkey: "PubkeyString",
  tokens: [
    {
      id: "TokenMint1",
      decimals: 9,
      name: "Token1",
      symbol: "TK1",
      type: "spl-token",
      balance: "100000000"
    }
  ]
};

PriorityFees

export interface PriorityFees {
  baseFee: string
  slow: string
  average: string
  fast: string
}
const fees: PriorityFees = {
  baseFee: "5000",
  slow: "10000",
  average: "20000",
  fast: "40000"
};

Type Alias: EstimateFeesBody

export type EstimateFeesBody =
  | { serializedTx?: string }
  | { message?: string }
const feeBody1: EstimateFeesBody = { serializedTx: "base64string" };
const feeBody2: EstimateFeesBody = { message: "some transaction message" };

Interface: API

export interface API {
  getTransaction(txid: string): Promise<Tx>
}

Method

Parameters

Returns

Description

`getTransaction`

`txid: string`

`Promise`

Retrieves the transaction details for the given transaction signature

async function fetchAndLogTx(api: API, txid: string) {
  const tx = await api.getTransaction(txid);
  console.log(tx);
}

Important Implementation Details


Integration with Other System Components


Visual Diagram

classDiagram
    %% Tx is intersection of BaseTx and EnrichedTransaction (external)
    class Tx {
      <<type alias>>
    }

    %% TxHistory is BaseTxHistory generic on Tx
    class TxHistory {
      <<type alias>>
    }

    class Address {
      +balance: string
      +pubkey: string
    }

    class Token {
      +id: string
      +decimals: number
      +name: string
      +symbol: string
      +type: string
    }

    class TokenBalance {
      +balance: string
      +id: string
      +decimals: number
      +name: string
      +symbol: string
      +type: string
    }

    class Account {
      +tokens: Array~TokenBalance~
    }

    class PriorityFees {
      +baseFee: string
      +slow: string
      +average: string
      +fast: string
    }

    class EstimateFeesBody {
      <<union type>>
      +serializedTx?: string
      +message?: string
    }

    class API {
      +getTransaction(txid: string): Promise~Tx~
    }

    %% Inheritance
    TokenBalance --|> Token
    Account --|> BaseAccount

    %% Notes on external types
    note for Tx "Tx = BaseTx & EnrichedTransaction (external)"
    note for TxHistory "TxHistory = BaseTxHistory<Tx> (generic base)"
    note for Account "Account extends BaseAccount imported from common API"

Summary

The [models.ts](/projects/291/68901) file provides a structured and extensible set of type definitions and interfaces essential for representing blockchain transactions, accounts, tokens, and fee estimations within the system. It bridges external SDK data and internal base models, enabling consistency and clarity across the codebase. The API interface defines key blockchain data retrieval methods, facilitating modular and testable integration with backend services.

This file is foundational for any module working with blockchain data, ensuring reliable type contracts and supporting scalable system architecture.