Skip to content

Commit ae8b5b0

Browse files
authored
Merge pull request #393 from JohnnyLawDGB/fix/mint-utxo-consolidation
fix: mint UTXO auto-consolidation fails when margin exceeds balance
2 parents a39f22a + 9e20435 commit ae8b5b0

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

src/rpc/digidollar.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -986,24 +986,24 @@ RPCHelpMan mintdigidollar()
986986
LogPrintf("DigiDollar RPC Mint: UTXO fragmentation detected (%zu UTXOs). Auto-consolidating...\n",
987987
availableUtxos.size());
988988

989-
// Calculate consolidation target: collateral + fees + 10% margin
990-
CAmount consolidationTarget = result.collateralRequired +
991-
(result.collateralRequired / 10) + 10000000; // +10% + 0.1 DGB fee buffer
992-
993-
// Cap at available balance
989+
// Check if total balance covers collateral (ignore margin — the
990+
// consolidation itself reduces input count, not total value)
994991
CAmount totalAvailable = 0;
995992
for (const auto& [outpoint, value] : utxoValues) {
996993
totalAvailable += value;
997994
}
998-
if (consolidationTarget > totalAvailable) {
995+
// Need collateral + estimated fees (~0.2 DGB buffer)
996+
CAmount minRequired = result.collateralRequired + 20000000;
997+
if (totalAvailable < minRequired) {
999998
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS,
1000-
strprintf("Insufficient funds for collateral. Need %.2f DGB, have %.2f DGB across %zu small UTXOs.",
999+
strprintf("Insufficient funds for collateral. Need %.2f DGB, have %.2f DGB.",
10011000
result.collateralRequired / 100000000.0,
1002-
totalAvailable / 100000000.0,
1003-
availableUtxos.size()));
1001+
totalAvailable / 100000000.0));
10041002
}
10051003

1006-
// Create consolidation transaction using wallet's standard coin selection
1004+
// Consolidate: send-to-self sweeping as many UTXOs as possible
1005+
// into one large output. Use subtractfeefromamount so the
1006+
// consolidation always succeeds regardless of margin.
10071007
CTxDestination consolidationDest;
10081008
{
10091009
LOCK(pwallet->cs_wallet);
@@ -1014,8 +1014,9 @@ RPCHelpMan mintdigidollar()
10141014
consolidationDest = *op_dest;
10151015
}
10161016

1017+
// Send entire balance to self, fee subtracted from amount
10171018
wallet::CCoinControl coin_control;
1018-
wallet::CRecipient recipient{consolidationDest, consolidationTarget, false};
1019+
wallet::CRecipient recipient{consolidationDest, totalAvailable, /*subtract_fee=*/true};
10191020
std::vector<wallet::CRecipient> recipients = {recipient};
10201021

10211022
auto consolidation_result = wallet::CreateTransaction(*pwallet, recipients, /*change_pos=*/-1, coin_control, /*sign=*/true);
@@ -1033,8 +1034,8 @@ RPCHelpMan mintdigidollar()
10331034
pwallet->CommitTransaction(consolidation_tx, {}, {});
10341035
}
10351036

1036-
LogPrintf("DigiDollar RPC Mint: Consolidation tx broadcast: %s (%.2f DGB)\n",
1037-
consolidation_txid, consolidationTarget / 100000000.0);
1037+
LogPrintf("DigiDollar RPC Mint: Consolidation tx broadcast: %s (swept %.2f DGB)\n",
1038+
consolidation_txid, totalAvailable / 100000000.0);
10381039

10391040
// Re-gather UTXOs (now includes unconfirmed consolidation output)
10401041
availableUtxos.clear();

0 commit comments

Comments
 (0)