controller.ts


Overview

[controller.ts](/projects/291/68853) serves as the API controller layer for Ethereum-related endpoints within the application. It extends a generic Ethereum Virtual Machine (EVM) controller to expose RESTful API routes that allow clients to:

The file integrates several internal services and external dependencies to interact with blockchain data providers, gas oracles, and RPC endpoints. It manages configuration via environment variables, instantiates service classes, and binds them to the controller. The controller uses decorators from the `tsoa` framework to define routes, request/response schemas, and API documentation.

This file acts as the bridge between external API consumers and internal blockchain service logic, encapsulating Ethereum-specific behavior and exposing it through a versioned API.


Detailed Explanations

Imports and Environment Setup


Class: Ethereum

Extends the generic `EVM` controller and implements the [BaseAPI](/projects/291/69264) and `API` interfaces to expose Ethereum-specific API endpoints.

Decorators

Methods


estimateGas(body: EstimateGasBody): Promise<GasEstimate>

Estimates the gas required to perform a given Ethereum transaction.


getGasFees(): Promise<GasFees>

Retrieves the current recommended gas fee values for Ethereum transactions, supporting both EIP-1559 and legacy transaction types.


Important Implementation Details and Algorithms


Interaction With Other Parts of the System

The controller is the entry point for external API consumers and delegates complex blockchain logic to underlying service layers and clients.


Usage Examples

**Estimate Gas:**

POST /api/v1/gas/estimate
Content-Type: application/json

{
  "data": "0x",
  "from": "0xYourAddress",
  "to": "0xRecipientAddress",
  "value": "1000000000000000000"
}

**Response:**

{
  "gasLimit": "21000"
}

**Get Gas Fees:**

GET /api/v1/gas/fees

**Response:**

{
  "baseFeePerGas": "1000000000",
  "slow": {
    "gasPrice": "1100000000",
    "maxFeePerGas": "1200000000",
    "maxPriorityFeePerGas": "150000000"
  },
  "average": {
    "gasPrice": "1300000000",
    "maxFeePerGas": "1400000000",
    "maxPriorityFeePerGas": "200000000"
  },
  "fast": {
    "gasPrice": "1500000000",
    "maxFeePerGas": "1600000000",
    "maxPriorityFeePerGas": "250000000"
  }
}

Mermaid Class Diagram

classDiagram
    class Ethereum {
        <<controller>>
        +estimateGas(body: EstimateGasBody): Promise~GasEstimate~
        +getGasFees(): Promise~GasFees~
    }

    class EVM {
        <<base controller>>
        +service: Service
    }

    class Service {
        +estimateGas(body: EstimateGasBody): Promise~GasEstimate~
        +getGasFees(): Promise~GasFees~
    }

    class Blockbook {
        +httpURL: string
        +wsURL: string
        +logger: Logger
        +apiKey: string | undefined
    }

    class GasOracle {
        +logger: Logger
        +client: any
        +coinstack: string
    }

    class Logger {
        +namespace: string[]
        +level: string | undefined
    }

    Ethereum --|> EVM
    EVM --> Service
    Service --> Blockbook
    Service --> GasOracle
    Service --> Logger

Summary

The [controller.ts](/projects/291/68853) file implements a versioned Ethereum API controller extending a generic EVM controller. It exposes two main endpoints for estimating gas costs and fetching current gas fees, leveraging a centralized `Service` class that interacts with blockchain clients and external APIs. The controller ensures proper configuration, error handling, and API documentation using `tsoa`. This design cleanly separates API routing from business logic and external service integration, facilitating maintainability and scalability in the Ethereum coinstack API module.