Skip to content

Commit 4abcebc

Browse files
authored
feat: add sort_ascending flag to list_payments (#679)
1 parent 4bd9e5e commit 4abcebc

File tree

13 files changed

+62
-11
lines changed

13 files changed

+62
-11
lines changed

cli/src/commands.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::time::Duration;
66

77
use anyhow::{anyhow, Result};
88
use breez_sdk_liquid::prelude::*;
9-
use clap::{arg, Parser};
9+
use clap::{arg, ArgAction, Parser};
1010
use qrcode_rs::render::unicode;
1111
use qrcode_rs::{EcLevel, QrCode};
1212
use rustyline::highlight::Highlighter;
@@ -125,6 +125,10 @@ pub(crate) enum Command {
125125
/// Optional Liquid/Bitcoin address for Bitcoin payment method
126126
#[clap(short = 'a', long = "address")]
127127
address: Option<String>,
128+
129+
/// Whether or not to sort the payments by ascending timestamp
130+
#[clap(long = "ascending", action = ArgAction::SetTrue)]
131+
sort_ascending: Option<bool>,
128132
},
129133
/// Retrieve a payment
130134
GetPayment {
@@ -490,6 +494,7 @@ pub(crate) async fn handle_command(
490494
offset,
491495
destination,
492496
address,
497+
sort_ascending,
493498
} => {
494499
let details = match (destination, address) {
495500
(Some(destination), None) => Some(ListPaymentDetails::Liquid { destination }),
@@ -506,6 +511,7 @@ pub(crate) async fn handle_command(
506511
limit,
507512
offset,
508513
details,
514+
sort_ascending,
509515
})
510516
.await?;
511517
command_result!(payments)

lib/bindings/langs/flutter/breez_sdk_liquid/include/breez_sdk_liquid.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ typedef struct wire_cst_list_payments_request {
127127
uint32_t *offset;
128128
uint32_t *limit;
129129
struct wire_cst_list_payment_details *details;
130+
bool *sort_ascending;
130131
} wire_cst_list_payments_request;
131132

132133
typedef struct wire_cst_ln_url_auth_request_data {

lib/bindings/src/breez_sdk_liquid.udl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ dictionary ListPaymentsRequest {
539539
u32? offset = null;
540540
u32? limit = null;
541541
ListPaymentDetails? details = null;
542+
boolean? sort_ascending = null;
542543
};
543544

544545
[Enum]

lib/core/src/frb_generated.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2928,6 +2928,7 @@ impl SseDecode for crate::model::ListPaymentsRequest {
29282928
let mut var_offset = <Option<u32>>::sse_decode(deserializer);
29292929
let mut var_limit = <Option<u32>>::sse_decode(deserializer);
29302930
let mut var_details = <Option<crate::model::ListPaymentDetails>>::sse_decode(deserializer);
2931+
let mut var_sortAscending = <Option<bool>>::sse_decode(deserializer);
29312932
return crate::model::ListPaymentsRequest {
29322933
filters: var_filters,
29332934
states: var_states,
@@ -2936,6 +2937,7 @@ impl SseDecode for crate::model::ListPaymentsRequest {
29362937
offset: var_offset,
29372938
limit: var_limit,
29382939
details: var_details,
2940+
sort_ascending: var_sortAscending,
29392941
};
29402942
}
29412943
}
@@ -5243,6 +5245,7 @@ impl flutter_rust_bridge::IntoDart for crate::model::ListPaymentsRequest {
52435245
self.offset.into_into_dart().into_dart(),
52445246
self.limit.into_into_dart().into_dart(),
52455247
self.details.into_into_dart().into_dart(),
5248+
self.sort_ascending.into_into_dart().into_dart(),
52465249
]
52475250
.into_dart()
52485251
}
@@ -7405,6 +7408,7 @@ impl SseEncode for crate::model::ListPaymentsRequest {
74057408
<Option<u32>>::sse_encode(self.offset, serializer);
74067409
<Option<u32>>::sse_encode(self.limit, serializer);
74077410
<Option<crate::model::ListPaymentDetails>>::sse_encode(self.details, serializer);
7411+
<Option<bool>>::sse_encode(self.sort_ascending, serializer);
74087412
}
74097413
}
74107414

@@ -9644,6 +9648,7 @@ mod io {
96449648
offset: self.offset.cst_decode(),
96459649
limit: self.limit.cst_decode(),
96469650
details: self.details.cst_decode(),
9651+
sort_ascending: self.sort_ascending.cst_decode(),
96479652
}
96489653
}
96499654
}
@@ -11089,6 +11094,7 @@ mod io {
1108911094
offset: core::ptr::null_mut(),
1109011095
limit: core::ptr::null_mut(),
1109111096
details: core::ptr::null_mut(),
11097+
sort_ascending: core::ptr::null_mut(),
1109211098
}
1109311099
}
1109411100
}
@@ -13304,6 +13310,7 @@ mod io {
1330413310
offset: *mut u32,
1330513311
limit: *mut u32,
1330613312
details: *mut wire_cst_list_payment_details,
13313+
sort_ascending: *mut bool,
1330713314
}
1330813315
#[repr(C)]
1330913316
#[derive(Clone, Copy)]

