Skip to content

Commit

Permalink
Merge pull request #559 from breez/ok300-default-trait-requests
Browse files Browse the repository at this point in the history
Add Default trait, util constructor for Request args
  • Loading branch information
ok300 authored Oct 27, 2023
2 parents 0f90197 + cc7bafb commit 8bffae0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 88 deletions.
31 changes: 5 additions & 26 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))?
Expand Down Expand Up @@ -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();

Expand All @@ -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()]);
Expand All @@ -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()]);
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions libs/sdk-core/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ pub struct Payment {
}

/// Represents a list payments request.
#[derive(Default)]
pub struct ListPaymentsRequest {
pub filters: Option<Vec<PaymentTypeFilter>>,
pub from_timestamp: Option<i64>,
Expand Down Expand Up @@ -724,13 +725,13 @@ pub struct ClosedChannelPaymentDetails {
pub closing_txid: Option<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ReverseSwapFeesRequest {
pub send_amount_sat: Option<u64>,
}

/// 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,
Expand Down Expand Up @@ -763,7 +764,7 @@ 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<u64>,
}

Expand Down Expand Up @@ -803,7 +804,7 @@ pub struct OpenChannelFeeResponse {
pub used_fee_params: Option<OpeningFeeParams>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ReceiveOnchainRequest {
pub opening_fee_params: Option<OpeningFeeParams>,
}
Expand Down
59 changes: 9 additions & 50 deletions libs/sdk-core/src/persist/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {
)?;

// 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);

Expand All @@ -491,11 +484,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {
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]);
Expand All @@ -509,11 +498,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {
//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]);
Expand All @@ -522,36 +507,18 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {
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);

Expand All @@ -561,33 +528,25 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {
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);
Expand Down
11 changes: 3 additions & 8 deletions tools/sdk-cli/src/command_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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}"))?;

Expand Down Expand Up @@ -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()),
Expand Down

0 comments on commit 8bffae0

Please sign in to comment.