Skip to content

Commit d74f723

Browse files
committed
Support greedy addressees
Setting the 'greedy' flag on an addressee means it will consume any change instead of it going to a change output.
1 parent 3a1156f commit d74f723

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/ga_tx.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -572,14 +572,20 @@ namespace sdk {
572572

573573
// Add all outputs and compute the total amount of satoshi to be sent
574574
amount required_total{ 0 };
575+
uint32_t greedy_index = NO_CHANGE_INDEX;
575576

576577
if (num_addressees) {
578+
int addressee_index = 0;
577579
for (auto& addressee : *addressees_p) {
578580
const auto addressee_asset_id = asset_id_from_json(net_params, addressee);
579581
if (addressee_asset_id == asset_id) {
580582
required_total += add_tx_addressee(session, net_params, result, tx, addressee);
581583
reordered_addressees.push_back(addressee);
584+
if (addressee.value("is_greedy", false)) {
585+
greedy_index = addressee_index;
586+
}
582587
}
588+
++addressee_index;
583589
}
584590
}
585591

@@ -745,6 +751,14 @@ namespace sdk {
745751

746752
change = total - required_with_fee;
747753

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+
748762
if ((!have_change_output && change < dust_threshold)
749763
|| (have_change_output && change >= dust_threshold)) {
750764
// We don't have a change output, and have only dust left over, or

src/transaction_utils.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,10 @@ namespace sdk {
424424

425425
// Transactions with outputs below the dust threshold (except OP_RETURN)
426426
// are not relayed by network nodes
427-
if (!result.value("send_all", false) && satoshi.value() < session.get_dust_threshold()) {
427+
// This check only applies when the amount has been set explicitly, so send all
428+
// and greedy outputs are ignored as the amount is calculated later
429+
if (!result.value("send_all", false) && !addressee.value("is_greedy", false)
430+
&& satoshi.value() < session.get_dust_threshold()) {
428431
set_tx_error(result, res::id_invalid_amount);
429432
}
430433

0 commit comments

Comments
 (0)