controller.ts


Overview

The [controller.ts](/projects/291/68853) file implements a REST API controller for the Base blockchain network, built atop an Ethereum Virtual Machine (EVM) compatible architecture. It extends a generic `EVM` controller to provide Base-specific endpoints focused on gas estimation and gas fee retrieval. The controller integrates with various services, such as a Blockbook indexer, a Gas Oracle, and an RPC client, to calculate accurate gas costs and fees for transactions on Base.

Key functionalities provided by this controller include:

This controller serves as a critical interface between clients and the underlying blockchain data/services, exposing Base-specific gas-related information via RESTful HTTP endpoints under the `/api/v1` route.


Classes and Methods

class Base

The `Base` class is a REST API controller decorated with [tsoa](/projects/291/68848) annotations to automatically generate OpenAPI specifications and route handlers. It extends the generic `EVM` controller class and implements both [BaseAPI](/projects/291/69264) and `API` interfaces.

It exposes two primary endpoints:


1. estimateGas(body: EstimateGasBody): Promise<BaseGasEstimate>

Estimate the gas cost for a transaction on the Base network.

**Implementation Details:**

**Usage Example:**

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

Response:

{
  "gasLimit": "21000",
  "l1GasLimit": "1664"
}

2. getGasFees(): Promise<BaseGasFees>

Retrieve the current recommended gas fees for transactions on Base.

**Implementation Details:**

**Usage Example:**

GET /api/v1/gas/fees

Response:

{
  "l1GasPrice": "5349789102",
  "baseFeePerGas": "51497198",
  "slow": {
    "gasPrice": "51815704",
    "maxFeePerGas": "51947082",
    "maxPriorityFeePerGas": "428650"
  },
  "average": {
    "gasPrice": "53149928",
    "maxFeePerGas": "52728076",
    "maxPriorityFeePerGas": "1209644"
  },
  "fast": {
    "gasPrice": "105353129",
    "maxFeePerGas": "145357641",
    "maxPriorityFeePerGas": "93839209"
  }
}

Important Implementation Details

Environment Variables and Configuration

The file requires several environment variables to be set for operation:

The controller throws errors at startup if any required environment variables are missing.

Service and Client Initialization

Gas Price Oracle Contract

Gas Limit Serialization

Layer 1 Gas Price Calculation


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class Base {
        +estimateGas(body: EstimateGasBody): Promise<BaseGasEstimate>
        +getGasFees(): Promise<BaseGasFees>
    }
    class EVM {
        <<abstract>>
        +service: Service
    }
    class Service {
        +estimateGas(body: EstimateGasBody): Promise<string>
        +getGasFees(): Promise<object>
    }
    class GasOracle {
        +constructor(logger, client, coinstack)
    }
    class Blockbook {
        +constructor({httpURL, wsURL, logger})
    }
    class PublicClient {
        +getTransactionCount(params): Promise<number>
    }
    class Contract {
        +read.getL1GasUsed(params): Promise<BigNumber>
        +read.isEcotone(): Promise<boolean>
        +read.l1BaseFee(): Promise<BigNumber>
        +read.baseFeeScalar(): Promise<BigNumber>
        +read.blobBaseFeeScalar(): Promise<BigNumber>
        +read.decimals(): Promise<BigNumber>
        +read.scalar(): Promise<BigNumber>
    }

    Base --|> EVM
    Base --> Service : uses
    Service --> GasOracle : uses
    Service --> Blockbook : uses
    Service --> PublicClient : uses
    Base --> Contract : uses (Gas Price Oracle)

Summary

The [controller.ts](/projects/291/68853) file provides the Base blockchain-specific REST API controller extending a generic EVM controller. It exposes endpoints to estimate transaction gas costs and fetch current gas fee recommendations, integrating tightly with blockchain RPC, indexers, and smart contracts. Its design leverages modular services and contracts to deliver accurate Layer 1 and Layer 2 gas metrics, crucial for clients building on the Base network.

The file plays a vital role in bridging blockchain infrastructure components and client applications, encapsulating complex gas fee logic behind simple HTTP endpoints.