Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion key-wallet-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub use events::WalletEvent;
pub use key_wallet::wallet::managed_wallet_info::coin_selection::{
CoinSelector, SelectionResult, SelectionStrategy,
};
pub use key_wallet::wallet::managed_wallet_info::fee::{FeeEstimator, FeeRate};
pub use key_wallet::wallet::managed_wallet_info::fee::FeeRate;
pub use key_wallet::wallet::managed_wallet_info::transaction_builder::TransactionBuilder;
pub use wallet_interface::BlockProcessingResult;
pub use wallet_manager::{WalletError, WalletManager};
99 changes: 0 additions & 99 deletions key-wallet/src/wallet/managed_wallet_info/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//! This module provides fee rate management and fee estimation
//! for transactions.

use core::cmp;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -120,89 +118,6 @@ impl FeeLevel {
}
}

/// Fee estimator for dynamic fee calculation
pub struct FeeEstimator {
/// Minimum fee rate
min_fee_rate: FeeRate,
/// Maximum fee rate
max_fee_rate: FeeRate,
/// Current estimated rates for different confirmation targets
estimates: Vec<(u32, FeeRate)>, // (blocks, fee_rate)
}

impl FeeEstimator {
/// Create a new fee estimator
pub fn new() -> Self {
Self {
min_fee_rate: FeeRate::min(),
max_fee_rate: FeeRate::new(100_000), // 100 sat/byte max
estimates: vec![
(1, FeeRate::new(10_000)), // 10 sat/byte for next block
(3, FeeRate::new(5_000)), // 5 sat/byte for 3 blocks
(6, FeeRate::new(2_000)), // 2 sat/byte for 6 blocks
(12, FeeRate::new(1_000)), // 1 sat/byte for 12 blocks
(24, FeeRate::new(500)), // 0.5 sat/byte for 24 blocks
],
}
}

/// Update fee estimates (would be called with data from network)
pub fn update_estimates(&mut self, estimates: Vec<(u32, FeeRate)>) {
self.estimates = estimates;
self.estimates.sort_by_key(|(blocks, _)| *blocks);
}

/// Get fee rate for target confirmation in blocks
pub fn estimate_fee(&self, target_blocks: u32) -> FeeRate {
// Find the appropriate estimate
for &(blocks, rate) in &self.estimates {
if target_blocks <= blocks {
return self.clamp_fee_rate(rate);
}
}

// Use the lowest rate if target is beyond our estimates
self.estimates
.last()
.map(|&(_, rate)| self.clamp_fee_rate(rate))
.unwrap_or(self.min_fee_rate)
}

/// Get fee rate for a specific level
pub fn estimate_fee_level(&self, level: FeeLevel) -> FeeRate {
match level {
FeeLevel::Priority => self.estimate_fee(1),
FeeLevel::Normal => self.estimate_fee(6),
FeeLevel::Economy => self.estimate_fee(24),
FeeLevel::Custom(rate) => self.clamp_fee_rate(rate),
}
}

/// Clamp fee rate to min/max bounds
fn clamp_fee_rate(&self, rate: FeeRate) -> FeeRate {
FeeRate::new(cmp::min(
cmp::max(rate.sat_per_kb, self.min_fee_rate.sat_per_kb),
self.max_fee_rate.sat_per_kb,
))
}

/// Set minimum fee rate
pub fn set_min_fee_rate(&mut self, rate: FeeRate) {
self.min_fee_rate = rate;
}

/// Set maximum fee rate
pub fn set_max_fee_rate(&mut self, rate: FeeRate) {
self.max_fee_rate = rate;
}
}

impl Default for FeeEstimator {
fn default() -> Self {
Self::new()
}
}

/// Calculate the size of a transaction
pub fn estimate_tx_size(num_inputs: usize, num_outputs: usize, has_change: bool) -> usize {
// Base size: version (2) + type (2) + locktime (4) + varint counts
Expand Down Expand Up @@ -266,20 +181,6 @@ mod tests {
assert_eq!(custom.fee_rate().as_sat_per_kb(), 3000);
}

#[test]
fn test_fee_estimator() {
let estimator = FeeEstimator::new();

// Test different confirmation targets
let fee_1_block = estimator.estimate_fee(1);
let fee_6_blocks = estimator.estimate_fee(6);
let fee_24_blocks = estimator.estimate_fee(24);

// Fees should decrease with longer confirmation targets
assert!(fee_1_block.as_sat_per_kb() >= fee_6_blocks.as_sat_per_kb());
assert!(fee_6_blocks.as_sat_per_kb() >= fee_24_blocks.as_sat_per_kb());
}

#[test]
fn test_tx_size_estimation() {
// 1 input, 1 output, no change
Expand Down
Loading