Skip to content

Commit 9bcba7d

Browse files
committed
Fix Spark mint fee subtraction edge case
1 parent 94d6306 commit 9bcba7d

1 file changed

Lines changed: 27 additions & 23 deletions

File tree

lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,11 +1664,7 @@ mixin SparkInterface<T extends ElectrumXCurrencyInterface>
16641664

16651665
if (autoMintAll) {
16661666
singleTxOutputs.add(
1667-
MutableSparkRecipient(
1668-
autoMintSparkAddress!,
1669-
mintedValue,
1670-
"",
1671-
),
1667+
MutableSparkRecipient(autoMintSparkAddress!, mintedValue, ""),
16721668
);
16731669
} else {
16741670
BigInt remainingMintValue = BigInt.parse(mintedValue.toString());
@@ -1696,26 +1692,34 @@ mixin SparkInterface<T extends ElectrumXCurrencyInterface>
16961692
}
16971693
}
16981694

1699-
if (subtractFeeFromAmount) {
1700-
final BigInt singleFee =
1701-
nFeeRet ~/ BigInt.from(singleTxOutputs.length);
1702-
BigInt remainder = nFeeRet % BigInt.from(singleTxOutputs.length);
1703-
1704-
for (int i = 0; i < singleTxOutputs.length; ++i) {
1705-
if (singleTxOutputs[i].value <= singleFee) {
1706-
final removed = singleTxOutputs.removeAt(i);
1707-
remainder += removed.value - singleFee;
1708-
--i;
1709-
continue;
1695+
if (subtractFeeFromAmount && nFeeRet > BigInt.zero) {
1696+
var remainingFee = nFeeRet;
1697+
var outputIndex = 0;
1698+
while (outputIndex < singleTxOutputs.length &&
1699+
remainingFee > BigInt.zero) {
1700+
final outputsLeft = BigInt.from(
1701+
singleTxOutputs.length - outputIndex,
1702+
);
1703+
var feeShare = remainingFee ~/ outputsLeft;
1704+
if (remainingFee % outputsLeft != BigInt.zero) {
1705+
feeShare += BigInt.one;
17101706
}
1711-
singleTxOutputs[i].value -= singleFee;
1712-
if (remainder > BigInt.zero &&
1713-
singleTxOutputs[i].value >
1714-
nFeeRet % BigInt.from(singleTxOutputs.length)) {
1715-
// first receiver pays the remainder not divisible by output count
1716-
singleTxOutputs[i].value -= remainder;
1717-
remainder = BigInt.zero;
1707+
1708+
if (singleTxOutputs[outputIndex].value <= feeShare) {
1709+
remainingFee -= singleTxOutputs[outputIndex].value;
1710+
singleTxOutputs.removeAt(outputIndex);
1711+
continue;
17181712
}
1713+
1714+
singleTxOutputs[outputIndex].value -= feeShare;
1715+
remainingFee -= feeShare;
1716+
++outputIndex;
1717+
}
1718+
1719+
if (singleTxOutputs.isEmpty) {
1720+
valueAndUTXOs.remove(itr);
1721+
skipCoin = true;
1722+
break;
17191723
}
17201724
}
17211725

0 commit comments

Comments
 (0)