Skip to content

Commit 3aff59d

Browse files
committed
Refactor getTransactions to return stream, get memos in parallel, add _TempTx class
1 parent a5958ef commit 3aff59d

File tree

3 files changed

+47
-41
lines changed

3 files changed

+47
-41
lines changed

.qodo/history.sqlite

132 KB
Binary file not shown.

packages/stellar_client/lib/src/client.dart

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -401,65 +401,65 @@ class Client {
401401
}
402402
}
403403

404-
Future<List<ITransaction>> getTransactions({
404+
Stream<ITransaction> getTransactions({
405405
String? assetCodeFilter,
406406
int limit = 10,
407407
String? pagingToken,
408-
}) async {
409-
try {
410-
List<ITransaction> transactionDetails = [];
411-
String? currentCursor = pagingToken;
408+
}) async* {
409+
String? currentCursor = pagingToken;
410+
int count = 0;
412411

413-
while (transactionDetails.length < limit) {
414-
var request = _sdk.payments
412+
try {
413+
while (count < limit) {
414+
final request = _sdk.payments
415415
.forAccount(accountId)
416416
.order(RequestBuilderOrder.DESC)
417-
.limit(limit);
417+
.limit(limit)
418+
.cursor(currentCursor ?? '');
418419

419-
if (currentCursor != null) {
420-
request = request.cursor(currentCursor);
421-
}
420+
final page = await request.execute();
421+
if (page.records.isEmpty) break;
422422

423-
final Page<OperationResponse> page = await request.execute();
423+
final tempList = <_TempTx>[];
424424

425-
if (page.records.isEmpty) {
426-
break;
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
}
428436

429-
for (final response in page.records) {
430-
currentCursor = response.pagingToken;
437+
if (tempList.isEmpty) break;
431438

432-
if (response is! PaymentOperationResponse) continue;
439+
currentCursor = tempList.last.response.pagingToken;
433440

434-
final assetCode = response.assetCode ?? 'XLM';
435-
if (assetCodeFilter != null && assetCode != assetCodeFilter) {
436-
continue;
437-
}
441+
final memoList = await Future.wait(
442+
tempList.map((tx) => getMemoText(tx.href)),
443+
);
438444

439-
final memo =
440-
await getMemoText(response.links.transaction.toJson()["href"]);
441-
442-
transactionDetails.add(PaymentTransaction(
443-
pagingToken: response.pagingToken,
444-
hash: response.transactionHash,
445-
from: response.from,
446-
to: response.to,
447-
asset: assetCode,
448-
amount: response.amount,
449-
type: response.to == accountId
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
450455
? TransactionType.Receive
451456
: TransactionType.Payment,
452-
status: response.transactionSuccessful,
453-
date: DateTime.parse(response.createdAt).toLocal().toString(),
454-
memo: memo,
455-
));
456-
457-
if (transactionDetails.length >= limit) break;
457+
status: tx.transactionSuccessful,
458+
date: DateTime.parse(tx.createdAt).toLocal().toString(),
459+
memo: memoList[i],
460+
);
458461
}
459462
}
460-
461-
logger.i('Fetched ${transactionDetails.length} transactions');
462-
return transactionDetails;
463463
} catch (e) {
464464
logger.e('Failed to get transactions: $e');
465465
throw Exception('Failed to get transactions');

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)