types.ts


Overview

The [types.ts](/projects/291/69107) file defines TypeScript types and utility type-guard functions primarily related to handling WebSocket responses and parsed Solana blockchain transactions. It provides strong typing for WebSocket subscription messages, transaction data structures, and balances, facilitating safer and clearer interaction with Solana's Web3.js library and WebSocket communication.

Key functionalities include:

This file acts as a foundational type-definition module to ensure consistent and error-resistant handling of Solana blockchain transaction and WebSocket data within the application.


Detailed Explanation

Type Aliases and Interfaces

Transaction

export type Transaction = Omit<ParsedTransactionWithMeta, 'slot'>

WebsocketSubscribeResponse<T = unknown>

export interface WebsocketSubscribeResponse<T = unknown> {
  jsonrpc: '2.0'
  id: string
  method: string
  result: T
}

WebsocketResponse

export interface WebsocketResponse {
  jsonrpc: '2.0'
  method: string
  params: Params
}

Internal Interfaces: Params and Result

interface Params {
  subscription: number
  result: Result
}

interface Result {
  value: Logs
  context: {
    slot: number
  }
}

NativeBalance

export type NativeBalance = {
  lamports: number
  price_per_sol: number
  total_price: number
}

Functions

isWebsocketResponse

export const isWebsocketResponse = (data: unknown): data is WebsocketResponse => {
  return Boolean(typeof data === 'object' && data && 'method' in data && 'params' in data)
}
const msg: unknown = JSON.parse(incomingMessage);
if (isWebsocketResponse(msg)) {
  // TypeScript narrows type of msg to WebsocketResponse here
  console.log(msg.method, msg.params.subscription);
}

isWebsocketSubscribeResponse

export const isWebsocketSubscribeResponse = (data: unknown): data is WebsocketSubscribeResponse<number> => {
  return Boolean(typeof data === 'object' && data && 'result' in data && typeof data.result === 'number')
}
const msg: unknown = JSON.parse(incomingMessage);
if (isWebsocketSubscribeResponse(msg)) {
  console.log('Subscription ID:', msg.result);
}

Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram: Flowchart of WebSocket Message Handling Types

flowchart TD
    A[Incoming WebSocket Data] --> B{Is WebsocketSubscribeResponse?}
    B -- Yes --> C[Handle Subscription Response]
    B -- No --> D{Is WebsocketResponse?}
    D -- Yes --> E[Handle Notification Response]
    D -- No --> F[Unknown Message Type]

    subgraph Types
      B
      D
    end

    subgraph Handlers
      C
      E
      F
    end

Summary

[types.ts](/projects/291/69107) provides essential typing and validation utilities for handling Solana WebSocket JSON-RPC messages and transaction data. Its well-defined interfaces and type guards improve type safety and clarity in processing blockchain event streams and subscription responses, serving as a foundational piece in the application's Solana integration layer.