Skip to content

Commit 26427ca

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 cec8139 commit 26427ca

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/ga_tx.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,14 +555,20 @@ namespace sdk {
555555

556556
// Add all outputs and compute the total amount of satoshi to be sent
557557
amount required_total{ 0 };
558+
uint32_t greedy_index = NO_CHANGE_INDEX;
558559

559560
if (num_addressees) {
561+
int addressee_index = 0;
560562
for (auto& addressee : *addressees_p) {
561563
const auto addressee_asset_id = asset_id_from_json(net_params, addressee);
562564
if (addressee_asset_id == asset_id) {
563565
required_total += add_tx_addressee(session, net_params, result, tx, addressee);
564566
reordered_addressees.push_back(addressee);
567+
if (addressee.value("greedy", false)) {
568+
greedy_index = addressee_index;
569+
}
565570
}
571+
++addressee_index;
566572
}
567573
}
568574

@@ -730,6 +736,14 @@ namespace sdk {
730736

731737
change = total - required_with_fee;
732738

739+
// If a 'greedy' addressee exists send change there
740+
if (change > 0 && greedy_index != NO_CHANGE_INDEX) {
741+
set_tx_output_value(net_params, tx, greedy_index, asset_id, change.value());
742+
addressees_p->at(greedy_index)["satoshi"] = change.value();
743+
required_total += change;
744+
continue;
745+
}
746+
733747
if ((!have_change_output && change < dust_threshold)
734748
|| (have_change_output && change >= dust_threshold)) {
735749
// We don't have a change output, and have only dust left over, or

src/transaction_utils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ namespace sdk {
421421

422422
// Transactions with outputs below the dust threshold (except OP_RETURN)
423423
// are not relayed by network nodes
424-
if (!result.value("send_all", false) && satoshi.value() < session.get_dust_threshold()) {
424+
if (!result.value("send_all", false) && !addressee.value("greedy", false)
425+
&& satoshi.value() < session.get_dust_threshold()) {
425426
set_tx_error(result, res::id_invalid_amount);
426427
}
427428

0 commit comments

Comments
 (0)