Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 9d4f1b3

Browse files
committed
Use JSON strings for amount fields in LSPS1
1 parent a6b509f commit 9d4f1b3

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

src/lsps1/msgs.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Message, request, and other primitive types used to implement LSPS1.
22
33
use crate::lsps0::msgs::{LSPSMessage, RequestId, ResponseError};
4+
use crate::lsps0::ser::{string_amount, string_amount_option};
5+
46
use crate::prelude::{String, Vec};
57

68
use serde::{Deserialize, Serialize};
@@ -41,20 +43,27 @@ pub struct OptionsSupported {
4143
pub supports_zero_channel_reserve: bool,
4244
/// Indicates the minimum amount of satoshi that is required for the LSP to accept a payment
4345
/// on-chain.
44-
pub min_onchain_payment_size_sat: Option<u32>,
46+
#[serde(with = "string_amount_option")]
47+
pub min_onchain_payment_size_sat: Option<u64>,
4548
/// The maximum number of blocks a channel can be leased for.
4649
pub max_channel_expiry_blocks: u32,
4750
/// The minimum number of satoshi that the client MUST request.
51+
#[serde(with = "string_amount")]
4852
pub min_initial_client_balance_sat: u64,
4953
/// The maximum number of satoshi that the client MUST request.
54+
#[serde(with = "string_amount")]
5055
pub max_initial_client_balance_sat: u64,
5156
/// The minimum number of satoshi that the LSP will provide to the channel.
57+
#[serde(with = "string_amount")]
5258
pub min_initial_lsp_balance_sat: u64,
5359
/// The maximum number of satoshi that the LSP will provide to the channel.
60+
#[serde(with = "string_amount")]
5461
pub max_initial_lsp_balance_sat: u64,
5562
/// The minimal channel size.
63+
#[serde(with = "string_amount")]
5664
pub min_channel_balance_sat: u64,
5765
/// The maximal channel size.
66+
#[serde(with = "string_amount")]
5867
pub max_channel_balance_sat: u64,
5968
}
6069

@@ -81,11 +90,13 @@ pub struct CreateOrderRequest {
8190
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
8291
pub struct OrderParams {
8392
/// Indicates how many satoshi the LSP will provide on their side.
93+
#[serde(with = "string_amount")]
8494
pub lsp_balance_sat: u64,
8595
/// Indicates how many satoshi the client will provide on their side.
8696
///
8797
/// The client sends these funds to the LSP, who will push them back to the client upon opening
8898
/// the channel.
99+
#[serde(with = "string_amount")]
89100
pub client_balance_sat: u64,
90101
/// The number of blocks the client wants to wait maximally for the channel to be confirmed.
91102
pub confirms_within_blocks: u32,
@@ -135,8 +146,10 @@ pub struct OrderPayment {
135146
/// Indicates the current state of the payment.
136147
pub state: PaymentState,
137148
/// The total fee the LSP will charge to open this channel in satoshi.
149+
#[serde(with = "string_amount")]
138150
pub fee_total_sat: u64,
139151
/// What the client needs to pay in total to open the requested channel.
152+
#[serde(with = "string_amount")]
140153
pub order_total_sat: u64,
141154
/// A BOLT11 invoice the client can pay to have to channel opened.
142155
pub bolt11_invoice: String,
@@ -172,6 +185,7 @@ pub struct OnchainPayment {
172185
/// The outpoint of the payment.
173186
pub outpoint: String,
174187
/// The amount of satoshi paid.
188+
#[serde(with = "string_amount")]
175189
pub sat: u64,
176190
/// Indicates if the LSP regards the transaction as sufficiently confirmed.
177191
pub confirmed: bool,
@@ -287,3 +301,42 @@ impl From<LSPS1Message> for LSPSMessage {
287301
LSPSMessage::LSPS1(message)
288302
}
289303
}
304+
305+
#[cfg(test)]
306+
mod tests {
307+
use super::*;
308+
use crate::alloc::string::ToString;
309+
310+
#[test]
311+
fn options_supported_serialization() {
312+
let min_channel_confirmations = 6;
313+
let min_onchain_payment_confirmations = Some(6);
314+
let supports_zero_channel_reserve = true;
315+
let min_onchain_payment_size_sat = Some(100_000);
316+
let max_channel_expiry_blocks = 144;
317+
let min_initial_client_balance_sat = 10_000_000;
318+
let max_initial_client_balance_sat = 100_000_000;
319+
let min_initial_lsp_balance_sat = 100_000;
320+
let max_initial_lsp_balance_sat = 100_000_000;
321+
let min_channel_balance_sat = 100_000;
322+
let max_channel_balance_sat = 100_000_000;
323+
324+
let options_supported = OptionsSupported {
325+
min_channel_confirmations,
326+
min_onchain_payment_confirmations,
327+
supports_zero_channel_reserve,
328+
min_onchain_payment_size_sat,
329+
max_channel_expiry_blocks,
330+
min_initial_client_balance_sat,
331+
max_initial_client_balance_sat,
332+
min_initial_lsp_balance_sat,
333+
max_initial_lsp_balance_sat,
334+
min_channel_balance_sat,
335+
max_channel_balance_sat,
336+
};
337+
338+
let json_str = r#"{"max_channel_balance_sat":"100000000","max_channel_expiry_blocks":144,"max_initial_client_balance_sat":"100000000","max_initial_lsp_balance_sat":"100000000","min_channel_balance_sat":"100000","min_channel_confirmations":6,"min_initial_client_balance_sat":"10000000","min_initial_lsp_balance_sat":"100000","min_onchain_payment_confirmations":6,"min_onchain_payment_size_sat":"100000","supports_zero_channel_reserve":true}"#;
339+
assert_eq!(json_str, serde_json::json!(options_supported).to_string());
340+
assert_eq!(options_supported, serde_json::from_str(json_str).unwrap());
341+
}
342+
}

0 commit comments

Comments
 (0)