Skip to main content
Estimating transaction costs on Paxeer Network is exactly the same as on Ethereum. You can use the same tools and methods you’re already familiar with.

Overview

It’s important to properly estimate the cost of a transaction on Paxeer Network before submitting it. This guide shows you how to estimate the execution gas fee for your transactions.

Execution Gas Fee

A transaction’s execution gas fee is equal to the amount of gas used multiplied by the gas price attached to the transaction.
executionGasFee = gasUsed × (baseFee + priorityFee)

Estimation Steps

1

Estimate the Gas Limit

Use eth_estimateGas to estimate how much gas your transaction will consume:
import { ethers } from 'ethers';

const provider = new ethers.JsonRpcProvider('https://public-rpc.paxeer.app/rpc');

async function estimateGas(tx) {
  const gasEstimate = await provider.estimateGas(tx);
  console.log('Estimated gas:', gasEstimate.toString());
  return gasEstimate;
}

// Example: ETH transfer
const tx = {
  to: '0x...',
  value: ethers.parseEther('1.0'),
};

const gas = await estimateGas(tx);
Paxeer Network is EVM equivalent, so gas estimates will match Ethereum exactly. A transaction that uses 100,000 gas on Ethereum will use 100,000 gas on Paxeer Network.
2

Get Current Fee Data

Retrieve current base fee and recommended priority fee:
async function getFeeData() {
  const feeData = await provider.getFeeData();
  
  return {
    maxFeePerGas: feeData.maxFeePerGas,
    maxPriorityFeePerGas: feeData.maxPriorityFeePerGas,
    gasPrice: feeData.gasPrice,
  };
}

const fees = await getFeeData();
console.log('Max fee per gas:', ethers.formatUnits(fees.maxFeePerGas, 'gwei'), 'gwei');
console.log('Priority fee:', ethers.formatUnits(fees.maxPriorityFeePerGas, 'gwei'), 'gwei');
3

Calculate Total Cost

Multiply gas estimate by gas price:
async function estimateTotalCost(tx) {
  // Get gas estimate
  const gasEstimate = await provider.estimateGas(tx);
  
  // Get fee data
  const feeData = await provider.getFeeData();
  
  // Calculate cost with max fee
  const maxCost = gasEstimate * feeData.maxFeePerGas;
  
  // Calculate likely cost with current base fee
  const likelyCost = gasEstimate * (feeData.maxFeePerGas - feeData.maxPriorityFeePerGas);
  
  return {
    gasEstimate: gasEstimate.toString(),
    maxFeePerGas: ethers.formatUnits(feeData.maxFeePerGas, 'gwei'),
    maxCostPAX: ethers.formatEther(maxCost),
    likelyCostPAX: ethers.formatEther(likelyCost),
  };
}

const estimate = await estimateTotalCost(tx);
console.log('Cost estimate:', estimate);

Complete Example

Here’s a complete example of estimating costs for a token transfer:
CompleteEstimate.ts
import { ethers } from 'ethers';

const provider = new ethers.JsonRpcProvider('https://public-rpc.paxeer.app/rpc');

const ERC20_ABI = [
  'function transfer(address to, uint256 amount) returns (bool)',
];

async function estimateTokenTransfer(
  tokenAddress: string,
  to: string,
  amount: string
) {
  try {
    // Create contract instance
    const token = new ethers.Contract(tokenAddress, ERC20_ABI, provider);
    
    // Estimate gas for the transfer
    const gasEstimate = await token.transfer.estimateGas(to, amount);
    
    // Get current fee data
    const feeData = await provider.getFeeData();
    
    // Add 20% buffer to gas estimate
    const gasLimit = gasEstimate * 120n / 100n;
    
    // Calculate max cost
    const maxCost = gasLimit * feeData.maxFeePerGas;
    
    // Calculate likely cost (using current base fee)
    const currentBaseFee = feeData.maxFeePerGas - feeData.maxPriorityFeePerGas;
    const priorityFee = feeData.maxPriorityFeePerGas;
    const likelyCost = gasLimit * (currentBaseFee + priorityFee);
    
    return {
      gasEstimate: gasEstimate.toString(),
      gasLimit: gasLimit.toString(),
      baseFee: ethers.formatUnits(currentBaseFee, 'gwei') + ' gwei',
      priorityFee: ethers.formatUnits(priorityFee, 'gwei') + ' gwei',
      maxFeePerGas: ethers.formatUnits(feeData.maxFeePerGas, 'gwei') + ' gwei',
      maxCostPAX: ethers.formatEther(maxCost),
      likelyCostPAX: ethers.formatEther(likelyCost),
    };
  } catch (error) {
    console.error('Estimation failed:', error);
    throw error;
  }
}

// Usage
const estimate = await estimateTokenTransfer(
  '0xTokenAddress',
  '0xRecipient',
  ethers.parseUnits('100', 18).toString()
);

console.log('Transfer Cost Estimate:', estimate);

React Hook for Cost Estimation

useEstimateCost.ts
import { useState, useEffect } from 'react';
import { usePublicClient } from 'wagmi';
import { formatEther } from 'viem';

export function useEstimateCost(tx: any) {
  const [estimate, setEstimate] = useState(null);
  const [loading, setLoading] = useState(true);
  const client = usePublicClient();

  useEffect(() => {
    if (!tx || !client) return;

    async function estimateCost() {
      try {
        const [gasEstimate, gasPrice] = await Promise.all([
          client.estimateGas(tx),
          client.getGasPrice(),
        ]);

        const cost = gasEstimate * gasPrice;

        setEstimate({
          gas: gasEstimate.toString(),
          gasPrice: gasPrice.toString(),
          costWei: cost.toString(),
          costPAX: formatEther(cost),
        });
      } catch (error) {
        console.error('Cost estimation failed:', error);
      } finally {
        setLoading(false);
      }
    }

    estimateCost();
  }, [tx, client]);

  return { estimate, loading };
}

Gas Estimation Best Practices

Gas estimates can be slightly inaccurate. Add a 10-20% buffer:
const gasEstimate = await provider.estimateGas(tx);
const gasLimit = gasEstimate * 120n / 100n; // 20% buffer
eth_estimateGas will revert if the transaction would fail:
try {
  const gasEstimate = await provider.estimateGas(tx);
} catch (error) {
  // Transaction would fail - check error message
  console.error('Transaction would revert:', error.message);
}
Gas estimates assume current blockchain state. If state changes before your transaction is mined, actual gas usage may differ:
// Get estimate close to submission time
const gasEstimate = await provider.estimateGas(tx);

// Send transaction immediately
const txResponse = await signer.sendTransaction({
  ...tx,
  gasLimit: gasEstimate * 120n / 100n,
});
Test gas estimates under various conditions:
  • Different account balances
  • Different contract states
  • Edge cases and error scenarios

Checking Historical Fees

Analyze historical fee data to predict future costs:
async function analyzeFeeTrends() {
  // Get fee history for last 100 blocks
  const feeHistory = await provider.send('eth_feeHistory', [
    '0x64', // 100 blocks in hex
    'latest',
    [25, 50, 75] // 25th, 50th, 75th percentile
  ]);

  const baseFees = feeHistory.baseFeePerGas.map(fee => 
    ethers.formatUnits(fee, 'gwei')
  );

  console.log('Average base fee:', 
    baseFees.reduce((a, b) => parseFloat(a) + parseFloat(b)) / baseFees.length
  );
  
  return feeHistory;
}

Tools & Resources

Next Steps