listRequests method
Implementation
Future<List<PaymentRequestDoc>> listRequests({
required String operator,
required String accountId,
int minTxId = 0,
int maxTxId = 0,
int limit = 10,
}) async {
final result = <PaymentRequestDoc>[];
// Setting maxRequestTxId to max int if '0'
var maxRequestTxId = maxTxId == 0 ? ~(-1 << 63) : maxTxId;
do {
final txGroups = await groupTransactions(
operator: operator,
accountId: accountId,
minTxId: minTxId,
maxTxId: maxRequestTxId,
limitGroups: limit,
);
if (txGroups.isEmpty) {
// Exist loop if there is no request in the given range
break;
}
// Update the max request ID
maxRequestTxId = txGroups
.map(
(group) => group.map((tx) => tx.txId).fold(maxRequestTxId, min),
)
.reduce(min) -
1;
// Filter for groups with actions of type 'Request'
final requests =
txGroups.map(PaymentRequestDoc.from).whereNotNull().toList();
// Augment the account info fields
final requestDocs = requests.map(
(request) async => request
..fromAccount = await getAccountInfoCached(
id: request.fromAccountId,
operator: operator,
)
..toAccount = await getAccountInfoCached(
id: request.toAccountId,
operator: operator,
),
);
final ar = await Future.wait(requestDocs);
result.addAll(ar);
} while (result.length < limit && maxRequestTxId != 0);
if (result.length > limit) {
// Potentially the result list can be longer than the limit, so it needs
// to be trimmed. This removes from the end, since sorted that will be the
// older requests
result.removeRange(limit, result.length);
}
result.sort((a, b) => b.timestamp.compareTo(a.timestamp));
return result;
}