@@ -572,14 +572,20 @@ namespace sdk {
572
572
573
573
// Add all outputs and compute the total amount of satoshi to be sent
574
574
amount required_total{ 0 };
575
+ uint32_t greedy_index = NO_CHANGE_INDEX;
575
576
576
577
if (num_addressees) {
578
+ int addressee_index = 0 ;
577
579
for (auto & addressee : *addressees_p) {
578
580
const auto addressee_asset_id = asset_id_from_json (net_params, addressee);
579
581
if (addressee_asset_id == asset_id) {
580
582
required_total += add_tx_addressee (session, net_params, result, tx, addressee);
581
583
reordered_addressees.push_back (addressee);
584
+ if (addressee.value (" is_greedy" , false )) {
585
+ greedy_index = addressee_index;
586
+ }
582
587
}
588
+ ++addressee_index;
583
589
}
584
590
}
585
591
@@ -745,6 +751,14 @@ namespace sdk {
745
751
746
752
change = total - required_with_fee;
747
753
754
+ if (change != 0 && greedy_index != NO_CHANGE_INDEX) {
755
+ // If a 'greedy' addressee exists send change there
756
+ set_tx_output_value (net_params, tx, greedy_index, asset_id, change.value ());
757
+ addressees_p->at (greedy_index)[" satoshi" ] = change.value ();
758
+ required_total += change;
759
+ continue ;
760
+ }
761
+
748
762
if ((!have_change_output && change < dust_threshold)
749
763
|| (have_change_output && change >= dust_threshold)) {
750
764
// We don't have a change output, and have only dust left over, or
0 commit comments