controller.ts


Overview

[controller.ts](/projects/291/68853) defines the Arbitrum-specific API controller extending a generic Ethereum Virtual Machine (EVM) controller. It provides HTTP endpoints for estimating gas costs and retrieving current gas fees on the Arbitrum chain. The file integrates multiple services and clients (including Blockbook, a gas oracle, and an RPC client) to handle blockchain data and transactions. It leverages environmental configurations to connect to different RPC and indexer endpoints with necessary authentication.

This controller acts as the interface layer between HTTP API requests and the underlying blockchain data services, exposing essential blockchain utilities tailored for the Arbitrum network.


Detailed Explanation

Imports and Configuration


Class: Arbitrum

Extends the generic `EVM` controller and implements [BaseAPI](/projects/291/69264) and `API` interfaces to provide Arbitrum-specific endpoints. Decorated with `@Route` and `@Tags` for API versioning and grouping.

Methods


estimateGas

async estimateGas(body: EstimateGasBody): Promise<GasEstimate>

getGasFees

async getGasFees(): Promise<GasFees>

Implementation Details and Algorithms


Interaction with Other Parts of the System


Usage Examples

Estimate Gas

**HTTP Request:**

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

{
  "data": "0x",
  "from": "0x0000000000000000000000000000000000000000",
  "to": "0x0000000000000000000000000000000000000000",
  "value": "1337"
}

**HTTP Response:**

{
  "gasLimit": "374764"
}

Get Gas Fees

**HTTP Request:**

GET /api/v1/gas/fees

**HTTP Response:**

{
  "baseFeePerGas": "100000000",
  "slow": {
    "gasPrice": "184334277",
    "maxFeePerGas": "190000001",
    "maxPriorityFeePerGas": "90000001"
  },
  "average": {
    "gasPrice": "187859277",
    "maxFeePerGas": "205000001",
    "maxPriorityFeePerGas": "105000001"
  },
  "fast": {
    "gasPrice": "199001183",
    "maxFeePerGas": "290000001",
    "maxPriorityFeePerGas": "190000001"
  }
}

Mermaid Diagram

classDiagram
    class Arbitrum {
        +estimateGas(body: EstimateGasBody): Promise<GasEstimate>
        +getGasFees(): Promise<GasFees>
    }
    class EVM {
        <<abstract>>
        +service: Service
    }
    class Service {
        -blockbook: Blockbook
        -gasOracle: GasOracle
        -explorerApiUrl: URL
        -client: PublicClient
        -logger: Logger
        -rpcUrl: string
        -rpcApiKey: string | undefined
        +estimateGas(body: EstimateGasBody): Promise<GasEstimate>
        +getGasFees(): Promise<GasFees>
    }
    class Blockbook {
        -httpURL: string
        -wsURL: string
        -logger: Logger
        -apiKey: string | undefined
    }
    class GasOracle {
        -logger: Logger
        -client: PublicClient
        -coinstack: string
    }
    class Logger {
        -namespace: string[]
        -level: string
    }
    class PublicClient {
        -chain: object
        -transport: object
    }

    Arbitrum --|> EVM
    EVM o-- Service : uses
    Service *-- Blockbook : contains
    Service *-- GasOracle : contains
    Service *-- PublicClient : contains
    Service *-- Logger : contains

Summary

The [controller.ts](/projects/291/68853) file defines the Arbitrum API controller exposing endpoints to estimate transaction gas and retrieve gas fee recommendations. It is built on top of a generic EVM controller and leverages a robust service layer integrating blockchain clients and gas oracles. The controller ensures proper configuration via environment variables and supports different indexer and RPC providers. It forms a critical part of the backend API enabling clients to interact with the Arbitrum blockchain efficiently and reliably.