controller.ts


Overview

`controller.ts` serves as the API controller layer for interacting with the Gnosis chain, specifically tailored to the Ethereum-compatible environment (EVM). It exposes RESTful endpoints for gas-related operations such as estimating gas costs for transactions and retrieving recommended gas fees. This file leverages a shared `Service` instance that abstracts the underlying blockchain data sources and logic, including interaction with Blockbook, Etherscan, and a gas oracle.

The controller extends a base `EVM` controller, inheriting common Ethereum JSON-RPC and transaction-related functionalities, while specializing them for the Gnosis network. It uses the `tsoa` framework to define routes, request/response models, validation, and documentation annotations, enabling automatic OpenAPI spec generation.


Detailed Explanation

Imports and Environment Configuration

Logger Initialization

export const logger = new Logger({
  namespace: ['unchained', 'coinstacks', 'gnosis', 'api'],
  level: process.env.LOG_LEVEL,
})

Blockchain Client and Service Instances

export const service = new Service({
  blockbook,
  gasOracle,
  explorerApiUrl: new URL(`https://api.etherscan.io/v2/api?chainid=100&apikey=${ETHERSCAN_API_KEY}`),
  client,
  logger,
  rpcUrl: RPC_URL,
})

Class Gnosis

@Route('api/v1')
@Tags('v1')
export class Gnosis extends EVM implements BaseAPI, API { ... }

Methods

estimateGas

@Post('/gas/estimate')
async estimateGas(@Body() body: EstimateGasBody): Promise<GasEstimate>
{
  "data": "0x",
  "from": "0x0000000000000000000000000000000000000000",
  "to": "0xBA7A4c521DfCD18fEB7cdA4B7CA182d739B7A6a0",
  "value": "1337"
}

getGasFees

@Get('/gas/fees')
async getGasFees(): Promise<GasFees>
{
  "baseFeePerGas": "7",
  "slow": {
    "gasPrice": "1967447305",
    "maxFeePerGas": "1554947308",
    "maxPriorityFeePerGas": "1554947301"
  },
  "average": {
    "gasPrice": "2545383854",
    "maxFeePerGas": "2055458056",
    "maxPriorityFeePerGas": "2055458049"
  },
  "fast": {
    "gasPrice": "2927445852",
    "maxFeePerGas": "2245520056",
    "maxPriorityFeePerGas": "2245520049"
  }
}

Important Implementation Details


Interaction with Other Parts of the System


Usage Examples

Estimating Gas

curl -X POST https://api.yourdomain.com/api/v1/gas/estimate \
  -H "Content-Type: application/json" \
  -d '{
        "data": "0x",
        "from": "0x0000000000000000000000000000000000000000",
        "to": "0xBA7A4c521DfCD18fEB7cdA4B7CA182d739B7A6a0",
        "value": "1337"
      }'

**Response:**

{
  "gasLimit": "21000"
}

Getting Gas Fees

curl https://api.yourdomain.com/api/v1/gas/fees

**Response:**

{
  "baseFeePerGas": "7",
  "slow": {
    "gasPrice": "1967447305",
    "maxFeePerGas": "1554947308",
    "maxPriorityFeePerGas": "1554947301"
  },
  "average": {
    "gasPrice": "2545383854",
    "maxFeePerGas": "2055458056",
    "maxPriorityFeePerGas": "2055458049"
  },
  "fast": {
    "gasPrice": "2927445852",
    "maxFeePerGas": "2245520056",
    "maxPriorityFeePerGas": "2245520049"
  }
}

Mermaid Class Diagram

classDiagram
    class Gnosis {
        +estimateGas(body: EstimateGasBody): Promise~GasEstimate~
        +getGasFees(): Promise~GasFees~
    }
    class EVM {
        <<abstract>>
        +service: Service
    }
    class Service {
        +estimateGas(body: EstimateGasBody): Promise~GasEstimate~
        +getGasFees(): Promise~GasFees~
    }
    class Blockbook {
        +httpURL: string
        +wsURL: string
    }
    class GasOracle {
        +getGasFees(): Promise~GasFees~
    }
    class Logger {
        +namespace: string[]
        +level: string
    }
    class Client {
        +chain: any
        +transport: any
    }

    Gnosis --|> EVM
    EVM --> Service : uses
    Service --> Blockbook
    Service --> GasOracle
    Service --> Client
    Service --> Logger

Summary

`controller.ts` is a specialized API controller providing gas estimation and fee retrieval endpoints for the Gnosis network, built on an Ethereum-compatible stack. It integrates multiple blockchain data sources and services behind a clean RESTful interface, leveraging inheritance and modular service design. The file ensures robustness through environment validation and provides well-documented, typed endpoints suitable for automated API generation and client consumption.