Skip to content

Commit 0ce3b57

Browse files
authored
Merge pull request #144 from threefoldtech/development_fix_transactions_pagination
Fix getTransactions pagination
2 parents f3afde6 + 3aff59d commit 0ce3b57

File tree

4 files changed

+62
-53
lines changed

4 files changed

+62
-53
lines changed

.qodo/history.sqlite

132 KB
Binary file not shown.

packages/stellar_client/lib/models/transaction.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class ITransaction {}
44

55
class PaymentTransaction extends ITransaction {
66
PaymentTransaction({
7+
required this.pagingToken,
78
required this.hash,
89
required this.from,
910
required this.to,
@@ -14,6 +15,7 @@ class PaymentTransaction extends ITransaction {
1415
required this.date,
1516
required this.type,
1617
});
18+
final String pagingToken;
1719
final String hash;
1820
final String from;
1921
final String to;
@@ -26,6 +28,6 @@ class PaymentTransaction extends ITransaction {
2628

2729
@override
2830
String toString() {
29-
return 'ITransaction(hash: $hash, from: $from, to: $to, asset: $asset, amount: $amount, memo: $memo, status: $status, date: $date)';
31+
return 'ITransaction(pagingToken: $pagingToken, hash: $hash, from: $from, to: $to, asset: $asset, amount: $amount, memo: $memo, status: $status, date: $date)';
3032
}
3133
}

packages/stellar_client/lib/src/client.dart

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -401,67 +401,68 @@ class Client {
401401
}
402402
}
403403

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;
414411

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
418415
.forAccount(accountId)
419416
.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+
}
427435
}
428-
}
429436

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+
);
458461
}
459462
}
460-
461-
return transactionDetails;
462463
} catch (e) {
463464
logger.e('Failed to get transactions: $e');
464-
throw Exception('Could not get transactions due to $e');
465+
throw Exception('Failed to get transactions');
465466
}
466467
}
467468

packages/stellar_client/lib/src/helpers.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ Future<List<BalanceInfo>> getBalanceByAccountID({
3939
throw Exception(e);
4040
}
4141
}
42+
43+
class _TempTx {
44+
final String href;
45+
final PaymentOperationResponse response;
46+
_TempTx(this.href, this.response);
47+
}

0 commit comments

Comments
 (0)