models.ts
Overview
The [models.ts](/projects/291/68901) file defines TypeScript interfaces that extend existing gas estimation and fee models to accommodate the specifics of the Optimism Layer 2 (L2) scaling solution for Ethereum. Specifically, it introduces additional gas-related fields relevant to transactions that involve both Layer 1 (L1) and Layer 2 layers in the Optimism network.
This file serves as a type definition module to ensure that gas estimation and fee data structures used throughout the application accurately reflect the dual-layer nature of Optimism transactions. It enables type-safe handling of these enhanced gas models in the application’s logic, UI, and API interactions.
Detailed Explanations
Interfaces
1. OptimismGasEstimate
export interface OptimismGasEstimate extends GasEstimate {
l1GasLimit: string
}
Description:
Represents an estimated gas cost of a transaction that includes gas usage on both the Ethereum mainnet Layer 1 and the Optimism Layer 2.Extends:
GasEstimate— imported from a common EVM API module, which typically contains standard gas estimation fields such asgasLimitandgasPrice.Properties:
l1GasLimit: string
The gas limit specifically for the Layer 1 portion of the transaction, expressed as a string to handle large numeric values safely.
Usage Example:
const estimate: OptimismGasEstimate = {
gasLimit: "21000",
gasPrice: "1000000000",
l1GasLimit: "5000"
};
This interface would typically be used when estimating the total gas required for a transaction that will be executed on Optimism, including the L1 gas needed for finalization or rollup proofs.
2. OptimismGasFees
export interface OptimismGasFees extends GasFees {
l1GasPrice: string
}
Description:
Represents the recommended fees for a transaction on both L1 and L2 in the Optimism network, extending the standard gas fee structure.Extends:
GasFees— imported from the common EVM API module, which generally includes fee-related fields such asmaxFeePerGasandmaxPriorityFeePerGas.Properties:
l1GasPrice: string
The current gas price for Layer 1, expressed as a string. This is important since fees on L1 affect the total cost of submitting transactions that eventually settle on the Ethereum mainnet.
Usage Example:
const fees: OptimismGasFees = {
maxFeePerGas: "2000000000",
maxPriorityFeePerGas: "1000000000",
l1GasPrice: "1500000000"
};
This interface is helpful for calculating the total fee to be paid for a transaction that involves both layers, ensuring the user or system accounts for L1 cost in addition to L2 fees.
Important Implementation Details
Extending Existing Models:
By extendingGasEstimateandGasFees, the file leverages existing standardized models for Ethereum gas while adding Optimism-specific properties. This approach promotes code reuse and consistency.String Types for Gas Values:
Gas limits and prices are expressed as strings rather than numbers to safely handle very large values that exceed JavaScript's safe integer range, avoiding precision loss.No Methods or Logic:
This file only contains type definitions (interfaces) without any functions or classes. It is purely declarative and meant for type safety in TypeScript.
Interaction with Other Parts of the System
Imports from Common API Module:
TheGasEstimateandGasFeesinterfaces are imported from a shared API module located at'../../../common/api/src/evm'. This implies these base models are shared across different parts of the application, possibly including backend services, frontend clients, or middleware.Usage in Transaction Gas Estimation and Fee Calculation:
Modules responsible for estimating transaction costs on Optimism will use these interfaces to type their data structures. This can include services that:Fetch gas estimates from RPC nodes or APIs,
Calculate total transaction fees,
Display gas costs in the UI,
Validate transaction parameters before submission.
Layered Architecture Compliance:
This file fits into the data modeling layer of the application’s architecture, defining domain-specific types that enable seamless integration between Ethereum Layer 1 and Optimism Layer 2 transaction handling.
Mermaid Class Diagram
classDiagram
class GasEstimate {
<<imported>>
+gasLimit: string
+gasPrice: string
...
}
class GasFees {
<<imported>>
+maxFeePerGas: string
+maxPriorityFeePerGas: string
...
}
class OptimismGasEstimate {
+l1GasLimit: string
}
OptimismGasEstimate --|> GasEstimate
class OptimismGasFees {
+l1GasPrice: string
}
OptimismGasFees --|> GasFees
Summary
models.ts defines two interfaces,
OptimismGasEstimateandOptimismGasFees, extending Ethereum gas estimation and fee models to include Layer 1 gas parameters specific to the Optimism network.These interfaces support accurate, type-safe representation of gas data for transactions involving both L1 and L2, crucial for correct fee calculation and transaction submission workflows.
The file imports base models from a common API and adds properties as strings to handle large numerical values.
It plays a foundational role in the application’s transaction gas estimation and fee calculation modules, ensuring seamless interaction between Ethereum mainnet and Optimism Layer 2 logic.