lexe_common/ln/
priority.rs1use 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
12pub trait ToNumBlocks {
15 fn to_num_blocks(&self) -> u16;
17}
18
19impl ToNumBlocks for ConfirmationTarget {
20 fn to_num_blocks(&self) -> u16 {
21 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#[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}