From 94e69f193b98361847df21d5d1431efd6b39e213 Mon Sep 17 00:00:00 2001
From: julian <julian@cypherstack.com>
Date: Thu, 21 Dec 2023 16:04:49 -0600
Subject: [PATCH] send all spark tweaks

---
 .../spark_interface.dart                      | 37 +++++++++++--------
 1 file changed, 22 insertions(+), 15 deletions(-)

diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart
index 24ac235e2..e38cf0f73 100644
--- a/lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart
+++ b/lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart
@@ -218,21 +218,21 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
     txb.setLockTime(await chainHeight);
     txb.setVersion(3 | (9 << 16));
 
-    final List<({String address, Amount amount})> recipientsWithFeeSubtracted =
-        [];
-    final List<
+    List<({String address, Amount amount})>? recipientsWithFeeSubtracted;
+    List<
         ({
           String address,
           Amount amount,
           String memo,
-        })> sparkRecipientsWithFeeSubtracted = [];
-    final outputCount = (txData.recipients
-                ?.where(
-                  (e) => e.amount.raw > BigInt.zero,
-                )
-                .length ??
-            0) +
-        (txData.sparkRecipients?.length ?? 0);
+        })>? sparkRecipientsWithFeeSubtracted;
+    final recipientCount = (txData.recipients
+            ?.where(
+              (e) => e.amount.raw > BigInt.zero,
+            )
+            .length ??
+        0);
+    final totalRecipientCount =
+        recipientCount + (txData.sparkRecipients?.length ?? 0);
     final BigInt estimatedFee;
     if (isSendAll) {
       final estFee = LibSpark.estimateSparkFee(
@@ -248,13 +248,20 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
       estimatedFee = BigInt.zero;
     }
 
+    if ((txData.sparkRecipients?.length ?? 0) > 0) {
+      sparkRecipientsWithFeeSubtracted = [];
+    }
+    if (recipientCount > 0) {
+      recipientsWithFeeSubtracted = [];
+    }
+
     for (int i = 0; i < (txData.sparkRecipients?.length ?? 0); i++) {
-      sparkRecipientsWithFeeSubtracted.add(
+      sparkRecipientsWithFeeSubtracted!.add(
         (
           address: txData.sparkRecipients![i].address,
           amount: Amount(
             rawValue: txData.sparkRecipients![i].amount.raw -
-                (estimatedFee ~/ BigInt.from(outputCount)),
+                (estimatedFee ~/ BigInt.from(totalRecipientCount)),
             fractionDigits: cryptoCurrency.fractionDigits,
           ),
           memo: txData.sparkRecipients![i].memo,
@@ -266,12 +273,12 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
       if (txData.recipients![i].amount.raw == BigInt.zero) {
         continue;
       }
-      recipientsWithFeeSubtracted.add(
+      recipientsWithFeeSubtracted!.add(
         (
           address: txData.recipients![i].address,
           amount: Amount(
             rawValue: txData.recipients![i].amount.raw -
-                (estimatedFee ~/ BigInt.from(outputCount)),
+                (estimatedFee ~/ BigInt.from(totalRecipientCount)),
             fractionDigits: cryptoCurrency.fractionDigits,
           ),
         ),