From f244e1925a1871b1fe153b914011e2201d6e0e28 Mon Sep 17 00:00:00 2001 From: ok300 <106775972+ok300@users.noreply.github.com> Date: Thu, 26 Oct 2023 13:29:02 +0200 Subject: [PATCH 1/2] Add Default trait, util constructor for Request args --- libs/sdk-core/src/breez_services.rs | 38 +++------------ libs/sdk-core/src/models.rs | 46 ++++++++++++++++-- libs/sdk-core/src/persist/transactions.rs | 59 ++++------------------- tools/sdk-cli/src/command_handlers.rs | 16 ++---- 4 files changed, 63 insertions(+), 96 deletions(-) diff --git a/libs/sdk-core/src/breez_services.rs b/libs/sdk-core/src/breez_services.rs index 48e5da435..7a2fb11e0 100644 --- a/libs/sdk-core/src/breez_services.rs +++ b/libs/sdk-core/src/breez_services.rs @@ -356,11 +356,8 @@ impl BreezServices { .receive_payment(ReceivePaymentRequest { amount_msat: req.amount_msat, description: req.description.unwrap_or_default(), - preimage: None, - opening_fee_params: None, use_description_hash: Some(false), - expiry: None, - cltv: None, + ..Default::default() }) .await .map_err(|_| anyhow!("Failed to receive payment"))? @@ -2050,14 +2047,7 @@ pub(crate) mod tests { assert_eq!(fetched_state, dummy_node_state); let all = breez_services - .list_payments(ListPaymentsRequest { - filters: None, - from_timestamp: None, - to_timestamp: None, - include_failures: None, - offset: None, - limit: None, - }) + .list_payments(ListPaymentsRequest::default()) .await?; let mut cloned = all.clone(); @@ -2068,11 +2058,7 @@ pub(crate) mod tests { let received = breez_services .list_payments(ListPaymentsRequest { filters: Some(vec![PaymentTypeFilter::Received]), - from_timestamp: None, - to_timestamp: None, - include_failures: None, - offset: None, - limit: None, + ..Default::default() }) .await?; assert_eq!(received, vec![cloned[1].clone(), cloned[0].clone()]); @@ -2083,11 +2069,7 @@ pub(crate) mod tests { PaymentTypeFilter::Sent, PaymentTypeFilter::ClosedChannels, ]), - from_timestamp: None, - to_timestamp: None, - include_failures: None, - offset: None, - limit: None, + ..Default::default() }) .await?; assert_eq!(sent, vec![cloned[2].clone()]); @@ -2131,11 +2113,8 @@ pub(crate) mod tests { .receive_payment(ReceivePaymentRequest { amount_msat: 3_000_000, description: "should populate lsp hints".to_string(), - preimage: None, - opening_fee_params: None, use_description_hash: Some(false), - expiry: None, - cltv: None, + ..Default::default() }) .await? .ln_invoice; @@ -2189,10 +2168,9 @@ pub(crate) mod tests { let breez_services = breez_services().await?; breez_services.sync().await?; let moonpay_url = breez_services - .buy_bitcoin(BuyBitcoinRequest { - provider: BuyBitcoinProvider::Moonpay, - opening_fee_params: None, - }) + .buy_bitcoin(BuyBitcoinRequest::from_provider( + BuyBitcoinProvider::Moonpay, + )) .await? .url; let parsed = Url::parse(&moonpay_url)?; diff --git a/libs/sdk-core/src/models.rs b/libs/sdk-core/src/models.rs index a4b09fdb8..f066148d8 100644 --- a/libs/sdk-core/src/models.rs +++ b/libs/sdk-core/src/models.rs @@ -657,6 +657,7 @@ pub struct Payment { } /// Represents a list payments request. +#[derive(Default)] pub struct ListPaymentsRequest { pub filters: Option>, pub from_timestamp: Option, @@ -724,13 +725,13 @@ pub struct ClosedChannelPaymentDetails { pub closing_txid: Option, } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Default, Serialize, Deserialize)] pub struct ReverseSwapFeesRequest { pub send_amount_sat: Option, } /// Represents a receive payment request. -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Default, Serialize, Deserialize)] pub struct ReceivePaymentRequest { /// The amount in satoshis for this payment request pub amount_msat: u64, @@ -750,6 +751,16 @@ pub struct ReceivePaymentRequest { pub cltv: Option, } +impl ReceivePaymentRequest { + pub fn from_amount_and_description(amount_msat: u64, description: String) -> Self { + ReceivePaymentRequest { + amount_msat, + description, + ..Default::default() + } + } +} + /// Represents a receive payment response. #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ReceivePaymentResponse { @@ -763,10 +774,19 @@ pub struct ReceivePaymentResponse { pub struct SendPaymentRequest { /// The bolt11 invoice pub bolt11: String, - /// The amount to pay in millisatoshis + /// The amount to pay in millisatoshis. Should only be set when `bolt11` is a zero-amount invoice. pub amount_msat: Option, } +impl SendPaymentRequest { + pub fn from_bolt11(bolt11: String) -> Self { + SendPaymentRequest { + bolt11, + amount_msat: None, + } + } +} + /// Represents a send spontaneous payment request. #[derive(Clone, Debug, Serialize, Deserialize)] pub struct SendSpontaneousPaymentRequest { @@ -797,13 +817,22 @@ pub struct OpenChannelFeeRequest { pub expiry: Option, } +impl OpenChannelFeeRequest { + pub fn from_amount(amount_msat: u64) -> Self { + OpenChannelFeeRequest { + amount_msat, + expiry: None, + } + } +} + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct OpenChannelFeeResponse { pub fee_msat: u64, pub used_fee_params: Option, } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Default, Serialize, Deserialize)] pub struct ReceiveOnchainRequest { pub opening_fee_params: Option, } @@ -814,6 +843,15 @@ pub struct BuyBitcoinRequest { pub opening_fee_params: Option, } +impl BuyBitcoinRequest { + pub fn from_provider(provider: BuyBitcoinProvider) -> Self { + BuyBitcoinRequest { + provider, + opening_fee_params: None, + } + } +} + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct BuyBitcoinResponse { pub url: String, diff --git a/libs/sdk-core/src/persist/transactions.rs b/libs/sdk-core/src/persist/transactions.rs index 69ed24077..94a6b4de5 100644 --- a/libs/sdk-core/src/persist/transactions.rs +++ b/libs/sdk-core/src/persist/transactions.rs @@ -474,14 +474,7 @@ fn test_ln_transactions() -> Result<(), Box> { )?; // retrieve all - let retrieve_txs = storage.list_payments(ListPaymentsRequest { - filters: None, - from_timestamp: None, - to_timestamp: None, - include_failures: None, - offset: None, - limit: None, - })?; + let retrieve_txs = storage.list_payments(ListPaymentsRequest::default())?; assert_eq!(retrieve_txs.len(), 2); assert_eq!(retrieve_txs, txs); @@ -491,11 +484,7 @@ fn test_ln_transactions() -> Result<(), Box> { PaymentTypeFilter::Sent, PaymentTypeFilter::ClosedChannels, ]), - from_timestamp: None, - to_timestamp: None, - include_failures: None, - offset: None, - limit: None, + ..Default::default() })?; assert_eq!(retrieve_txs.len(), 1); assert_eq!(retrieve_txs[0], txs[0]); @@ -509,11 +498,7 @@ fn test_ln_transactions() -> Result<(), Box> { //test only received let retrieve_txs = storage.list_payments(ListPaymentsRequest { filters: Some(vec![PaymentTypeFilter::Received]), - from_timestamp: None, - to_timestamp: None, - include_failures: None, - offset: None, - limit: None, + ..Default::default() })?; assert_eq!(retrieve_txs.len(), 1); assert_eq!(retrieve_txs[0], txs[1]); @@ -522,36 +507,18 @@ fn test_ln_transactions() -> Result<(), Box> { assert_eq!(max_ts, 2000); storage.insert_or_update_payments(&txs)?; - let retrieve_txs = storage.list_payments(ListPaymentsRequest { - filters: None, - from_timestamp: None, - to_timestamp: None, - include_failures: None, - offset: None, - limit: None, - })?; + let retrieve_txs = storage.list_payments(ListPaymentsRequest::default())?; assert_eq!(retrieve_txs.len(), 2); assert_eq!(retrieve_txs, txs); storage.insert_open_channel_payment_info("123", 150)?; - let retrieve_txs = storage.list_payments(ListPaymentsRequest { - filters: None, - from_timestamp: None, - to_timestamp: None, - include_failures: None, - offset: None, - limit: None, - })?; + let retrieve_txs = storage.list_payments(ListPaymentsRequest::default())?; assert_eq!(retrieve_txs[0].fee_msat, 50); // test all with failures let retrieve_txs = storage.list_payments(ListPaymentsRequest { - filters: None, - from_timestamp: None, - to_timestamp: None, include_failures: Some(true), - offset: None, - limit: None, + ..Default::default() })?; assert_eq!(retrieve_txs.len(), 3); @@ -561,33 +528,25 @@ fn test_ln_transactions() -> Result<(), Box> { PaymentTypeFilter::Sent, PaymentTypeFilter::ClosedChannels, ]), - from_timestamp: None, - to_timestamp: None, include_failures: Some(true), - offset: None, - limit: None, + ..Default::default() })?; assert_eq!(retrieve_txs.len(), 2); // test limit let retrieve_txs = storage.list_payments(ListPaymentsRequest { - filters: None, - from_timestamp: None, - to_timestamp: None, include_failures: Some(false), - offset: None, limit: Some(1), + ..Default::default() })?; assert_eq!(retrieve_txs.len(), 1); // test offset let retrieve_txs = storage.list_payments(ListPaymentsRequest { - filters: None, - from_timestamp: None, - to_timestamp: None, include_failures: Some(false), offset: Some(1), limit: Some(1), + ..Default::default() })?; assert_eq!(retrieve_txs.len(), 1); assert_eq!(retrieve_txs[0].id, payment_hash_with_lnurl_withdraw); diff --git a/tools/sdk-cli/src/command_handlers.rs b/tools/sdk-cli/src/command_handlers.rs index 46f4a98f3..b9f57b92f 100644 --- a/tools/sdk-cli/src/command_handlers.rs +++ b/tools/sdk-cli/src/command_handlers.rs @@ -132,11 +132,10 @@ pub(crate) async fn handle_command( .receive_payment(ReceivePaymentRequest { amount_msat, description, - preimage: None, - opening_fee_params: None, use_description_hash, expiry, cltv, + ..Default::default() }) .await?; let mut result = serde_json::to_string(&recv_payment_response)?; @@ -150,9 +149,7 @@ pub(crate) async fn handle_command( sat_per_vbyte, } => { let pair_info = sdk()? - .fetch_reverse_swap_fees(ReverseSwapFeesRequest { - send_amount_sat: None, - }) + .fetch_reverse_swap_fees(ReverseSwapFeesRequest::default()) .await .map_err(|e| anyhow!("Failed to fetch reverse swap fee infos: {e}"))?; @@ -297,9 +294,7 @@ pub(crate) async fn handle_command( } Commands::ReceiveOnchain {} => serde_json::to_string_pretty( &sdk()? - .receive_onchain(ReceiveOnchainRequest { - opening_fee_params: None, - }) + .receive_onchain(ReceiveOnchainRequest::default()) .await?, ) .map_err(|e| e.into()), @@ -437,10 +432,7 @@ pub(crate) async fn handle_command( } Commands::BuyBitcoin { provider } => { let res = sdk()? - .buy_bitcoin(BuyBitcoinRequest { - provider: provider.clone(), - opening_fee_params: None, - }) + .buy_bitcoin(BuyBitcoinRequest::from_provider(provider.clone())) .await?; Ok(format!("Here your {:?} url: {}", provider, res.url)) } From cc7bafb5a28e6d7cf59faacee2f4f813961b4f54 Mon Sep 17 00:00:00 2001 From: ok300 <106775972+ok300@users.noreply.github.com> Date: Fri, 27 Oct 2023 17:50:12 +0200 Subject: [PATCH 2/2] Removed from..() constructors --- libs/sdk-core/src/breez_services.rs | 7 ++--- libs/sdk-core/src/models.rs | 37 --------------------------- tools/sdk-cli/src/command_handlers.rs | 5 +++- 3 files changed, 8 insertions(+), 41 deletions(-) diff --git a/libs/sdk-core/src/breez_services.rs b/libs/sdk-core/src/breez_services.rs index 7a2fb11e0..443459baf 100644 --- a/libs/sdk-core/src/breez_services.rs +++ b/libs/sdk-core/src/breez_services.rs @@ -2168,9 +2168,10 @@ pub(crate) mod tests { let breez_services = breez_services().await?; breez_services.sync().await?; let moonpay_url = breez_services - .buy_bitcoin(BuyBitcoinRequest::from_provider( - BuyBitcoinProvider::Moonpay, - )) + .buy_bitcoin(BuyBitcoinRequest { + provider: BuyBitcoinProvider::Moonpay, + opening_fee_params: None, + }) .await? .url; let parsed = Url::parse(&moonpay_url)?; diff --git a/libs/sdk-core/src/models.rs b/libs/sdk-core/src/models.rs index f066148d8..61276c68c 100644 --- a/libs/sdk-core/src/models.rs +++ b/libs/sdk-core/src/models.rs @@ -751,16 +751,6 @@ pub struct ReceivePaymentRequest { pub cltv: Option, } -impl ReceivePaymentRequest { - pub fn from_amount_and_description(amount_msat: u64, description: String) -> Self { - ReceivePaymentRequest { - amount_msat, - description, - ..Default::default() - } - } -} - /// Represents a receive payment response. #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ReceivePaymentResponse { @@ -778,15 +768,6 @@ pub struct SendPaymentRequest { pub amount_msat: Option, } -impl SendPaymentRequest { - pub fn from_bolt11(bolt11: String) -> Self { - SendPaymentRequest { - bolt11, - amount_msat: None, - } - } -} - /// Represents a send spontaneous payment request. #[derive(Clone, Debug, Serialize, Deserialize)] pub struct SendSpontaneousPaymentRequest { @@ -817,15 +798,6 @@ pub struct OpenChannelFeeRequest { pub expiry: Option, } -impl OpenChannelFeeRequest { - pub fn from_amount(amount_msat: u64) -> Self { - OpenChannelFeeRequest { - amount_msat, - expiry: None, - } - } -} - #[derive(Clone, Debug, Serialize, Deserialize)] pub struct OpenChannelFeeResponse { pub fee_msat: u64, @@ -843,15 +815,6 @@ pub struct BuyBitcoinRequest { pub opening_fee_params: Option, } -impl BuyBitcoinRequest { - pub fn from_provider(provider: BuyBitcoinProvider) -> Self { - BuyBitcoinRequest { - provider, - opening_fee_params: None, - } - } -} - #[derive(Clone, Debug, Serialize, Deserialize)] pub struct BuyBitcoinResponse { pub url: String, diff --git a/tools/sdk-cli/src/command_handlers.rs b/tools/sdk-cli/src/command_handlers.rs index b9f57b92f..6c7f1c796 100644 --- a/tools/sdk-cli/src/command_handlers.rs +++ b/tools/sdk-cli/src/command_handlers.rs @@ -432,7 +432,10 @@ pub(crate) async fn handle_command( } Commands::BuyBitcoin { provider } => { let res = sdk()? - .buy_bitcoin(BuyBitcoinRequest::from_provider(provider.clone())) + .buy_bitcoin(BuyBitcoinRequest { + provider: provider.clone(), + opening_fee_params: None, + }) .await?; Ok(format!("Here your {:?} url: {}", provider, res.url)) }