models.ts


Overview

The [models.ts](/projects/291/68901) file defines TypeScript interfaces and types that model core Ethereum Virtual Machine (EVM) blockchain data structures used in the application. It standardizes representations of accounts, transactions, tokens, fees, and gas estimates to facilitate consistent data handling across different modules. Additionally, it specifies an `API` interface that outlines EVM-specific asynchronous methods to interact with blockchain data, such as fetching transaction details, estimating gas, retrieving gas fees, and obtaining token metadata.

This file essentially acts as a contract for the data exchanged between the application and the underlying blockchain service layer, enabling type-safe API implementations and integrations.


Detailed Explanations

Interfaces and Types


GasEstimate

export interface GasEstimate {
  gasLimit: string
}
const estimate: GasEstimate = { gasLimit: "21000" };
console.log(`Estimated gas limit: ${estimate.gasLimit}`);

Fees

export interface Fees {
  gasPrice: string
  maxFeePerGas?: string
  maxPriorityFeePerGas?: string
}

GasFees

export interface GasFees {
  baseFeePerGas?: string
  slow: Fees
  average: Fees
  fast: Fees
}

Token

export interface Token {
  contract: string
  decimals: number
  name: string
  symbol: string
  type: string
  id?: string
}
const token: Token = {
  contract: "0xabc123...",
  decimals: 18,
  name: "MyToken",
  symbol: "MTK",
  type: "erc20",
};

TokenType

export type TokenType = 'erc721' | 'erc1155'

TokenMetadata

export interface TokenMetadata {
  name: string
  description: string
  media: {
    url: string
    type?: 'image' | 'video'
  }
}

TokenBalance

export interface TokenBalance extends Token {
  balance: string
}

TokenTransfer

export interface TokenTransfer extends Token {
  from: string
  to: string
  value: string
  id?: string
}

Account

export interface Account extends BaseAccount {
  nonce: number
  tokens: Array<TokenBalance>
}

Tx

export interface Tx extends BaseTx {
  from: string
  to: string
  confirmations: number
  value: string
  fee: string
  gasLimit: string
  gasUsed?: string
  gasPrice: string
  status: number
  inputData?: string
  tokenTransfers?: Array<TokenTransfer>
  internalTxs?: Array<InternalTx>
}

InternalTx

export interface InternalTx {
  from: string
  to: string
  value: string
}

TxHistory

export type TxHistory = BaseTxHistory<Tx>

API

export interface API {
  getTransaction(txid: string): Promise<Tx>;
  estimateGas(body: EstimateGasBody): Promise<GasEstimate>;
  getGasFees(): Promise<GasFees>;
  getTokenMetadata(contract: string, id: string, type: string): Promise<TokenMetadata>;
}

getTransaction

api.getTransaction('0xabc123...').then(tx => {
  console.log(tx.from, tx.to, tx.status);
});

estimateGas

const estimate = await api.estimateGas({ from: '0x...', to: '0x...', data: '0x...' });
console.log(`Estimated gas limit: ${estimate.gasLimit}`);

getGasFees

api.getGasFees().then(fees => {
  console.log('Fast gas price:', fees.fast.gasPrice);
});

getTokenMetadata

const metadata = await api.getTokenMetadata('0xabc...', '123', 'erc721');
console.log(metadata.name, metadata.description);

Important Implementation Details and Algorithms


Interactions with Other System Components


Visual Diagram

classDiagram
    class GasEstimate {
        +gasLimit: string
    }
    class Fees {
        +gasPrice: string
        +maxFeePerGas?: string
        +maxPriorityFeePerGas?: string
    }
    class GasFees {
        +baseFeePerGas?: string
        +slow: Fees
        +average: Fees
        +fast: Fees
    }
    class Token {
        +contract: string
        +decimals: number
        +name: string
        +symbol: string
        +type: string
        +id?: string
    }
    class TokenMetadata {
        +name: string
        +description: string
        +media: object
    }
    class TokenBalance {
        +balance: string
    }
    class TokenTransfer {
        +from: string
        +to: string
        +value: string
        +id?: string
    }
    class Account {
        +nonce: number
        +tokens: TokenBalance[]
    }
    class Tx {
        +from: string
        +to: string
        +confirmations: number
        +value: string
        +fee: string
        +gasLimit: string
        +gasUsed?: string
        +gasPrice: string
        +status: number
        +inputData?: string
        +tokenTransfers?: TokenTransfer[]
        +internalTxs?: InternalTx[]
    }
    class InternalTx {
        +from: string
        +to: string
        +value: string
    }
    class API {
        +getTransaction(txid: string): Promise<Tx>
        +estimateGas(body: EstimateGasBody): Promise<GasEstimate>
        +getGasFees(): Promise<GasFees>
        +getTokenMetadata(contract: string, id: string, type: string): Promise<TokenMetadata>
    }

    TokenBalance --|> Token
    TokenTransfer --|> Token
    Account --|> BaseAccount
    Tx --|> BaseTx
    InternalTx <-- Tx : internalTxs
    TokenTransfer <-- Tx : tokenTransfers

Summary

The [models.ts](/projects/291/68901) file defines a comprehensive set of TypeScript interfaces and types modeling Ethereum blockchain entities, including gas estimates, fee structures, tokens, accounts, and transactions. It extends base blockchain models with EVM-specific properties and exposes an `API` interface for core asynchronous blockchain interactions. This structured typing supports robust, scalable, and maintainable blockchain application development by providing a clear data contract layer between services and consumers.