controller.ts


Overview

[controller.ts](/projects/291/68853) defines the API controller for the Binance Smart Chain (BSC) implementation of an Ethereum Virtual Machine (EVM)-compatible blockchain service. This controller exposes HTTP endpoints for gas estimation and gas fee retrieval, leveraging a shared service layer that interacts with blockchain data providers and RPC clients.

Built with the `tsoa` framework for TypeScript, it integrates various components including:

This file essentially acts as the REST API interface for clients to estimate gas costs and obtain current gas fees on BSC, ensuring consistent and validated responses.


Classes

BNBSmartChain

Extends the generic `EVM` controller to provide BSC-specific implementations of the API methods for gas estimation and gas fee retrieval.

Decorators

Methods


estimateGas(body: EstimateGasBody): Promise<GasEstimate>

Estimates the gas required to execute a provided transaction on BSC.

const estimate = await bnBSCController.estimateGas({
  data: '0x',
  from: '0x0000000000000000000000000000000000000000',
  to: '0xC480394241c76F3993ec5D121ce4F198f7844443',
  value: '1337',
});
console.log(estimate.gasLimit);  // e.g. '21000'

getGasFees(): Promise<GasFees>

Fetches the current recommended gas fee tiers (slow, average, fast) for sending transactions on BSC.

const fees = await bnBSCController.getGasFees();
console.log(fees.fast.gasPrice);  // e.g. '5187055981'

Important Implementation Details


Interactions with Other System Parts


Visual Diagram

classDiagram
    class BNBSmartChain {
        +estimateGas(body: EstimateGasBody): Promise~GasEstimate~
        +getGasFees(): Promise~GasFees~
    }
    class EVM {
        <<abstract>>
        +service: Service
    }
    BNBSmartChain --|> EVM

    class Service {
        -blockbook: Blockbook
        -gasOracle: GasOracle
        -client: PublicClient
        -logger: Logger
        +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
    }

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

Summary

The [controller.ts](/projects/291/68853) file provides a robust, well-structured REST API controller tailored for Binance Smart Chain operations within an EVM-compatible framework. It offers essential transaction gas utilities—estimation and fee retrieval—by composing several services and clients that abstract blockchain interactions. With strong type safety, validation, and documentation via `tsoa`, it forms an integral part of the modular blockchain API system.


End of Documentation