Skip to main content

lexe_common/ln/
priority.rs

1use std::{
2    fmt::{self, Display},
3    str::FromStr,
4};
5
6use anyhow::anyhow;
7use lightning::chain::chaininterface::ConfirmationTarget;
8#[cfg(any(test, feature = "test-utils"))]
9use proptest_derive::Arbitrary;
10use serde::{Deserialize, Serialize};
11
12/// Small extension trait to ease interop between our [`ConfirmationPriority`]
13/// and LDK's [`ConfirmationTarget`].
14pub trait ToNumBlocks {
15    /// Convert a confirmation priority into a target number of blocks.
16    fn to_num_blocks(&self) -> u16;
17}
18
19impl ToNumBlocks for ConfirmationTarget {
20    fn to_num_blocks(&self) -> u16 {
21        // Based on ldk-node's FeeEstimator implementation.
22        match self {
23            ConfirmationTarget::MaximumFeeEstimate => 1,
24            ConfirmationTarget::UrgentOnChainSweep => 6,
25            ConfirmationTarget::MinAllowedAnchorChannelRemoteFee => 1008,
26            ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => 144,
27            ConfirmationTarget::AnchorChannelFee => 1008,
28            ConfirmationTarget::NonAnchorChannelFee => 12,
29            ConfirmationTarget::ChannelCloseMinimum => 144,
30            ConfirmationTarget::OutputSpendingFee => 12,
31        }
32    }
33}
34
35/// Transaction confirmation priority for on-chain payments: `"high"` (~1
36/// block), `"normal"` (~3 blocks), or `"background"` (~72 blocks).
37//
38// Basically a simplified version of LDK's [`ConfirmationTarget`] type.
39// Lexe code should prefer to use this type when possible.
40#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
41#[serde(rename_all = "snake_case")]
42#[cfg_attr(any(test, feature = "test-utils"), derive(Arbitrary))]
43#[cfg_attr(test, derive(strum::VariantArray))]
44pub enum ConfirmationPriority {
45    High,
46    Normal,
47    Background,
48}
49
50impl ToNumBlocks for ConfirmationPriority {
51    fn to_num_blocks(&self) -> u16 {
52        match self {
53            ConfirmationPriority::High => 1,
54            ConfirmationPriority::Normal => 3,
55            ConfirmationPriority::Background => 72,
56        }
57    }
58}
59
60impl FromStr for ConfirmationPriority {
61    type Err = anyhow::Error;
62    fn from_str(s: &str) -> Result<Self, Self::Err> {
63        match s {
64            "high" => Ok(Self::High),
65            "normal" => Ok(Self::Normal),
66            "background" => Ok(Self::Background),
67            _ => Err(anyhow!("Must be one of: 'high', 'normal', 'background'")),
68        }
69    }
70}
71
72impl Display for ConfirmationPriority {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        match self {
75            Self::High => write!(f, "high"),
76            Self::Normal => write!(f, "normal"),
77            Self::Background => write!(f, "background"),
78        }
79    }
80}
81
82#[cfg(test)]
83mod test {
84    use super::*;
85    use crate::test_utils::roundtrip::json_unit_enum_backwards_compat;
86
87    #[test]
88    fn conf_prio_json_backward_compat() {
89        let expected_ser = r#"["high","normal","background"]"#;
90        json_unit_enum_backwards_compat::<ConfirmationPriority>(expected_ser);
91    }
92}