@@ -401,67 +401,68 @@ class Client {
401
401
}
402
402
}
403
403
404
- Future <List <ITransaction >> getTransactions (
405
- {String ? assetCodeFilter, int ? limit, int ? offset}) async {
406
- try {
407
- var paymentsRequest =
408
- _sdk.payments.forAccount (accountId).order (RequestBuilderOrder .DESC );
409
-
410
- // Add pagination
411
- if (limit != null ) {
412
- paymentsRequest = paymentsRequest.limit (limit);
413
- }
404
+ Stream <ITransaction > getTransactions ({
405
+ String ? assetCodeFilter,
406
+ int limit = 10 ,
407
+ String ? pagingToken,
408
+ }) async * {
409
+ String ? currentCursor = pagingToken;
410
+ int count = 0 ;
414
411
415
- // get first page
416
- if (offset != null && offset > 0 ) {
417
- var initialPage = await _sdk.payments
412
+ try {
413
+ while (count < limit ) {
414
+ final request = _sdk.payments
418
415
.forAccount (accountId)
419
416
.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);
417
+ .limit (limit)
418
+ .cursor (currentCursor ?? '' );
419
+
420
+ final page = await request.execute ();
421
+ if (page.records.isEmpty) break ;
422
+
423
+ final tempList = < _TempTx > [];
424
+
425
+ for (final response in page.records) {
426
+ if (response is PaymentOperationResponse &&
427
+ (assetCodeFilter == null ||
428
+ response.assetCode == assetCodeFilter)) {
429
+ tempList.add (_TempTx (
430
+ response.links.transaction.toJson ()["href" ],
431
+ response,
432
+ ));
433
+ if (++ count >= limit) break ;
434
+ }
427
435
}
428
- }
429
436
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
- }
437
+ if (tempList.isEmpty) break ;
438
+
439
+ currentCursor = tempList.last.response.pagingToken;
440
+
441
+ final memoList = await Future .wait (
442
+ tempList.map ((tx) => getMemoText (tx.href)),
443
+ );
444
+
445
+ for (var i = 0 ; i < tempList.length; i++ ) {
446
+ final tx = tempList[i].response;
447
+ yield PaymentTransaction (
448
+ pagingToken: tx.pagingToken,
449
+ hash: tx.transactionHash,
450
+ from: tx.from,
451
+ to: tx.to,
452
+ asset: tx.assetCode ?? 'XLM' ,
453
+ amount: tx.amount,
454
+ type: tx.to == accountId
455
+ ? TransactionType .Receive
456
+ : TransactionType .Payment ,
457
+ status: tx.transactionSuccessful,
458
+ date: DateTime .parse (tx.createdAt).toLocal ().toString (),
459
+ memo: memoList[i],
460
+ );
458
461
}
459
462
}
460
-
461
- return transactionDetails;
462
463
} catch (e) {
463
464
logger.e ('Failed to get transactions: $e ' );
464
- throw Exception ('Could not get transactions due to $ e ' );
465
+ throw Exception ('Failed to get transactions' );
465
466
}
466
467
}
467
468
0 commit comments