Skip to content

Commit 41fb41f

Browse files
committed
Prefactor: Move fee estimator defaults to util methods
1 parent 4e305e6 commit 41fb41f

File tree

1 file changed

+67
-54
lines changed

1 file changed

+67
-54
lines changed

src/fee_estimator.rs

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -86,34 +86,9 @@ where
8686
return Err(Error::FeerateEstimationUpdateFailed);
8787
}
8888

89-
let confirmation_targets = vec![
90-
ConfirmationTarget::OnchainPayment,
91-
ConfirmationTarget::ChannelFunding,
92-
LdkConfirmationTarget::MaximumFeeEstimate.into(),
93-
LdkConfirmationTarget::UrgentOnChainSweep.into(),
94-
LdkConfirmationTarget::MinAllowedAnchorChannelRemoteFee.into(),
95-
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee.into(),
96-
LdkConfirmationTarget::AnchorChannelFee.into(),
97-
LdkConfirmationTarget::NonAnchorChannelFee.into(),
98-
LdkConfirmationTarget::ChannelCloseMinimum.into(),
99-
LdkConfirmationTarget::OutputSpendingFee.into(),
100-
];
101-
89+
let confirmation_targets = get_all_conf_targets();
10290
for target in confirmation_targets {
103-
let num_blocks = match target {
104-
ConfirmationTarget::OnchainPayment => 6,
105-
ConfirmationTarget::ChannelFunding => 12,
106-
ConfirmationTarget::Lightning(ldk_target) => match ldk_target {
107-
LdkConfirmationTarget::MaximumFeeEstimate => 1,
108-
LdkConfirmationTarget::UrgentOnChainSweep => 6,
109-
LdkConfirmationTarget::MinAllowedAnchorChannelRemoteFee => 1008,
110-
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => 144,
111-
LdkConfirmationTarget::AnchorChannelFee => 1008,
112-
LdkConfirmationTarget::NonAnchorChannelFee => 12,
113-
LdkConfirmationTarget::ChannelCloseMinimum => 144,
114-
LdkConfirmationTarget::OutputSpendingFee => 12,
115-
},
116-
};
91+
let num_blocks = get_num_block_defaults_for_target(target);
11792

11893
let converted_estimate_sat_vb =
11994
esplora_client::convert_fee_rate(num_blocks, estimates.clone()).map_err(|e| {
@@ -130,15 +105,7 @@ where
130105

131106
// LDK 0.0.118 introduced changes to the `ConfirmationTarget` semantics that
132107
// require some post-estimation adjustments to the fee rates, which we do here.
133-
let adjusted_fee_rate = match target {
134-
ConfirmationTarget::Lightning(
135-
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee,
136-
) => {
137-
let slightly_less_than_background = fee_rate.to_sat_per_kwu() - 250;
138-
FeeRate::from_sat_per_kwu(slightly_less_than_background)
139-
},
140-
_ => fee_rate,
141-
};
108+
let adjusted_fee_rate = apply_post_estimation_adjustments(target, fee_rate);
142109

143110
let mut locked_fee_rate_cache = self.fee_rate_cache.write().unwrap();
144111
locked_fee_rate_cache.insert(target, adjusted_fee_rate);
@@ -160,24 +127,7 @@ where
160127
fn estimate_fee_rate(&self, confirmation_target: ConfirmationTarget) -> FeeRate {
161128
let locked_fee_rate_cache = self.fee_rate_cache.read().unwrap();
162129

163-
let fallback_sats_kwu = match confirmation_target {
164-
ConfirmationTarget::OnchainPayment => 5000,
165-
ConfirmationTarget::ChannelFunding => 1000,
166-
ConfirmationTarget::Lightning(ldk_target) => match ldk_target {
167-
LdkConfirmationTarget::MaximumFeeEstimate => 8000,
168-
LdkConfirmationTarget::UrgentOnChainSweep => 5000,
169-
LdkConfirmationTarget::MinAllowedAnchorChannelRemoteFee => {
170-
FEERATE_FLOOR_SATS_PER_KW
171-
},
172-
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => {
173-
FEERATE_FLOOR_SATS_PER_KW
174-
},
175-
LdkConfirmationTarget::AnchorChannelFee => 500,
176-
LdkConfirmationTarget::NonAnchorChannelFee => 1000,
177-
LdkConfirmationTarget::ChannelCloseMinimum => 500,
178-
LdkConfirmationTarget::OutputSpendingFee => 1000,
179-
},
180-
};
130+
let fallback_sats_kwu = get_fallback_rate_for_target(confirmation_target);
181131

182132
// We'll fall back on this, if we really don't have any other information.
183133
let fallback_rate = FeeRate::from_sat_per_kwu(fallback_sats_kwu as u64);
@@ -198,3 +148,66 @@ where
198148
self.estimate_fee_rate(confirmation_target.into()).to_sat_per_kwu() as u32
199149
}
200150
}
151+
152+
pub(crate) fn get_num_block_defaults_for_target(target: ConfirmationTarget) -> usize {
153+
match target {
154+
ConfirmationTarget::OnchainPayment => 6,
155+
ConfirmationTarget::ChannelFunding => 12,
156+
ConfirmationTarget::Lightning(ldk_target) => match ldk_target {
157+
LdkConfirmationTarget::MaximumFeeEstimate => 1,
158+
LdkConfirmationTarget::UrgentOnChainSweep => 6,
159+
LdkConfirmationTarget::MinAllowedAnchorChannelRemoteFee => 1008,
160+
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => 144,
161+
LdkConfirmationTarget::AnchorChannelFee => 1008,
162+
LdkConfirmationTarget::NonAnchorChannelFee => 12,
163+
LdkConfirmationTarget::ChannelCloseMinimum => 144,
164+
LdkConfirmationTarget::OutputSpendingFee => 12,
165+
},
166+
}
167+
}
168+
169+
pub(crate) fn get_fallback_rate_for_target(target: ConfirmationTarget) -> u32 {
170+
match target {
171+
ConfirmationTarget::OnchainPayment => 5000,
172+
ConfirmationTarget::ChannelFunding => 1000,
173+
ConfirmationTarget::Lightning(ldk_target) => match ldk_target {
174+
LdkConfirmationTarget::MaximumFeeEstimate => 8000,
175+
LdkConfirmationTarget::UrgentOnChainSweep => 5000,
176+
LdkConfirmationTarget::MinAllowedAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW,
177+
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW,
178+
LdkConfirmationTarget::AnchorChannelFee => 500,
179+
LdkConfirmationTarget::NonAnchorChannelFee => 1000,
180+
LdkConfirmationTarget::ChannelCloseMinimum => 500,
181+
LdkConfirmationTarget::OutputSpendingFee => 1000,
182+
},
183+
}
184+
}
185+
186+
pub(crate) fn get_all_conf_targets() -> [ConfirmationTarget; 10] {
187+
[
188+
ConfirmationTarget::OnchainPayment,
189+
ConfirmationTarget::ChannelFunding,
190+
LdkConfirmationTarget::MaximumFeeEstimate.into(),
191+
LdkConfirmationTarget::UrgentOnChainSweep.into(),
192+
LdkConfirmationTarget::MinAllowedAnchorChannelRemoteFee.into(),
193+
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee.into(),
194+
LdkConfirmationTarget::AnchorChannelFee.into(),
195+
LdkConfirmationTarget::NonAnchorChannelFee.into(),
196+
LdkConfirmationTarget::ChannelCloseMinimum.into(),
197+
LdkConfirmationTarget::OutputSpendingFee.into(),
198+
]
199+
}
200+
201+
pub(crate) fn apply_post_estimation_adjustments(
202+
target: ConfirmationTarget, estimated_rate: FeeRate,
203+
) -> FeeRate {
204+
match target {
205+
ConfirmationTarget::Lightning(
206+
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee,
207+
) => {
208+
let slightly_less_than_background = estimated_rate.to_sat_per_kwu() - 250;
209+
FeeRate::from_sat_per_kwu(slightly_less_than_background)
210+
},
211+
_ => estimated_rate,
212+
}
213+
}

0 commit comments

Comments
 (0)