@@ -1106,22 +1106,30 @@ void AdjustMasternodePayment(CMutableTransaction &tx, const CTxOut &txoutMastern
11061106{
11071107 auto it = std::find (std::begin (tx.vout ), std::end (tx.vout ), txoutMasternodePayment);
11081108
1109- if (it != std::end (tx.vout ))
1110- {
1109+ if (it != std::end (tx.vout )) {
11111110 int mnPaymentOutIndex = std::distance (std::begin (tx.vout ), it);
11121111 auto masternodePayment = tx.vout [mnPaymentOutIndex].nValue ;
11131112
1114- int i = tx.vout .size () - 2 ;
1115- if (tposContract.IsValid ()) // here we have 3 outputs, first as stake reward, second as tpos reward, third as MN reward
1116- {
1117- tx.vout [i - 1 ].nValue -= TPoSUtils::GetOwnerPayment (masternodePayment, tposContract.nOperatorReward ); // adjust reward for owner.
1118- // it might be that last vout is masternode, since operator reward was 0
1119- if (tx.vout [i].scriptPubKey == tposContract.scriptMerchantAddress ) {
1120- tx.vout [i].nValue -= TPoSUtils::GetOperatorPayment (masternodePayment, tposContract.nOperatorReward ); // adjust reward for merchant
1113+ if (tposContract.IsValid ()) {
1114+ // here we have 3 outputs, first as stake reward, second as tpos reward, third as MN reward
1115+ auto ownerOutIt = std::find_if (tx.vout .rbegin (),tx.vout .rend (), [&tposContract](const CTxOut &out) {
1116+ return out.scriptPubKey == tposContract.scriptTPoSAddress ;
1117+ });
1118+
1119+ if (ownerOutIt != tx.vout .rend ()) {
1120+ ownerOutIt->nValue -= TPoSUtils::GetOwnerPayment (masternodePayment, tposContract.nOperatorReward ); // adjust reward for owner.
11211121 }
1122- }
1123- else // here we have 2 outputs, first as stake reward, second as MN reward
1124- {
1122+
1123+ auto merchantOutIt = std::find_if (std::begin (tx.vout ), std::end (tx.vout ), [&tposContract](const CTxOut &out) {
1124+ return out.scriptPubKey == tposContract.scriptMerchantAddress ;
1125+ });
1126+
1127+ if (merchantOutIt != std::end (tx.vout )) {
1128+ merchantOutIt->nValue -= TPoSUtils::GetOperatorPayment (masternodePayment, tposContract.nOperatorReward ); // adjust reward for merchant
1129+ }
1130+ } else {
1131+ // here we have 2 outputs, first as stake reward, second as MN reward
1132+ int i = tx.vout .size () - 2 ;
11251133 tx.vout [i].nValue -= masternodePayment; // last vout is mn payment.
11261134 }
11271135 }
0 commit comments