1use std::collections::HashMap;
2use std::str::FromStr;
3
4use anyhow::anyhow;
5use bitcoin::{Amount, ScriptBuf, SignedAmount};
6use chrono::DateTime;
7
8use ark::VtxoId;
9use ark::lightning::{Invoice, Offer};
10use bark::lnurllib::lightning_address::LightningAddress;
11use bark::movement::MovementId;
12
13
14#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
15#[serde(rename_all = "kebab-case")]
16#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
17pub enum MovementStatus {
18 Pending,
20 Successful,
22 Failed,
25 Canceled,
28}
29
30impl From<bark::movement::MovementStatus> for MovementStatus {
31 fn from(v: bark::movement::MovementStatus) -> Self {
32 match v {
33 bark::movement::MovementStatus::Pending => Self::Pending,
34 bark::movement::MovementStatus::Successful => Self::Successful,
35 bark::movement::MovementStatus::Failed => Self::Failed,
36 bark::movement::MovementStatus::Canceled => Self::Canceled,
37 }
38 }
39}
40
41#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
43#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
44pub struct Movement {
45 #[cfg_attr(feature = "utoipa", schema(value_type = u32))]
47 pub id: MovementId,
48 pub status: MovementStatus,
50 pub subsystem: MovementSubsystem,
53 #[serde(default, skip_serializing_if = "Option::is_none")]
56 pub metadata: Option<HashMap<String, serde_json::Value>>,
57 #[serde(rename="intended_balance_sat", with="bitcoin::amount::serde::as_sat")]
60 #[cfg_attr(feature = "utoipa", schema(value_type = i64))]
61 pub intended_balance: SignedAmount,
62 #[serde(rename="effective_balance_sat", with="bitcoin::amount::serde::as_sat")]
66 #[cfg_attr(feature = "utoipa", schema(value_type = i64))]
67 pub effective_balance: SignedAmount,
68 #[serde(rename="offchain_fee_sat", with="bitcoin::amount::serde::as_sat")]
72 #[cfg_attr(feature = "utoipa", schema(value_type = u64))]
73 pub offchain_fee: Amount,
74 pub sent_to: Vec<MovementDestination>,
76 pub received_on: Vec<MovementDestination>,
79 #[cfg_attr(feature = "utoipa", schema(value_type = Vec<String>))]
82 pub input_vtxos: Vec<VtxoId>,
83 #[cfg_attr(feature = "utoipa", schema(value_type = Vec<String>))]
86 pub output_vtxos: Vec<VtxoId>,
87 #[cfg_attr(feature = "utoipa", schema(value_type = Vec<String>))]
92 pub exited_vtxos: Vec<VtxoId>,
93 pub time: MovementTimestamp,
95}
96
97impl From<bark::movement::Movement> for Movement {
98 fn from(m: bark::movement::Movement) -> Self {
99 Movement {
100 id: m.id,
101 status: m.status.into(),
102 subsystem: MovementSubsystem::from(m.subsystem),
103 metadata: if m.metadata.is_empty() { None } else { Some(m.metadata) },
104 intended_balance: m.intended_balance,
105 effective_balance: m.effective_balance,
106 offchain_fee: m.offchain_fee,
107 sent_to: m.sent_to.into_iter().map(MovementDestination::from).collect(),
108 received_on: m.received_on.into_iter().map(MovementDestination::from).collect(),
109 input_vtxos: m.input_vtxos,
110 output_vtxos: m.output_vtxos,
111 exited_vtxos: m.exited_vtxos,
112 time: MovementTimestamp::from(m.time),
113 }
114 }
115}
116
117#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
120#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
121pub struct MovementDestination {
122 pub destination: PaymentMethod,
124 #[serde(rename="amount_sat", with="bitcoin::amount::serde::as_sat")]
126 #[cfg_attr(feature = "utoipa", schema(value_type = u64))]
127 pub amount: Amount,
128}
129
130impl From<bark::movement::MovementDestination> for MovementDestination {
131 fn from(d: bark::movement::MovementDestination) -> Self {
132 MovementDestination {
133 destination: PaymentMethod::from(d.destination),
134 amount: d.amount,
135 }
136 }
137}
138
139#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
143#[serde(tag = "type", content = "value", rename_all = "kebab-case")]
144pub enum PaymentMethod {
145 Ark(String),
147 Bitcoin(String),
149 OutputScript(String),
152 Invoice(String),
154 Offer(String),
156 LightningAddress(String),
158 Custom(String),
160}
161
162#[cfg(feature = "utoipa")]
163impl utoipa::PartialSchema for PaymentMethod {
164 fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
165 use utoipa::openapi::schema;
166
167 schema::ObjectBuilder::new()
168 .title(Some("PaymentMethod"))
169 .description(Some("A payment method with a type discriminator and string value"))
170 .property(
171 "type",
172 schema::ObjectBuilder::new()
173 .schema_type(schema::SchemaType::Type(schema::Type::String))
174 .enum_values(Some([
175 "ark",
176 "bitcoin",
177 "output-script",
178 "invoice",
179 "offer",
180 "lightning-address",
181 "custom",
182 ]))
183 .description(Some("The type of payment method"))
184 )
185 .required("type")
186 .property(
187 "value",
188 schema::ObjectBuilder::new()
189 .schema_type(schema::SchemaType::Type(schema::Type::String))
190 .description(Some("The payment method value (address, invoice, etc.)"))
191 )
192 .required("value")
193 .into()
194 }
195}
196
197#[cfg(feature = "utoipa")]
198impl utoipa::ToSchema for PaymentMethod {
199 fn name() -> std::borrow::Cow<'static, str> {
200 std::borrow::Cow::Borrowed("PaymentMethod")
201 }
202}
203
204impl From<bark::movement::PaymentMethod> for PaymentMethod {
205 fn from(p: bark::movement::PaymentMethod) -> Self {
206 match p {
207 bark::movement::PaymentMethod::Ark(a) => Self::Ark(a.to_string()),
208 bark::movement::PaymentMethod::Bitcoin(b) => Self::Bitcoin(b.assume_checked().to_string()),
209 bark::movement::PaymentMethod::OutputScript(s) => Self::OutputScript(s.to_hex_string()),
210 bark::movement::PaymentMethod::Invoice(i) => Self::Invoice(i.to_string()),
211 bark::movement::PaymentMethod::Offer(o) => Self::Offer(o.to_string()),
212 bark::movement::PaymentMethod::LightningAddress(l) => Self::LightningAddress(l.to_string()),
213 bark::movement::PaymentMethod::Custom(c) => Self::Custom(c),
214 }
215 }
216}
217
218impl TryFrom<PaymentMethod> for bark::movement::PaymentMethod {
219 type Error = anyhow::Error;
220
221 fn try_from(p: PaymentMethod) -> Result<Self, Self::Error> {
222 match p {
223 PaymentMethod::Ark(a) => Ok(bark::movement::PaymentMethod::Ark(
224 ark::Address::from_str(&a)?,
225 )),
226 PaymentMethod::Bitcoin(b) => Ok(bark::movement::PaymentMethod::Bitcoin(
227 bitcoin::Address::from_str(&b)?,
228 )),
229 PaymentMethod::OutputScript(s) => Ok(bark::movement::PaymentMethod::OutputScript(
230 ScriptBuf::from_hex(&s)?,
231 )),
232 PaymentMethod::Invoice(i) => Ok(bark::movement::PaymentMethod::Invoice(
233 Invoice::from_str(&i)?,
234 )),
235 PaymentMethod::Offer(o) => Ok(bark::movement::PaymentMethod::Offer(
236 Offer::from_str(&o).map_err(|e| anyhow!("Failed to parse offer: {:?}", e))?,
237 )),
238 PaymentMethod::LightningAddress(l) => Ok(bark::movement::PaymentMethod::LightningAddress(
239 LightningAddress::from_str(&l)?,
240 )),
241 PaymentMethod::Custom(c) => Ok(bark::movement::PaymentMethod::Custom(c)),
242 }
243 }
244}
245
246#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
249#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
250pub struct MovementSubsystem {
251 pub name: String,
253 pub kind: String,
255}
256
257impl From<bark::movement::MovementSubsystem> for MovementSubsystem {
258 fn from(s: bark::movement::MovementSubsystem) -> Self {
259 MovementSubsystem {
260 name: s.name,
261 kind: s.kind,
262 }
263 }
264}
265
266#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
268#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
269pub struct MovementTimestamp {
270 pub created_at: DateTime<chrono::Local>,
272 pub updated_at: DateTime<chrono::Local>,
274 #[serde(default, skip_serializing_if = "Option::is_none")]
276 pub completed_at: Option<DateTime<chrono::Local>>,
277}
278
279impl From<bark::movement::MovementTimestamp> for MovementTimestamp {
280 fn from(t: bark::movement::MovementTimestamp) -> Self {
281 MovementTimestamp {
282 created_at: t.created_at,
283 updated_at: t.updated_at,
284 completed_at: t.completed_at,
285 }
286 }
287}