Skip to content

Implement Accurate Gas Estimation for All Contract Operations #338

Description

@llinsss

Problem

Users don't know gas costs before executing contract operations. Need accurate gas estimation for tag registration, deposits, swaps, and withdrawals.

Proposed Solution

Build gas estimation service that simulates contract calls and returns accurate gas estimates with current prices.

Technical Implementation

New Files:

  • contracts/solidity_contract/src/GasEstimator.sol - Gas estimation helper
  • contracts/solidity_contract/test/GasEstimator.t.sol - Test suite
  • backend/services/ContractGasEstimator.js - Gas estimation service
  • backend/controllers/gasEstimationController.js - Estimation endpoints
  • backend/routes/gasEstimation.js - Estimation routes
  • backend/tests/gasEstimation.test.js - Test suite

Modify:

  • backend/contracts/evm.js - Add gas estimation methods
  • backend/contracts/starknet.js - Add Starknet gas estimation
  • backend/services/FeeEstimationService.js - Use contract gas estimates

Gas Estimation Contract

contract GasEstimator {
    function estimateRegisterTag(string memory tag) external returns (uint256) {
        uint256 gasBefore = gasleft();
        // Simulate tag registration
        uint256 gasAfter = gasleft();
        return gasBefore - gasAfter;
    }
    
    function estimateDeposit(string memory tag, uint256 amount) external returns (uint256) {
        // Simulate deposit
    }
    
    function estimateSwap(address token, uint256 amount) external returns (uint256) {
        // Simulate swap
    }
}

API Endpoints

POST /api/gas/estimate/register-tag
Body: { tag: "@newuser", chain: "base" }
Response: {
  gasLimit: 150000,
  gasPrice: "15",
  estimatedCost: {
    eth: "0.00225",
    usd: "5.40"
  }
}

POST /api/gas/estimate/deposit
Body: { tag: "@john", amount: "100", token: "USDC", chain: "base" }

POST /api/gas/estimate/swap
Body: { fromToken: "ETH", toToken: "USDC", amount: "1", chain: "base" }

Acceptance Criteria

  • Gas estimation for all contract operations
  • Simulation-based accurate estimates
  • Current gas price integration
  • USD cost calculation
  • Support for all chains
  • Estimation caching (5 minute TTL)
  • Fallback to static estimates
  • Test coverage > 85%

Priority

High - Important for user experience


Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions