bark/
movement.rs

1
2use ark::Vtxo;
3use bitcoin::Amount;
4
5use crate::vtxo_state::VtxoState;
6
7#[derive(Debug, Deserialize, Serialize)]
8pub enum MovementKind {
9	Board,
10	Round,
11	Offboard,
12	Exit,
13	ArkoorSend,
14	ArkoorReceive,
15	LightningSend,
16	LightningSendRevocation,
17	LightningReceive,
18}
19
20impl MovementKind {
21	pub fn from_str(s: &str) -> anyhow::Result<Self> {
22		match s {
23			"onboard" => Ok(MovementKind::Board),
24			"round" => Ok(MovementKind::Round),
25			"offboard" => Ok(MovementKind::Offboard),
26			"arkoor-send" => Ok(MovementKind::ArkoorSend),
27			"arkoor-receive" => Ok(MovementKind::ArkoorReceive),
28			"lightning-send" => Ok(MovementKind::LightningSend),
29			"lightning-send-revocation" => Ok(MovementKind::LightningSendRevocation),
30			"lightning-receive" => Ok(MovementKind::LightningReceive),
31			"exit" => Ok(MovementKind::Exit),
32			_ => bail!("Invalid movement kind: {}", s),
33		}
34	}
35
36	pub fn as_str(&self) -> &str {
37		match self {
38			MovementKind::Board => "onboard",
39			MovementKind::Round => "round",
40			MovementKind::Offboard => "offboard",
41			MovementKind::ArkoorSend => "arkoor-send",
42			MovementKind::ArkoorReceive => "arkoor-receive",
43			MovementKind::LightningSend => "lightning-send",
44			MovementKind::LightningSendRevocation => "lightning-send-revocation",
45			MovementKind::LightningReceive => "lightning-receive",
46			MovementKind::Exit => "exit",
47		}
48	}
49}
50
51#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
52pub struct MovementRecipient {
53	/// Can either be a publickey, spk or a bolt11 invoice
54	pub recipient: String,
55	/// Amount sent to the recipient
56	#[serde(rename = "amount_sat", with = "bitcoin::amount::serde::as_sat")]
57	pub amount: Amount
58}
59
60/// A [`Movement`] represents any offchain balance change,
61/// either by receiving, sending or refreshing VTXO
62#[derive(Debug)]
63pub struct Movement {
64	pub id: u32,
65	/// Movement kind
66	pub kind: MovementKind,
67	/// Fees paid for the movement
68	pub fees: Amount,
69	/// wallet's VTXOs spent in this movement
70	pub spends: Vec<Vtxo>,
71	/// Received VTXOs from this movement
72	pub receives: Vec<Vtxo>,
73	/// External recipients of the movement
74	pub recipients: Vec<MovementRecipient>,
75	/// Movement date
76	pub created_at: String,
77}
78
79/// Arguments used to create a movement
80pub struct MovementArgs<'a> {
81	/// Movement kind
82	pub kind: MovementKind,
83	/// VTXOs that are spent in the movement.
84	///
85	/// They will be marked as spent and linked to the created movement
86	pub spends: &'a [&'a Vtxo],
87	/// New VTXOs to store and link to the created movement
88	pub receives: &'a [(&'a Vtxo, VtxoState)],
89	/// External destinations of the movement
90	pub recipients: &'a [(&'a str, Amount)],
91	/// Optional offchain fees paid for the movement
92	pub fees: Option<Amount>
93}