@@ -2449,7 +2449,7 @@ bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAm
24492449 if (pcoin->tx ->vout .size () <= outpoint.n )
24502450 return false ;
24512451 // Just to calculate the marginal byte size
2452- mapValueFromPresetInputs[pcoin->GetOutputAsset (outpoint.n )] += pcoin->tx -> vout [ outpoint.n ]. nValue . GetAmount ( );
2452+ mapValueFromPresetInputs[pcoin->GetOutputAsset (outpoint.n )] += pcoin->GetOutputValueOut ( outpoint.n );
24532453 setPresetCoins.insert (CInputCoin (pcoin->tx , outpoint.n ));
24542454 } else
24552455 return false ; // TODO: Allow non-wallet inputs
@@ -2478,8 +2478,7 @@ bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAm
24782478 size_t max_descendants = (size_t )std::max<int64_t >(1 , gArgs .GetArg (" -limitdescendantcount" , DEFAULT_DESCENDANT_LIMIT));
24792479 bool fRejectLongChains = gArgs .GetBoolArg (" -walletrejectlongchains" , DEFAULT_WALLET_REJECT_LONG_CHAINS);
24802480
2481- CAmountMap mapTargetMinusPreset = mapTargetValue;
2482- mapTargetMinusPreset -= mapValueFromPresetInputs;
2481+ CAmountMap mapTargetMinusPreset = mapTargetValue - mapValueFromPresetInputs;
24832482
24842483 bool res = mapTargetValue <= mapValueFromPresetInputs ||
24852484 SelectCoinsMinConf (mapTargetMinusPreset, CoinEligibilityFilter (1 , 6 , 0 ), groups, setCoinsRet, mapValueRet, coin_selection_params, bnb_used) ||
@@ -2814,40 +2813,44 @@ bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CTransac
28142813 const CAmountMap mapChange = mapValueIn - mapValueToSelect;
28152814 LogPrintf (" mapChange:\n " );
28162815 PrintAmountMap (mapChange);
2816+ unsigned int changeCounter = 0 ;
28172817 for (std::map<CAsset, CAmount>::const_iterator it = mapChange.begin (); it != mapChange.end (); ++it) {
2818- if (it->second > 0 )
2819- {
2820- // Fill a vout to ourself
2821- CTxOut newTxOut (it->first , it->second , scriptChange);
2818+ if (it->second == 0 ) {
2819+ continue ;
2820+ }
2821+
2822+ // Fill a vout to ourself
2823+ CTxOut newTxOut (it->first , it->second , scriptChange);
28222824
2823- // Never create dust outputs; if we would, just
2824- // add the dust to the fee.
2825- // The nChange when BnB is used is always going to go to fees.
2826- if (it->first == policyAsset && (IsDust (newTxOut, discard_rate) || bnb_used))
2825+ // Never create dust outputs; if we would, just
2826+ // add the dust to the fee.
2827+ // The nChange when BnB is used is always going to go to fees.
2828+ if (it->first == policyAsset && (IsDust (newTxOut, discard_rate) || bnb_used))
2829+ {
2830+ nFeeRet += it->second ;
2831+ }
2832+ else
2833+ {
2834+ if (nChangePosInOut == -1 )
28272835 {
2828- nChangePosInOut = - 1 ;
2829- nFeeRet += it-> second ;
2836+ // Insert change txn at random position:
2837+ nChangePosInOut = GetRandInt (txNew. vout . size ()+ 1 ) ;
28302838 }
2831- else
2839+ else if (( unsigned int )nChangePosInOut > txNew. vout . size ())
28322840 {
2833- if (nChangePosInOut == -1 )
2834- {
2835- // Insert change txn at random position:
2836- nChangePosInOut = GetRandInt (txNew.vout .size ()+1 );
2837- }
2838- else if ((unsigned int )nChangePosInOut > txNew.vout .size ())
2839- {
2840- strFailReason = _ (" Change index out of range" );
2841- return false ;
2842- }
2843-
2844- std::vector<CTxOut>::iterator position = txNew.vout .begin ()+nChangePosInOut;
2845- txNew.vout .insert (position, newTxOut);
2841+ strFailReason = _ (" Change index out of range" );
2842+ return false ;
28462843 }
2847- } else {
2848- nChangePosInOut = -1 ;
2844+
2845+ std::vector<CTxOut>::iterator position = txNew.vout .begin ()+nChangePosInOut;
2846+ txNew.vout .insert (position, newTxOut);
2847+ changeCounter++;
28492848 }
28502849 }
2850+ if (changeCounter == 0 ) {
2851+ // no change inserted
2852+ nChangePosInOut = -1 ;
2853+ }
28512854
28522855 // Dummy fill vin for maximum size estimation
28532856 //
0 commit comments