updateTransaction method

Future<void> updateTransaction (
  1. Transaction transaction,
  2. {bool newTransaction: true}
)
override

All Transaction updates go through updateTransaction. From either getNextTransactions, Peer.filterAdd results, or _readPendingTraRsactions. Balance changes only if transactionsChanged and newTransaction (or undoneByreorg).

Implementation

Future<void> updateTransaction(Transaction transaction,
    {bool newTransaction = true}) async {
  int height = transaction.height ?? 0;
  bool undoneByReorg = height < 0, transactionsChanged;
  if (undoneByReorg) {
    transactionsChanged = transactions.remove(transaction);
    transactionIds.remove(transaction.id().toJson());
  } else {
    transactionsChanged = transactions.add(transaction);
    transactionIds[transaction.id().toJson()] = transaction;
  }
  bool balanceChanged =
      transactionsChanged && (newTransaction || undoneByReorg);
  bool mature = transaction.maturity <= network.tipHeight;

  /// Track [Address].[state] changes.
  Address from =
      transaction.from == null ? null : addresses[transaction.from.toJson()];
  Address to = addresses[transaction.to.toJson()];
  if (from != null) {
    if (height > 0) from.updateSeenHeight(height);
    await updateAddressState(from, AddressState.used, store: !balanceChanged);
  }
  if (to != null) {
    if (height > 0) to.updateSeenHeight(height);
    await updateAddressState(to, AddressState.used, store: !balanceChanged);
  }

  /// Track [Address].[balance] changes.
  if (balanceChanged) {
    if (from != null) {
      num cost = transaction.amount + transaction.fee;
      await _updateBalance(from, undoneByReorg ? cost : -cost);
    }
    if (to != null && mature) {
      await _updateBalance(
          to, undoneByReorg ? -transaction.amount : transaction.amount);
    }
  }

  /// Track [Address].[maturesBalance] changes.
  if (to != null && !mature && transactionsChanged) {
    _applyMaturesBalanceDelta(
        to,
        undoneByReorg ? -transaction.amount : transaction.amount,
        transaction);
  }

  /*debugPrint('${transaction.fromText} -> ${transaction.toText} ' +
      currency.format(transaction.amount) +
      ' mature=$mature, newTxn=$newTransaction txnChanged=$transactionsChanged');*/
}