The Stellar Fee Management System dynamically optimizes transaction fees based on network conditions, ensuring transactions are processed efficiently without overpaying. The system continuously monitors the Stellar network’s fee market and recommends appropriate fees based on congestion levels and transaction priority.
The system analyzes Stellar network congestion using fee percentile comparisons:
Congestion Level | Description | Indicators | Fee Multiplier |
---|---|---|---|
Low | Normal network conditions | Fees are relatively uniform (p90 < 1.5x p10) | 1.0x |
Medium | Moderate congestion | p90 is 1.5-3x higher than p10 | 1.5x |
High | High congestion | p90 is 3-5x higher than p10 | 2.0x |
Critical | Severe congestion | p90 is 5x+ higher than p10 | 3.0x |
When a transaction is stuck due to low fees, the system can “bump” the fee by:
This is particularly useful during sudden network congestion or when transaction processing times are critical.
// Initialize the fee manager
$feeManager = new StellarFeeManager([
'useTestnet' => true, // Use Stellar testnet
'enableLogging' => true // Enable detailed logging
;
])
// Get recommended fee for a standard priority transaction
$recommendedFee = $feeManager->getRecommendedFee([
'priorityLevel' => 'medium'
;
])
// Estimate fee for a transaction with multiple operations
$estFee = $feeManager->estimateTransactionFee(5, [ // Transaction with 5 operations
'priorityLevel' => 'high'
; ])
// Initialize transaction builder
$sourceAccount = $stellarServer->accounts()->account($sourcePublicKey);
$transactionBuilder = new TransactionBuilder($sourceAccount);
// Add operations to the transaction
$transactionBuilder->addOperation(new PaymentOperation(
$destinationAddress,
Asset::native(),
"100" // Amount in XLM
;
))
// Use fee manager to set the appropriate fee
$transaction = $feeManager->createTransactionWithRecommendedFee($transactionBuilder, [
'priorityLevel' => 'high',
'operationCount' => 1
;
])
// Sign and submit the transaction
$transaction->sign($sourceKeypair, Network::testnet());
$response = $stellarServer->submitTransaction($transaction);
try {
// Attempt to submit a transaction
$response = $stellarServer->submitTransaction($transaction);
catch (Exception $e) {
} // Check if the error is fee-related
if ($feeManager->isFeeRelatedError($e)) {
// Create a fee bump transaction
$feeBumpTransaction = $feeManager->createFeeBumpTransaction(
$sourceSecretKey,
$transaction
;
)
// Submit the fee bump transaction
$response = $stellarServer->submitTransaction($feeBumpTransaction);
else {
} // Handle other errors
throw $e;
} }
The fee management system provides reporting capabilities to monitor network conditions:
// Get detailed fee statistics
$feeStats = $feeManager->getFeeStatistics();
// Output includes:
// - Current timestamp
// - Network congestion level
// - Network type (testnet/public)
// - Fee statistics (min, max, median, p90)
// - Recommended fees for different priority levels
Option | Description | Default |
---|---|---|
defaultBaseFee |
Default base fee in stroops | 100 |
lowMultiplier |
Fee multiplier for low congestion | 1.0 |
mediumMultiplier |
Fee multiplier for medium congestion | 1.5 |
highMultiplier |
Fee multiplier for high congestion | 2.0 |
criticalMultiplier |
Fee multiplier for critical congestion | 3.0 |
cacheDuration |
Duration to cache fee stats (milliseconds) | 60000 (1 minute) |
useTestnet |
Whether to use Stellar testnet | true |
horizonUrl |
Custom Horizon server URL | Based on testnet setting |
enableLogging |
Enable detailed logging | false |
estimateTransactionFee()
with the correct operation
countThe fee management system is implemented in the
StellarFeeManager.php
class, which integrates with the
Soneso Stellar SDK. The system maintains compatibility with the overall
application architecture while providing a comprehensive fee management
solution.