controller.ts


Overview

`controller.ts` defines an API controller for interacting with the Avalanche blockchain network. It extends a generic Ethereum Virtual Machine (EVM) controller to provide Avalanche-specific functionality, such as estimating gas costs for transactions and retrieving current recommended gas fees. This controller is implemented using the [tsoa](https://tsoa-community.github.io/docs/) framework to generate OpenAPI-compliant REST endpoints.

Key responsibilities:

This file acts as a bridge between HTTP API requests and the Avalanche blockchain service layer, enabling clients to programmatically query gas-related data relevant to Avalanche transactions.


Classes and Functions

Class: Avalanche

`Avalanche` is the primary controller class exposing Avalanche-specific endpoints for gas estimation and fee retrieval. It inherits common EVM functionality and overrides or extends it with Avalanche-focused implementations.


Methods

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

**Description:** Estimates the gas cost for a given transaction on the Avalanche network. It accepts transaction details such as sender, recipient, data payload, and value. The method delegates the estimation process to the injected `service` instance, which handles communication with Avalanche nodes and gas oracles.

**Usage Example:**

import { Avalanche } from './controller'
const avalancheController = new Avalanche()

const gasEstimate = await avalancheController.estimateGas({
  from: '0x0000000000000000000000000000000000000000',
  to: '0x9D1170D30944F2E30664Be502aC57F6096fB5366',
  value: '1337',
  data: '0x',
})
console.log(gasEstimate.gasLimit) // e.g. "21000"

getGasFees
async getGasFees(): Promise<GasFees>

**Description:** Fetches the current recommended gas fees for transactions on Avalanche. This includes base fees and suggested values for slow, average, and fast transaction speeds. The response supports both EIP-1559 and legacy transaction fee models.

**Usage Example:**

const fees = await avalancheController.getGasFees()
console.log(fees.average.maxFeePerGas) // e.g. "32489417391"

Important Implementation Details

Environment Configuration and Validation

The file reads multiple critical environment variables at startup:

If any required environment variable is missing, the module throws an error immediately to prevent misconfiguration.


Client and Service Initialization

`service` is assigned to the static property `EVM.service` so that all inherited EVM controllers share the same service implementation.


Inheritance from EVM Controller

`Avalanche` extends the generic `EVM` controller, inheriting common blockchain API endpoints and logic. This design promotes code reuse across EVM-compatible chains by:


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class Avalanche {
        +estimateGas(body: EstimateGasBody): Promise<GasEstimate>
        +getGasFees(): Promise<GasFees>
    }
    Avalanche --|> EVM

    class EVM {
        <<abstract>>
        +service: Service
    }

    class Service {
        -blockbook: Blockbook
        -gasOracle: GasOracle
        -explorerApiUrl: URL
        -client: PublicClient
        -logger: Logger
        -rpcUrl: string
        -rpcApiKey: string
        +estimateGas(body: EstimateGasBody): Promise<GasEstimate>
        +getGasFees(): Promise<GasFees>
    }

    class Blockbook {
        -httpURL: string
        -wsURL: string
        -apiKey: string
        -logger: Logger
    }

    class GasOracle {
        -logger: Logger
        -client: PublicClient
        -coinstack: string
        +getGasFees(): Promise<GasFees>
    }

    Avalanche ..> Service : uses
    Service *-- Blockbook : composes
    Service *-- GasOracle : composes

Summary

`controller.ts` is the Avalanche-specific API controller extending a common EVM controller. It initializes necessary clients and services with proper configuration, exposes REST endpoints to estimate gas and retrieve gas fees, and delegates core logic to a shared service layer. This modular and environment-driven design enables scalable multi-chain support with clean separation between API routing, service logic, and blockchain communication.