lightning_liquidity/lsps2/
event.rs

1// This file is Copyright its original authors, visible in version control
2// history.
3//
4// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7// You may not use this file except in accordance with one or both of these
8// licenses.
9
10//! Contains bLIP-52 / LSPS2 event types
11
12use super::msgs::LSPS2OpeningFeeParams;
13use crate::lsps0::ser::{LSPSRequestId, LSPSResponseError};
14use alloc::string::String;
15use alloc::vec::Vec;
16
17use bitcoin::secp256k1::PublicKey;
18
19use lightning::impl_writeable_tlv_based_enum;
20
21/// An event which an LSPS2 client should take some action in response to.
22#[derive(Clone, Debug, PartialEq, Eq)]
23pub enum LSPS2ClientEvent {
24	/// Information from the LSP about their current fee rates and channel parameters.
25	///
26	/// You must call [`LSPS2ClientHandler::select_opening_params`] with the fee parameter
27	/// you want to use if you wish to proceed opening a channel.
28	///
29	/// **Note: ** This event will *not* be persisted across restarts.
30	///
31	/// [`LSPS2ClientHandler::select_opening_params`]: crate::lsps2::client::LSPS2ClientHandler::select_opening_params
32	OpeningParametersReady {
33		/// The identifier of the issued bLIP-52 / LSPS2 `get_info` request, as returned by
34		/// [`LSPS2ClientHandler::request_opening_params`]
35		///
36		/// This can be used to track which request this event corresponds to.
37		///
38		/// [`LSPS2ClientHandler::request_opening_params`]: crate::lsps2::client::LSPS2ClientHandler::request_opening_params
39		request_id: LSPSRequestId,
40		/// The node id of the LSP that provided this response.
41		counterparty_node_id: PublicKey,
42		/// The menu of fee parameters the LSP is offering at this time.
43		/// You must select one of these if you wish to proceed.
44		opening_fee_params_menu: Vec<LSPS2OpeningFeeParams>,
45	},
46	/// Provides the necessary information to generate a payable invoice that then may be given to
47	/// the payer.
48	///
49	/// When the invoice is paid, the LSP will open a channel with the previously agreed upon
50	/// parameters to you.
51	///
52	/// **Note: ** This event will *not* be persisted across restarts.
53	InvoiceParametersReady {
54		/// The identifier of the issued bLIP-52 / LSPS2 `buy` request, as returned by
55		/// [`LSPS2ClientHandler::select_opening_params`].
56		///
57		/// This can be used to track which request this event corresponds to.
58		///
59		/// [`LSPS2ClientHandler::select_opening_params`]: crate::lsps2::client::LSPS2ClientHandler::select_opening_params
60		request_id: LSPSRequestId,
61		/// The node id of the LSP.
62		counterparty_node_id: PublicKey,
63		/// The intercept short channel id to use in the route hint.
64		intercept_scid: u64,
65		/// The `cltv_expiry_delta` to use in the route hint.
66		cltv_expiry_delta: u32,
67		/// The initial payment size you specified.
68		payment_size_msat: Option<u64>,
69	},
70	/// A request previously issued via [`LSPS2ClientHandler::request_opening_params`]
71	/// failed as the LSP returned an error response.
72	///
73	/// **Note: ** This event will *not* be persisted across restarts.
74	///
75	/// [`LSPS2ClientHandler::request_opening_params`]: crate::lsps2::client::LSPS2ClientHandler::request_opening_params
76	GetInfoFailed {
77		/// The identifier of the issued LSPS2 `get_info` request, as returned by
78		/// [`LSPS2ClientHandler::request_opening_params`].
79		///
80		/// This can be used to track which request this event corresponds to.
81		///
82		/// [`LSPS2ClientHandler::request_opening_params`]: crate::lsps2::client::LSPS2ClientHandler::request_opening_params
83		request_id: LSPSRequestId,
84		/// The node id of the LSP.
85		counterparty_node_id: PublicKey,
86		/// The error that was returned.
87		error: LSPSResponseError,
88	},
89	/// A request previously issued via [`LSPS2ClientHandler::select_opening_params`]
90	/// failed as the LSP returned an error response.
91	///
92	/// **Note: ** This event will *not* be persisted across restarts.
93	///
94	/// [`LSPS2ClientHandler::select_opening_params`]: crate::lsps2::client::LSPS2ClientHandler::select_opening_params
95	BuyRequestFailed {
96		/// The identifier of the issued LSPS2 `buy` request, as returned by
97		/// [`LSPS2ClientHandler::select_opening_params`].
98		///
99		/// This can be used to track which request this event corresponds to.
100		///
101		/// [`LSPS2ClientHandler::select_opening_params`]: crate::lsps2::client::LSPS2ClientHandler::select_opening_params
102		request_id: LSPSRequestId,
103		/// The node id of the LSP.
104		counterparty_node_id: PublicKey,
105		/// The error that was returned.
106		error: LSPSResponseError,
107	},
108}
109
110/// An event which an bLIP-52 / LSPS2 server should take some action in response to.
111#[derive(Clone, Debug, PartialEq, Eq)]
112pub enum LSPS2ServiceEvent {
113	/// A request from a client for information about JIT Channel parameters.
114	///
115	/// You must calculate the parameters for this client and pass them to
116	/// [`LSPS2ServiceHandler::opening_fee_params_generated`].
117	///
118	/// If an unrecognized or stale token is provided you can use
119	/// `[LSPS2ServiceHandler::invalid_token_provided`] to error the request.
120	///
121	/// **Note: ** This event will *not* be persisted across restarts.
122	///
123	/// [`LSPS2ServiceHandler::opening_fee_params_generated`]: crate::lsps2::service::LSPS2ServiceHandler::opening_fee_params_generated
124	/// [`LSPS2ServiceHandler::invalid_token_provided`]: crate::lsps2::service::LSPS2ServiceHandler::invalid_token_provided
125	GetInfo {
126		/// An identifier that must be passed to [`LSPS2ServiceHandler::opening_fee_params_generated`].
127		///
128		/// [`LSPS2ServiceHandler::opening_fee_params_generated`]: crate::lsps2::service::LSPS2ServiceHandler::opening_fee_params_generated
129		request_id: LSPSRequestId,
130		/// The node id of the client making the information request.
131		counterparty_node_id: PublicKey,
132		/// An optional token that can be used as an API key, coupon code, etc.
133		token: Option<String>,
134	},
135	/// A client has selected a opening fee parameter to use and would like to
136	/// purchase a channel with an optional initial payment size.
137	///
138	/// If `payment_size_msat` is [`Option::Some`] then the payer is allowed to use MPP.
139	/// If `payment_size_msat` is [`Option::None`] then the payer cannot use MPP.
140	///
141	/// You must generate a `cltv_expiry_delta` and obtain an intercept scid using
142	/// [`ChannelManager::get_intercept_scid`] for them to use and then call
143	/// [`LSPS2ServiceHandler::invoice_parameters_generated`].
144	///
145	/// **Note: ** This event will *not* be persisted across restarts.
146	///
147	/// [`ChannelManager::get_intercept_scid`]: lightning::ln::channelmanager::ChannelManager::get_intercept_scid
148	///
149	/// [`LSPS2ServiceHandler::invoice_parameters_generated`]: crate::lsps2::service::LSPS2ServiceHandler::invoice_parameters_generated
150	BuyRequest {
151		/// An identifier that must be passed into [`LSPS2ServiceHandler::invoice_parameters_generated`].
152		///
153		/// [`LSPS2ServiceHandler::invoice_parameters_generated`]: crate::lsps2::service::LSPS2ServiceHandler::invoice_parameters_generated
154		request_id: LSPSRequestId,
155		/// The client node id that is making this request.
156		counterparty_node_id: PublicKey,
157		/// The channel parameters they have selected.
158		opening_fee_params: LSPS2OpeningFeeParams,
159		/// The size of the initial payment they would like to receive.
160		payment_size_msat: Option<u64>,
161	},
162	/// You should open a channel using [`ChannelManager::create_channel`].
163	///
164	/// **Note: ** As this event is persisted and might get replayed after restart, you'll need to
165	/// ensure channel creation idempotency. I.e., please check if you already created a
166	/// corresponding channel based on the given `their_network_key` and `user_channel_id` and
167	/// ignore this event in case you did.
168	///
169	/// [`ChannelManager::create_channel`]: lightning::ln::channelmanager::ChannelManager::create_channel
170	OpenChannel {
171		/// The node to open channel with.
172		their_network_key: PublicKey,
173		/// The amount to forward after fees.
174		amt_to_forward_msat: u64,
175		/// The fee earned for opening the channel.
176		opening_fee_msat: u64,
177		/// A user specified id used to track channel open.
178		user_channel_id: u128,
179		/// The intercept short channel id to use in the route hint.
180		intercept_scid: u64,
181	},
182}
183
184impl_writeable_tlv_based_enum!(LSPS2ServiceEvent,
185	(0, GetInfo) => {
186		(0, request_id, required),
187		(2, counterparty_node_id, required),
188		(4, token, option),
189	},
190	(2, BuyRequest) => {
191		(0, request_id, required),
192		(2, counterparty_node_id, required),
193		(4, opening_fee_params, required),
194		(6, payment_size_msat, option),
195	},
196	(4, OpenChannel) => {
197		(0, their_network_key, required),
198		(2, amt_to_forward_msat, required),
199		(4, opening_fee_msat, required),
200		(6, user_channel_id, required),
201		(8, intercept_scid, required),
202	}
203);