Builders Guide
Application Development
Transactions
Fee Estimation

Estimating Transaction Fees

⚠️

This guide focuses on Testnet3. Gas estimation methods may vary on mainnet based on updated network conditions.

Understanding Gas Estimation on GOAT Network

On the GOAT Network, transaction fees are calculated using EIP-1559 standards. This model dynamically adjusts gas prices based on network demand, ensuring fair and efficient fee estimation. The transaction fee consists of:

  1. Base Fee: Determined by EIP-1559 dynamically.
  2. Priority Fee (Tip): Optional, used to incentivize faster transaction processing.

Steps for Estimating Fees

1. Estimating Gas Usage

  • Leverage Gas Estimation Libraries:

    • Use web3.js or ethers.js to estimate the gas required for transaction execution.

    • Example with web3.js:

      const estimateGasUsage = async (transaction) => {
        const gasEstimate = await web3.eth.estimateGas(transaction);
        return gasEstimate;
      };
  • Batch Operations with EIP-1559:

    • Batch transactions reduce gas usage per operation. Use this for high-volume transfers or contract interactions.

2. Retrieving Gas Prices

  • Dynamic Gas Price Retrieval:

    • Retrieve gas prices (base fee and priority fee) dynamically using the latest network data.

      const getBaseFee = async () => {
        // Get the latest block
        const latestBlock = await web3.eth.getBlock('latest');
       
        // Get the base fee (for EIP-1559 transactions)
        const baseFeePerGas = web3.utils.fromWei(latestBlock.baseFeePerGas.toString(), 'wei')
        return baseFeePerGas;
      };
  • Include Priority Fee:

    • Use an additional priority fee for faster confirmations:

      async function getMaxPriorityFee() {
        // Call the eth_maxPriorityFeePerGas method
        const maxPriorityFeeWei = await web3.eth.getMaxPriorityFeePerGas();
        return maxPriorityFeeWei
      }

3. Calculating the Total Fee

  • EIP-1559 Fee Formula:

    const estimateTransactionFee = async (transaction) => {
      const gasEstimate = await estimateGasUsage(transaction);
      const baseFeePerGas = await getBaseFee();
     
      const priorityFee = await getMaxPriorityFee();
     
      // the best practice is to use baseFeePerGas * 2 + priorityFeePerGas
      const baseFee = web3.utils.toBN(baseFeePerGas).mul(web3.utils.toBN(2));
      const maxFeePerGas = web3.utils.toBN(baseFee).add(web3.utils.toBN(priorityFee));
     
      const totalFee = web3.utils.toBN(gasEstimate).mul(maxFeePerGas);
      return totalFee;
    };

4. Example Fee Estimation

Single Token Transfer

const gasEstimate = await web3.eth.estimateGas({
  from: senderAddress,
  to: tokenContractAddress,
  data: tokenContract.methods.transfer(recipientAddress, amount).encodeABI(),
});
 
const transaction = {
  from: senderAddress,
  to: tokenContractAddress,
  data: tokenContract.methods.transfer(recipientAddress, amount).encodeABI(),
  gasLimit: gasEstimate,
  maxPriorityFeePerGas: web3.utils.toWei('100000', 'wei'), // Minimal priority fee
};
 
const totalFee = await estimateTransactionFee(transaction);
console.log(`Estimated Fee: ${web3.utils.fromWei(totalFee, 'ether')} ETH`);

Batch Operation Example (EIP-1559 Optimized)

const gasEstimate = await web3.eth.estimateGas({
  from: senderAddress,
  to: tokenContractAddress,
  data: tokenContract.methods.safeBatchTransferFrom(
    senderAddress,
    recipientAddress,
    tokenIds,
    amounts,
    "0x"
  ).encodeABI(),
});
 
const transaction = {
  from: senderAddress,
  to: tokenContractAddress,
  data: tokenContract.methods.safeBatchTransferFrom(
    senderAddress,
    recipientAddress,
    tokenIds,
    amounts,
    "0x"
  ).encodeABI(),
  gasLimit: gasEstimate,
  maxPriorityFeePerGas: web3.utils.toWei('100000', 'wei'), // Minimal priority fee
};
 
const totalFee = await estimateTransactionFee(transaction);
console.log(`Estimated Batch Fee: ${web3.utils.fromWei(totalFee, 'ether')} ETH`);

Best Practices

  1. Accurate Gas Estimation:

    • Use tools like ethers.js or web3.js for precise gas predictions.
  2. Monitor Base Fee Trends:

  3. Optimize Batch Transactions:

    • Leverage EIP-1559's efficiency for batch operations to lower costs.
  4. Test on Testnet3:

    • Validate your fee estimation logic and transactions on Testnet3 before mainnet deployment.

Additional Resources