lib/core/src/model.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ pub struct ListPaymentsRequest {
620620
pub offset: Option<u32>,
621621
pub limit: Option<u32>,
622622
pub details: Option<ListPaymentDetails>,
623+
pub sort_ascending: Option<bool>,
623624
}
624625

625626
/// An argument of [ListPaymentsRequest] when calling [crate::sdk::LiquidSdk::list_payments].

lib/core/src/persist/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ impl Persister {
356356
where_clause: Option<&str>,
357357
offset: Option<u32>,
358358
limit: Option<u32>,
359+
sort_ascending: Option<bool>,
359360
) -> String {
360361
format!(
361362
"
@@ -438,11 +439,15 @@ impl Persister {
438439
AND ptx.tx_id NOT IN (SELECT refund_tx_id FROM chain_swaps WHERE refund_tx_id NOT NULL))
439440
AND {}
440441
ORDER BY -- Order by swap creation time or tx timestamp (in case of direct tx)
441-
COALESCE(rs.created_at, ss.created_at, cs.created_at, ptx.timestamp) DESC
442+
COALESCE(rs.created_at, ss.created_at, cs.created_at, ptx.timestamp) {}
442443
LIMIT {}
443444
OFFSET {}
444445
",
445446
where_clause.unwrap_or("true"),
447+
match sort_ascending.unwrap_or(false) {
448+
true => "ASC",
449+
false => "DESC",
450+
},
446451
limit.unwrap_or(u32::MAX),
447452
offset.unwrap_or(0),
448453
)
@@ -735,6 +740,7 @@ impl Persister {
735740
Some("(ptx.tx_id = ?1 OR COALESCE(rs.id, ss.id, cs.id) = ?1)"),
736741
None,
737742
None,
743+
None,
738744
),
739745
params![id],
740746
|row| self.sql_row_to_payment(row),
@@ -752,7 +758,7 @@ impl Persister {
752758
Ok(self
753759
.get_connection()?
754760
.query_row(
755-
&self.select_payment_query(Some(where_clause), None, None),
761+
&self.select_payment_query(Some(where_clause), None, None, None),
756762
params![param],
757763
|row| self.sql_row_to_payment(row),
758764
)
@@ -768,8 +774,12 @@ impl Persister {
768774

769775
// Assumes there is no swap chaining (send swap lockup tx = receive swap claim tx)
770776
let con = self.get_connection()?;
771-
let mut stmt =
772-
con.prepare(&self.select_payment_query(maybe_where_clause, req.offset, req.limit))?;
777+
let mut stmt = con.prepare(&self.select_payment_query(
778+
maybe_where_clause,
779+
req.offset,
780+
req.limit,
781+
req.sort_ascending,
782+
))?;
773783
let payments: Vec<Payment> = stmt
774784
.query_map(params_from_iter(where_params), |row| {
775785
self.sql_row_to_payment(row)

packages/dart/lib/src/frb_generated.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,7 +2092,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
20922092
ListPaymentsRequest dco_decode_list_payments_request(dynamic raw) {
20932093
// Codec=Dco (DartCObject based), see doc to use other codecs
20942094
final arr = raw as List<dynamic>;
2095-
if (arr.length != 7) throw Exception('unexpected arr length: expect 7 but see ${arr.length}');
2095+
if (arr.length != 8) throw Exception('unexpected arr length: expect 8 but see ${arr.length}');
20962096
return ListPaymentsRequest(
20972097
filters: dco_decode_opt_list_payment_type(arr[0]),
20982098
states: dco_decode_opt_list_payment_state(arr[1]),
@@ -2101,6 +2101,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
21012101
offset: dco_decode_opt_box_autoadd_u_32(arr[4]),
21022102
limit: dco_decode_opt_box_autoadd_u_32(arr[5]),
21032103
details: dco_decode_opt_box_autoadd_list_payment_details(arr[6]),
2104+
sortAscending: dco_decode_opt_box_autoadd_bool(arr[7]),
21042105
);
21052106
}
21062107

@@ -4179,14 +4180,16 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
41794180
var var_offset = sse_decode_opt_box_autoadd_u_32(deserializer);
41804181
var var_limit = sse_decode_opt_box_autoadd_u_32(deserializer);
41814182
var var_details = sse_decode_opt_box_autoadd_list_payment_details(deserializer);
4183+
var var_sortAscending = sse_decode_opt_box_autoadd_bool(deserializer);
41824184
return ListPaymentsRequest(
41834185
filters: var_filters,
41844186
states: var_states,
41854187
fromTimestamp: var_fromTimestamp,
41864188
toTimestamp: var_toTimestamp,
41874189
offset: var_offset,
41884190
limit: var_limit,
4189-
details: var_details);
4191+
details: var_details,
4192+
sortAscending: var_sortAscending);
41904193
}
41914194

41924195
@protected
@@ -6321,6 +6324,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
63216324
sse_encode_opt_box_autoadd_u_32(self.offset, serializer);
63226325
sse_encode_opt_box_autoadd_u_32(self.limit, serializer);
63236326
sse_encode_opt_box_autoadd_list_payment_details(self.details, serializer);
6327+
sse_encode_opt_box_autoadd_bool(self.sortAscending, serializer);
63246328
}
63256329

63266330
@protected

packages/dart/lib/src/frb_generated.io.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,6 +2554,7 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
25542554
wireObj.offset = cst_encode_opt_box_autoadd_u_32(apiObj.offset);
25552555
wireObj.limit = cst_encode_opt_box_autoadd_u_32(apiObj.limit);
25562556
wireObj.details = cst_encode_opt_box_autoadd_list_payment_details(apiObj.details);
2557+
wireObj.sort_ascending = cst_encode_opt_box_autoadd_bool(apiObj.sortAscending);
25572558
}
25582559

25592560
@protected
@@ -5860,6 +5861,8 @@ final class wire_cst_list_payments_request extends ffi.Struct {
58605861
external ffi.Pointer<ffi.Uint32> limit;
58615862

58625863
external ffi.Pointer<wire_cst_list_payment_details> details;
5864+
5865+
external ffi.Pointer<ffi.Bool> sort_ascending;
58635866
}
58645867

58655868
final class wire_cst_ln_url_auth_request_data extends ffi.Struct {

packages/dart/lib/src/model.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ class ListPaymentsRequest {
453453
final int? offset;
454454
final int? limit;
455455
final ListPaymentDetails? details;
456+
final bool? sortAscending;
456457

457458
const ListPaymentsRequest({
458459
this.filters,
@@ -462,6 +463,7 @@ class ListPaymentsRequest {
462463
this.offset,
463464
this.limit,
464465
this.details,
466+
this.sortAscending,
465467
});
466468

467469
@override
@@ -472,7 +474,8 @@ class ListPaymentsRequest {
472474
toTimestamp.hashCode ^
473475
offset.hashCode ^
474476
limit.hashCode ^
475-
details.hashCode;
477+
details.hashCode ^
478+
sortAscending.hashCode;
476479

477480
@override
478481
bool operator ==(Object other) =>
@@ -485,7 +488,8 @@ class ListPaymentsRequest {
485488
toTimestamp == other.toTimestamp &&
486489
offset == other.offset &&
487490
limit == other.limit &&
488-
details == other.details;
491+
details == other.details &&
492+
sortAscending == other.sortAscending;
489493
}
490494

491495
/// Represents the payment LNURL info

packages/flutter/lib/flutter_breez_liquid_bindings_generated.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4190,6 +4190,8 @@ final class wire_cst_list_payments_request extends ffi.Struct {
41904190
external ffi.Pointer<ffi.Uint32> limit;
41914191

41924192
external ffi.Pointer<wire_cst_list_payment_details> details;
4193+
4194+
external ffi.Pointer<ffi.Bool> sort_ascending;
41934195
}
41944196

41954197
final class wire_cst_ln_url_auth_request_data extends ffi.Struct {

packages/react-native/android/src/main/java/com/breezsdkliquid/BreezSDKLiquidMapper.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,8 @@ fun asListPaymentsRequest(listPaymentsRequest: ReadableMap): ListPaymentsRequest
957957
} else {
958958
null
959959
}
960-
return ListPaymentsRequest(filters, states, fromTimestamp, toTimestamp, offset, limit, details)
960+
val sortAscending = if (hasNonNullKey(listPaymentsRequest, "sortAscending")) listPaymentsRequest.getBoolean("sortAscending") else null
961+
return ListPaymentsRequest(filters, states, fromTimestamp, toTimestamp, offset, limit, details, sortAscending)
961962
}
962963

963964
fun readableMapOf(listPaymentsRequest: ListPaymentsRequest): ReadableMap =
@@ -969,6 +970,7 @@ fun readableMapOf(listPaymentsRequest: ListPaymentsRequest): ReadableMap =
969970
"offset" to listPaymentsRequest.offset,
970971
"limit" to listPaymentsRequest.limit,
971972
"details" to listPaymentsRequest.details?.let { readableMapOf(it) },
973+
"sortAscending" to listPaymentsRequest.sortAscending,
972974
)
973975

974976
fun asListPaymentsRequestList(arr: ReadableArray): List<ListPaymentsRequest> {

packages/react-native/ios/BreezSDKLiquidMapper.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,15 @@ enum BreezSDKLiquidMapper {
11351135
details = try asListPaymentDetails(listPaymentDetails: detailsTmp)
11361136
}
11371137

1138-
return ListPaymentsRequest(filters: filters, states: states, fromTimestamp: fromTimestamp, toTimestamp: toTimestamp, offset: offset, limit: limit, details: details)
1138+
var sortAscending: Bool?
1139+
if hasNonNilKey(data: listPaymentsRequest, key: "sortAscending") {
1140+
guard let sortAscendingTmp = listPaymentsRequest["sortAscending"] as? Bool else {
1141+
throw SdkError.Generic(message: errUnexpectedValue(fieldName: "sortAscending"))
1142+
}
1143+
sortAscending = sortAscendingTmp
1144+
}
1145+
1146+
return ListPaymentsRequest(filters: filters, states: states, fromTimestamp: fromTimestamp, toTimestamp: toTimestamp, offset: offset, limit: limit, details: details, sortAscending: sortAscending)
11391147
}
11401148

11411149
static func dictionaryOf(listPaymentsRequest: ListPaymentsRequest) -> [String: Any?] {
@@ -1147,6 +1155,7 @@ enum BreezSDKLiquidMapper {
11471155
"offset": listPaymentsRequest.offset == nil ? nil : listPaymentsRequest.offset,
11481156
"limit": listPaymentsRequest.limit == nil ? nil : listPaymentsRequest.limit,
11491157
"details": listPaymentsRequest.details == nil ? nil : dictionaryOf(listPaymentDetails: listPaymentsRequest.details!),
1158+
"sortAscending": listPaymentsRequest.sortAscending == nil ? nil : listPaymentsRequest.sortAscending,
11501159
]
11511160
}
11521161

packages/react-native/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ export interface ListPaymentsRequest {
183183
offset?: number
184184
limit?: number
185185
details?: ListPaymentDetails
186+
sortAscending?: boolean
186187
}
187188

188189
export interface LnOfferBlindedPath {

0 commit comments

Comments
 (0)