@@ -401,67 +401,66 @@ class Client {
401
401
}
402
402
}
403
403
404
- Future <List <ITransaction >> getTransactions (
405
- {String ? assetCodeFilter, int ? limit, int ? offset}) async {
404
+ Future <List <ITransaction >> getTransactions ({
405
+ String ? assetCodeFilter,
406
+ int limit = 10 ,
407
+ String ? pagingToken,
408
+ }) async {
406
409
try {
407
- var paymentsRequest =
408
- _sdk.payments.forAccount (accountId).order (RequestBuilderOrder .DESC );
410
+ var paymentsRequest = _sdk.payments
411
+ .forAccount (accountId)
412
+ .order (RequestBuilderOrder .DESC )
413
+ .limit (limit);
409
414
410
- // Add pagination
411
- if (limit != null ) {
412
- paymentsRequest = paymentsRequest.limit (limit);
415
+ if (pagingToken != null ) {
416
+ paymentsRequest = paymentsRequest.cursor (pagingToken);
413
417
}
414
418
415
- // get first page
416
- if (offset != null && offset > 0 ) {
417
- var initialPage = await _sdk.payments
418
- .forAccount (accountId)
419
- .order (RequestBuilderOrder .DESC )
420
- .limit (offset)
421
- .execute ();
422
-
423
- if (initialPage.records.isNotEmpty) {
424
- // Use the last record's paging token as cursor
425
- paymentsRequest =
426
- paymentsRequest.cursor (initialPage.records.last.pagingToken);
419
+ final Page <OperationResponse > payments = await paymentsRequest.execute ();
420
+ final List <ITransaction > transactionDetails = [];
421
+
422
+ // Keep track of the last raw paging token (even if filtered out later)
423
+ String ? lastRawPagingToken;
424
+
425
+ for (final response in payments.records) {
426
+ // Save the last raw paging token no matter what.
427
+ lastRawPagingToken = response.pagingToken;
428
+
429
+ if (response is ! PaymentOperationResponse ) {
430
+ logger.i ("Skipping non-payment operation: ${response .runtimeType }" );
431
+ continue ;
427
432
}
428
- }
429
433
430
- Page <OperationResponse > payments = await paymentsRequest.execute ();
431
- List <ITransaction > transactionDetails = [];
432
-
433
- if (payments.records.isNotEmpty) {
434
- for (OperationResponse response in payments.records) {
435
- if (response is PaymentOperationResponse ) {
436
- final memoText = await this
437
- .getMemoText (response.links.transaction.toJson ()["href" ]);
438
- String assetCode = response.assetCode ?? 'XLM' ;
439
- if (assetCodeFilter == null || assetCode == assetCodeFilter) {
440
- final details = PaymentTransaction (
441
- hash: response.transactionHash,
442
- from: response.from,
443
- to: response.to,
444
- asset: response.assetCode.toString (),
445
- amount: response.amount,
446
- type: response.to == this .accountId
447
- ? TransactionType .Receive
448
- : TransactionType .Payment ,
449
- status: response.transactionSuccessful,
450
- date: DateTime .parse (response.createdAt).toLocal ().toString (),
451
- memo: memoText);
452
-
453
- transactionDetails.add (details);
454
- }
455
- } else {
456
- logger.i ("Unhandled operation type: ${response .runtimeType }" );
457
- }
434
+ final memoText =
435
+ await getMemoText (response.links.transaction.toJson ()["href" ]);
436
+ final assetCode = response.assetCode ?? 'XLM' ;
437
+
438
+ // Only include if it meets the asset code filter.
439
+ if (assetCodeFilter != null && assetCode != assetCodeFilter) {
440
+ continue ;
458
441
}
442
+
443
+ transactionDetails.add (PaymentTransaction (
444
+ pagingToken: response.pagingToken,
445
+ hash: response.transactionHash,
446
+ from: response.from,
447
+ to: response.to,
448
+ asset: assetCode,
449
+ amount: response.amount,
450
+ type: response.to == accountId
451
+ ? TransactionType .Receive
452
+ : TransactionType .Payment ,
453
+ status: response.transactionSuccessful,
454
+ date: DateTime .parse (response.createdAt).toLocal ().toString (),
455
+ memo: memoText,
456
+ ));
459
457
}
460
458
459
+ logger.i ('Fetched ${transactionDetails .length } transactions' );
461
460
return transactionDetails;
462
461
} catch (e) {
463
462
logger.e ('Failed to get transactions: $e ' );
464
- throw Exception ('Could not get transactions due to $ e ' );
463
+ throw Exception ('Could not get transactions: ${ e . toString ()} ' );
465
464
}
466
465
}
467
466
0 commit comments