bark_json/exit/
states.rs

1use std::collections::HashSet;
2use std::fmt;
3
4use bitcoin::{Amount, FeeRate, Txid};
5
6use bitcoin_ext::{BlockHeight, BlockRef};
7#[cfg(feature = "utoipa")]
8use utoipa::ToSchema;
9
10#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
11#[cfg_attr(feature = "utoipa", derive(ToSchema))]
12pub struct ExitTx {
13	#[cfg_attr(feature = "utoipa", schema(value_type = String))]
14	pub txid: Txid,
15	pub status: ExitTxStatus,
16}
17
18#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
19#[cfg_attr(feature = "utoipa", derive(ToSchema))]
20#[serde(tag = "type", rename_all = "kebab-case")]
21pub enum ExitTxStatus {
22	#[default]
23	VerifyInputs,
24	AwaitingInputConfirmation {
25		#[cfg_attr(feature = "utoipa", schema(value_type = Vec<String>))]
26		txids: HashSet<Txid>
27	},
28	NeedsSignedPackage,
29	NeedsReplacementPackage {
30		#[serde(rename = "min_fee_rate_kwu")]
31		#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
32		min_fee_rate: FeeRate,
33		#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
34		min_fee: Amount,
35	},
36	NeedsBroadcasting {
37		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
38		child_txid: Txid,
39		origin: ExitTxOrigin,
40	},
41	BroadcastWithCpfp {
42		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
43		child_txid: Txid,
44		origin: ExitTxOrigin,
45	},
46	Confirmed {
47		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
48		child_txid: Txid,
49		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
50		block: BlockRef,
51		origin: ExitTxOrigin,
52	},
53}
54
55impl fmt::Display for ExitTxStatus {
56	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57		fmt::Debug::fmt(self, f)
58	}
59}
60
61impl From<bark::exit::models::ExitTxStatus> for ExitTxStatus {
62	fn from(v: bark::exit::models::ExitTxStatus) -> Self {
63		match v {
64			bark::exit::models::ExitTxStatus::VerifyInputs => {
65				ExitTxStatus::VerifyInputs
66			},
67			bark::exit::models::ExitTxStatus::AwaitingInputConfirmation { txids } => {
68				ExitTxStatus::AwaitingInputConfirmation { txids }
69			},
70			bark::exit::models::ExitTxStatus::NeedsSignedPackage => {
71				ExitTxStatus::NeedsSignedPackage
72			},
73			bark::exit::models::ExitTxStatus::NeedsReplacementPackage { min_fee_rate, min_fee } => {
74				ExitTxStatus::NeedsReplacementPackage { min_fee_rate, min_fee }
75			},
76			bark::exit::models::ExitTxStatus::NeedsBroadcasting { child_txid, origin } => {
77				ExitTxStatus::NeedsBroadcasting { child_txid, origin: origin.into() }
78			},
79			bark::exit::models::ExitTxStatus::BroadcastWithCpfp { child_txid, origin } => {
80				ExitTxStatus::BroadcastWithCpfp { child_txid, origin: origin.into() }
81			},
82			bark::exit::models::ExitTxStatus::Confirmed { child_txid, block, origin } => {
83				ExitTxStatus::Confirmed { child_txid, block, origin: origin.into() }
84			},
85		}
86	}
87}
88
89#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
90#[cfg_attr(feature = "utoipa", derive(ToSchema))]
91#[serde(tag = "type", rename_all = "kebab-case")]
92pub enum ExitTxOrigin {
93	Wallet {
94		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
95		confirmed_in: Option<BlockRef>
96	},
97	Mempool {
98		/// This is the effective fee rate of the transaction (including CPFP ancestors)
99		#[serde(rename = "fee_rate_kwu")]
100		#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
101		fee_rate: FeeRate,
102		/// This includes the fees of the CPFP ancestors
103		#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
104		total_fee: Amount,
105	},
106	Block {
107		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
108		confirmed_in: BlockRef
109	},
110}
111
112impl fmt::Display for ExitTxOrigin {
113	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114		fmt::Debug::fmt(self, f)
115	}
116}
117
118impl From<bark::exit::models::ExitTxOrigin> for ExitTxOrigin {
119	fn from(v: bark::exit::models::ExitTxOrigin) -> Self {
120		match v {
121			bark::exit::models::ExitTxOrigin::Wallet { confirmed_in } => {
122				ExitTxOrigin::Wallet { confirmed_in }
123			},
124			bark::exit::models::ExitTxOrigin::Mempool { fee_rate, total_fee } => {
125				ExitTxOrigin::Mempool { fee_rate, total_fee }
126			},
127			bark::exit::models::ExitTxOrigin::Block { confirmed_in } => {
128				ExitTxOrigin::Block { confirmed_in }
129			},
130		}
131	}
132}
133
134#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
135#[cfg_attr(feature = "utoipa", derive(ToSchema))]
136pub struct ExitStartState {
137	pub tip_height: BlockHeight,
138}
139
140#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
141#[cfg_attr(feature = "utoipa", derive(ToSchema))]
142pub struct ExitProcessingState {
143	pub tip_height: BlockHeight,
144	pub transactions: Vec<ExitTx>,
145}
146
147#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
148#[cfg_attr(feature = "utoipa", derive(ToSchema))]
149pub struct ExitAwaitingDeltaState {
150	pub tip_height: BlockHeight,
151	#[cfg_attr(feature = "utoipa", schema(value_type = String))]
152	pub confirmed_block: BlockRef,
153	pub claimable_height: BlockHeight,
154}
155
156#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
157#[cfg_attr(feature = "utoipa", derive(ToSchema))]
158pub struct ExitClaimableState {
159	pub tip_height: BlockHeight,
160	#[cfg_attr(feature = "utoipa", schema(value_type = String))]
161	pub claimable_since: BlockRef,
162	#[cfg_attr(feature = "utoipa", schema(value_type = String, nullable = true))]
163	pub last_scanned_block: Option<BlockRef>,
164}
165
166#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
167#[cfg_attr(feature = "utoipa", derive(ToSchema))]
168pub struct ExitClaimInProgressState {
169	pub tip_height: BlockHeight,
170	#[cfg_attr(feature = "utoipa", schema(value_type = String))]
171	pub claimable_since: BlockRef,
172	#[cfg_attr(feature = "utoipa", schema(value_type = String))]
173	pub claim_txid: Txid,
174}
175
176#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
177#[cfg_attr(feature = "utoipa", derive(ToSchema))]
178pub struct ExitClaimedState {
179	pub tip_height: BlockHeight,
180	#[cfg_attr(feature = "utoipa", schema(value_type = String))]
181	pub txid: Txid,
182	#[cfg_attr(feature = "utoipa", schema(value_type = String))]
183	pub block: BlockRef,
184}