web3dart_get_fee_data 1.0.0
web3dart_get_fee_data: ^1.0.0 copied to clipboard
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 comprehensive Dart package for retrieving Ethereum fee data, including full support for EIP-1559 transactions. This package provides an easy-to-use API that mirrors ethers.js getFeeData()
functionality.
Features #
- ✅ EIP-1559 Support: Get
maxFeePerGas
andmaxPriorityFeePerGas
for modern transactions - ✅ Legacy Compatibility: Automatic fallback to traditional
gasPrice
for older networks - ✅ Network Detection: Seamlessly handles both EIP-1559 and legacy Ethereum networks
- ✅ ethers.js Compatible: API designed to match ethers.js behavior for easy migration
- ✅ Comprehensive Testing: Well-tested with real network integration tests
- ✅ Type Safe: Full Dart type safety with nullable types for optional fee parameters
Installation #
Add this package to your pubspec.yaml
:
dependencies:
web3dart_get_fee_data: ^1.0.0
Then run:
dart pub get
Quick Start #
import 'package:web3dart/web3dart.dart';
import 'package:web3dart_get_fee_data/web3dart_get_fee_data.dart';
import 'package:http/http.dart';
void main() async {
// Create Web3Client instance
final httpClient = Client();
final ethClient = Web3Client('https://eth.llamarpc.com', httpClient);
try {
// Get current fee data
final feeData = await getFeeData(ethClient);
// Check if network supports EIP-1559
if (feeData.maxFeePerGas != null) {
print('✅ EIP-1559 Network Detected');
print('Max Fee Per Gas: ${feeData.maxFeePerGas} wei');
print('Max Priority Fee: ${feeData.maxPriorityFeePerGas} wei');
} else {
print('📜 Legacy Network - Using Gas Price');
print('Gas Price: ${feeData.gasPrice} wei');
}
} catch (e) {
print('❌ Error: $e');
} finally {
ethClient.dispose();
}
}
Usage Examples #
Basic Fee Data Retrieval #
import 'package:web3dart_get_fee_data/web3dart_get_fee_data.dart';
final feeData = await getFeeData(ethClient);
print('Current fee data: $feeData');
EIP-1559 Transaction Estimation #
final feeData = await getFeeData(ethClient);
if (feeData.maxFeePerGas != null && feeData.maxPriorityFeePerGas != null) {
// Use EIP-1559 transaction
final transaction = Transaction.callContract(
contract: deployedContract,
function: function,
parameters: parameters,
maxFeePerGas: EtherAmount.fromBigInt(EtherUnit.wei, feeData.maxFeePerGas!),
maxPriorityFeePerGas: EtherAmount.fromBigInt(EtherUnit.wei, feeData.maxPriorityFeePerGas!),
);
} else {
// Fallback to legacy transaction
final transaction = Transaction.callContract(
contract: deployedContract,
function: function,
parameters: parameters,
gasPrice: EtherAmount.fromBigInt(EtherUnit.wei, feeData.gasPrice!),
);
}
API Reference #
getFeeData(Web3Client client)
#
Retrieves current fee data from an Ethereum network.
Parameters:
client
- A connectedWeb3Client
instance
Returns:
Future<FeeData>
- Fee data object containing current network fees
FeeData
Class #
Represents Ethereum fee data for both legacy and EIP-1559 transactions.
Properties:
Property | Type | Description |
---|---|---|
gasPrice |
BigInt? |
Legacy gas price in wei (always provided) |
maxFeePerGas |
BigInt? |
Maximum fee per gas for EIP-1559 (null for legacy networks) |
maxPriorityFeePerGas |
BigInt? |
Maximum priority fee per gas for EIP-1559 (null for legacy networks) |
EIP-1559 vs Legacy Networks #
EIP-1559 Networks #
For networks supporting EIP-1559, you'll receive:
gasPrice
- Current gas price (for compatibility)maxFeePerGas
- Calculated as2 * baseFee + priorityFee
maxPriorityFeePerGas
- Current priority fee suggestion
Legacy Networks #
For older networks, you'll receive:
gasPrice
- Current gas pricemaxFeePerGas
-null
maxPriorityFeePerGas
-null
Testing #
Run the test suite:
dart test
The tests include:
- ✅ Real network integration tests
- ✅ Error handling verification
- ✅ Data validation checks
- ✅ EIP-1559 and legacy network compatibility
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Development Setup #
- Clone the repository
- Run
dart pub get
- Make your changes
- Run
dart test
to ensure tests pass - Submit a pull request
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog #
See CHANGELOG.md for a detailed list of changes and version history.