Skip to content

Commit 2d7224a

Browse files
authored
Merge pull request #131 from threefoldtech/development_get_transactions_pagination
Paginate get transactions in Stellar client
2 parents c0cf900 + 4a8c77a commit 2d7224a

File tree

1 file changed

+58
-33
lines changed

1 file changed

+58
-33
lines changed

packages/stellar_client/lib/src/client.dart

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -381,43 +381,68 @@ class Client {
381381
}
382382
}
383383

384-
Future<List<ITransaction>> getTransactions({String? assetCodeFilter}) async {
385-
Page<OperationResponse> payments = await _sdk.payments
386-
.forAccount(accountId)
387-
.order(RequestBuilderOrder.DESC)
388-
.execute();
389-
List<ITransaction> transactionDetails = [];
390-
391-
if (payments.records.isNotEmpty) {
392-
for (OperationResponse response in payments.records) {
393-
if (response is PaymentOperationResponse) {
394-
final memoText = await this
395-
.getMemoText(response.links.transaction.toJson()["href"]);
396-
String assetCode = response.assetCode ?? 'XLM';
397-
if (assetCodeFilter == null || assetCode == assetCodeFilter) {
398-
final details = PaymentTransaction(
399-
hash: response.transactionHash,
400-
from: response.from,
401-
to: response.to,
402-
asset: response.assetCode.toString(),
403-
amount: response.amount,
404-
type: response.to == this.accountId
405-
? TransactionType.Receive
406-
: TransactionType.Payment,
407-
status: response.transactionSuccessful,
408-
date: DateTime.parse(response.createdAt).toLocal().toString(),
409-
memo: memoText);
410-
411-
transactionDetails.add(details);
384+
Future<List<ITransaction>> getTransactions(
385+
{String? assetCodeFilter, int? limit, int? offset}) async {
386+
try {
387+
var paymentsRequest =
388+
_sdk.payments.forAccount(accountId).order(RequestBuilderOrder.DESC);
389+
390+
// Add pagination
391+
if (limit != null) {
392+
paymentsRequest = paymentsRequest.limit(limit);
393+
}
394+
395+
// get first page
396+
if (offset != null && offset > 0) {
397+
var initialPage = await _sdk.payments
398+
.forAccount(accountId)
399+
.order(RequestBuilderOrder.DESC)
400+
.limit(offset)
401+
.execute();
402+
403+
if (initialPage.records.isNotEmpty) {
404+
// Use the last record's paging token as cursor
405+
paymentsRequest =
406+
paymentsRequest.cursor(initialPage.records.last.pagingToken);
407+
}
408+
}
409+
410+
Page<OperationResponse> payments = await paymentsRequest.execute();
411+
List<ITransaction> transactionDetails = [];
412+
413+
if (payments.records.isNotEmpty) {
414+
for (OperationResponse response in payments.records) {
415+
if (response is PaymentOperationResponse) {
416+
final memoText = await this
417+
.getMemoText(response.links.transaction.toJson()["href"]);
418+
String assetCode = response.assetCode ?? 'XLM';
419+
if (assetCodeFilter == null || assetCode == assetCodeFilter) {
420+
final details = PaymentTransaction(
421+
hash: response.transactionHash,
422+
from: response.from,
423+
to: response.to,
424+
asset: response.assetCode.toString(),
425+
amount: response.amount,
426+
type: response.to == this.accountId
427+
? TransactionType.Receive
428+
: TransactionType.Payment,
429+
status: response.transactionSuccessful,
430+
date: DateTime.parse(response.createdAt).toLocal().toString(),
431+
memo: memoText);
432+
433+
transactionDetails.add(details);
434+
}
435+
} else {
436+
logger.i("Unhandled operation type: ${response.runtimeType}");
412437
}
413-
} else {
414-
logger.i("Unhandled operation type: ${response.runtimeType}");
415438
}
416439
}
417-
} else {
418-
logger.i("No payment records found.");
440+
441+
return transactionDetails;
442+
} catch (e) {
443+
logger.e('Failed to get transactions: $e');
444+
throw Exception('Could not get transactions due to $e');
419445
}
420-
return transactionDetails;
421446
}
422447

423448
Future<List<BalanceInfo>> getBalance() async {

0 commit comments

Comments
 (0)