getPoolTransactions function

Future<List<PoolTransaction?>> getPoolTransactions(
  1. String? accountIndex,
  2. String? bJJ
)

Fetches the transaction details for each transaction in the pool for the specified account index and bjj

@param {String} accountIndex - The account index @param {String} bjj - The account's BabyJubJub

@returns {List

Implementation

Future<List<PoolTransaction?>> getPoolTransactions(
    String? accountIndex, String? bJJ) async {
  final chainId = getCurrentEnvironment()!.chainId.toString();

  final SharedPreferences prefs = await _prefs;
  if (!prefs.containsKey(TRANSACTION_POOL_KEY)) {
    final emptyTransactionPool = {};
    prefs.setString(TRANSACTION_POOL_KEY, json.encode(emptyTransactionPool));
  }
  final transactionPool =
      json.decode(prefs.get(TRANSACTION_POOL_KEY) as String);

  final chainIdTransactionPool =
      transactionPool.containsKey(chainId) ? transactionPool[chainId] : {};
  final accountTransactionPool = chainIdTransactionPool.containsKey(bJJ)
      ? chainIdTransactionPool[bJJ]
      : [];

  // filter txs from accountIndex
  accountTransactionPool.removeWhere((transaction) =>
      accountIndex != null &&
      Transaction.fromJson(json.decode(transaction)).fromAccountIndex !=
          accountIndex);
  List<PoolTransaction?> successfulTransactions = [];
  for (String transactionString in accountTransactionPool) {
    final transaction = Transaction.fromJson(json.decode(transactionString));
    ForgedTransaction? historyTransaction;
    // TODO: History tx is needed???
    try {
      historyTransaction = await getHistoryTransaction(transaction.id!);
    } catch (e) {
      print(e.toString());
    }
    try {
      final poolTransaction = await getPoolTransaction(transaction.id!);
      if (historyTransaction != null) {
        // TODO: pool txs are unforged, is it needed??
        if (poolTransaction.info != null || poolTransaction.state == 'fged') {
          removePoolTransaction(bJJ, poolTransaction.id);
        } else {
          successfulTransactions.add(poolTransaction);
        }
      } else {
        successfulTransactions.add(poolTransaction);
      }
    } catch (e) {
      // on ItemNotFoundException {
      if (historyTransaction != null) {
        removePoolTransaction(bJJ, transaction.id);
      }
    }
  }

  return successfulTransactions;
}