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
}
Purpose: Represents an estimate of the gas required to execute a particular transaction.
Properties:
gasLimit(string): The estimated upper bound of gas units needed.
Usage Example:
const estimate: GasEstimate = { gasLimit: "21000" };
console.log(`Estimated gas limit: ${estimate.gasLimit}`);
Fees
export interface Fees {
gasPrice: string
maxFeePerGas?: string
maxPriorityFeePerGas?: string
}
Purpose: Captures fee-related information for transactions, supporting both legacy and EIP-1559 fee models.
Properties:
gasPrice(string): Fee per gas unit for legacy transactions.maxFeePerGas(optional string): Maximum fee per gas for EIP-1559 transactions.maxPriorityFeePerGas(optional string): Priority fee per gas for EIP-1559 transactions.
Notes: Either
gasPrice(legacy) or the EIP-1559 fields (maxFeePerGasandmaxPriorityFeePerGas) will be used depending on transaction type.
GasFees
export interface GasFees {
baseFeePerGas?: string
slow: Fees
average: Fees
fast: Fees
}
Purpose: Provides recommended gas fee parameters for different confirmation speed categories.
Properties:
baseFeePerGas(optional string): The base fee for the pending block (EIP-1559).slow(Fees): Fees for slow confirmation speed.average(Fees): Fees for average confirmation speed.fast(Fees): Fees for fast confirmation speed.
Usage: Allows users to select a fee level according to their urgency and cost preferences.
Token
export interface Token {
contract: string
decimals: number
name: string
symbol: string
type: string
id?: string
}
Purpose: Represents an ERC token with essential metadata.
Properties:
contract(string): Token contract address.decimals(number): Number of decimals used by the token.name(string): Token name.symbol(string): Token symbol (e.g., "ETH").type(string): Token standard/type (e.g., "erc20", "erc721").id(optional string): Token ID for NFTs or multi-tokens.
Usage Example:
const token: Token = {
contract: "0xabc123...",
decimals: 18,
name: "MyToken",
symbol: "MTK",
type: "erc20",
};
TokenType
export type TokenType = 'erc721' | 'erc1155'
Purpose: Enumerates supported token standards for metadata retrieval.
Values:
'erc721'and'erc1155'
TokenMetadata
export interface TokenMetadata {
name: string
description: string
media: {
url: string
type?: 'image' | 'video'
}
}
Purpose: Holds descriptive metadata about tokens, especially NFTs.
Properties:
name(string): Name of the token.description(string): Human-readable description.media(object):url(string): URL to media resource.type(optional 'image' | 'video'): Media type.
Use Case: Displaying token information in wallets or marketplaces.
TokenBalance
export interface TokenBalance extends Token {
balance: string
}
Purpose: Extends
Tokenby adding balance information for a specific address.Properties:
balance(string): Token balance owned.
Usage: Useful for wallet UI to show balances of various tokens.
TokenTransfer
export interface TokenTransfer extends Token {
from: string
to: string
value: string
id?: string
}
Purpose: Represents a token transfer event.
Properties:
from(string): Sender address.to(string): Recipient address.value(string): Transferred amount.id(optional string): NFT or multi-token ID.
Usage: Used for displaying token transfer histories.
Account
export interface Account extends BaseAccount {
nonce: number
tokens: Array<TokenBalance>
}
Purpose: Represents an EVM account with nonce and token balances.
Extends:
BaseAccount(imported from../models).Properties:
nonce(number): Transaction nonce for the account.tokens(TokenBalance[]): List of tokens and their balances owned by the account.
Usage: Used to display account-specific blockchain state.
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>
}
Purpose: Models an EVM blockchain transaction.
Extends:
BaseTx(imported from../models).Properties:
from(string): Sender address.to(string): Recipient address.confirmations(number): Number of block confirmations.value(string): Amount transferred.fee(string): Total transaction fee paid.gasLimit(string): Gas limit set for the transaction.gasUsed(optional string): Actual gas consumed.gasPrice(string): Gas price used.status(number): Transaction status code (e.g., success/failure).inputData(optional string): Input data/encoded call data.tokenTransfers(optional TokenTransfer[]): Token transfer events triggered.internalTxs(optional InternalTx[]): Internal transactions (calls within contract execution).
Usage: Used to display transaction details in explorers or wallets.
InternalTx
export interface InternalTx {
from: string
to: string
value: string
}
Purpose: Represents internal EVM transactions that occur within a contract call.
Properties:
from(string): Caller address.to(string): Callee address.value(string): Amount transferred internally.
Notes: Internal transactions are not direct blockchain transactions but calls made by contracts during execution.
TxHistory
export type TxHistory = BaseTxHistory<Tx>
Purpose: Defines a transaction history type specialized for EVM transactions.
Based on: Generic
BaseTxHistorywrappingTxtype.Usage: Used to represent paginated or aggregated transaction lists.
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>;
}
Purpose: Defines asynchronous methods for interacting with the EVM blockchain.
Methods:
getTransaction
Signature:
getTransaction(txid: string): Promise<Tx>Description: Retrieves detailed information on a specific transaction by its hash.
Parameters:
txid(string): Transaction hash identifier.
Returns: Promise resolving to a
Txobject.Example:
api.getTransaction('0xabc123...').then(tx => {
console.log(tx.from, tx.to, tx.status);
});
estimateGas
Signature:
estimateGas(body: EstimateGasBody): Promise<GasEstimate>Description: Estimates the gas required to perform the provided transaction data.
Parameters:
body(EstimateGasBody): Transaction details needed for estimation (imported).
Returns: Promise resolving to
GasEstimate.Example:
const estimate = await api.estimateGas({ from: '0x...', to: '0x...', data: '0x...' });
console.log(`Estimated gas limit: ${estimate.gasLimit}`);
getGasFees
Signature:
getGasFees(): Promise<GasFees>Description: Fetches the current recommended fees for different confirmation speeds.
Returns: Promise resolving to
GasFees.Example:
api.getGasFees().then(fees => {
console.log('Fast gas price:', fees.fast.gasPrice);
});
getTokenMetadata
Signature:
getTokenMetadata(contract: string, id: string, type: string): Promise<TokenMetadata>Description: Retrieves metadata for a specific token (usually NFT).
Parameters:
contract(string): Token contract address.id(string): Token identifier.type(string): Token standard (erc721orerc1155).
Returns: Promise resolving to
TokenMetadata.Example:
const metadata = await api.getTokenMetadata('0xabc...', '123', 'erc721');
console.log(metadata.name, metadata.description);
Important Implementation Details and Algorithms
The file relies on interfaces to strongly type blockchain entities without implementing logic.
It supports both legacy and EIP-1559 transaction fee models via the
FeesandGasFeesinterfaces.The
APIinterface abstracts asynchronous operations typical in blockchain interactions, promoting separation of concerns and enabling different implementations (e.g., REST API clients or direct node RPC clients).The import of
BaseAccount,BaseTx,BaseTxHistory, andEstimateGasBodyindicates an extension pattern, where base models are augmented with EVM-specific properties.
Interactions with Other System Components
The file imports foundational models (
BaseAccount,BaseTx, etc.) from a common module (../models), suggesting it extends generic blockchain data structures into EVM-specific variants.The
APIinterface is likely implemented by a service layer that communicates with blockchain nodes or an Ethereum API provider.Other parts of the application, such as UI components or business logic modules, consume these interfaces to display blockchain data, handle user transactions, and estimate fees.
This file serves as a shared contract between the API service layer and the rest of the application, ensuring consistent typing and data validation.
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.