determineTransactionValue function

(BigInt, BigInt) determineTransactionValue(
  1. List<ElectrumOutput> outputs,
  2. TransactionTransferMethod transferMethod,
  3. Iterable<NodeWithAddress> nodes,
  4. UTXONetworkType type,
)

Implementation

(BigInt, BigInt) determineTransactionValue(
  List<ElectrumOutput> outputs,
  TransactionTransferMethod transferMethod,
  Iterable<NodeWithAddress> nodes,
  UTXONetworkType type,
) {
  final ourValue = switch (transferMethod) {
    TransactionTransferMethod.receive => outputs.fold(
        BigInt.zero,
        (prev, output) {
          if (output.belongsToUs) {
            return prev + output.value;
          }
          return prev;
        },
      ),
    TransactionTransferMethod.own => outputs
            .singleWhereOrNull(
              (output) => output.node is ReceiveNode,
            )
            ?.value ??
        BigInt.from(-1),
    TransactionTransferMethod.send => outputs.fold(
        BigInt.zero,
        (prev, output) {
          if (!output.belongsToUs) {
            return prev + output.value;
          }
          return prev;
        },
      ),
    TransactionTransferMethod.unknown => BigInt.from(-1),
  };
  final totalValue = outputs.fold(
    BigInt.zero,
    (prev, output) => prev + output.value,
  );

  return (ourValue, totalValue);
}