Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/stellar_client/lib/models/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class ITransaction {}

class PaymentTransaction extends ITransaction {
PaymentTransaction({
required this.pagingToken,
required this.hash,
required this.from,
required this.to,
Expand All @@ -14,6 +15,7 @@ class PaymentTransaction extends ITransaction {
required this.date,
required this.type,
});
final String pagingToken;
final String hash;
final String from;
final String to;
Expand All @@ -26,6 +28,6 @@ class PaymentTransaction extends ITransaction {

@override
String toString() {
return 'ITransaction(hash: $hash, from: $from, to: $to, asset: $asset, amount: $amount, memo: $memo, status: $status, date: $date)';
return 'ITransaction(pagingToken: $pagingToken, hash: $hash, from: $from, to: $to, asset: $asset, amount: $amount, memo: $memo, status: $status, date: $date)';
}
}
93 changes: 47 additions & 46 deletions packages/stellar_client/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -401,67 +401,68 @@ class Client {
}
}

Future<List<ITransaction>> getTransactions(
{String? assetCodeFilter, int? limit, int? offset}) async {
Future<List<ITransaction>> getTransactions({
String? assetCodeFilter,
int limit = 10,
String? pagingToken,
}) async {
try {
var paymentsRequest =
_sdk.payments.forAccount(accountId).order(RequestBuilderOrder.DESC);

// Add pagination
if (limit != null) {
paymentsRequest = paymentsRequest.limit(limit);
}
List<ITransaction> transactionDetails = [];
String? currentCursor = pagingToken;

// get first page
if (offset != null && offset > 0) {
var initialPage = await _sdk.payments
while (transactionDetails.length < limit) {
var request = _sdk.payments
.forAccount(accountId)
.order(RequestBuilderOrder.DESC)
.limit(offset)
.execute();
.limit(limit);

if (initialPage.records.isNotEmpty) {
// Use the last record's paging token as cursor
paymentsRequest =
paymentsRequest.cursor(initialPage.records.last.pagingToken);
if (currentCursor != null) {
request = request.cursor(currentCursor);
}
}

Page<OperationResponse> payments = await paymentsRequest.execute();
List<ITransaction> transactionDetails = [];
final Page<OperationResponse> page = await request.execute();

if (payments.records.isNotEmpty) {
for (OperationResponse response in payments.records) {
if (response is PaymentOperationResponse) {
final memoText = await this
.getMemoText(response.links.transaction.toJson()["href"]);
String assetCode = response.assetCode ?? 'XLM';
if (assetCodeFilter == null || assetCode == assetCodeFilter) {
final details = PaymentTransaction(
hash: response.transactionHash,
from: response.from,
to: response.to,
asset: response.assetCode.toString(),
amount: response.amount,
type: response.to == this.accountId
? TransactionType.Receive
: TransactionType.Payment,
status: response.transactionSuccessful,
date: DateTime.parse(response.createdAt).toLocal().toString(),
memo: memoText);

transactionDetails.add(details);
}
} else {
logger.i("Unhandled operation type: ${response.runtimeType}");
if (page.records.isEmpty) {
break;
}

for (final response in page.records) {
currentCursor = response.pagingToken;

if (response is! PaymentOperationResponse) continue;

final assetCode = response.assetCode ?? 'XLM';
if (assetCodeFilter != null && assetCode != assetCodeFilter) {
continue;
}

final memo =
await getMemoText(response.links.transaction.toJson()["href"]);

transactionDetails.add(PaymentTransaction(
pagingToken: response.pagingToken,
hash: response.transactionHash,
from: response.from,
to: response.to,
asset: assetCode,
amount: response.amount,
type: response.to == accountId
? TransactionType.Receive
: TransactionType.Payment,
status: response.transactionSuccessful,
date: DateTime.parse(response.createdAt).toLocal().toString(),
memo: memo,
));

if (transactionDetails.length >= limit) break;
}
}

logger.i('Fetched ${transactionDetails.length} transactions');
return transactionDetails;
} catch (e) {
logger.e('Failed to get transactions: $e');
throw Exception('Could not get transactions due to $e');
throw Exception('Failed to get transactions');
}
}

Expand Down