addPoolTransaction function

void addPoolTransaction(
  1. String transaction,
  2. String? bJJ
)

Adds a transaction to the transaction pool

@param {string} transaction - The transaction to add to the pool @param {string} bJJ - The account with which the transaction was made @returns {void}

Implementation

void addPoolTransaction(String transaction, 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]
      : [];

  final newAccountTransactionPool = List.from(accountTransactionPool);
  newAccountTransactionPool.add(transaction);

  final newChainIdTransactionPool = {}..addAll(chainIdTransactionPool);
  newChainIdTransactionPool.update(bJJ, (value) => newAccountTransactionPool,
      ifAbsent: () => newAccountTransactionPool);

  final newTransactionPool = {}..addAll(transactionPool);
  newTransactionPool.update(chainId, (value) => newChainIdTransactionPool,
      ifAbsent: () => newChainIdTransactionPool);

  prefs.setString(TRANSACTION_POOL_KEY, json.encode(newTransactionPool));
}