Skip to content

Commit

Permalink
Define list payments request filters as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
dleutenegger committed Oct 24, 2023
1 parent bc9462f commit 970a13b
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 49 deletions.
2 changes: 1 addition & 1 deletion libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ dictionary Payment {
};

dictionary ListPaymentsRequest {
sequence<PaymentTypeFilter> filters;
sequence<PaymentTypeFilter>? filters = null;
i64? from_timestamp = null;
i64? to_timestamp = null;
boolean? include_failures = null;
Expand Down
9 changes: 6 additions & 3 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2032,7 +2032,7 @@ pub(crate) mod tests {

let all = breez_services
.list_payments(ListPaymentsRequest {
filters: vec![],
filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand All @@ -2048,7 +2048,7 @@ pub(crate) mod tests {

let received = breez_services
.list_payments(ListPaymentsRequest {
filters: vec![PaymentTypeFilter::Received],
filters: Some(vec![PaymentTypeFilter::Received]),
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand All @@ -2060,7 +2060,10 @@ pub(crate) mod tests {

let sent = breez_services
.list_payments(ListPaymentsRequest {
filters: vec![PaymentTypeFilter::Sent, PaymentTypeFilter::ClosedChannels],
filters: Some(vec![
PaymentTypeFilter::Sent,
PaymentTypeFilter::ClosedChannels,
]),
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-core/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ pub struct Payment {

/// Represents a list payments request.
pub struct ListPaymentsRequest {
pub filters: Vec<PaymentTypeFilter>,
pub filters: Option<Vec<PaymentTypeFilter>>,
pub from_timestamp: Option<i64>,
pub to_timestamp: Option<i64>,
pub include_failures: Option<bool>,
Expand Down
70 changes: 39 additions & 31 deletions libs/sdk-core/src/persist/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ impl SqliteStorage {
}

fn filter_to_where_clause(
type_filters: Vec<PaymentTypeFilter>,
type_filters: Option<Vec<PaymentTypeFilter>>,
from_timestamp: Option<i64>,
to_timestamp: Option<i64>,
include_failures: Option<bool>,
Expand All @@ -281,30 +281,32 @@ fn filter_to_where_clause(
where_clause.push(format!("status != {}", PaymentStatus::Failed as i64));
};

if !type_filters.is_empty() {
let mut type_filter_clause: HashSet<PaymentType> = HashSet::new();
for type_filter in type_filters {
match type_filter {
PaymentTypeFilter::Sent => {
type_filter_clause.insert(PaymentType::Sent);
}
PaymentTypeFilter::Received => {
type_filter_clause.insert(PaymentType::Received);
}
PaymentTypeFilter::ClosedChannels => {
type_filter_clause.insert(PaymentType::ClosedChannel);
if let Some(filters) = type_filters {
if !filters.is_empty() {
let mut type_filter_clause: HashSet<PaymentType> = HashSet::new();
for type_filter in filters {
match type_filter {
PaymentTypeFilter::Sent => {
type_filter_clause.insert(PaymentType::Sent);
}
PaymentTypeFilter::Received => {
type_filter_clause.insert(PaymentType::Received);
}
PaymentTypeFilter::ClosedChannels => {
type_filter_clause.insert(PaymentType::ClosedChannel);
}
}
}
}

where_clause.push(format!(
"payment_type in ({})",
type_filter_clause
.iter()
.map(|t| format!("'{}'", t))
.collect::<Vec<_>>()
.join(", ")
));
where_clause.push(format!(
"payment_type in ({})",
type_filter_clause
.iter()
.map(|t| format!("'{}'", t))
.collect::<Vec<_>>()
.join(", ")
));
}
}

let mut where_clause_str = String::new();
Expand Down Expand Up @@ -473,7 +475,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

// retrieve all
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filters: vec![],
filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand All @@ -485,7 +487,10 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

//test only sent
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filters: vec![PaymentTypeFilter::Sent, PaymentTypeFilter::ClosedChannels],
filters: Some(vec![
PaymentTypeFilter::Sent,
PaymentTypeFilter::ClosedChannels,
]),
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand All @@ -503,7 +508,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

//test only received
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filters: vec![PaymentTypeFilter::Received],
filters: Some(vec![PaymentTypeFilter::Received]),
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand All @@ -518,7 +523,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

storage.insert_or_update_payments(&txs)?;
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filters: vec![],
filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand All @@ -530,7 +535,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

storage.insert_open_channel_payment_info("123", 150)?;
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filters: vec![],
filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand All @@ -541,7 +546,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

// test all with failures
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filters: vec![],
filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: Some(true),
Expand All @@ -552,7 +557,10 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

// test sent with failures
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filters: vec![PaymentTypeFilter::Sent, PaymentTypeFilter::ClosedChannels],
filters: Some(vec![
PaymentTypeFilter::Sent,
PaymentTypeFilter::ClosedChannels,
]),
from_timestamp: None,
to_timestamp: None,
include_failures: Some(true),
Expand All @@ -563,7 +571,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

// test limit
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filters: vec![],
filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: Some(false),
Expand All @@ -574,7 +582,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

// test offset
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filters: vec![],
filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: Some(false),
Expand Down
12 changes: 9 additions & 3 deletions libs/sdk-flutter/lib/bridge_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -571,15 +571,15 @@ class InvoicePaidDetails {

/// Represents a list payments request.
class ListPaymentsRequest {
final List<PaymentTypeFilter> filters;
final List<PaymentTypeFilter>? filters;
final int? fromTimestamp;
final int? toTimestamp;
final bool? includeFailures;
final int? offset;
final int? limit;

const ListPaymentsRequest({
required this.filters,
this.filters,
this.fromTimestamp,
this.toTimestamp,
this.includeFailures,
Expand Down Expand Up @@ -3568,6 +3568,12 @@ class BreezSdkCorePlatform extends FlutterRustBridgeBase<BreezSdkCoreWire> {
return raw == null ? ffi.nullptr : api2wire_box_autoadd_u64(raw);
}

@protected
ffi.Pointer<wire_list_payment_type_filter> api2wire_opt_list_payment_type_filter(
List<PaymentTypeFilter>? raw) {
return raw == null ? ffi.nullptr : api2wire_list_payment_type_filter(raw);
}

@protected
ffi.Pointer<wire_uint_8_list> api2wire_opt_uint_8_list(Uint8List? raw) {
return raw == null ? ffi.nullptr : api2wire_uint_8_list(raw);
Expand Down Expand Up @@ -3733,7 +3739,7 @@ class BreezSdkCorePlatform extends FlutterRustBridgeBase<BreezSdkCoreWire> {
}

void _api_fill_to_wire_list_payments_request(ListPaymentsRequest apiObj, wire_ListPaymentsRequest wireObj) {
wireObj.filters = api2wire_list_payment_type_filter(apiObj.filters);
wireObj.filters = api2wire_opt_list_payment_type_filter(apiObj.filters);
wireObj.from_timestamp = api2wire_opt_box_autoadd_i64(apiObj.fromTimestamp);
wireObj.to_timestamp = api2wire_opt_box_autoadd_i64(apiObj.toTimestamp);
wireObj.include_failures = api2wire_opt_box_autoadd_bool(apiObj.includeFailures);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,14 +726,19 @@ fun asLnInvoiceList(arr: ReadableArray): List<LnInvoice> {
fun asListPaymentsRequest(listPaymentsRequest: ReadableMap): ListPaymentsRequest? {
if (!validateMandatoryFields(
listPaymentsRequest,
arrayOf(
"filters",
),
arrayOf(),
)
) {
return null
}
val filters = listPaymentsRequest.getArray("filters")?.let { asPaymentTypeFilterList(it) }!!
val filters =
if (hasNonNullKey(listPaymentsRequest, "filters")) {
listPaymentsRequest.getArray("filters")?.let {
asPaymentTypeFilterList(it)
}
} else {
null
}
val fromTimestamp =
if (hasNonNullKey(
listPaymentsRequest,
Expand Down Expand Up @@ -769,7 +774,7 @@ fun asListPaymentsRequest(listPaymentsRequest: ReadableMap): ListPaymentsRequest

fun readableMapOf(listPaymentsRequest: ListPaymentsRequest): ReadableMap {
return readableMapOf(
"filters" to readableArrayOf(listPaymentsRequest.filters),
"filters" to listPaymentsRequest.filters?.let { readableArrayOf(it) },
"fromTimestamp" to listPaymentsRequest.fromTimestamp,
"toTimestamp" to listPaymentsRequest.toTimestamp,
"includeFailures" to listPaymentsRequest.includeFailures,
Expand Down
8 changes: 5 additions & 3 deletions libs/sdk-react-native/ios/BreezSDKMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,10 @@ class BreezSDKMapper {
}

static func asListPaymentsRequest(listPaymentsRequest: [String: Any?]) throws -> ListPaymentsRequest {
guard let filtersTmp = listPaymentsRequest["filters"] as? [String] else { throw SdkError.Generic(message: "Missing mandatory field filters for type ListPaymentsRequest") }
let filters = filtersTmp
var filters: [PaymentTypeFilter]?
if let filtersTmp = listPaymentsRequest["filters"] as? [String] {
filters = filtersTmp
}

let fromTimestamp = listPaymentsRequest["fromTimestamp"] as? Int64
let toTimestamp = listPaymentsRequest["toTimestamp"] as? Int64
Expand All @@ -678,7 +680,7 @@ class BreezSDKMapper {

static func dictionaryOf(listPaymentsRequest: ListPaymentsRequest) -> [String: Any?] {
return [
"filters": arrayOf(paymentTypeFilterList: listPaymentsRequest.filters),
"filters": listPaymentsRequest.filters == nil ? nil : arrayOf(paymentTypeFilterList: listPaymentsRequest.filters!),
"fromTimestamp": listPaymentsRequest.fromTimestamp == nil ? nil : listPaymentsRequest.fromTimestamp,
"toTimestamp": listPaymentsRequest.toTimestamp == nil ? nil : listPaymentsRequest.toTimestamp,
"includeFailures": listPaymentsRequest.includeFailures == nil ? nil : listPaymentsRequest.includeFailures,
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-react-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export type LnInvoice = {
}

export type ListPaymentsRequest = {
filters: PaymentTypeFilter[]
filters?: PaymentTypeFilter[]
fromTimestamp?: number
toTimestamp?: number
includeFailures?: boolean
Expand Down
2 changes: 1 addition & 1 deletion tools/sdk-cli/src/command_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub(crate) async fn handle_command(
} => {
let payments = sdk()?
.list_payments(ListPaymentsRequest {
filters: vec![],
filters: None,
from_timestamp,
to_timestamp,
include_failures: Some(include_failures),
Expand Down

0 comments on commit 970a13b

Please sign in to comment.