controller.ts


Overview

`controller.ts` provides an API controller implementation for interacting with the Optimism Layer 2 Ethereum network. It extends a generic Ethereum Virtual Machine (EVM) controller to specialize functionality for Optimism, including gas estimation and fetching current gas fee recommendations. This file acts as a bridge between HTTP API requests and underlying blockchain service logic, integrating multiple utility libraries and external services (like Blockbook and Etherscan) to deliver transaction-related data.

Key functionalities include:

This controller is part of a larger modular API server that supports multiple blockchains and coinstacks with shared and specialized components.


Classes and Methods

Class: Optimism

Extends the generic `EVM` controller to implement Optimism-specific API endpoints.

Methods


estimateGas(body: EstimateGasBody): Promise<OptimismGasEstimate>

Estimates the gas cost required for an Optimism transaction, including Layer 2 gas limit and Layer 1 gas limit components.

POST /api/v1/gas/estimate
{
  "data": "0x",
  "from": "0x0000000000000000000000000000000000000000",
  "to": "0x15E03a18349cA885482F59935Af48C5fFbAb8DE1",
  "value": "1337"
}

getGasFees(): Promise<OptimismGasFees>

Fetches the current recommended gas fees from the Optimism Gas Price Oracle.

GET /api/v1/gas/fees

Important Implementation Details


Interaction with Other System Components


Visual Diagram

classDiagram
    class Optimism {
        +estimateGas(body: EstimateGasBody): Promise<OptimismGasEstimate>
        +getGasFees(): Promise<OptimismGasFees>
    }
    class EVM {
        <<abstract>>
        +service: Service
        ...
    }
    class Service {
        +estimateGas(body: EstimateGasBody): Promise<string>
        +getGasFees(): Promise<GasFees>
        ...
    }
    class GasOracle {
        +client: PublicClient
        +logger: Logger
        +coinstack: string
        ...
    }
    class Blockbook {
        +httpURL: string
        +wsURL: string
        +logger: Logger
        +apiKey?: string
        ...
    }

    Optimism --|> EVM
    Optimism --> Service : uses
    Service --> GasOracle : uses
    Service --> Blockbook : uses

Summary

`controller.ts` is the Optimism-specific API controller responsible for exposing endpoints to estimate gas and retrieve gas price data. It leverages a modular service architecture integrating blockchain clients, gas oracles, and external indexers. The code carefully handles environment-based configurations and interacts with Optimism's Gas Price Oracle smart contract to provide accurate gas estimations tailored to Optimism's Layer 2 setup. This file exemplifies a clean separation of concerns and practical use of inheritance to extend a generic EVM API controller for a specialized blockchain network.