web3dart_get_fee_data 1.1.0

SDKdartflutter
Platformandroidioswindowslinuxmacosweb

A Dart package for getting Ethereum fee data, including support for EIP-1559 transactions. Provides an easy-to-use API similar to ethers.js getFeeData().

web3dart_get_fee_data

A Dart package for retrieving Ethereum fee data with full EIP-1559 support. Provides an API similar to ethers.js getFeeData().

pub package License: MIT

Features

  • EIP-1559 Support: Get maxFeePerGas and maxPriorityFeePerGas for modern transactions
  • Legacy Compatibility: Automatic fallback to gasPrice for older networks
  • Custom Error Handling: Optional onError callback for custom fallback strategies

Installation

Add this package to your pubspec.yaml:

dependencies:
  web3dart_get_fee_data: ^1.1.0

Then run:

dart pub get

Usage

import 'package:web3dart/web3dart.dart';
import 'package:web3dart_get_fee_data/web3dart_get_fee_data.dart';
import 'package:http/http.dart';

void main() async {
  final httpClient = Client();
  final ethClient = Web3Client('https://eth.llamarpc.com', httpClient);

  try {
    final feeData = await getFeeData(ethClient);

    if (feeData.maxFeePerGas != null) {
      print('EIP-1559: ${feeData.maxFeePerGas} wei');
    } else {
      print('Legacy: ${feeData.gasPrice} wei');
    }
  } finally {
    ethClient.dispose();
    httpClient.close();
  }
}

Advanced Usage

Custom Error Handling

Use the onError callback for custom fallback strategies:

final feeData = await getFeeData(
  client,
  onError: (context) {
    print('Error in ${context.operation}: ${context.error}');
    print('Stack trace: ${context.stackTrace}');
    
    // Custom fallback strategies
    if (context.operation == 'eth_maxPriorityFeePerGas') {
      // Use zero priority fee for networks like Arbitrum
      return BigInt.zero;
    }
    
    // Use default fallback
    return context.fallbackValue;
  }
);

API Reference

getFeeData(Web3Client client, {onError})

Parameters:

  • client: A connected Web3Client instance
  • onError: Optional callback for custom error handling

Returns: Future<FeeData> with current network fees

Example:

Future<FeeData> getFeeData(
  Web3Client client, {
  BigInt? Function(ErrorContext context)? onError,
})

FeeData

PropertyTypeDescription
gasPriceBigInt?Legacy gas price in wei (always provided)
maxFeePerGasBigInt?EIP-1559 max fee per gas (when supported)
maxPriorityFeePerGasBigInt?EIP-1559 priority fee (when supported)

ErrorContext

Provides comprehensive error information to onError callbacks:

PropertyTypeDescription
operationStringThe operation that failed (e.g., 'eth_maxPriorityFeePerGas')
errordynamicThe original error that occurred
stackTraceStackTraceStack trace when the error occurred
fallbackValueBigInt?Default fallback value that would be used
gasPriceBigIntCurrent gas price in wei
baseFeePerGasBigInt?Current base fee per gas in wei (EIP-1559 networks)

License

MIT License - see LICENSE file.