Skip to content

Estimate Gas

Estimate transaction fees (gas) for cryptocurrency transfers before executing them. Currently supports EVM chains (Ethereum, BSC, Polygon).

Endpoint

POST /api/v1/wallet/customer/estimate-gas

Authentication

Requires API key authentication via X-API-Key header.

Request Body

FieldTypeRequiredDescription
networkstringYesBlockchain network (ETH, BSC, POL)
currencystringYesCurrency to send (ETH, BNB, POL, USDT, USDC)
from_addressstringYesSender's address
to_addressstringYesRecipient's address
amountstringYesAmount in smallest unit (wei for EVM)

Amount Units

NetworkNativeUnitDecimalsExample
EthereumETHwei181 ETH = 1000000000000000000 wei
BSCBNBwei181 BNB = 1000000000000000000 wei
PolygonPOLwei181 POL = 1000000000000000000 wei
ERC-20USDT/USDCwei18/61 USDT = 1000000 (6 decimals)

Example Request

javascript
// Estimate gas for sending 0.1 ETH
const response = await fetch(
  'https://payments-api-dev-966260606560.europe-west2.run.app/api/v1/wallet/customer/estimate-gas',
  {
    method: 'POST',
    headers: {
      'X-API-Key': 'your-api-key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      network: 'ETH',
      currency: 'ETH',
      from_address: '0x8B3192f2f0f0D7E4F5C3A1B9E2D7A6C5B4D3E2F1',
      to_address: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
      amount: '100000000000000000' // 0.1 ETH in wei
    })
  }
);

const estimate = await response.json();
console.log('Gas Price:', estimate.gas_price);
console.log('Gas Limit:', estimate.gas_limit);
console.log('Total Fee:', estimate.estimated_fee);
python
import requests

# Estimate gas for sending USDT on BSC
response = requests.post(
    'https://payments-api-dev-966260606560.europe-west2.run.app/api/v1/wallet/customer/estimate-gas',
    headers={
        'X-API-Key': 'your-api-key',
        'Content-Type': 'application/json'
    },
    json={
        'network': 'BSC',
        'currency': 'USDT',
        'from_address': '0x8B3192f2f0f0D7E4F5C3A1B9E2D7A6C5B4D3E2F1',
        'to_address': '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
        'amount': '10000000'  # 10 USDT (6 decimals)
    }
)

estimate = response.json()
print(f"Estimated Fee: {estimate['estimated_fee']} {estimate['currency']}")
bash
curl -X POST https://payments-api-dev-966260606560.europe-west2.run.app/api/v1/wallet/customer/estimate-gas \
  -H "X-API-Key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "network": "POL",
    "currency": "USDC",
    "from_address": "0x8B3192f2f0f0D7E4F5C3A1B9E2D7A6C5B4D3E2F1",
    "to_address": "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
    "amount": "50000000"
  }'

Response

Success Response (200 OK)

FieldTypeDescription
networkstringBlockchain network
currencystringCurrency
gas_pricestringCurrent gas price in wei/gwei
gas_limitstringEstimated gas limit
estimated_feestringTotal fee in native currency
estimated_fee_usdstringFee in USD (if available)
json
{
  "network": "ETH",
  "currency": "ETH",
  "gas_price": "30000000000",
  "gas_limit": "21000",
  "estimated_fee": "0.00063",
  "estimated_fee_usd": "1.89"
}

Error Responses

400 Bad Request

json
{
  "error": "invalid amount format"
}

500 Internal Server Error

json
{
  "error": "failed to estimate g as: RPC connection failed"
}

Network Support

NetworkNative TransfersToken Transfers (USDT/USDC)
Ethereum (ETH)
BSC
Polygon (POL)
Bitcoin (BTC)❌ Coming soon❌ N/A
Tron (TRX)❌ Coming soon❌ Coming soon
Solana (SOL)❌ Coming soon❌ Coming soon

Use Cases

Show Fee Before Transfer

Display estimated fee to user before confirming transfer:

javascript
async function showTransferConfirmation(transfer) {
  // Estimate gas
  const estimate = await fetch(
    'https://payments-api-dev-966260606560.europe-west2.run.app/api/v1/wallet/customer/estimate-gas',
    {
      method: 'POST',
      headers: {
        'X-API-Key': process.env.API_KEY,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(transfer)
    }
  );
  
  const data = await estimate.json();
  
  // Show confirmation dialog
  const confirmed = confirm(
    `Send ${formatAmount(transfer.amount)} ${transfer.currency}\n` +
    `To: ${transfer.to_address}\n` +
    `Network Fee: ${data.estimated_fee} ${data.network}\n` +
    `(~$${data.estimated_fee_usd})\n\n` +
    `Confirm transfer?`
  );
  
  return confirmed;
}

function formatAmount(weiAmount) {
  return (parseFloat(weiAmount) / 1e18).toFixed(6);
}

Insufficient Balance Check

Check if user has enough balance to cover amount + gas:

javascript
async function canAffordTransfer(fromAddress, amount, network) {
  // Get balance
  const balance = await getBalance(fromAddress, network);
  
  // Estimate gas
  const estimate = await fetch(
    'https://payments-api-dev-966260606560.europe-west2.run.app/api/v1/wallet/customer/estimate-gas',
    {
      method: 'POST',
      headers: {
        'X-API-Key': process.env.API_KEY,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        network,
        currency: network, // Native currency
        from_address: fromAddress,
        to_address: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
        amount
      })
    }
  );
  
  const data = await estimate.json();
  const totalNeeded = parseFloat(amount) + parseFloat(data.estimated_fee);
  
  return balance >= totalNeeded;
}

Dynamic Fee Selection

Allow users to choose fee tier (fast/normal/slow):

javascript
async function getFeeTiers(transfer) {
  const estimate = await fetch(
    'https://payments-api-dev-966260606560.europe-west2.run.app/api/v1/wallet/customer/estimate-gas',
    {
      method: 'POST',
      headers: {
        'X-API-Key': process.env.API_KEY,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(transfer)
    }
  );
  
  const data = await estimate.json();
  const baseGasPrice = parseFloat(data.gas_price);
  
  return {
    slow: {
      gasPrice: baseGasPrice * 0.8,
      fee: data.estimated_fee * 0.8,
      time: '~10 mins'
    },
    normal: {
      gasPrice: baseGasPrice,
      fee: data.estimated_fee,
      time: '~3 mins'
    },
    fast: {
      gasPrice: baseGasPrice * 1.5,
      fee: data.estimated_fee * 1.5,
      time: '~30 secs'
    }
  };
}

Best Practices

Gas Estimation

  • Always estimate before initiating transfer
  • Add buffer: Add 10-20% buffer to estimated gas limit for safety
  • Check balance: Ensure user has enough for amount + gas
  • Real-time updates: Re-estimate if user delays confirmation (gas prices fluctuate)
  • Display in USD: Show USD equivalent for better UX
  • Network congestion: During high traffic, estimates may be higher

Built with ❤️ by Rach Finance